Cant see how to draw objects on all previous bars

Back to topics list To post a new topic, please log in or register
avatar
52
renzbub 2016.02.06 18: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);
}   



 

 

avatar
Moderator
3412
GumRai 2016.02.06 23: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.

avatar
12992
WHRoeder 2016.02.07 15:29 #
 
double tBLow3 = iLow(NULL,PERIOD_M30,i+bc3);
datetime tBTime3 = iTime(NULL,0,i+bc3);
You are mixing apples and oranges
Back to topics list  

To add comments, please log in or register