prev_calculated - page 9

 
Alexey Kozitsyn:
Yes, the loop is probably too much. But if at prev_calculated = 0 (when the buffer is previously full) some values in that buffer are reset, perhaps that's a bug. Let's check now...

It's not that they are reset, but the whole indicator, the whole depth of the history is recalculated. Therefore, if I need to fill only the rightmost index of the buffer, but these values must remain when I shift it to the left, then I have two options:

1. When initializing the indicator buffer, it is full of rubbish and it needs to be cleaned.

2. I have cleaned it, but when prev_calculated = 0, the buffer is cleaned again and deletes all the accumulated values.


And in general, the solution is insanely simple. prev_calculated = 0 together with a flag solves this problem.

 
Alexey Viktorov:

...

In general, the solution is insanely simple. prev_calculated = 0 together with a flag solves the problem.

It doesn't. There will be a connection break of several bars duration, several bars will end up uninitialized with rubbish.
 
Alexey Viktorov:

Don't pretend... It's all clear, but there's a picture just for you.



The computer was working without shutting down, the chart was not closed, the indicator was not reset...

Question: Where did the 2 minute bars disappear to?

You probably have an error in your code. I have now written a test indicator, when I reset by the refresh button the buffer size does not change, the data in the buffer does not change...

#property indicator_buffers 1
#property indicator_plots 1
#property indicator_chart_window
#property indicator_type1 DRAW_NONE
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double buf[];
const double _price=98000;
bool _firstLaunch=true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,buf);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   if(prev_calculated==0)
     {
      if(_firstLaunch)
        {
         Print(__FUNCTION__,": Первый запуск! Размер буфера = ",ArraySize(buf));
         ArrayInitialize(buf,_price);
         _firstLaunch=false;
        }
      else
        {
         Print(__FUNCTION__,": prev_calculated после обновления = ",prev_calculated,". Размер буфера = ",ArraySize(buf));
         //--- Проверяем отличие значений от эталонного
         for(int i=rates_total-1; i>=0; i--)
            if(buf[i]!=_price)
               Print(__FUNCTION__,": Значение #",i," '"+DoubleToString(buf[i],_Digits)+
                     "' отличается от эталонного '"+DoubleToString(_price,_Digits));
        }
     }
   else
     {
      if(rates_total>prev_calculated)
         buf[rates_total-1]=_price;
     }
   return(rates_total);
  }
 

Fromprev_calculated to rates_total the buffer must be cleaned.

 
Either, as Dimitri said above, there was a break in communication of a few bars... By the way, will prev_calculated return 0 when the connection is broken too?
 
Dmitry Fedoseev:
Show me the code. Let's laugh and explain.

I'm laughing already.

/********************************************************************\
|                                               BalansEquityTest.mq5 |
|                                                           Viktorov |
|                                                  v4forex@yandex.ru |
\********************************************************************/

#property copyright "Viktorov"
#property link      "v4forex@yandex.ru"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers   6
#property indicator_plots     3
#property indicator_type1     DRAW_COLOR_LINE
#property indicator_color1    clrGold, clrGreen, clrRed
#property indicator_width1    2
#property indicator_label1    "Balance"
#property indicator_type2     DRAW_HISTOGRAM2
#property indicator_color2    clrCrimson
#property indicator_width2    2
#property indicator_label2    "EquityMin"
#property indicator_type3     DRAW_HISTOGRAM2
#property indicator_color3    clrDarkViolet
#property indicator_width3    2
#property indicator_label3    "EquityMax"

double balance[];
double clrBalance[];
double equityMaxB[];
double equityMinB[];
double equityMax[];
double equityMin[];
double maxEquity, minEquity;

bool flag = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, balance, INDICATOR_DATA);
   SetIndexBuffer(1, clrBalance, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, equityMin, INDICATOR_DATA);
   SetIndexBuffer(3, equityMinB, INDICATOR_DATA);
   SetIndexBuffer(4, equityMax, INDICATOR_DATA);
   SetIndexBuffer(5, equityMaxB, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, 0.0);
   ArraySetAsSeries(balance, true);
   ArraySetAsSeries(clrBalance, true);
   ArraySetAsSeries(equityMax, true);
   ArraySetAsSeries(equityMaxB, true);
   ArraySetAsSeries(equityMin, true);
   ArraySetAsSeries(equityMinB, true);
   IndicatorSetInteger(INDICATOR_DIGITS, 2);
   IndicatorSetString(INDICATOR_SHORTNAME, "Show Money");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
if(prev_calculated == 0)
  {
   Print("prev_calculated = ", prev_calculated);
   if(flag)
    {
     ArrayInitialize(balance, 0.0);
      ArrayInitialize(equityMax, 0.0);
       ArrayInitialize(equityMaxB, 0.0);
      ArrayInitialize(equityMin, 0.0);
     ArrayInitialize(equityMinB, 0.0);
      flag = false;
    }
   return(rates_total);
  }
  double bal = NormalizeDouble(AccountInfoDouble(ACCOUNT_BALANCE), 2);
  double equity = NormalizeDouble(AccountInfoDouble(ACCOUNT_EQUITY), 2);
  if(rates_total > prev_calculated)
   {
    minEquity = 0;
    maxEquity = 0;
   }
   minEquity = NormalizeDouble(fmin((minEquity == 0 ? bal : minEquity), equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);
    balance[0] = bal;
    clrBalance[0] = 0.0;
    equityMinB[0] = balance[0];
    equityMin[0] = minEquity;
    equityMaxB[0] = balance[0];
    equityMax[0] = maxEquity;
    if(balance[0] > balance[1])
     clrBalance[0] = 1.0;
    if(balance[0] < balance[1])
     clrBalance[0] = 2.0;
    Comment(PositionsTotal());
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Overnight there were this many zeroing of prev_calculate

2016.10.19 08:42:23.309 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 05:47:04.915 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 05:47:04.075 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 04:46:15.300 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 04:46:15.030 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 04:45:37.590 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 04:45:37.100 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 01:40:31.224 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 00:00:32.196 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.19 00:00:31.806 BalansEquityTest (EURUSD,M1)    prev_calculated = 0
2016.10.18 20:33:02.954 BalansEquityTest (EURUSD,M1)    prev_calculated = 0 // Это время последнего запуска индикатора.
 
Dmitry Fedoseev:

Fromprev_calculated to rates_total the buffer must be cleaned.

If this is my answer, then at the first start of the indicator the buffer is fully initialised with the given value. When a new bar is formed, new values are overwritten.
 
Alexey Kozitsyn:
Or, as Dmitriy said above, there was a connection failure in several bars... By the way, will prev_calculated also return 0 in case of a connection failure?

It used to be that with all problems prev_calculated returned 0. Then there was a discussion in the forum that it would be possible not always to reset to 0, but in some cases you could reset to the last value counted, in the thread Slava participated and promised to look into the issue.

After a while I noticed that prev_calculated sometimes has intermediate values. Now I do not know about it, I don't watch every day what prev_calculated returns.

 
Alexey Kozitsyn:
If this is my answer, the first time the indicator is started, the buffer is fully initialised with the set value. When a new bar is formed - the new values are overwritten.
Not you.
 
Alexey Viktorov:

I'm already laughing.

/********************************************************************\
|                                               BalansEquityTest.mq5 |
|                                                           Viktorov |
|                                                  v4forex@yandex.ru |
\********************************************************************/

#property copyright "Viktorov"
#property link      "v4forex@yandex.ru"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers   6
#property indicator_plots     3
#property indicator_type1     DRAW_COLOR_LINE
#property indicator_color1    clrGold, clrGreen, clrRed
#property indicator_width1    2
#property indicator_label1    "Balance"
#property indicator_type2     DRAW_HISTOGRAM2
#property indicator_color2    clrCrimson
#property indicator_width2    2
#property indicator_label2    "EquityMin"
#property indicator_type3     DRAW_HISTOGRAM2
#property indicator_color3    clrDarkViolet
#property indicator_width3    2
#property indicator_label3    "EquityMax"

double balance[];
double clrBalance[];
double equityMaxB[];
double equityMinB[];
double equityMax[];
double equityMin[];
double maxEquity, minEquity;

bool flag = true;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0, balance, INDICATOR_DATA);
   SetIndexBuffer(1, clrBalance, INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2, equityMin, INDICATOR_DATA);
   SetIndexBuffer(3, equityMinB, INDICATOR_DATA);
   SetIndexBuffer(4, equityMax, INDICATOR_DATA);
   SetIndexBuffer(5, equityMaxB, INDICATOR_DATA);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, 0.0);
   PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, 0.0);
   ArraySetAsSeries(balance, true);
   ArraySetAsSeries(clrBalance, true);
   ArraySetAsSeries(equityMax, true);
   ArraySetAsSeries(equityMaxB, true);
   ArraySetAsSeries(equityMin, true);
   ArraySetAsSeries(equityMinB, true);
   IndicatorSetInteger(INDICATOR_DIGITS, 2);
   IndicatorSetString(INDICATOR_SHORTNAME, "Show Money");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
if(prev_calculated == 0)
  {
   Print("prev_calculated = ", prev_calculated);
   if(flag)
    {
     ArrayInitialize(balance, 0.0);
      ArrayInitialize(equityMax, 0.0);
       ArrayInitialize(equityMaxB, 0.0);
      ArrayInitialize(equityMin, 0.0);
     ArrayInitialize(equityMinB, 0.0);
      flag = false;
    }
   return(rates_total);
  }
  double bal = NormalizeDouble(AccountInfoDouble(ACCOUNT_BALANCE), 2);
  double equity = NormalizeDouble(AccountInfoDouble(ACCOUNT_EQUITY), 2);
  if(rates_total > prev_calculated)
   {
    minEquity = 0;
    maxEquity = 0;
   }
   minEquity = NormalizeDouble(fmin((minEquity == 0 ? bal : minEquity), equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);
    balance[0] = bal;
    clrBalance[0] = 0.0;
    equityMinB[0] = balance[0];
    equityMin[0] = minEquity;
    equityMaxB[0] = balance[0];
    equityMax[0] = maxEquity;
    if(balance[0] > balance[1])
     clrBalance[0] = 1.0;
    if(balance[0] < balance[1])
     clrBalance[0] = 2.0;
    Comment(PositionsTotal());
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Potentially, the fault could be here:

if(rates_total > prev_calculated)
   {
    minEquity = 0;
    maxEquity = 0;
   }
   minEquity = NormalizeDouble(fmin((minEquity == 0 ? bal : minEquity), equity), 2);
    maxEquity = NormalizeDouble(fmax(maxEquity, equity), 2);

When a new bar arrives, you reset the values to 0 - fine. But then you check minEquity and 0 for equality, in a way that is not recommended.

In confirmation of my words - your picture. You can see that the "rubbish" values, as you said, are around zero.

And it is better, of course, to add the data window with the "rubbish" value to the figure.
Reason: