Chart output

MQL5 Candlestick Trend Coloring

AlgoSpecial8 November 202611 min read

Coloring candles is a rendering problem, not a signal problem. The trend state must already exist before the colour buffer is written. If the colour logic recalculates structure while drawing, the chart becomes a second, inconsistent engine.

1. Use DRAW_COLOR_CANDLES correctly

A colour-candle plot needs four OHLC buffers and one colour-index buffer. The colour index is not a price and must be set on every bar.

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_label1  "Trend Open;Trend High;Trend Low;Trend Close"
#property indicator_type1   DRAW_COLOR_CANDLES
#property indicator_color1  C'8,153,129',C'242,54,69'

double openBuf[], highBuf[], lowBuf[], closeBuf[], colorBuf[];

int OnInit()
{
   SetIndexBuffer(0, openBuf,  INDICATOR_DATA);
   SetIndexBuffer(1, highBuf,  INDICATOR_DATA);
   SetIndexBuffer(2, lowBuf,   INDICATOR_DATA);
   SetIndexBuffer(3, closeBuf, INDICATOR_DATA);
   SetIndexBuffer(4, colorBuf, INDICATOR_COLOR_INDEX);
   PlotIndexSetInteger(0, PLOT_COLOR_INDEXES, 2);
   return INIT_SUCCEEDED;
}

If trend colouring is disabled, set the plot to DRAW_NONE and make the price buffers EMPTY_VALUE. Leaving stale prices in the buffers changes the chart scale.

2. Colour from committed state, then update structure

The reference captures internalTrend before it processes the current closed bar. The candle is therefore coloured by the structure that was committed on the previous bar; the new event is used for the next candle. Do not update structure first and then colour the same bar.

int colorIndex = (internalTrend == 1) ? 0 : 1;

openBuf[i]  = rates[i].open;
highBuf[i]  = rates[i].high;
lowBuf[i]   = rates[i].low;
closeBuf[i] = rates[i].close;
colorBuf[i] = colorIndex;

// Then update the structure state for the next bar.

The reference uses internal trend for this plot. If the user wants swing colouring as well, expose a separate plot; never mix both trends in one colour buffer.

3. The forming bar is a preview

In real time, the final bar can change. Recalculate the preview on a copy of the state. When the bar closes, commit the colour once. Do not leave drawing objects or buffers in a half-updated state after a failed recalculation.

  • Build committed history from closed bars.
  • Build the last bar preview from a copy.
  • On a new bar, commit the previous preview and discard the old copy.
  • Never revise a closed bar without a documented full-history rebuild.

4. Test cases

CaseExpected
Trend colouring disabledPlot hidden and price buffers empty
No bullish trend yetColour index 1 until a bullish leg is committed, matching the reference
Forming bar changesOnly preview bar changes
History reloadClosed-bar colours are identical
Object limit reachedColour buffers are unaffected

Use the Object Limit Simulator when the same chart also draws labels, lines and boxes.

FAQ

Does colouring candles affect EA signals?

No. The colour is output only. Expose the trend state separately if an EA needs it.

Should I colour from internal or swing trend?

Pick one and document it. Internal colour changes more often; swing colour is slower.

Why do colours look delayed?

The pivot is confirmed only after the right-side bars close. That delay is intentional.

🚀 Invite friends — Earn $5 You both get $5 credit on Go Ad · opencode.ai AI-Powered Coding Agent — Try Free Build apps, fix bugs & ship faster with opencode. Get $5 free credit when you join. × Ad · quo.com QuoPhone — $20 Visa Gift Card Free Sign up to Quo, subscribe 3 months, get a $20 Visa gift card. Atif's referral gift for you. ×