Vertical line. - page 4

 

I'm still learning the intricacies of indicators and have questions. I reset indicator buffer in OnInit(), but for some reason buffers are not reset during indicator compilation. For logic, there should be one full cell for each buffer after compilation, but in the picture you can see that there are many of them (I think that these are the previous buffer cells). Where do I make a mistake?

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ColorHistogram_2Buffer1,INDICATOR_DATA);
   SetIndexBuffer(1,ColorHistogram_2Buffer2,INDICATOR_DATA);
   SetIndexBuffer(2,ColorHistogram_2Colors,INDICATOR_COLOR_INDEX);
//--- установим пустое значение
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   ArrayInitialize(ColorHistogram_2Buffer1,0);
   ArrayInitialize(ColorHistogram_2Buffer2,0);
   ArrayInitialize(ColorHistogram_2Colors,EMPTY_VALUE);

   Print("-INITALIZATION-");

   false;
//---
   return(INIT_SUCCEEDED);
  }
      if(rates_total-8==i)// || rates_total-5==i || rates_total-2==i)
        {
         ColorHistogram_2Buffer1[i]=high[i];
         ColorHistogram_2Buffer2[i]=low[i];
         ColorHistogram_2Colors[i]=0;
        }


.
 
Nauris Zukas:

I'm still learning the intricacies of indicators and have questions. I reset indicator buffer in OnInit(), but for some reason buffers are not reset during indicator compilation. For logic, after compilation there should be one full cell for each buffer . Where do I make a mistake?

In OnInit the indicator buffers have zero length.


That's why they can do whatever you want with them - it all looks like a dead man's work to them.

And when code execution switches to OnCalculate, the size immediately becomes equal to rates_total.

and there is "rubbish" in all cells of the array. There may be leftover from old indicator calculation as well.

 
Alexey Viktorov:

In OnInit, indicator buffers have zero length.


That's why you can do whatever you want with them, they are a dead giveaway.

And when code execution passes to OnCalculate, the size immediately becomes equal to rates_total.

and all the cells in the array have "rubbish" in them. There may be some left over from the old indicator calculation.

Thank you very much, I got it all and it worked! Did it like this:

bool ResetOnInit=false;
int OnInit()
  {   
   ResetOnInit=false;
   return(INIT_SUCCEEDED);
  }
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[])
  {
   if(!ResetOnInit)
     {
      ArrayInitialize(ColorHistogram_2Buffer1,0);
      ArrayInitialize(ColorHistogram_2Buffer2,0);
      ArrayInitialize(ColorHistogram_2Colors,EMPTY_VALUE);
      ResetOnInit=true;
     }
  }
 
Nauris Zukas:

Thank you very much, it all worked out! I did it like this:

Why assign a value to a variable twice?

bool ResetOnInit=false;
int OnInit()
  {   
   ResetOnInit=false;
   return(INIT_SUCCEEDED);
  }

And a question: What's the point of doing all this? After all, passing through all bars in the loop, all cells of the array must be filled with something anyway. Unless the indicator should only show the current position, but then the array must be initialized on each bar, not only at the beginning...

 
Alexey Viktorov:

Why assign a value to a variable twice?

I wasn't sure if all actions with the graph are imitated by OnInit(), for example changing taimfreims. Changing taimfreim reset (just checked), but maybe there's something else, so I put the second one in OnInit() to be sure. But if not, then I won't put it there.
 
Alexey Viktorov:

And the question is: Why do we need all this? After all, passing through all the bars in the loop, all cells of the array must be filled with something, anyway. Unless the indicator should only show the current position, but then the array should be initialized on each bar, not only at the beginning...

In this example, I just wanted to understand how to reset the buffer. But in future (in the indicator that I'm working on now), I will need to both leave the previous cells and add new ones. And just on this I began to work.So to speak - the first tests.

 
Nauris Zukas:
I was not sure that all actions with the chart are imitated by OnInit(), e.g. change of taimfreims. Changing taimfreim reset (checked it now), but maybe there's something else, so I put the second one in OnInit() to be sure. But if not, then I won't put it there.

Ahh. Well yes, the global level variables are not initialised when the chart period changes. ???I think... can't remember exactly.

And in general you can do without that extra bool variable.

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[])
  {
   if(prev_calculated == 0)
     {
      ArrayInitialize(ColorHistogram_2Buffer1,0);
      ArrayInitialize(ColorHistogram_2Buffer2,0);
      ArrayInitialize(ColorHistogram_2Colors,EMPTY_VALUE);
     }
  }
 
Alexey Viktorov:

And in general, you can do without this extra bool variable.

Yes, exactly, even better, thank you.

 

Can you tell me why, if the period is more than 30 minutes, it returns such a strange number?

Print("PERIOD: ",Period()); 

KP 0 14:33:22.423 Test_DRAW_COLOR_HISTOGRAM2 (EURUSD,M30) PERIOD: 30

GG 0 14:33:24.402 Test_DRAW_COLOR_HISTOGRAM2 (EURUSD,H1) PERIOD: 16385

RJ 0 14:33:25.675 Test_DRAW_COLOR_HISTOGRAM2 (EURUSD,H4) PERIOD: 16388
 
Nauris Zukas:

Can you tell me why if the period is more than 30 minutes, it returns such a strange number?

In MT5, period values are not equal to the number of minutes, as in MT4

Reason: