1

I want to know How can I get data from a time frame other than period setting in Strategy Tester (a part of MetaTrader4 Trading Terminal), using the sample code below, I get zero results for op2 & EMA2 whenever I choose a period setting other than M5 in the Strategy Tester.

How can I fix it?

  void OnTick()
  {  
       string print = "\n\n\n\n\n\n" +
                      "\n op1= "   + iOpen(NULL,PERIOD_CURRENT,0) +
                      "\n op2= "   + iOpen(NULL,PERIOD_M5,0) +
                      "\n EMA1 : " +   iMA(NULL,PERIOD_CURRENT,21,0,MODE_EMA,PRICE_CLOSE,0) +
                      "\n EMA2 : " +   iMA(NULL,PERIOD_M5,21,0,MODE_EMA,PRICE_CLOSE,0);
    
       Comment(print);        
  }

enter image description here

user3666197
  • 1
  • 6
  • 50
  • 92
hamed razi
  • 19
  • 3

2 Answers2

0

Q : "How can I fix it?"

Well,

for about the last 12+ years, this (otherwise properly crafted piece of syntax) has never worked correctly inside the MetaTrader Terminal 4 Strategy Tester.

If in doubts here, one may try to re-run the very same piece of code inside an Expert Advisor or Script type-of-MQL4-code & you shall see no troubles there (sure, only in cases the FOREX Market QUOTE-message-feed is live & delivering QUOTE-tick FX market-events that trigger the EA-OnTick()-method ... for obvious reasons this is no problem for a Script type-of-MQL4-code)

user3666197
  • 1
  • 6
  • 50
  • 92
  • thanks for your comment, So you are saying its strategy tester problem and code will work fine in expert advisor? I want to test multi time frame trading strategy in chart history and use it as trading strategy based on back test results. for this, I need different time frame data in my script to make sure that everything works just fine, and the only way I know for that is using strategy tester in MT4 step by step ! – hamed razi Nov 03 '20 at 18:35
  • Welcome. This is not a Comment, this is an Answer. I use the same principle to by-pass (circumvent) the Strategy Tester trouble for about 7 years (complementing the built-in Strategy Tester engine by an add-on, Script-based, own backtesting / validation engine) – user3666197 Nov 03 '20 at 22:27
  • honestly I am no more than two months working on this area, can you explain your answer in any easier steps or by example please, thank you in advance. – hamed razi Nov 04 '20 at 13:54
  • No problem. In simple words - this will never work inside Strategy Tester (as-is in 2020-Q4 since ever). There is nothing complicated in understanding & accepting this fact. – user3666197 Nov 04 '20 at 15:00
0

Actually, this can be fixed, but with one limitation - you can only request higher-timeframe data when running a backtest on a lower timeframe. For example, if you were to run a backtest on M5, this would return non-zero values:

  void OnTick()
  {  
       string print = "\n\n\n\n\n\n" +
                      "\n op1= "   + iOpen(NULL,PERIOD_CURRENT,0) +
                      "\n op2= "   + iOpen(NULL,PERIOD_M15,0) +
                      "\n EMA1 : " +   iMA(NULL,PERIOD_CURRENT,21,0,MODE_EMA,PRICE_CLOSE,0) +
                      "\n EMA2 : " +   iMA(NULL,PERIOD_M15,21,0,MODE_EMA,PRICE_CLOSE,0);
    
       Comment(print);        
  }

Luckily, it is often possible to rewrite the EA's logic in such a way as to work from lower timeframe with a higher timeframe for backtesting purposes.

Enivid
  • 210
  • 2
  • 9