This guide will be particularly interesting to MQL4 or MQL5 coders who work with chart objects in MetaTrader.
Drawing a chart rectangle using MQL4 and MQL5 code
Drawing a rectangle in an ObjectCreate()
function with rather simple parameters. For example, in MQL4 it goes like this:
ObjectCreate("Rectangle", OBJ_RECTANGLE, 0, Time[0], price1, Time[5], price2);
And in MQL5 it would look almost the same:
ObjectCreate(0, "Rectangle", OBJ_RECTANGLE, 0, iTime(NULL, 0, 0), price1, iTime(NULL, 0, 5), price2);
But filling the rectangle in MQL5 or drawing it unfilled in MQL4 wasn't so obvious to me. The rectangles come filled by default in MT4 and unfilled by default in MT5.
Here is an example of a default rectangle in MetaTrader 4:
And here is a rectangle with default properties in MetaTrader 5:
Changing filled status of rectangles
OBJPROP_BACK
is the property that responds for this in MetaTrader 4 (MQL4). For example, to make a rectangle unfilled in MQL4 you can call this function:
ObjectSet("Rectangle", OBJPROP_BACK, false);
This is what happens to the rectangle from the chart above when the function is called in MT4:
However, in MQL5 (MetaTrader 5), OBJPROP_BACK
only controls whether the chart object is drawn behind the chart or on top of the chart. To actually fill a rectangle in MQL5, you would have to use a different property — OBJPROP_FILL
, switching it to true:
ObjectSetInteger(0, "Rectangle", OBJPROP_FILL, true);
After execution, the previous example rectangle changes to this:
Drawing chart rectangles manually in MT4 and MT5
If you would rather draw a chart rectangle manually in MetaTrader, then you can do so easily via Insert->Shapes->Rectangle in MT4:
The filled/unfilled property can then be controlled via the rectangle's properties window (select the rectangle by double-clicking it, right-click, and select Rectangle properties...) in the Common tab:
In MetaTrader 5, this is a bit different. To add a rectangle to your chart, you need to use Insert->Objects->Shapes->Rectangle menu in MT5:
To make the rectangle unfilled, you also have to go the rectangle's properties window (select the rectangle by double-clicking it, right-click, and select Properties of ...) in the Parameters tab:
Frequently asked questions
How do I fill or unfill all the rectangles on the current chart using MQL4/MQL5?
The following code makes all rectangles on the current chart unfilled in MetaTrader 4:
// Get the total number objects on the chart. int total = ObjectsTotal(ChartID()); // Cycle through all objects. for (int i = 0; i < total; i++) { // Get the object's name. string object_name = ObjectName(ChartID(), i); // If the object is not a rectangle - skip. if (ObjectGetInteger(ChartID(), object_name, OBJPROP_TYPE) != OBJ_RECTANGLE) continue; // Change filled status to false. ObjectSet(object_name, OBJPROP_BACK, false); }
And just a little bit different piece of code fills all rectangle on the current chart in MetaTrader 5:
// Get the total number objects on the chart. int total = ObjectsTotal(ChartID()); // Cycle through all objects. for (int i = 0; i < total; i++) { // Get the object's name. string object_name = ObjectName(ChartID(), i); // If the object is not a rectangle - skip. if (ObjectGetInteger(ChartID(), object_name, OBJPROP_TYPE) != OBJ_RECTANGLE) continue; // Change filled status to false. ObjectSetInteger(ChartID(), object_name, OBJPROP_FILL, true); }
If you have any questions or comments regarding drawing of the rectangles in MQL4 or MQL5, please use our Forex forum to get in touch with fellow coders and traders.