Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Indicator | Discover: Target Trend v3 Indicator | Expert Advisor Code

New free code from MQL5: indicators, EAs, and scripts for traders.

#property 
#property version   "1.02"
#property indicator_chart_window


#property indicator_buffers 6
#property indicator_plots   3

//--- Buy Arrow (Filled)
#property indicator_label1  "Buy Signal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  C'6,182,144'
#property indicator_width1  4

//--- Sell Arrow (Filled)
#property indicator_label2  "Sell Signal"
#property indicator_type2   DRAW_ARROW
#property indicator_color2  C'182,112,6'
#property indicator_width2  4

//--- Trend Trailing Stop Line (Color Line)
#property indicator_label3  "Trend Line"
#property indicator_type3   DRAW_COLOR_LINE
// Define the two colors for the line: Index 0 = Bullish, Index 1 = Bearish
#property indicator_color3  C'6,182,144', C'182,112,6' 
#property indicator_style3  STYLE_SOLID
#property indicator_width3  2

//+------------------------------------------------------------------+
//| Input Parameters                                                 |
//+------------------------------------------------------------------+
input int InpLength    = 10; 
input int InpLookback  = 4;  

//+------------------------------------------------------------------+
//| Indicator Buffers                                                |
//+------------------------------------------------------------------+
double BuyBuffer[];
double SellBuffer[];
double TrendBuffer[];
double TrendColorBuffer[]; // Buffer for the line colors
double TrendState[];       
double AtrSmaBuffer[];     

int g_atr_handle, g_sma_high_handle, g_sma_low_handle;

//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, BuyBuffer,        INDICATOR_DATA);
   SetIndexBuffer(1, SellBuffer,       INDICATOR_DATA);
   SetIndexBuffer(2, TrendBuffer,      INDICATOR_DATA);
   SetIndexBuffer(3, TrendColorBuffer, INDICATOR_COLOR_INDEX); // Links to Plot 3
   SetIndexBuffer(4, TrendState,       INDICATOR_CALCULATIONS);
   SetIndexBuffer(5, AtrSmaBuffer,     INDICATOR_CALCULATIONS);

   // Codes 233 and 234 are filled Wingdings arrows
   PlotIndexSetInteger(0, PLOT_ARROW, 233); 
   PlotIndexSetInteger(1, PLOT_ARROW, 234);

   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0.0);

   g_atr_handle      = iATR(_Symbol, _Period, 200);
   g_sma_high_handle = iMA(_Symbol, _Period, InpLength, 0, MODE_SMA, PRICE_HIGH);
   g_sma_low_handle  = iMA(_Symbol, _Period, InpLength, 0, MODE_SMA, PRICE_LOW);

   return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
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[])
{
   const int ATR_PERIOD     = 200;
   const int ATR_SMA_PERIOD = 200;
   const int MIN_BARS       = ATR_PERIOD + ATR_SMA_PERIOD + InpLength + 2;

   if(rates_total < MIN_BARS) return 0;

   if(BarsCalculated(g_atr_handle) < rates_total ||
      BarsCalculated(g_sma_high_handle) < rates_total ||
      BarsCalculated(g_sma_low_handle) < rates_total)
      return prev_calculated;

   double atr_vals[], sma_h[], sma_l[];
   if(CopyBuffer(g_atr_handle, 0, 0, rates_total, atr_vals) != rates_total ||
      CopyBuffer(g_sma_high_handle, 0, 0, rates_total, sma_h) != rates_total ||
      CopyBuffer(g_sma_low_handle, 0, 0, rates_total, sma_l) != rates_total)
      return prev_calculated;

   int compute_start;
   if(prev_calculated <= 0) {
      compute_start = MIN_BARS - 2;
      TrendState[compute_start] = 0.0;
   } else {
      compute_start = MathMax(MIN_BARS - 2, prev_calculated - 1 - InpLookback);
   }

   // Pass 1: ATR SMA
   for(int i = compute_start; i < rates_total; i++) {
      double sum = 0.0;
      for(int k = 0; k < ATR_SMA_PERIOD; k++) sum += atr_vals[i - k];
      AtrSmaBuffer[i] = (sum / ATR_SMA_PERIOD) * 0.8;
   }

   // Pass 2: Trend and Colors
   for(int i = compute_start + 1; i < rates_total; i++) {
      double atr_cur  = AtrSmaBuffer[i];
      double sma_high_cur  = sma_h[i] + atr_cur;
      double sma_low_cur   = sma_l[i] - atr_cur;
      
      double sma_high_prev = sma_h[i - 1] + AtrSmaBuffer[i-1];
      double sma_low_prev  = sma_l[i - 1] - AtrSmaBuffer[i-1];

      bool crossover  = (close[i] > sma_high_cur) && (close[i - 1] <= sma_high_prev);
      bool crossunder = (close[i] < sma_low_cur)  && (close[i - 1] >= sma_low_prev);

      double prev_trend = TrendState[i - 1];
      double cur_trend  = prev_trend;

      if(crossover)  cur_trend =  1.0;
      if(crossunder) cur_trend = -1.0;

      TrendState[i] = cur_trend;

      if(cur_trend == 1.0) {
         TrendBuffer[i] = sma_low_cur;
         TrendColorBuffer[i] = 0.0; // Bullish Color (Index 0)
      } else if(cur_trend == -1.0) {
         TrendBuffer[i] = sma_high_cur;
         TrendColorBuffer[i] = 1.0; // Bearish Color (Index 1)
      } else {
         TrendBuffer[i] = 0.0;
         TrendColorBuffer[i] = EMPTY_VALUE;
      }

      BuyBuffer[i]  = 0.0;
      SellBuffer[i] = 0.0;

      if(cur_trend == 1.0 && prev_trend != 1.0)
         BuyBuffer[i] = low[i] - atr_cur * 2.0;

      if(cur_trend == -1.0 && prev_trend != -1.0)
         SellBuffer[i] = high[i] + atr_cur * 2.0;
   }

   return rates_total;
}

Never miss news-driven moves – use the News OCO Expert Advisor to place pending orders safely around economic releases. Find out more.

Level up your trading with professional RobotFX expert advisors and indicators. Visit www.robotfx.org for proven MT4/MT5 tools.

70134

Best MetaTrader Indicators + Profitable Expert Advisors