Drawing Candles, what's wrong?

 

last 2 lines in this code, Tow almost identical IF condition.

The first draw candles in one color which is not the required output.

The second if you removed the remark "//" will show candles in 2 colors

The task simply is to show the first 5 candles in different colors.

What can I do?

Thank you.

//+------------------------------------------------------------------------------+
//|                   Candle Color Indicator                              |
//+------------------------------------------------------------------------------+

#property indicator_chart_window

#property indicator_buffers 5
#property indicator_plots 1 

#property indicator_label1 "Open;High;Low;Close"
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_width1 3

input color Color_Bar_Up_1=clrAqua;
input color Color_Bar_Down_1=clrMaroon;
input color Color_Bar_Up_0=clrGreen;
input color Color_Bar_Down_0=clrRed;

double buf_open[], buf_high[], buf_low[], buf_close[];
double buf_color_line[];  


//+------------------------------------------------------------------+
//| Init function                                                    |
//+------------------------------------------------------------------+
int OnInit()
{
    // Assign buffers
    SetIndexBuffer(0, buf_open, INDICATOR_DATA);
    SetIndexBuffer(1, buf_high, INDICATOR_DATA);
    SetIndexBuffer(2, buf_low, INDICATOR_DATA);
    SetIndexBuffer(3, buf_close, INDICATOR_DATA);
    SetIndexBuffer(4, buf_color_line, INDICATOR_COLOR_INDEX);

    PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_COLOR_CANDLES);
    PlotIndexSetInteger(0, PLOT_COLOR_INDEXES, 4); 

    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, Color_Bar_Up_0);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, Color_Bar_Down_0);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 2, Color_Bar_Up_1);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 3, Color_Bar_Down_1);

    IndicatorSetInteger(0,INDICATOR_COLOR_INDEX, true);
    ChartRedraw();


    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| OnCalculate 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 start = prev_calculated > 0 ? prev_calculated - 1 : 0;
    for (int i = start; i < rates_total; i++)  {
        // Set data for plotting
        buf_open[i]  = open[i];
        buf_high[i]  = high[i];
        buf_low[i]   = low[i];
        buf_close[i] = close[i];
        
        // Those 2 lines the first shows only red candles, the second shows dual colors, Why?
        if (i>=5) buf_color_line[i]=1.0; else buf_color_line[i]=2.0;  
        //if (open[i]<close[i]) buf_color_line[i]=1.0; else buf_color_line[i]=2.0; 
    }

    ChartRedraw();

    return (rates_total);
}


This Code is trying to color candles according to a condition.
if you run the code, each candle will be plotted with a different color scheme
if you uncomment line 67, all candles will apply only one color scheme, although the condition almost the same.

Any one has an idea
Thank you all
//+------------------------------------------------------------------------------+
//|                   Candle Color Indicator                              |
//+------------------------------------------------------------------------------+

#property indicator_chart_window

#property indicator_buffers 5
#property indicator_plots 1 

#property indicator_label1 "Open;High;Low;Close"
#property indicator_type1 DRAW_COLOR_CANDLES
#property indicator_width1 3

input color Color_Bar_Up_1=clrAqua;
input color Color_Bar_Down_1=clrMaroon;
input color Color_Bar_Up_0=clrGreen;
input color Color_Bar_Down_0=clrRed;

double buf_open[], buf_high[], buf_low[], buf_close[];
double buf_color_line[];  

int d2;
bool C2;
//+------------------------------------------------------------------+
//| Init function                                                    |
//+------------------------------------------------------------------+
int OnInit()
{
    // Assign buffers
    SetIndexBuffer(0, buf_open, INDICATOR_DATA);
    SetIndexBuffer(1, buf_high, INDICATOR_DATA);
    SetIndexBuffer(2, buf_low, INDICATOR_DATA);
    SetIndexBuffer(3, buf_close, INDICATOR_DATA);
    SetIndexBuffer(4, buf_color_line, INDICATOR_COLOR_INDEX);
    PlotIndexSetInteger(0, PLOT_COLOR_INDEXES, 4); 

    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 0, Color_Bar_Up_0);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 1, Color_Bar_Down_0);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 2, Color_Bar_Up_1);
    PlotIndexSetInteger(0, PLOT_LINE_COLOR, 3, Color_Bar_Down_1);


    return (INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| OnCalculate 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[])
{
   for(int i=prev_calculated;i<=rates_total-1;i++) {
      buf_open[i]  = open[i];
      buf_high[i]  = high[i];
      buf_low[i]   = low[i];
      buf_close[i] = close[i];

      d2= MathMod(i,2); 
      //if (i>10) d2=0; else d2=1;

      if (d2==0) C2= true; else C2=false;
      if (C2) {
            if(open[i]>=close[i]) buf_color_line[i]=3;
            else buf_color_line[i]=2;
      } else {  
            if(open[i]>=close[i]) buf_color_line[i]=1;
            else buf_color_line[i]=0;
        }
    }

    return (rates_total);
}
 
because you assign the red color to buffer index 1 and the condition i>=5 will always be true after the very first 5 bars exist

the line you comment out is a proper method to coloring the candles as it gives a valid rule for color switching
 
Conor Mcnamara #:
because you assign the red color to buffer index 1 and the condition i>=5 will always be true after the very first 5 bars exist

the line you comment out is a proper method to coloring the candles as it gives a valid rule for color switching

But the condition is working right. I printed the log of results, Color Index Changed after Bar 5. But that does not show on the chart

2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 0 Color Index: 2.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 1 Color Index: 2.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 2 Color Index: 2.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 3 Color Index: 2.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 4 Color Index: 2.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 5 Color Index: 1.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 6 Color Index: 1.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 7 Color Index: 1.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 8 Color Index: 1.0
2025.02.24 12:26:34.481 (USDJPY,H1) Bar: 9 Color Index: 1.0
 
egyptnile #:But the condition is working right. I printed the log of results, Color Index Changed after Bar 5. But that does not show on the chart

Because the first 5 bars is so far back in time that it probably won't be visible on the chart at all. In terms of correctness, the code itself is working fine as far as I can see. Those dates seem wrong and I don't know how you are printing it. If you want bar 0 to 5 to be from newest to oldest then you can set buffers as series

 
egyptnile:
This Code is trying to color candles according to a condition.
if you run the code, each candle will be plotted with a different color scheme
if you uncomment line 67, all candles will apply only one color scheme, although the condition almost the same.

Any one has an idea
Thank you all

You are telling the terminal "if you are above the 10th bar in the entire chart set d2 to 1"

so you are limiting the colors to 2 when the line is uncommented.

This is true for 99.9% of the chart
 

The counter starts from 0 and up. It will be under 10 with the first 10 candles.

What's wrong with the logic? 

 
egyptnile #: The counter starts from 0 and up. It will be under 10 with the first 10 candles. What's wrong with the logic? 

if you mean the 10 bars from the right then change it to 

if (i<(rates_total-10)) d2=0; else d2=1;

bar [0] is the oldest

But then you will also have to paint the bar that slides out of the 10 bars range
 

I printed the color index. the values are right, after bar 10 , values changed


 
Lorentzos Roussos #:

if you mean the 10 bars from the right then change it to 

bar [0] is the oldest

you are excellent

It works.

in MQL4, bar 0 is the first.

Thank you

 
why make two threads which are practically the same?
 

@egyptnile

Please don't post the same topic multiple times. Post only once and in a relevant thread or forum section.

I have merged your two separate topics into just this one.

And when posting log output please also use the CODE button (Alt-S), just as is the case of source code.