Download MetaTrader 5

ObjectCreate on Indicator problem

To add comments, please log in or register
Millions of traders download codes from MQL5 library. Share your app with them!
Stephen Reynolds
Stephen Reynolds 2016.02.04 20:04 

Why is it that when I place my Indicator onto a chart it only shows the objects on current day. But when I run it on tester it displays all previous days?

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 bars = rates_total - 1; 
   if(prev_calculated > 0) bars = rates_total - prev_calculated; 

   for(int i = bars; i >= 0; i--)  // -- Counts down from last bar on chart EG 99th
   int weekday = DayOfWeek();
   int shift = 0; 
   if(weekday == 1) shift = 2; 
   if(weekday != 1) shift = 1;
   double ycloseBuffer = iClose(_Symbol,PERIOD_D1,shift); 
   double yhighBuffer = iHigh(_Symbol,PERIOD_D1,shift); 
   double ylowBuffer = iLow(_Symbol,PERIOD_D1,shift); 

   datetime timeStart = CreateDateTime(0,0);  
   datetime timeEnd = CreateDateTime(23,0);

   string ob_yc = "ycloseline "+TimeToStr(timeStart);    
   string ob_yh = "yhighline "+TimeToStr(timeStart);   
   string ob_yl = "ylowline "+TimeToStr(timeStart); 


//--- return value of prev_calculated for next call
Keith Watford
Keith Watford 2016.02.04 21:13  

I doubt that your code will draw previous lines whether in tester or not. It may paint the lines as the tester is running.

We don't know what the function CreateDateTime does, but as it doesn't have any index passed to it, I can only guess that it uses TimeCurrent() or Time[0] or something like that. This means that when the indicator is first attached to a chart, it will create the same times for every run through the loop. So it will try to create new objects with the same names, and that will fail

To add comments, please log in or register