Download MetaTrader 5

Cant see how to draw objects on all previous bars

To add comments, please log in or register
Do you wonder how to use widgets? Read the article!
Stephen Reynolds
2043
Stephen Reynolds 2016.02.06 17:28 

Im trying to see why it is that the following Indicator code only creates objects on strategytester as and when each bar closes. But when I place the Indicator onto a chart it doesn't draw any previous objects?

#property indicator_chart_window

//+------------------------------------------------------------------+
//| 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[])
{

   int bars = rates_total - 1;   
   if(prev_calculated > 0) bars = rates_total - prev_calculated; 

        for(int i = bars; i >= 0; i--)   
   {

      //------------- 
      // 3 Bar Triangle
      //--------------   
      double high1 = iHigh(_Symbol,_Period,1);   
      double high3 = iHigh(_Symbol,_Period,3);  
      double low1 = iLow(_Symbol,_Period,1);   
      double low3 = iLow(_Symbol,_Period,3); 

      bool barTriangle = (high1 < high3 && low1 > low3);

      datetime time = iTime(NULL,0,i);  
      int bc1 = 1;
      int bc3 = 3;
      
      string signal1 = "signal1 "+TimeToStr(time); 
      string signal2 = "signal2 "+TimeToStr(time);                 
                                                    
      double tBHigh1 = iHigh(NULL,PERIOD_M30,i);
      double tBHigh3 = iHigh(NULL,PERIOD_M30,i+bc3); // Here i use i (which is 1 then +bc3 which is 3 so it should always count 3 bars back from 1)
      double tBLow1 = iLow(NULL,PERIOD_M30,i);
      double tBLow3 = iLow(NULL,PERIOD_M30,i+bc3);     
      datetime tBTime1 = iTime(NULL,0,i);             
      datetime tBTime3 = iTime(NULL,0,i+bc3);      

      if(barTriangle)
      {
         ObjectCreate(0,signal1,OBJ_TREND,0,tBTime1,tBHigh1,tBTime3,tBHigh3);
         ObjectCreate(0,signal2,OBJ_TREND,0,tBTime1,tBLow1,tBTime3,tBLow3);
         ObjectSet(signal1,OBJPROP_RAY,false);
         ObjectSet(signal2,OBJPROP_RAY,false);                       
      } 
   }     
  
//--- return value of prev_calculated for next call
   return(rates_total);
}   



 
Keith Watford
Moderator
9644
Keith Watford 2016.02.06 22:20  
 
      double high1 = iHigh(_Symbol,_Period,1);   
      double high3 = iHigh(_Symbol,_Period,3);  
      double low1 = iLow(_Symbol,_Period,1);   
      double low3 = iLow(_Symbol,_Period,3); 

      bool barTriangle = (high1 < high3 && low1 > low3);

Objects are only drawn if barTriangle is true.

It makes no sense that you use values from bars 1 and 3 for the condition in a loop that uses i. 

      string signal1 = "signal1 "+TimeToStr(time); 
      string signal2 = "signal2 "+TimeToStr(time);                 
                                                    
      double tBHigh1 = iHigh(NULL,PERIOD_M30,i);
      double tBHigh3 = iHigh(NULL,PERIOD_M30,i+bc3); // Here i use i (which is 1 then +bc3 which is 3 so it should always count 3 bars back from 1)
      double tBLow1 = iLow(NULL,PERIOD_M30,i);
      double tBLow3 = iLow(NULL,PERIOD_M30,i+bc3);     
      datetime tBTime1 = iTime(NULL,0,i);             
      datetime tBTime3 = iTime(NULL,0,i+bc3);      

      if(barTriangle)
      {
         ObjectCreate(0,signal1,OBJ_TREND,0,tBTime1,tBHigh1,tBTime3,tBHigh3);
         ObjectCreate(0,signal2,OBJ_TREND,0,tBTime1,tBLow1,tBTime3,tBLow3);
         ObjectSet(signal1,OBJPROP_RAY,false);
         ObjectSet(signal2,OBJPROP_RAY,false);                       
      } 

 

 Here you are mixing time from the current timeframe with prices from M30 using the same bar index, Again, it makes no sense.

whroeder1
14532
whroeder1 2016.02.07 14:29  
double tBLow3 = iLow(NULL,PERIOD_M30,i+bc3);
datetime tBTime3 = iTime(NULL,0,i+bc3);
You are mixing apples and oranges
To add comments, please log in or register