Indicator bugs all over the Chart

 

Hello Community

I got a little problem with a Indicator I made.
Sometimes if i switch the timeframe, The Indicator is drawn randomly all over the chart.

Indicator normally:

Indicator normally

Indicator Buggy:

Indicator_Bug

Does someone know where the problem is?

Here are some parts of the code:

//Indicator drawing and buffers
#property indicator_chart_window
#property indicator_buffers 3

//Colors and sizes for buffers
#property indicator_color1 clrBurlyWood
#property indicator_width1 1
#property indicator_color2 clrBlue
#property indicator_width2 1
#property indicator_color3 clrRed
#property indicator_width3 1


//Buffer Arrays
double         RsiLevelBuffer[];
double         BBCon1UpBuffer[];
double         BBCon1DownBuffer[];
int OnInit()
{
   // RSI
   SetIndexBuffer(0, RsiLevelBuffer);     // Assign buffer array
   SetIndexStyle(0, DRAW_ARROW);          // Style to arrow
   SetIndexArrow(0, 119);                 // Arrow code 119 115
   
   SetIndexBuffer(1, BBCon1UpBuffer);     // Assign buffer array
   SetIndexStyle(1, DRAW_ARROW);          // Style to arrow
   SetIndexArrow(1, 225);                 // Arrow code
   
   SetIndexBuffer(2, BBCon1DownBuffer);   // Assign buffer array
   SetIndexStyle(2, DRAW_ARROW);          // Style to arrow
   SetIndexArrow(2, 226);                 // Arrow code
}
if(firstTick) { OnInit2(); }
   
// Start and limit
   int start = 1;
    
   int countedBars = IndicatorCounted();        // Bars counted so far
   if(countedBars < 0) { return(0); }           // No more bars?     
   if(countedBars > 0) { countedBars --; } 
   int limit = Bars - countedBars;              // Do not check repeated bars
   
   //For drawing Indicators 
   for(int i = limit; i >= start; i--)          // Iterate bars from past to present
   {
      if(i > Bars-2) continue;                  // If not enough data...
      
      if(BBCondition_1(i)) { continue; }
      if(BBCondition_2(i)) { continue; }
      if(BBCondition_3(i)) { continue; }
      
      RsiCondition(i);  
    }

The arrows are drawn inside the condition functions

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 

sorry, was not on purpose

thank you

 

Are you using OnCalculate()?

You should try to avoid the old variables such as IndicatorCounted(). Use prev_calculated instead.

Your problem is possibly due to the chart being updated after you change timeframes.

When prev_calculated ==0, you should use ArrayInitialize() to set all index to EMPTY_VALUE.

 
Keith Watford:

Are you using OnCalculate()?

You should try to avoid the old variables such as IndicatorCounted(). Use prev_calculated instead.

Your problem is possibly due to the chart being updated after you change timeframes.

When prev_calculated ==0, you should use ArrayInitialize() to set all index to EMPTY_VALUE.

Yes I use the OnCalculate() function.

Thank you for the tip with the  ArrayInitialize() , i think that was the main problem. 

Now the code (in the OnCalculate) looks like this:

if(prev_calculated == 0)                           //changing TF f.e.
   {
       ArrayInitialize(RsiLevelBuffer,EMPTY_VALUE);
       ArrayInitialize(BBCon1UpBuffer,EMPTY_VALUE);
       ArrayInitialize(BBCon1DownBuffer,EMPTY_VALUE);
   }
   if(rates_total != prev_calculated)
   {
      int limit = rates_total - prev_calculated;            // Do not check repeated bars
   
      //For drawing Indicators 
      for(int i = limit; i >= 1; i--)                       // Iterate bars from past to present
      {
         if(i > Bars -2) continue;                          // If not enough data...
         
         if(BBCondition_1(i)) { continue; }
         if(BBCondition_2(i)) { continue; }
         if(BBCondition_3(i)) { continue; }
         
         RsiCondition(i);  
       }
    }

Would oyu solve it differently? Or how do you do it?

Have  a nice day, and thank you :)

Documentation on MQL5: Array Functions / ArrayInitialize
Documentation on MQL5: Array Functions / ArrayInitialize
  • www.mql5.com
ArrayInitialize - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: