drawing
Drawing-tool objects. A script places Long / Short positions drawn exactly as the chart's own Long position / Short position tools (the profit and risk zones, the R:R, price and % tags, the member's tool colours and theme), and, when the member allows it, reads the member's own drawings on this chart (drawing.user).
#Functions
#drawing.deletefunction
Removes the position (na is ignored).
drawing.delete(id: drawing)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Example
const p = drawing.position("long", close, close - 100, close + 200)
if (!barstate.islast) {
p.delete()
}Since ChartScript 1
#drawing.get_entryfunction
The entry price.
drawing.get_entry(id: drawing): float| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns float.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const e = p.get_entry()
}Since ChartScript 1
#drawing.get_fromfunction
The start bar.
drawing.get_from(id: drawing): int| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns int.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const x0 = p.get_from()
}Since ChartScript 1
#drawing.get_qtyfunction
The size (na when none).
drawing.get_qty(id: drawing): float| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns float.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const q = p.get_qty()
}Since ChartScript 1
#drawing.get_resultfunction
"open", "won", "lost" or "closed".
drawing.get_result(id: drawing): string| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns string.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const r = p.get_result()
}Since ChartScript 1
#drawing.get_sidefunction
"long" or "short".
drawing.get_side(id: drawing): string| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns string.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const s = p.get_side()
}Since ChartScript 1
#drawing.get_stopfunction
The stop price.
drawing.get_stop(id: drawing): float| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns float.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const s = p.get_stop()
}Since ChartScript 1
#drawing.get_targetfunction
The target price.
drawing.get_target(id: drawing): float| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns float.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const t = p.get_target()
}Since ChartScript 1
#drawing.get_titlefunction
The title.
drawing.get_title(id: drawing): string| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns string.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const t = p.get_title()
}Since ChartScript 1
#drawing.get_tofunction
The end bar.
drawing.get_to(id: drawing): int| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
Returns int.
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
const x1 = p.get_to()
}Since ChartScript 1
#drawing.positionfunction
A Long or Short position drawn with the drawing tools' own Long position / Short position look: the target zone, the stop zone, the entry line and the tags (target and stop with the move, % and ticks or P&L, the middle tag with the side, entry and R:R), in the member's colours for that tool. It belongs to the script: it is removed and drawn again with every run, the oldest goes past max_drawings_count (50 by default, 500 at most), a live tick's changes are rolled back like box.*, and hovering it shows the script's card. The member can press "Edit this trade" on that card (or double-click the position) to turn it into their own editable drawing: the script's version is hidden while theirs exists, and comes back when they delete it.
drawing.position(side: string, entry: float, stop: float, target: float, { from?: int = na, to?: int = na, qty?: float = na, title?: string = "", tooltip?: string = "", result?: string = "open", xloc?: simple string = xloc.bar_index }): drawing| Parameter | Type | Description |
|---|---|---|
side | string | "long" (drawing.side_long) or "short" (drawing.side_short); another text is error CS2014. |
entry | float | The entry price. |
stop | float | The stop price (below the entry for a long, above it for a short). |
target | float | The target price (above the entry for a long, below it for a short). |
from | intdefault na | The bar the position starts at (na: the current bar), read as xloc says. |
to | intdefault na | The bar the position ends at (na: 20 bars after from), read as xloc says. Extend it with set_to while the trade is open. |
qty | floatdefault na | The size in base units (na: none). With a size the target and stop tags show the P&L instead of ticks. |
title | stringdefault "" | A short name shown first in the middle tag ("" = none); 60 characters at most. |
tooltip | stringdefault "" | The hover text (the script's card); "" shows the title, the side and the R:R. |
result | stringdefault "open" | How the trade ended: "open" (the default), "won" (the target zone stands out), "lost" (the stop zone stands out) or "closed" (both zones faded). The middle tag names it. |
xloc | simple stringdefault xloc.bar_index | How from and to are read: xloc.bar_index (the default) or xloc.bar_time. |
Returns drawing.
Example
const atr = ta.atr(14)
if (ta.crossover(ta.ema(close, 9), ta.ema(close, 21))) {
drawing.position("long", close, close - 1.5 * atr, close + 3 * atr, { to: bar_index + 20, title: "EMA cross" })
}Since ChartScript 1
#drawing.set_entryfunction
Sets the entry price.
drawing.set_entry(id: drawing, price: float)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
price | float | The price. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_entry(close)
}Since ChartScript 1
#drawing.set_fromfunction
Moves the start of the position to another bar.
drawing.set_from(id: drawing, x: int)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
x | int | A bar index (or a time when the position was made with xloc.bar_time). |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_from(bar_index - 5)
}Since ChartScript 1
#drawing.set_qtyfunction
Sets the size (na: none).
drawing.set_qty(id: drawing, qty: float)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
qty | float | The size in base units (na: none). With a size the target and stop tags show the P&L instead of ticks. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_qty(0.5)
}Since ChartScript 1
#drawing.set_resultfunction
Sets how the trade ended: "open", "won", "lost" or "closed" (the settled look: the zone that was hit stands out and the middle tag names the result).
drawing.set_result(id: drawing, result: string)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
result | string | How the trade ended: "open" (the default), "won" (the target zone stands out), "lost" (the stop zone stands out) or "closed" (both zones faded). The middle tag names it. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
if (high >= p.get_target()) {
p.set_result(drawing.result_won)
}
}Since ChartScript 1
#drawing.set_sidefunction
Turns the position into a long or a short.
drawing.set_side(id: drawing, side: string)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
side | string | "long" (drawing.side_long) or "short" (drawing.side_short); another text is error CS2014. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_side(drawing.side_short)
}Since ChartScript 1
#drawing.set_stopfunction
Sets the stop price.
drawing.set_stop(id: drawing, price: float)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
price | float | The price. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_stop(low)
}Since ChartScript 1
#drawing.set_targetfunction
Sets the target price.
drawing.set_target(id: drawing, price: float)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
price | float | The price. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_target(high * 1.01)
}Since ChartScript 1
#drawing.set_titlefunction
Sets the title in the middle tag.
drawing.set_title(id: drawing, text: string)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
text | string | The text. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_title("Breakout")
}Since ChartScript 1
#drawing.set_tofunction
Moves the end of the position (extend it each bar while the trade is open).
drawing.set_to(id: drawing, x: int)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
x | int | A bar index (or a time when the position was made with xloc.bar_time). |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_to(bar_index + 10)
}Since ChartScript 1
#drawing.set_tooltipfunction
Sets the hover text.
drawing.set_tooltip(id: drawing, text: string)| Parameter | Type | Description |
|---|---|---|
id | drawing | The position. |
text | string | The text. |
Example
state p: drawing = na
if (barstate.islast && na(p)) {
p = drawing.position("long", close, close * 0.99, close * 1.02, { title: "Plan" })
}
if (!na(p)) {
p.set_tooltip("Entry on the close of the breakout bar")
}Since ChartScript 1
#drawing.userfunction
The member's own drawings of one kind on THIS chart (this market, this member), as read-only drawing.user_item records, oldest first (200 at most). Off by default: the script gets an empty list until the member turns on "Allow this script to read my drawings on this chart" in the script's settings (Style tab); drawing.user_allowed is then true. When the member adds, moves or removes a drawing the script runs again (after a short pause). Server runs (protected scripts, alerts, alert pictures) always get an empty list. Fields: kind ("hline", "trend", "position" or "rect"), tool (the drawing tool: "hline", "hray", "trend", "ray", "extended", "infoline", "trendangle", "long", "short", "rect"), price / time / index (the first point: its price, time in ms and bar index, na before the first bar), price2 / time2 / index2 (the second point; a horizontal line repeats the first), entry / stop / target and side ("long" / "short") for a position (na / "" otherwise), top / bottom (the highest and lowest price of the drawing) and text (the drawing's text, 200 characters at most).
drawing.user({ kind?: string = "all" }): array<drawing.user_item>| Parameter | Type | Description |
|---|---|---|
kind | stringdefault "all" | "all" (the default), "hline" (horizontal lines and rays), "trend" (trend lines, rays, extended and info lines), "position" (Long / Short positions) or "rect" (rectangles). |
Returns array<drawing.user_item>.
Example
const mine = drawing.user("hline")
let near = 0.0
for (const d of mine) {
if (math.abs(d.price - close) < ta.atr(14)) {
near += 1
}
}
plot(near, "My levels near the price")Since ChartScript 1
#Variables
#drawing.allvariable
Every position of the script that still exists, oldest first.
drawing.all: array<drawing>Type series array<drawing>.
Example
const open = drawing.all.size()Since ChartScript 1
#drawing.user_allowedvariable
true when the member allowed this script to read their drawings on this chart (drawing.user then returns them); false otherwise, and always false in server runs.
drawing.user_allowed: boolType simple bool.
Example
const allowed = drawing.user_allowedSince ChartScript 1
#Constants
| Name | Type | Description |
|---|---|---|
drawing.result_closed | string | The result "closed" (closed before either: both zones faded). |
drawing.result_lost | string | The result "lost" (the stop was hit). |
drawing.result_open | string | The result "open" (the trade is running: the normal look). |
drawing.result_won | string | The result "won" (the target was reached). |
drawing.side_long | string | The side "long". |
drawing.side_short | string | The side "short". |