box.new() in MQL5: OBJ_RECTANGLE, fill colour and border width
Short answer
box.new() becomes an OBJ_RECTANGLE created with ObjectCreate(0, name, OBJ_RECTANGLE, 0, t1, top, t2, bottom), with OBJPROP_FILL to paint the inside. A MetaTrader rectangle has one colour for border and fill and no transparency, so a translucent box with a strong border needs two objects or a compromise.
box.new() draws a rectangle between two bar/price corners, with a border colour and an optional background fill. MQL5's OBJ_RECTANGLE is the equivalent object: two anchors given as time and price, a colour, a border width and style, and OBJPROP_FILL to paint the inside. The difference that matters is that a MetaTrader rectangle has one colour for both border and fill and no transparency, so a Pine box with a pale translucent background and a strong border becomes two objects or a compromise.
What box.new() computes
box.new(left, top, right, bottom, border_color, border_width, border_style, extend, xloc, bgcolor, text, ...) creates a box whose left and right edges are bar indexes (or millisecond times with xloc.bar_time) and whose top and bottom are prices. bgcolor fills it; border_color, border_width and border_style draw the outline; extend stretches the box horizontally like a line's ray; text writes inside it. The call returns a box id for box.set_left, box.set_right, box.set_top, box.set_bottom, box.set_bgcolor and box.delete, so scripts often extend a box to the current bar on every bar until price breaks out of it. A script keeps at most max_boxes_count boxes (default 50, maximum 500), oldest removed first, and boxes created on the realtime bar are rolled back each tick.
The MQL5 equivalent
The Pine version boxes the high-low range of every 20-bar block.
//@version=6
indicator("Box demo", overlay=true, max_boxes_count=20)
len = input.int(20)
if bar_index % len == 0
box.new(bar_index - len, ta.highest(high, len), bar_index, ta.lowest(low, len), border_color=color.blue, border_width=1, bgcolor=color.new(color.blue, 90))In MQL5 each box is an OBJ_RECTANGLE named after its left bar's time, drawn behind the candles with OBJPROP_BACK, with a ring of names standing in for max_boxes_count. A pale colour replaces color.new(color.blue, 90), because object colours have no alpha channel.
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0
input int InpLength = 20; // Range length
input int InpMaxBoxes = 20; // max_boxes_count
string gBoxes[];
// box.new(left, top, right, bottom, border_color, border_width, bgcolor): an OBJ_RECTANGLE
void BoxNew(const datetime t1, const double top, const datetime t2, const double bottom,
const color clr, const int borderWidth, const bool filled)
{
string nm = "box_" + IntegerToString((long)t1);
if(ObjectFind(0, nm) >= 0)
return; // this range is already drawn
if(!ObjectCreate(0, nm, OBJ_RECTANGLE, 0, t1, top, t2, bottom))
return;
ObjectSetInteger(0, nm, OBJPROP_COLOR, clr); // border AND fill: one colour only
ObjectSetInteger(0, nm, OBJPROP_WIDTH, borderWidth);
ObjectSetInteger(0, nm, OBJPROP_FILL, filled); // bgcolor != na
ObjectSetInteger(0, nm, OBJPROP_BACK, true); // draw behind the candles
ObjectSetInteger(0, nm, OBJPROP_SELECTABLE, false);
int n = ArraySize(gBoxes);
ArrayResize(gBoxes, n + 1);
gBoxes[n] = nm;
if(n + 1 > InpMaxBoxes) // Pine removes the oldest box past the limit
{
ObjectDelete(0, gBoxes[0]);
ArrayRemove(gBoxes, 0, 1);
}
}
int OnInit() { return INIT_SUCCEEDED; }
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 = MathMax(prev_calculated - 1, InpLength);
for(int i = start; i < rates_total; i++)
{
if(i % InpLength != 0) // bar_index % len == 0
continue;
double hh = high[i], ll = low[i];
for(int k = i - InpLength + 1; k < i; k++)
{
hh = MathMax(hh, high[k]);
ll = MathMin(ll, low[k]);
}
// left = bar_index - len -> time[i - InpLength]; right = bar_index -> time[i]
BoxNew(time[i - InpLength], hh, time[i], ll, C'214,226,251', 1, true);
}
return rates_total;
}
void OnDeinit(const int reason) { ObjectsDeleteAll(0, "box_"); }The setters map to property writes: box.set_right(id, x) is ObjectSetInteger(0, name, OBJPROP_TIME, 1, t), box.set_top is ObjectSetDouble(0, name, OBJPROP_PRICE, 0, p), and box.set_bgcolor is OBJPROP_COLOR with OBJPROP_FILL on. extend.right has no rectangle property; move the right anchor to time[last] + n * PeriodSeconds() or keep updating it. Box text is a separate OBJ_TEXT placed at a corner. If you need a different border colour and fill colour, draw a filled OBJ_RECTANGLE with OBJPROP_BACK and a second unfilled one on top for the border.
Where the two differ
| Topic | Pine Script | MQL5 |
|---|---|---|
| Colours | Separate border_color and bgcolor, both with alpha | One OBJPROP_COLOR, opaque; OBJPROP_FILL on or off |
| Transparency | color.new(c, 90) | None; choose a pale colour and set OBJPROP_BACK so candles stay visible |
| Corners | Bar index or ms time, price | datetime in seconds, price; the anchor order (which corner is first) does not matter |
| Extension | extend.right / left / both | Not available; move the anchor |
| Text inside | text argument | Separate OBJ_TEXT |
| Limit and lifetime | max_boxes_count, removed with the script | Unlimited; persists until deleted in OnDeinit |
| Realtime bar | Rolled back each tick | Persistent; name by bar time and skip if it exists |
What the Stratlate converter emits
box.new(left, top, right, bottom) is emitted as pine_box_new(left, top, right, bottom), a runtime helper that creates an OBJ_RECTANGLE named pine_box_N with bar indexes converted to times and returns an integer id; the setters become pine_box_set_left, pine_box_set_right, pine_box_set_top, pine_box_set_bottom and pine_box_delete, and box.copy clones the object. When border_color, border_width or bgcolor are passed the creation is wrapped in pine_box_style(...), which sets OBJPROP_COLOR to the background colour if one is given and otherwise to the border colour, turns OBJPROP_FILL on when a background colour exists, and writes OBJPROP_WIDTH. A color.new transparency is blended towards the chart background by pine_color_new. border_style, extend and text are not applied. The runtime honours max_boxes_count from the indicator() call (the test above emitted __pineMaxBoxes = 20) and deletes the oldest box past it. The script converts CLEAN; compile and compare the boxes on a chart.
Pitfalls
- Filling with the border colour at full strength. The rectangle hides the candles. Pick a pale colour and set
OBJPROP_BACK = true. - Creating a box whose name already exists.
ObjectCreatereturns false and the box silently keeps its old coordinates; checkObjectFindand move the anchors instead. - Passing bar indexes as times. The box is drawn in 1970 and never seen.
- Updating the right edge with a new object per bar. The chart accumulates one rectangle per bar; reuse the name and set
OBJPROP_TIME, 1. - Omitting
ObjectsDeleteAllinOnDeinit. Range boxes from an old version of the indicator stay on the chart and confuse the next test. - Expecting
extend.right. There is no such property; a box that should run to the right edge needs its anchor moved on each new bar.
FAQ
How do I get a different border and fill colour?
Draw two rectangles with the same anchors: a filled one with OBJPROP_BACK in the fill colour and an unfilled one on top in the border colour. Move both when the box changes.
Can an MQL5 rectangle be semi-transparent?
No. Object colours are opaque; OBJPROP_BACK keeps the candles in front, and a colour mixed towards the chart background imitates transparency. That is also what the converter does for color.new.
What about boxes in a separate indicator window?
Pass the subwindow index instead of 0 to ObjectCreate; ChartWindowFind() returns the indicator's own window. Prices are then in the indicator's scale, as a Pine box in a non-overlay script would be.
Convert the whole script
Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter