見出し画像

Blackflag FTS MT4インジケーター

// Inputs for Blackflag FTS
trailType = input.string('modified', 'Trailtype', options=['modified', 'unmodified'])
ATRPeriod = input(28, 'ATR Period')
ATRFactor = input(5, 'ATR Factor')
// Blackflag FTS Calculations...
norm_o = request.security(ticker.new(syminfo.prefix, syminfo.ticker), timeframe.period, open)
norm_h = request.security(ticker.new(syminfo.prefix, syminfo.ticker), timeframe.period, high)
norm_l = request.security(ticker.new(syminfo.prefix, syminfo.ticker), timeframe.period, low)
norm_c = request.security(ticker.new(syminfo.prefix, syminfo.ticker), timeframe.period, close)

Wild_ma(_src, _malength) =>
    _wild = 0.0
    _wild := nz(_wild[1]) + (_src - nz(_wild[1])) / _malength
    _wild

HiLo = math.min(norm_h - norm_l, 1.5 * nz(ta.sma(norm_h - norm_l, ATRPeriod)))
HRef = norm_l <= norm_h[1] ? norm_h - norm_c[1] : norm_h - norm_c[1] - 0.5 * (norm_l - norm_h[1])
LRef = norm_h >= norm_l[1] ? norm_c[1] - norm_l : norm_c[1] - norm_l - 0.5 * (norm_l[1] - norm_h)
trueRange = trailType == 'modified' ? math.max(HiLo, HRef, LRef) : math.max(norm_h - norm_l, math.abs(norm_h - norm_c[1]), math.abs(norm_l - norm_c[1]))
loss = ATRFactor * Wild_ma(trueRange, ATRPeriod)
Up = norm_c - loss
Dn = norm_c + loss
TrendUp = Up
TrendDown = Dn
Trend = 1
TrendUp := norm_c[1] > TrendUp[1] ? math.max(Up, TrendUp[1]) : Up
TrendDown := norm_c[1] < TrendDown[1] ? math.min(Dn, TrendDown[1]) : Dn
Trend := norm_c > TrendDown[1] ? 1 : norm_c < TrendUp[1] ? -1 : nz(Trend[1], 1)
trail = Trend == 1 ? TrendUp : TrendDown

Blackflag FTS MT4インジケーター


//+------------------------------------------------------------------+
//|                                  EnhancedBlackflagFTSIndicator.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.01"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Green

// Enumeration for display options
enum ENUM_DISPLAY_MODE
{
   MODE_UP_DOWN,   // Up and Down
   MODE_TREND_UP   // TrendUp
};

// Input parameters
input string TrailType = "modified"; // Trail type: "modified" or "unmodified"
input int ATRPeriod = 28;            // ATR Period
input double ATRFactor = 5.0;        // ATR Factor
input ENUM_DISPLAY_MODE DisplayMode = MODE_UP_DOWN; // Display Mode

// Indicator buffers
double UpBuffer[];
double DownBuffer[];
double TrendUpBuffer[];

// Global variables
double TrendUp[], TrendDown[];
int Trend[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Indicator buffers mapping
   SetIndexBuffer(0, UpBuffer);
   SetIndexBuffer(1, DownBuffer);
   SetIndexBuffer(2, TrendUpBuffer);
   
   SetIndexStyle(0, DRAW_LINE);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexStyle(2, DRAW_LINE);
   
   SetIndexLabel(0, "Blackflag FTS Up");
   SetIndexLabel(1, "Blackflag FTS Down");
   SetIndexLabel(2, "Blackflag FTS TrendUp");
   
   // Initialize arrays
   ArrayResize(TrendUp, Bars);
   ArrayResize(TrendDown, Bars);
   ArrayResize(Trend, Bars);
   ArrayInitialize(TrendUp, 0);
   ArrayInitialize(TrendDown, 0);
   ArrayInitialize(Trend, 1);
   
   // Set visibility based on display mode
   if(DisplayMode == MODE_UP_DOWN)
   {
      SetIndexDrawBegin(2, 0); // Hide TrendUp
      SetIndexDrawBegin(2, Bars); // This effectively hides the TrendUp line
   }
   else // MODE_TREND_UP
   {
      SetIndexDrawBegin(0, 0); // Hide Up
      SetIndexDrawBegin(1, 0); // Hide Down
      SetIndexDrawBegin(0, Bars); // This effectively hides the Up line
      SetIndexDrawBegin(1, Bars); // This effectively hides the Down line
   }
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   int limit = rates_total - prev_calculated;
   if (prev_calculated == 0) limit = rates_total - ATRPeriod - 1;
   
   double trueRange[], wildMA[], HiLo[], HRef[], LRef[];
   ArrayResize(trueRange, rates_total);
   ArrayResize(wildMA, rates_total);
   ArrayResize(HiLo, rates_total);
   ArrayResize(HRef, rates_total);
   ArrayResize(LRef, rates_total);
   
   // Calculate True Range and HiLo
   for (int i = limit; i >= 0; i--)
   {
      HiLo[i] = MathMin(high[i] - low[i], 1.5 * iMA(NULL, 0, ATRPeriod, 0, MODE_SMA, PRICE_HIGH - PRICE_LOW, i));
      HRef[i] = low[i] <= high[i+1] ? high[i] - close[i+1] : high[i] - close[i+1] - 0.5 * (low[i] - high[i+1]);
      LRef[i] = high[i] >= low[i+1] ? close[i+1] - low[i] : close[i+1] - low[i] - 0.5 * (low[i+1] - high[i]);
      
      if (TrailType == "modified")
         trueRange[i] = MathMax(HiLo[i], MathMax(HRef[i], LRef[i]));
      else
         trueRange[i] = MathMax(high[i] - low[i], MathMax(MathAbs(high[i] - close[i+1]), MathAbs(low[i] - close[i+1])));
   }
   
   // Calculate Wild MA of True Range
   for (int i = limit; i >= 0; i--)
   {
      wildMA[i] = i < rates_total - 1 ? wildMA[i+1] + (trueRange[i] - wildMA[i+1]) / ATRPeriod : trueRange[i];
   }
   
   // Calculate Up, Down, TrendUp, TrendDown, and Trend
   for (int i = limit; i >= 0; i--)
   {
      double loss = ATRFactor * wildMA[i];
      double Up = close[i] - loss;
      double Dn = close[i] + loss;
      
      if (i < rates_total - 1)
      {
         TrendUp[i] = close[i+1] > TrendUp[i+1] ? MathMax(Up, TrendUp[i+1]) : Up;
         TrendDown[i] = close[i+1] < TrendDown[i+1] ? MathMin(Dn, TrendDown[i+1]) : Dn;
         Trend[i] = close[i] > TrendDown[i+1] ? 1 : close[i] < TrendUp[i+1] ? -1 : Trend[i+1];
      }
      else
      {
         TrendUp[i] = Up;
         TrendDown[i] = Dn;
         Trend[i] = 1;
      }
      
      // Assign values to buffers based on display mode
      if(DisplayMode == MODE_UP_DOWN)
      {
         UpBuffer[i] = Up;
         DownBuffer[i] = Dn;
         TrendUpBuffer[i] = EMPTY_VALUE; // Hide TrendUp
      }
      else // MODE_TREND_UP
      {
         UpBuffer[i] = EMPTY_VALUE; // Hide Up
         DownBuffer[i] = EMPTY_VALUE; // Hide Down
         TrendUpBuffer[i] = TrendUp[i];
      }
   }
   
   return(rates_total);
}
//+------------------------------------------------------------------+


トレンドの方向を示すオシレーターBlackflagFTSIndicatorTrendFlagインジケーター

いいなと思ったら応援しよう!

ホソノP
よろしければサポートお願いします! いただいたサポートはクリエイターとしての活動費に使わせていただきます!