multi time frame indicator does not work in backtesting

 

Following exactly (https://www.youtube.com/watch?v=dP4VrC_nhJA&list=LL&index=2) I coded the multi timeframe ema.

However, when I try to use it with the strategy tester it does not work and I'm not able to find the problem / solution. I attach a photo where the Ema computation becomes flat.

Show higher timeframe indicators on your Metatrader charts
Show higher timeframe indicators on your Metatrader charts
  • 2021.03.27
  • www.youtube.com
How to code indicators to use values from higher timeframes and address 2 common mistakes.Free downloads for Metatrader 4 and 5MT4: https://www.orchardforex....
Files:
Capture.PNG  61 kb
 

See How to do your lookbacks correctly #9#14 & #19. I specifically mention MTF

 
Hernandez18:

However, when I try to use it with the strategy tester it does not work and I'm not able to find the problem / solution. I attach a photo where the Ema computation becomes flat.

Unfortunately this can happen with MTF indicators in the strategy tester when testing an EA.

The odd thing is, if just testing the indicator in the tester, the indicator displays correctly.

So you can just test the indicator to check whether it is a problem with the indicator.

I have found that although the indicator displays incorrectly, iCustom calls return the correct values.

 
William Roeder #:

See How to do your lookbacks correctly #9#14 & #19. I specifically mention MTF

Hi William Thanks for your quick reply. I tried some of your tips from your links but I broke everything in my code and I'm not sure of what I need to fix yet. Could you help me?


#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#property indicator_chart_window 1
#property indicator_buffers 1
#property  indicator_color1 clrRed
#property indicator_width1 2

input ENUM_TIMEFRAMES      InpTimeFrame = PERIOD_H4;
input int                  InpEmaPeriod = 8;
input ENUM_MA_METHOD       InpEmaMethod = MODE_EMA;
input ENUM_APPLIED_PRICE   InpAppliedPrice = MODE_CLOSE;


int TimeFrame = InpTimeFrame;
double bufferEMA[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   if(TimeFrame == PERIOD_CURRENT)  TimeFrame   = (ENUM_TIMEFRAMES) _Period;
   if(TimeFrame < _Period) return(INIT_PARAMETERS_INCORRECT);
   
   SetIndexBuffer(0, bufferEMA);

   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[])
  {
   const int lookback = TimeFrame/_Period;
   int iBar = rates_total - 1 - MathMax(lookback, prev_calculated);
   #define LAST 0
   int iLast = MathMax(LAST, iBar - 2000);
   
   
   for(; iBar >= iLast; --iBar)
   {
      int mtfBar = iBarShift(Symbol(), TimeFrame, time[iBar]);
      bufferEMA[iBar] = iMA(Symbol(), TimeFrame, InpEmaPeriod, 0, InpEmaMethod, InpAppliedPrice, mtfBar);
   } 
   
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Keith Watford #:

Unfortunately this can happen with MTF indicators in the strategy tester when testing an EA.

The odd thing is, if just testing the indicator in the tester, the indicator displays correctly.

So you can just test the indicator to check whether it is a problem with the indicator.

I have found that although the indicator displays incorrectly, iCustom calls return the correct values.

Hi Keith thanks for your reply. However as I'm working for a client I need to provide him the MTF indicator which shows correctly during backtesting. If I attach my indicator in real time it seems to work correctly. 

Reason: