iCustom returns slightly wrong values

 

Hi
I found the topics like "iCustom returns wrong values" but this is little bit different.

I try to create indicator which compares size of identical type of bars (bull/bear type) in a sequence.
and shoes maximum values of comparison in histogram.
like this.
max_value
this result is made by many indicators which reads data each other to make simple comparison between bar_0 and bar_1 only.
and takes long time to be added to chart (more then 3min).
//-----------------------------
with ArrayMaximum the result is more faster. but there is something wrong with my code.
iCustom_diff
//-------------------------------
here is the code which compares iCustom values and returns wrong values:

#property indicator_separate_window
#property indicator_buffers 4


#property indicator_color1 DeepSkyBlue
#property indicator_width1 10


#property indicator_color2 Red
#property indicator_width2 10



#property indicator_color3 DodgerBlue
#property indicator_width3 4


#property indicator_color4 Crimson
#property indicator_width4 4


#property indicator_level1 0
#property indicator_levelcolor White
#property indicator_levelstyle 0

// exported variables


// local variables
string LF = "\n";  // use this in custom or utility blocks where you need line feeds
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0; // variable points to current bar




double Buffer1[];
double Buffer2[];

double Buffer3[];
double Buffer4[];





double bar_0;
double bar_1;
double bar_2;
double bar_3;
double bar_4;
double bar_5;
double bar_6;
double bar_7;
double bar_8;
double bar_9;
double bar_10;
double bar_11;






//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
    if (false) ObjectsDeleteAll();      // clear the chart

    IndicatorDigits(Digits-5);
    IndicatorBuffers(4);
    
    SetIndexBuffer(0, Buffer1);
    SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID);
    
    SetIndexBuffer(1, Buffer2);
    SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID);
    
    
    
    
    SetIndexBuffer(2, Buffer3);
    SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID);
    
    SetIndexBuffer(3, Buffer4);
    SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_SOLID);
    
    
    return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
if (false) ObjectsDeleteAll();

return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator start function                                  |
//+------------------------------------------------------------------+
int start()
{
OnEveryTick1();

return(0);
}

//+------------------------------------------------------------------+

void OnEveryTick1()
{
    
    int i;
    int counted_bars = IndicatorCounted();
    if(counted_bars < 0) return(-1);
    if(counted_bars > 0) counted_bars--;
    i = Bars - counted_bars - 1;
    // main calculation loop
    while (i >= 0)
    
{
current = i;
        
//current  >0
//current+1=0
TechnicalAnalysis00();

//current  >0
//current+1>0
//current+2=0
TechnicalAnalysis01();

//current  >0
//current+1>0
//current+2>0
//current+3=0
TechnicalAnalysis02();






i--;
}
}


//-----------------------------------------------------
//---------bar_0-------------------------------
//-----------------------------------------------------
void TechnicalAnalysis00()
{

bar_0 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current);
bar_1 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current+1);

if
(
bar_0 > 0
&&
bar_1 == 0
)

{
Histogram00();
}

}

void Histogram00()
{
Buffer1[current]= bar_0;
}
//-----------------------------------------------------
//---------bar_1-------------------------------
//-----------------------------------------------------
void TechnicalAnalysis01()
{

bar_0 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current);
bar_1 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current+1);
bar_2 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current+2);

if
(
bar_0 > 0
&&
bar_1 > 0
&&
bar_2 == 0
)

{
Histogram01();
}

}

void Histogram01()
{
int x_up_01[] = {0,0};

x_up_01[0] = bar_0;
x_up_01[1] = bar_1;

int maxValue_01 = ArrayMaximum(x_up_01);

Buffer1[current]= x_up_01[maxValue_01];
}
//-----------------------------------------------------
//---------bar_2-------------------------------
//-----------------------------------------------------
void TechnicalAnalysis02()
{

bar_0 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current);
bar_1 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current+1);
bar_2 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current+2);
bar_3 = iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current+3);

if
(
bar_0 > 0
&&
bar_1 > 0
&&
bar_2 > 0
&&
bar_3 == 0
)

{
Histogram02();
}

}

void Histogram02()
{
int x_up_02[] = {0,0,0};

x_up_02[0] = bar_0;
x_up_02[1] = bar_1;
x_up_02[2] = bar_2;

int maxValue_02 = ArrayMaximum(x_up_02);

Buffer1[current]= x_up_02[maxValue_02];
}
//-----------------------------------------------------
//---------bar_3-------------------------------
//-----------------------------------------------------

where is my mistake?

main code (first histogram) and second code (second histogram) based on it (on the second picture):

 
iCustom(NULL, NULL, "candles_from_chart-celi_masivi",0,current);


next

iCustom(NULL,0,"SampleInd",13,1,0);

and

double  iCustom( 
   string       symbol,           // symbol 
   int          timeframe,        // timeframe 
   string       name,             // path/name of the custom indicator compiled program 
   ...                            // custom indicator input parameters (if necessary) 
   int          mode,             // line index 
   int          shift             // shift 
   );
 
GrumpyDuckMan:


next

and

I don't understand . 

Reason: