Drawing Objects
The draw.* calls put named objects on the chart: levels, trend lines, zones, Fibonacci, channels, labels and symbols. A script can find, read and move them, read the trader's own drawings, and let the trader drag its objects and react in ChartEvent(). Indicators and strategies both draw.
Buffers vs. Objects
| Function / Property | Description |
|---|---|
display { } buffers | One value per bar: lines, histograms, arrows, text, zigzag … over the whole history. See Display & Visuals. |
draw.* objects | A few named objects at any time and price: a level, a zone, a trend line, a label. Created, moved and deleted by name. |
Use buffers for "something on every bar" and objects for "this one thing, here". Both can be used in the same script.
Rules
- Same name = same object. Calling again with the same name moves / restyles it. There are never duplicates.
- Arguments are positional. Trailing ones can be left out (defaults in the tables below).
- Times are bar times (
times[i]fromtime()). Prices are in chart price units. - Colours:
"#RRGGBB","#RRGGBBAA","rgba(r,g,b,a)"or a colour name. Styles:"solid","dashed","dotted". - Only what changed reaches the chart, so
draw.DeleteAll()+ redrawing everything every tick is fine. - While the script runs, its objects are locked: you can't select or erase them by hand (unless the script makes them editable), and they aren't saved with the chart.
- You draw it, it stays; you clear it, it goes. Removing a script does not delete its objects. See Lifecycle.
- The strategy tester draws nothing, so drawing costs nothing in a backtest or optimization.
Creating Objects
| Function / Property | Description |
|---|---|
draw.Line(name, time1, price1, time2, price2, color, width, style, ray) | Line between two points. ray = true extends it past point 2. Defaults: "#2962ff", 1, "solid", false. |
draw.HLine(name, price, color, width, style) | Horizontal level across the chart. Defaults: "#9c27b0", 1, "solid". |
draw.VLine(name, time, color, width, style) | Vertical line at a bar. Defaults: "#9c27b0", 1, "solid". |
draw.HRay(name, time, price, color, width, style) | Horizontal ray from time to the right edge. Defaults: "#9c27b0", 1, "solid". |
draw.Cross(name, time, price, color, width, style) | Crosshair lines through a point. Defaults: "#9c27b0", 1, "dotted". |
draw.Rectangle(name, time1, price1, time2, price2, color, fill, width) | Zone: border color, inside fill. Defaults: "#2962ff", "rgba(41,98,255,0.12)", 1. |
draw.Triangle(name, time1, price1, time2, price2, time3, price3, color, width) | Three-point outline. Defaults: "#2962ff", 1. |
draw.Ellipse(name, time1, price1, time2, price2, color, width) | Ellipse inside the box (time1, price1)–(time2, price2). Defaults: "#2962ff", 1. |
draw.Fib(name, time1, price1, time2, price2, color, width) | Fibonacci retracement between two points. Defaults: "#ff9800", 1. |
draw.Channel(name, time1, price1, time2, price2, price3, color, width) | Parallel channel. price3 = the parallel line's price at time1. Defaults: "#2962ff", 1. |
draw.Position(name, time, entry, target, long, color) | Long (long = true) or short position box from entry to target. Defaults: true, "#787b86". |
draw.Polyline(name, color, width, style) | A path through any number of points; add them with PolyPoint. Defaults: "#2962ff", 1, "solid". |
draw.PolyPoint(name, index, time, price) | Set point index of a polyline. An index past the end appends. |
draw.Arrow(name, time, price, up, color, size) | ▲ (up = true) or ▼ marker. Defaults: true, "#26a69a", 1. |
draw.Symbol(name, time, price, code, color, size) | Any arrow code (Arrow.Star, 252 …) centred on the point. size = font px. Defaults: 233, "#ffffff", 14. |
draw.Label(name, time, price, text, color, size) | Text centred on the point. size = font px. Defaults: "", "#ffffff", 12. |
Defaults are for the optional trailing arguments, in order. Symbol codes are listed on Arrow Codes.
calculate() {
double highs[] = high();
double lows[] = low();
double closes[] = close();
datetime times[] = time();
int last = length(closes) - 1;
double hi = highs[last];
double lo = lows[last];
for (int k = 1; k < 20; k = k + 1) {
if (highs[last - k] > hi) { hi = highs[last - k]; }
if (lows[last - k] < lo) { lo = lows[last - k]; }
}
draw.HLine("hi20", hi, "#ef4444", 1.0, "dashed");
draw.HLine("lo20", lo, "#22c55e", 1.0, "dashed");
draw.Rectangle("zone", times[last - 19], hi, times[last], lo, "#3b82f6", "rgba(59,130,246,0.10)");
draw.Label("close", times[last - 3], closes[last], "Close " + ToString(closes[last]));
draw.Symbol("top", times[last], hi, Arrow.Star, "#f59e0b", 18.0);
return closes;
}Delete, Hide, Exists
| Function / Property | Returns | Description |
|---|---|---|
draw.Delete(name) | void | Remove one object. |
draw.DeletePrefix(prefix) | void | Remove every object whose name starts with prefix ("swing_", "tmp_" …). |
draw.DeleteAll() | void | Remove all of this script's objects. |
draw.SetVisible(name, visible) | void | Hide / show. Kept when the object is redrawn. |
draw.Exists(name) | bool | Whether the object exists. Use it to create an object only once. |
Text on Objects
Any object can carry text: draw.SetText(name, text) (a Label takes its text directly). Set the position, font and direction once. They stay when the object is redrawn every tick.
| Function / Property | Description |
|---|---|
draw.SetText(name, text) | Text shown on the object. |
draw.SetTextPos(name, align, valign) | align "left" / "center" / "right", valign "top" / "middle" / "bottom". |
draw.SetFont(name, size, color) | Size in px (0 keeps it), colour ("" = the object's colour). |
draw.SetVertical(name, true) | Text written bottom to top. |
What the positions mean per kind:
| Function / Property | Parameters | Description |
|---|---|---|
HLine | chart left / centre / right | above / on the line (the line leaves a gap) / below. Default: right, above. |
VLine | left of / on / right of the line | top / middle / bottom of the chart. |
Line, ray | at the start / middle / end point | above / on / below the line. |
Rectangle | inside: left / centre / right | inside: top / middle / bottom (9 places). |
Label, Symbol | left of / centred on / right of the point | above / centred / below the point. |
draw.HLine("res", hi, "#f97316", 1.0, "dashed");
draw.SetText("res", "Resistance");
draw.SetTextPos("res", "left", "top"); // above the line, at the chart's left
draw.SetFont("res", 14.0, "#f97316");Find, Read, Modify
Objects are kept in name order. A prefix limits a call to names starting with it ("" = all).
| Function / Property | Returns | Description |
|---|---|---|
draw.Count(prefix) | int | Number of objects. |
draw.Name(index, prefix) | string | Name of object index ("" past the end). |
draw.Kind(name) | string | "line", "hline", "vline", "hray", "cross", "rectangle", "triangle", "ellipse", "fib", "channel", "position", "polyline", "arrow", "label", "symbol". |
draw.Points(name) | int | Number of points (Line / Rectangle / Fib 2, Triangle / Channel 3, Polyline its points, others 1). |
draw.Price(name, point) | double | A point's price (point 0, 1, 2 …). NaN when it has none (a VLine has no price). |
draw.Time(name, point) | datetime | A point's time. 0 when it has none (an HLine has no time). |
draw.LineValue(name, time) | double | A Line's price at time (on the segment, or past point 2 for a ray); an HLine / HRay price; NaN otherwise. |
draw.Color / Text / Style / Fill (name) | string | That property. |
draw.Screen(name) | string | The window anchor of a draw.SetScreen object as text ("left-top", "right-middle" …; "" = placed by time / price). |
draw.Width(name) | double | Line width. |
draw.Visible(name) | bool | Shown or hidden. |
draw.IsUser(name) | bool | true for the trader's own drawings (user: names). |
draw.Move(name, point, time, price) | void | Move one point (HLine: price only, VLine: time only). |
draw.SetColor / SetWidth / SetStyle / SetText / SetFill (name, value) | void | Change one property. |
String helpers for name filters: String.StartsWith(text, prefix), String.EndsWith(text, suffix), String.Contains, String.Find (index or -1), String.Length, String.Substring.
continue.for (int i = draw.Count() - 1; i >= 0; i = i - 1) {
string n = draw.Name(i);
if (!String.StartsWith(n, "swing_")) { continue; } // only swings
if (draw.Time(n, 0) < times[last - 30]) { // older than 30 bars: delete
draw.Delete(n);
continue;
}
if (draw.Price(n, 0) < closes[last]) { draw.SetColor(n, "#22c55e"); }
else { draw.SetColor(n, "#ef4444"); }
}
// A trend line as a signal
double lv = draw.LineValue("tl", times[last]);
if (closes[last] > lv) { /* price is above the line */ }Window Position
draw.SetScreen(name, anchor) pins any object to the chart window instead of a time and price. Its points become (x px, y px) counted from the anchor, so you can build any panel, badge, table or gauge from ordinary rectangles, labels, lines and symbols. There is no special panel type: you code the layout you want.
The anchor is one of 9 Anchor.* names. Type Anchor. and the editor lists them; a wrong name is a compile error.
Anchor.TopLeftAnchor.TopAnchor.TopRightAnchor.LeftAnchor.CenterAnchor.RightAnchor.BottomLeftAnchor.BottomAnchor.BottomRight| Function / Property | Description |
|---|---|
draw.SetScreen(name, anchor) | Pin the object to that anchor. Anchor.None = back to time / price. Kept when the object is redrawn. |
x | Pixels away from the left edge (left anchors) or the right edge (right anchors, counting leftwards). Centre anchors: pixels to the right of the centre. |
y | Pixels away from the top edge (top anchors, downwards) or the bottom edge (bottom anchors, upwards). Middle anchors: pixels below the middle. |
draw.Screen(name) | The anchor as text ("left-top", "right-middle" …; "" = time / price). |
- Opaque or see-through is just the colour:
rgba(16, 44, 109, 1)is solid,rgba(16, 44, 109, 0.6)see-through. Text is always drawn on top of fills. - It stays put while the chart scrolls or zooms and new bars arrive, and keeps its pixel size.
- No price / time axis labels, not selectable or draggable (it never blocks chart panning).
- Removed with its script: a pinned object holds pixels, so it never stays behind as a chart object.
- Pinned to the main price chart (not indicator sub-windows).
draw.Rectangle("box", 12, 90, 262, 240, "#334155", "rgba(15,23,42,0.6)"); // x1, y1, x2, y2 in px
draw.SetScreen("box", Anchor.TopLeft);
draw.Label("bal", 22, 108, "Balance " + ToString(ToDouble(Account.Balance(), 2)), "#e2e8f0", 11.0);
draw.SetScreen("bal", Anchor.TopLeft);
draw.SetTextPos("bal", "right", "middle"); // the text starts at the point and runs rightInteractive Objects
draw.SetEditable(name, true) lets the trader drag the object on the chart. Where it was dropped is what draw.Price / draw.Time return from then on, so a line becomes an input: create it once, then read it. A script redraw never moves an object while the trader is dragging it.
Tick() {
if (!Position.Exists(Symbol(), Magic)) { return; }
int ticket = Position.Ticket(Symbol(), Magic);
double posSL = Position.SL(Symbol(), Magic);
if (!draw.Exists("sl")) { // created once
draw.HLine("sl", posSL, "#ef4444", 2.0, "dashed");
draw.SetEditable("sl", true);
}
double lineSL = draw.Price("sl", 0); // where the trader left it
if (MathAbs(lineSL - posSL) > Symbol.Point()) {
Trade.Modify(ticket, Symbol(), Symbol.NormalizePrice(lineSL), -1.0); // -1 keeps the TP
}
}Only editable objects take the mouse (drag / click). The others never get in the way of chart panning.
ChartEvent()
Strategies can react to the mouse in a ChartEvent() { } block. Like Trade(), it runs once per event, on the next tick, oldest first. Read the event with Event.*.
| Function / Property | Parameters | Description |
|---|---|---|
Event.ObjectDragged | an editable object was dropped | Event.Object() = its name. Event.Price() / Time() = its (first) point. |
Event.ObjectClicked | an editable object was clicked (pressed and released in place) | Event.Object() = its name. Event.Price() / Time() = where it was clicked. |
Event.ChartClick | empty chart was clicked (not while drawing) | Event.Object() = "". Event.Price() / Time() = where it was clicked. |
ChartEvent() {
if (Event.Kind() == Event.ObjectDragged && Event.Object() == "sl") { /* modify the SL */ }
if (Event.Kind() == Event.ObjectClicked) { Print("clicked " + Event.Object()); }
if (Event.Kind() == Event.ChartClick) { draw.HLine("entry", Event.Price(), "#2962ff"); }
}ChartEvent() is for strategies. Indicators read dropped positions with draw.Price / draw.Time. Several clicks between two ticks arrive as several calls in one go, in order.The Trader's Drawings
Everything the trader draws by hand is visible to every script on that chart, named user:<label> (user:Trend Line, user:Trend Line 2, user:Horizontal Line …), through the same calls: draw.Count("user:"), draw.Name(i, "user:"), draw.Kind, draw.Price, draw.LineValue …
| Function / Property | Description |
|---|---|
Trend line, ray | "line" (a ray extends past point 2 in LineValue) |
Horizontal line / vertical line | "hline" / "vline" |
Rectangle, triangle | "rectangle", "triangle" |
Arrow marks | "arrow" |
Text, notes | "label" |
Other tools (Fibonacci, channels …) | Their tool name, with their points. |
They are read-only for scripts: Move, Set…, Delete and DeletePrefix never change them, and a script can't create a user: name. draw.IsUser(name) tells them apart. When the trader moves a drawing, scripts see the new place on the next tick.
for (int i = 0; i < draw.Count("user:"); i = i + 1) {
string u = draw.Name(i, "user:");
if (draw.Kind(u) != "line") { continue; }
double lv = draw.LineValue(u, times[last]);
if (closes[last - 1] < lv && closes[last] > lv) { /* crossed up through the trader's line */ }
}Lifecycle
You draw it, it stays; you clear it, it goes. Nothing is cleared automatically.
Exit() {
draw.DeleteAll(); // nothing stays
}
// or keep some
Exit() {
draw.DeletePrefix("tmp_"); // temporary objects go, the rest stays on the chart
}Drawing List
The chart's drawing list has a Script objects section, grouped by script. Each object (or a whole script) has an eye toggle to hide it on the chart; a hidden object stays hidden when the script redraws it. A lock icon marks objects owned by a running script: they have no delete button there (the script owns them). Once the script is removed, its leftover objects move to the normal list.
Limits & Performance
| Function / Property | Description |
|---|---|
5,000 objects per script | Extra names are ignored (no error, no crash). |
Only changes are sent | Each tick the chart receives only objects that were added, changed or deleted. Redrawing thousands of unchanged objects costs nothing on the chart. |
Strategy tester | draw.* does nothing there: no cost in backtests or optimization. |
Example: Every Object
A grey MA plus: dashed levels at the 20-bar high / low, a dotted VLine 30 bars back, a trend line and a ray, a zone around the last 20 bars, a triangle, ▲ / ▼ arrows on the current bar, a live "Close …" label and a ★ above the high. The "temp" line (deleted) and the "hidden" line never show. Journal once: draw.Exists: hi20=true temp=false.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "draw.* objects";
}
display {
outputs: 1
buffer[0] { type: "line"; color: "#64748b"; name: "MA 20"; width: 1; };
}
parameters {
int range = 20; // Range (bars)
}
static int t10_printed = 0;
calculate() {
double highs[] = high();
double lows[] = low();
double closes[] = close();
datetime times[] = time();
int len = length(closes);
double ma[] = create_array(len);
for (int i = range; i < len; i = i + 1) {
double s = 0.0;
for (int j = 0; j < range; j = j + 1) { s = s + closes[i - j]; }
ma[i] = s / range;
}
if (len < 40) { return ma; }
int last = len - 1;
// 20-bar high / low and where they are
int hiBar = last;
int loBar = last;
for (int k = 0; k < range; k = k + 1) {
if (highs[last - k] > highs[hiBar]) { hiBar = last - k; }
if (lows[last - k] < lows[loBar]) { loBar = last - k; }
}
double hi = highs[hiBar];
double lo = lows[loBar];
double pad = 10.0 * Symbol.Point();
// Levels
draw.HLine("hi20", hi, "#ef4444", 1.0, "dashed");
draw.HLine("lo20", lo, "#22c55e", 1.0, "dashed");
draw.VLine("v30", times[last - 30], "#a855f7", 1.0, "dotted");
// Lines
draw.Line("trend", times[last - 30], lows[last - 30], times[last], lows[last], "#3b82f6", 2.0);
draw.Line("ray", times[last - 25], highs[last - 25], times[last - 10], highs[last - 10], "#f97316", 1.0, "solid", true);
// Shapes
draw.Rectangle("zone", times[last - range + 1], hi, times[last], lo, "#3b82f6", "rgba(59,130,246,0.10)");
draw.Triangle("tri", times[hiBar], hi, times[loBar], lo, times[last], closes[last], "#eab308");
// Markers and text
draw.Arrow("up", times[last], lows[last] - pad, true, "#22c55e");
draw.Arrow("down", times[last], highs[last] + pad, false, "#ef4444");
draw.Label("close", times[last - 3], closes[last], "Close " + ToString(closes[last]), "#ffffff", 12.0);
draw.Symbol("star", times[hiBar], hi + 3.0 * pad, Arrow.Star, "#f59e0b", 18.0);
// Delete / SetVisible: neither may show
draw.HLine("temp", (hi + lo) / 2.0, "#ffffff");
draw.Delete("temp");
draw.HLine("hidden", (hi + lo) / 2.0, "#ffffff");
draw.SetVisible("hidden", false);
if (t10_printed == 0) {
Print("draw.Exists: hi20=" + ToString(draw.Exists("hi20")) + " temp=" + ToString(draw.Exists("temp")));
t10_printed = 1;
}
return ma;
}
Exit() {
draw.DeleteAll();
}Example: Text & More Kinds
Text in every position (above / on / below levels, vertical text on a VLine, text at a line's start or end, inside a rectangle, labels either side of a point), plus Fibonacci, channel, ellipse, horizontal ray, cross, a long position box and a polyline. Journal once: [TEXT] lvl text=R ABOVE-LEFT points: fib=2 channel=3 poly=4 hray@last=…
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Text positions and more draw kinds";
}
display {
outputs: 1
buffer[0] { type: "line"; color: "#334155"; name: "Close"; width: 1; };
}
static int t15_printed = 0;
calculate() {
double highs[] = high();
double lows[] = low();
double closes[] = close();
datetime times[] = time();
int len = length(closes);
if (len < 50) { return closes; }
int last = len - 1;
double hi = highs[last];
double lo = lows[last];
for (int k = 1; k < 30; k = k + 1) {
if (highs[last - k] > hi) { hi = highs[last - k]; }
if (lows[last - k] < lo) { lo = lows[last - k]; }
}
double hi15 = highs[last];
double lo15 = lows[last];
for (int k = 1; k < 15; k = k + 1) {
if (highs[last - k] > hi15) { hi15 = highs[last - k]; }
if (lows[last - k] < lo15) { lo15 = lows[last - k]; }
}
// ── Text ──
draw.HLine("lvl", hi, "#f97316", 1.0, "dashed");
draw.SetText("lvl", "R ABOVE-LEFT");
draw.SetTextPos("lvl", "left", "top");
draw.SetFont("lvl", 14.0, "#f97316");
draw.HLine("lvl_lo", lo, "#22c55e", 1.0, "dashed");
draw.SetText("lvl_lo", "S BELOW-RIGHT");
draw.SetTextPos("lvl_lo", "right", "bottom");
draw.HLine("lvl_mid", (hi + lo) / 2.0, "#94a3b8", 1.0, "solid");
draw.SetText("lvl_mid", "MID ON LINE");
draw.SetTextPos("lvl_mid", "center", "middle");
draw.VLine("v40", times[last - 40], "#a855f7", 1.0, "dotted");
draw.SetText("v40", "VERTICAL TEXT");
draw.SetTextPos("v40", "right", "top");
draw.SetVertical("v40", true);
draw.Line("tl_a", times[last - 25], lows[last - 25], times[last - 5], lows[last - 5], "#3b82f6", 2.0);
draw.SetText("tl_a", "start above");
draw.SetTextPos("tl_a", "left", "top");
draw.Line("tl_b", times[last - 25], highs[last - 25], times[last - 5], highs[last - 5], "#22d3ee", 1.0);
draw.SetText("tl_b", "end below");
draw.SetTextPos("tl_b", "right", "bottom");
draw.Rectangle("box", times[last - 14], hi15, times[last], lo15, "#64748b", "rgba(100,116,139,0.08)");
draw.SetText("box", "TOP-LEFT");
draw.SetTextPos("box", "left", "top");
draw.Label("lab_l", times[last], closes[last], "◀ L", "#ffffff", 12.0);
draw.SetTextPos("lab_l", "left", "middle");
draw.Label("lab_r", times[last], closes[last], "R ▶", "#ffffff", 12.0);
draw.SetTextPos("lab_r", "right", "middle");
// ── More kinds ──
draw.Fib("fib", times[last - 30], hi, times[last], lo);
draw.Channel("chan", times[last - 30], lows[last - 30], times[last], lows[last], highs[last - 30], "#a855f7");
draw.Ellipse("ell", times[last - 14], hi15, times[last], lo15, "#22c55e", 1.0);
draw.HRay("hray", times[last - 20], closes[last], "#ef4444", 1.0, "dashed");
draw.Cross("cross", times[last], highs[last], "#facc15", 1.0, "dotted");
draw.Position("pos", times[last], closes[last], hi, true);
draw.Polyline("poly", "#ffffff", 2.0, "solid");
int p = 0;
for (int k = last - 11; k <= last; k = k + 3) {
draw.PolyPoint("poly", p, times[k], closes[k]);
p = p + 1;
}
if (t15_printed == 0) {
Print("[TEXT] lvl text=" + draw.Text("lvl") + " points: fib=" + ToString(draw.Points("fib"))
+ " channel=" + ToString(draw.Points("chan")) + " poly=" + ToString(draw.Points("poly"))
+ " hray@last=" + ToString(draw.LineValue("hray", times[last])));
t15_printed = 1;
}
return closes;
}
Exit() {
draw.DeleteAll();
}Example: Loop & Modify
● markers over the swing highs of the last 30 bars (older ones are deleted by the loop), green below the close and red above it. A ray with "above line" / "below line" from draw.LineValue, a yellow line created once and then moved to the close every tick, and a live count of the objects.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "draw.* loop, read, modify, group delete";
}
display {
outputs: 1
buffer[0] { type: "line"; color: "#334155"; name: "Close"; width: 1; };
}
parameters {
int keep = 30; // Keep swings of the last N bars
}
static int t12_len = 0;
calculate() {
double highs[] = high();
double lows[] = low();
double closes[] = close();
datetime times[] = time();
int len = length(closes);
if (len < 70) { return closes; }
int last = len - 1;
// Create swing markers over the last 60 bars (same name = same object every tick)
for (int k = last - 60; k < last; k = k + 1) {
if (highs[k] > highs[k - 1] && highs[k] > highs[k + 1]) {
draw.Symbol("swing_" + ToString(k), times[k], highs[k] + 5.0 * Symbol.Point(), Arrow.Circle, "#94a3b8", 10.0);
}
}
// Loop over ALL objects: skip everything that isn't a swing, delete old swings, recolour
// the rest. Backwards, because a delete moves every later index down by one.
for (int i = draw.Count() - 1; i >= 0; i = i - 1) {
string n = draw.Name(i);
if (!String.StartsWith(n, "swing_")) { continue; }
if (draw.Time(n, 0) < times[last - keep]) {
draw.Delete(n);
continue;
}
if (draw.Price(n, 0) < closes[last]) {
draw.SetColor(n, "#22c55e");
} else {
draw.SetColor(n, "#ef4444");
}
}
// A ray and its value at the last bar
draw.Line("tl", times[last - 40], lows[last - 40], times[last - 10], lows[last - 10], "#3b82f6", 1.0, "solid", true);
double lv = draw.LineValue("tl", times[last]);
string side = "below line";
if (closes[last] > lv) { side = "above line"; }
draw.Label("tl_lbl", times[last], lv, side, "#93c5fd", 11.0);
// Created once, then only moved
if (!draw.Exists("follow")) {
draw.HLine("follow", closes[last], "#facc15", 1.0, "dashed");
}
draw.Move("follow", 0, times[last], closes[last]);
// Counts (prefix and all)
int swings = draw.Count("swing_");
draw.Label("counts", times[last - 20], highs[last] + 40.0 * Symbol.Point(), "objects " + ToString(draw.Count()) + " | swings " + ToString(swings), "#ffffff", 12.0);
if (len != t12_len) {
string first = draw.Name(0, "swing_");
Print("[DRAW] objects=" + ToString(draw.Count()) + " swings=" + ToString(swings) + " first=" + first
+ " kind=" + draw.Kind(first) + " line@last=" + ToString(lv));
t12_len = len;
}
return closes;
}
Exit() {
draw.DeleteAll();
}Example: Keep or Clear
Two green lines (keep_) and two red ones (tmp_). Remove the indicator: the red lines disappear, the green ones stay as ordinary chart objects. Add it again and it takes the green lines back.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "draw.* objects stay unless Exit() deletes them";
}
display {
outputs: 1
buffer[0] { type: "line"; color: "#334155"; name: "Close"; width: 1; };
}
calculate() {
double highs[] = high();
double lows[] = low();
double closes[] = close();
int len = length(closes);
if (len < 25) { return closes; }
int last = len - 1;
double hi = highs[last];
double lo = lows[last];
for (int k = 1; k < 20; k = k + 1) {
if (highs[last - k] > hi) { hi = highs[last - k]; }
if (lows[last - k] < lo) { lo = lows[last - k]; }
}
draw.HLine("keep_hi", hi, "#22c55e", 1.0, "dashed");
draw.HLine("keep_lo", lo, "#22c55e", 1.0, "dashed");
draw.HLine("tmp_mid", (hi + lo) / 2.0, "#ef4444", 1.0, "dotted");
draw.HLine("tmp_close", closes[last], "#ef4444", 1.0, "dotted");
return closes;
}
Exit() {
// Only the temporary objects; keep_* stay on the chart
draw.DeletePrefix("tmp_");
}Example: The Trader's Lines
Draw a trend line and a horizontal line by hand, then add this. Each of your drawings gets a cyan "◀ user:…" label and an "above" / "below" label at the last bar; move your drawing and the labels follow. The script also tries to recolour and delete your drawings every tick to show they are protected: the Journal prints protected=true.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Read the trader's drawings";
}
display {
outputs: 1
buffer[0] { type: "line"; color: "#334155"; name: "Close"; width: 1; };
}
static int t14_len = 0;
calculate() {
double closes[] = close();
datetime times[] = time();
int len = length(closes);
if (len < 5) { return closes; }
int last = len - 1;
bool _log = len != t14_len;
t14_len = len;
draw.DeletePrefix("seen_");
int n = draw.Count("user:");
for (int i = 0; i < n; i = i + 1) {
string u = draw.Name(i, "user:");
if (!draw.IsUser(u)) { continue; }
string kind = draw.Kind(u);
double p0 = draw.Price(u, 0);
datetime t0 = draw.Time(u, 0);
if (t0 == 0) { t0 = times[last - 2]; } // a horizontal line has no time
if (kind == "vline") { p0 = closes[last]; } // a vertical line has no price (NaN)
draw.Label("seen_" + u, t0, p0, "◀ " + u, "#22d3ee", 10.0);
double lv = draw.LineValue(u, times[last]);
if (kind == "line" || kind == "hline") {
string side = "below";
if (closes[last] > lv) { side = "above"; }
draw.Label("seen_side_" + u, times[last], lv, side, "#22d3ee", 10.0);
}
// Protection check: none of these may change the trader's drawing
string colorBefore = draw.Color(u);
draw.SetColor(u, "#ff00ff");
draw.Delete(u);
bool kept = draw.Exists(u) && draw.Color(u) == colorBefore;
if (_log) {
Print("[USER] " + u + " kind=" + kind + " points=" + ToString(draw.Points(u))
+ " p0=" + ToString(p0) + " t0=" + ToString(draw.Time(u, 0))
+ " line@last=" + ToString(lv) + " isUser=" + ToString(draw.IsUser(u))
+ " protected=" + ToString(kept));
}
}
if (_log && n == 0) {
Print("[USER] no user drawings on this chart - draw a Trend Line and a Horizontal Line");
}
return closes;
}
Exit() {
draw.DeleteAll(); // only this script's labels; your drawings are never touched
}Example: Drag SL / TP Lines
A strategy (no trading) with a red "SL" and a green "TP" line, both draggable. Drag one and drop it: [EVENT] dragged sl to …, and the line stays where you dropped it. Click TP: [EVENT] clicked tp and its label turns "tp ✓". Click empty chart: [EVENT] chart click at … and a yellow ● appears there. A real strategy would call Trade.Modify with draw.Price("sl", 0) where this prints.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Draggable lines + ChartEvent()";
}
static int t12_clicks = 0;
Tick() {
double bid = Symbol.Bid();
double pt = Symbol.Point();
// Created once: after that the trader moves them, the script only reads them
if (!draw.Exists("sl")) {
draw.HLine("sl", bid - 30.0 * pt, "#ef4444", 2.0, "dashed");
draw.SetText("sl", "SL (drag me)");
draw.SetTextPos("sl", "left", "top");
draw.SetEditable("sl", true);
}
if (!draw.Exists("tp")) {
draw.HLine("tp", bid + 30.0 * pt, "#22c55e", 2.0, "dashed");
draw.SetText("tp", "TP (click me)");
draw.SetTextPos("tp", "left", "top");
draw.SetEditable("tp", true);
}
if (Time.IsNewBar()) {
Print("[TICK] sl line at " + ToString(draw.Price("sl", 0)) + " tp line at " + ToString(draw.Price("tp", 0)));
}
}
ChartEvent() {
if (Event.Kind() == Event.ObjectDragged) {
Print("[EVENT] dragged " + Event.Object() + " to " + ToString(Event.Price()));
}
if (Event.Kind() == Event.ObjectClicked) {
Print("[EVENT] clicked " + Event.Object());
if (Event.Object() == "tp") { draw.SetText("tp", "tp ✓"); }
}
if (Event.Kind() == Event.ChartClick) {
Print("[EVENT] chart click at " + ToString(Event.Price()));
t12_clicks = t12_clicks + 1;
draw.Symbol("click_" + ToString(t12_clicks), Event.Time(), Event.Price(), Arrow.Circle, "#facc15", 12.0);
}
}
Exit() {
draw.DeleteAll(); // leave this out and the lines stay on the chart as ordinary objects
}Example: 4,000 Objects Every Tick
A ● over every bar's high and under every low (up to 2,000 bars = 4,000 objects), deleted and redrawn on every tick, plus a line at the last close. The chart stays smooth: only the last bar's objects and the line actually change, so only those are sent.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "draw.* stress (4,000 objects redrawn each tick)";
}
display {
outputs: 1
buffer[0] { type: "line"; color: "#334155"; name: "Close"; width: 1; };
}
parameters {
int max_bars = 2000; // Bars with markers
}
calculate() {
double highs[] = high();
double lows[] = low();
double closes[] = close();
datetime times[] = time();
int len = length(closes);
int from = len - max_bars;
if (from < 0) { from = 0; }
draw.DeleteAll();
for (int i = from; i < len; i = i + 1) {
draw.Symbol("h" + ToString(i), times[i], highs[i], Arrow.Circle, "#22d3ee", 8.0);
draw.Symbol("l" + ToString(i), times[i], lows[i], Arrow.Circle, "#e879f9", 8.0);
}
draw.HLine("last", closes[len - 1], "#facc15", 1.0, "solid");
return closes;
}
Exit() {
draw.DeleteAll();
}Example: Account Box
A see-through box pinned 12 px from the chart's left edge and 90 px from its top, with the account name, balance, equity, live profit (green / red), margin and margin level, each shown with exactly 2 decimals (ToString(ToDouble(x, 2))). A "LIVE" badge sits in the right-bottom corner (Anchor.BottomRight) and the spread at the middle of the right edge (Anchor.Right). Scroll, zoom or wait for a new bar: neither moves. Open a position and the profit follows every tick.
about {
author: "CoreXTrader";
version: "1.0.0";
about: "Account details in a window-pinned box";
}
parameters {
int box_x = 12; // Box left (px from the chart's left edge)
int box_y = 90; // Box top (px from the chart's top edge)
int box_w = 250; // Box width (px)
int box_h = 150; // Box height (px)
int row = 22; // Line height (px)
}
Tick() {
// The box: x / y in pixels from the left-top corner (y grows downwards)
draw.Rectangle("acct_box", box_x, box_y, box_x + box_w, box_y + box_h, "#334155", "rgba(15,23,42,0.6)", 1.0);
draw.SetScreen("acct_box", Anchor.TopLeft);
double pnl = Account.Profit();
string pnlColor = "#22c55e";
if (pnl < 0.0) { pnlColor = "#ef4444"; }
int tx = box_x + 10;
int ty = box_y + 18;
acctLine("acct_0", tx, ty, "ACCOUNT " + Account.Name() + " #" + ToString(Account.Number()), "#f8fafc", 13.0);
acctLine("acct_1", tx, ty + row, "Balance " + ToString(ToDouble(Account.Balance(), 2)) + " " + Account.Currency(), "#e2e8f0", 11.0);
acctLine("acct_2", tx, ty + 2 * row, "Equity " + ToString(ToDouble(Account.Equity(), 2)), "#e2e8f0", 11.0);
acctLine("acct_3", tx, ty + 3 * row, "Profit " + ToString(ToDouble(pnl, 2)), pnlColor, 11.0);
acctLine("acct_4", tx, ty + 4 * row, "Margin " + ToString(ToDouble(Account.Margin(), 2)) + " / free " + ToString(ToDouble(Account.FreeMargin(), 2)), "#e2e8f0", 11.0);
acctLine("acct_5", tx, ty + 5 * row, "Level " + ToString(ToDouble(Account.Level(), 2)) + " %", "#e2e8f0", 11.0);
// Another corner: a badge counted from the right-bottom corner (x leftwards, y upwards)
draw.Rectangle("acct_badge", 70, 40, 12, 14, "#22c55e", "rgba(34,197,94,0.25)", 1.0);
draw.SetScreen("acct_badge", Anchor.BottomRight);
draw.Label("acct_badge_txt", 41, 27, "LIVE", "#22c55e", 12.0);
draw.SetScreen("acct_badge_txt", Anchor.BottomRight);
// Middle of the right edge: x runs left from the right edge, y down from the middle
draw.Label("acct_spread", 12, 0, "spread " + ToString(ToDouble((Symbol.Ask() - Symbol.Bid()) / Symbol.Point(), 1)), "#94a3b8", 11.0);
draw.SetScreen("acct_spread", Anchor.Right);
draw.SetTextPos("acct_spread", "left", "middle"); // the text ends at the point
}
// One left-aligned text line pinned to the window: the text starts at (x, y) and runs right
void acctLine(string name, int x, int y, string text, string color, double size) {
draw.Label(name, x, y, text, color, size);
draw.SetScreen(name, Anchor.TopLeft);
draw.SetTextPos(name, "right", "middle");
}
Exit() {
draw.DeletePrefix("acct_");
}Not Yet Available
- Scripts changing the trader's own drawings (they are read-only today).
- The chart window's size for scripts (to centre or stretch window-pinned objects), and window-pinned objects in indicator sub-windows.
- Drawings in the visual tester.
- Gann, pitchfork and Elliott tools as
draw.*calls (the trader can still draw them by hand, and scripts can read them).