CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. -- % of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.

MT4 EA Required - Custom colour for current candle

I have searched high and low for a simple Indicator for MT4 that allows colour customisation of the current open candle. e.g. Current open candle is orange until close when it assumes the appropriate colour for a bull or bear candle. Can anyone help?

2 Likes

Hi @RacingSnake,

I’m not sure if MetaTrader allows direct access to a Bar “Object” via MQL…

But if you simply wanted to “paint over” the existing bar with an EA or Indicator or somewhere around it to achieve the tick-by-tick colour shift depending on the bullish/bearish status of the candle, you could do something like this (I just wrote this up quickly for you, so don’t expect miraculous attention to detail :wink:).

//+---------------------------------------------------------------------------------+
//|                                    DWX_Community_RacingSnake_BarColourQA_v1.mq4 |
//|                                       Copyright 2018, MetaQuotes Software Corp. |
//|                                                            https://www.mql5.com |
//+---------------------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input color BullishColour = clrMediumSpringGreen;
input color BearishColour = clrOrange;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
   ObjectDelete(0, "MyRect");
}
//+------------------------------------------------------------------+
//| 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[])
  {
//---

   // Draw a dynamically updating rectangle on the latest bar
   ObjectDelete("MyRect");
      
   if(Open[0] < Close[0]) // BULLISH
   {
      ObjectCreate("MyRect", OBJ_RECTANGLE, 0, Time[0], Open[0], Time[1], Close[0]);
      ObjectSet("MyRect", OBJPROP_COLOR, BullishColour);
   }
   else
   { // BEARISH
      ObjectCreate("MyRect", OBJ_RECTANGLE, 0, Time[0], Close[0], Time[1], Open[0]);
      ObjectSet("MyRect", OBJPROP_COLOR, BearishColour);
   }
   
   ObjectSet("MyRect", OBJPROP_BACK, true);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

It prints a rectangle on your chart that updates each tick according to the latest candle, changing colour and size depending on the current Open/Close of the candle.

Hope this helps at least progress further with your idea :thumbsup:

{ Cheers }

1 Like

Thanks @integracore2, much appreciated!

Just to clarify, I am trying to make the current candle stay orange while it is open and only take on the bullish or bearish colours of the chart scheme once it is closed, will your code achieve that?

1 Like

Hi @RacingSnake - I’m not on my workstation right now, but this should be fairly straightforward if you modify Open[0] and Close[0] to Open[1] and Close[1] respectively in both ObjectCreate calls.

These values won’t change for the duration of the current bar, so it should keep the rectangle plotted statically on the chart until the current candle closes and the rectangle shifts position again :thumbsup:

Just uploaded it and the orange rectangle is moving tick by tick but it is just to the left of the current candle so I will try your modification of Close[0] to Close[1]

1 Like

That’s what I’m unsure of - I.e. painting over the current bar itself.

If there’s an index shift function of some sort in the documentation that helps plot it over the bar (or an x,y axis alternative), that should probably help.

p.s. Remember that shifts in MetaTrader charts are right-aligned (increasing the shift from 0 to 1, 2 etc) moves your focus to the left not right :bulb:

The Open Close mod to 1 moved the rectangle vertically and lost the tick link too

In the ObjectCreate calls I changed Time[1] to Time[0] and now I suspect the rectangle has disappeared underneath the current candle so I need to bring it to the front.

Ah ok, I think I missed your clarification post earlier:

In this case, you really just need to modify your template so Bullish bars are blue and Bearish bars are red, then modify the initial code to simply paint over or along the current bar in orange.

Yes that is already the case, we are really close now. I have MyRect in the right location and it is moving tick by tick but is hidden behind the current candle. In the Objects list I deselected Draw object as background and clicked OK but it doesn’t seem to take. When I reselect Edit the box is still ticked.

Reset it all to the beginning to show you.

you can change the chart properties with uncheck “chart in foreground”

edit: sorry, it doesn’t work for rectangles.

Yes I even tried changing
ObjectSet(“MyRect”, OBJPROP_BACK, false);
but it just brought an orange line to the front not a rectangle

Yes it seems rectangles cannot easily be brought to the front unless there is a work around

1 Like

Time[0] now lines up with current candle but it is hidden behind so set ObjectSet(“MyRect”, OBJPROP_BACK, false); and now we get this

Anyone had any breakthrough ideas on this?