Graphics and alerts
Custom studies can draw objects that are independent of output plots and raise alerts from main(ctx).
Managed graphic primitives
ctx.drawings.line, ctx.drawings.box, and ctx.drawings.primitiveLabel create persistent objects. Their numeric IDs remain valid across bars, so store IDs in ctx.state when an object must be updated later.
Lines
const line = ctx.drawings.line;
if (ctx.state.lineId == null) {
ctx.state.lineId = line.new(ctx.index - 10, ctx.input.low(10), ctx.index, ctx.input.high(), {
xloc: 'bar_index',
extend: 'right',
color: '#2962FF',
width: 2,
style: 'SOLID',
});
} else {
line.setXY2(ctx.state.lineId, ctx.index, ctx.input.high());
}
line.new(x1, y1, x2, y2, options?) returns an ID or null. Options are xloc ('time' or 'bar_index'), extend ('none', 'left', 'right', or 'both'), color, width, style, and visible.
The API also provides setXY1, setXY2, setX1, setX2, setY1, setY2, setXLoc, setExtend, setColor, setStyle, setWidth, and setVisible. Matching getters return the current value or null. Use copy(id), delete(id), and the read-only all array to manage the registry.
Boxes
const id = ctx.drawings.box.new(left, top, right, bottom, {
xloc: 'bar_index',
bgColor: 'rgba(41, 98, 255, 0.2)',
borderColor: '#2962FF',
borderWidth: 1,
borderStyle: 'SOLID',
});
The box API provides setters and getters for left, right, top, bottom, xloc, borderColor, borderWidth, borderStyle, bgColor, and visible. It also provides setLeftTop, setRightBottom, copy, delete, and all.
Primitive labels
const id = ctx.drawings.primitiveLabel.new(ctx.index, ctx.input.high(), 'Breakout', {
xloc: 'bar_index',
yloc: 'abovebar',
color: '#2962FF',
textColor: '#FFFFFF',
tooltip: 'Price moved above the range',
});
Primitive labels are persistent managed objects. Their API includes setters and getters for coordinates, text, xloc, yloc, background color, style, textColor, size, textAlign, tooltip, font family, text formatting, and visibility, plus copy, delete, and all.
Per-bar graphics
These functions emit output for the current calculation bar; they do not return persistent IDs.
Labels and bubbles
ctx.drawings.label(signal, 'Buy', '#00A86B', 'ABOVE');
ctx.drawings.bubble(signal, ctx.input.low(), 'Buy', '#00A86B', {
placement: 'BOTTOM',
alignment: 'CENTER',
style: 'ARROW',
});
drawings.label(visible, text, backgroundColor, position?) emits a simple per-bar label. Do not confuse it with the managed drawings.primitiveLabel API.
drawings.bubble(visible, price, text, color, options?) emits a bubble. The final argument can be a boolean for legacy compatibility or an object with placement, alignment, and style.
Candles
ctx.drawings.plotcandle(open, high, low, close, {
title: 'Synthetic candle',
color: close >= open ? '#00A86B' : '#E53935',
wickColor: '#757575',
borderColor: '#212121',
showLast: 100,
});
plotcandle(open, high, low, close, options?) draws a candle for the current calculation bar on every chart type. Non-finite OHLC values are ignored.
To recolor an existing chart candle instead, use:
ctx.chart.changeCandleColor(ctx.index, '#2962FF');
ctx.chart.clearCandleColor(ctx.index);
These methods only affect real candles, not expansion candles.
Primitive limits
Lines, boxes, and primitive labels each keep up to 50 objects by default. Configure their limits in metainfo:
metainfo: {
// ...
maxLinesCount: 200,
maxBoxesCount: 100,
maxLabelsCount: 150,
}
Each value is capped at 500. When a registry exceeds its limit, the oldest object is removed. The legacy keys maxLines, maxBoxes, and maxLabels are accepted for migration only; use the *Count names in new studies. Per-bar labels, bubbles, and plot candles do not use these registries.
Alerts
const crossed = ctx.utils.cross(ctx.input.close(), ctx.parameters.level, ctx.index);
ctx.alert(crossed, 'Price crossed the level', 'CANDLE', 'BELL', 'level-cross');
Signature:
alert(
condition: boolean,
message: string,
mode: 'ONCE' | 'CANDLE' | 'TICK',
sound: 'NO' | 'BELL',
id?: string,
): boolean
The function returns condition, so it can be used inside an expression.
ONCEfires one time for an alert ID.CANDLEfires at most once per candle and alert ID.TICKcan fire again when the live candle timestamp or close changes.BARis a legacy alias forCANDLE.- An omitted
iddefaults to the message. - Alerts are evaluated only for the latest live candle. An empty message, a false condition, or a hidden custom study does not produce an alert.
- Unknown modes fall back to
CANDLE; unknown sounds fall back toNO.
Colors
Use valid CSS color values supported by DXcharts. Invalid configured study colors fall back to #FF00FF. Invalid runtime plot color and fillColor values are sanitized and produce a console warning. Managed line, box, and primitive-label color setters ignore invalid values.