iMA indicator Issue with calculated bars

 

Hi everyone,

I have an EA which uses a daily timeframe for trading but also uses a weekly timeframe as a filter. When testing the EA  I noticed that BarsCalculated returns -1 (No data). After playing around for a bit I created the following code to test this out further.

The behaviour is different depending on what I select for the trading timeframe under settings of the tester. When it is set to Daily I get BarsCalculated = -1 and when it is set to Weekly I get 103. Is there a way of forcing the correct amonut of weekly data to be loaded within the code when I have daily selected in tester settings? Any help would be appreciated.


// INCLUDE FILES
#include<EdCustom\Strategies\Strategy Functions.mqh>

int handle_MA;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   handle_MA = iMA("EURUSD", PERIOD_W1, 100, 0, MODE_SMA, PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   bool isNewBar = newBar();
   if(isNewBar)
     {
      double buffer_MA[];
      bool fillSuccessTrendFilterMedMA = tlamCopyBuffer(handle_MA, NULL, buffer_MA, 4, "EURUSD", "MA");
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| bool newBar()
//| Returns true if a new bar is detected on the Chart Symbol/Period that
//| the EA is attached to.                                                                 |
//+------------------------------------------------------------------+
bool newBar()
  {
   static datetime lastbar;
   datetime curbar = iTime(_Symbol,_Period,0);
   if(lastbar != curbar)
     {
      lastbar = curbar;
      return(true);
     }
   else
     {
      return(false);
     }
  }
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//| bool newBar()
//| Returns true if a new bar is detected on the Chart Symbol/Period that
//| the EA is attached to.                                                                 |
//+------------------------------------------------------------------+
bool newBar()
  {
   static datetime lastbar;
   datetime curbar = iTime(_Symbol,_Period,0);
   if(lastbar != curbar && BarsCalculated(handle_MA)>0)
     {
      lastbar = curbar;
      return(true);
     }
   return(false);
  }
//+------------------------------------------------------------------+
 
Ernst Van Der Merwe #:

Hi Ernst,

Thanks for your post. I don't think that will work as there doesn't seem to be any bars calculated ever when daily is selected as the timeframe in tester settings. It seems like the data is not being loaded. With an SMA period of 50 it works OK but 100 and 200 does not work.

Any other ideas?


Cheers

 
EdFuk #:

Hi Ernst,

Thanks for your post. I don't think that will work as there doesn't seem to be any bars calculated ever when daily is selected as the timeframe in tester settings. It seems like the data is not being loaded. With an SMA period of 50 it works OK but 100 and 200 does not work.

Any other ideas?


Cheers

It seems to only load the right amount of bars if the test starts on 2019.11.25 and ends today. For me anyway.

void OnTick()
  {
//---
   bool newbar=NewBar();
   if(newbar) {
      double MA[];
      if(CopyBuffer(handleMA,0,0,1,MA)==WRONG_VALUE) {
         Print("CopyBuffer failed. Error: ",_LastError);
         return; }
      printf("Date: %s MA: %f",TimeToString(TimeCurrent(),TIME_DATE),MA[0]); }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool NewBar()
  {
   static datetime preTime=0;
   datetime curTime=iTime(_Symbol,_Period,0);
   if(preTime!=curTime && BarsCalculated(handleMA)>0) {
      preTime=curTime;
      return(true); }
//---
   return(false);
  }   
//+------------------------------------------------------------------+
2022.01.26 16:18:53.107 EURUSD,Weekly: history cache allocated for 214 bars and contains 96 bars from 2017.12.31 00:00 to 2019.10.27 00:00
2022.01.26 16:18:53.107 EURUSD,Weekly: history begins from 2017.12.31 00:00
2022.01.26 16:19:13.073 2019.11.25 00:01:00   Date: 2019.11.25 MA: 1.141771
2022.01.26 16:19:13.498 2019.11.26 00:00:00   Date: 2019.11.26 MA: 1.141766
 
EdFuk #: Any other ideas?

Why look at the higher TF at all. There are five (5) daily bars per one weekly. Use a 500 on the daily chart.

 
William Roeder #:

Why look at the higher TF at all. There are five (5) daily bars per one weekly. Use a 500 on the daily chart.

Good idea