Trading Overlays (Price Lines)

Trading overlays are horizontal price lines for order, stop-loss, take-profit, and alert levels. They are drawn on the main chart as y-axis annotations and persist across zoom, theme change, chart-type switch, and streaming appends. Each line is a declarative config you own; ApexStock keeps the source of truth and re-applies it whenever the chart re-renders.

Adding lines

addPriceLine(config) is the generic form. addOrderLine, addStopLoss, addTakeProfit, and addAlert are typed shortcuts that preset the line's type (and, for order lines, color it from side). Each returns the line's id, or null if the config is invalid.

const entryId = apexStock.addOrderLine({ price: 98.5, side: 'buy', label: 'Entry' })
apexStock.addStopLoss({ price: 95 })
apexStock.addTakeProfit({ price: 104 })
apexStock.addAlert({ price: 100, onCross: (e) => notify(e.direction) })

Config reference

Only price is required.

FieldTypeDescription
pricenumberRequired. The y level the line sits at.
idstringStable id; auto-generated ("tl-N") when omitted.
type'order' | 'stop-loss' | 'take-profit' | 'alert'Line kind. Preset by the shortcut methods.
side'buy' | 'sell'For order lines; drives the default color.
labelstringDefaults to a type/side/price summary.
colorstringBorder + label color; defaults from the theme.
textColorstringLabel text color; defaults to white.
strokeDashArraynumberDash length; default varies by type.
widthnumberLine width.
labelPosition'left' | 'right'Which side the label sits on.
draggablebooleanAllow drag-to-reprice; fires onMove on drop.
closablebooleanShow a ✕ affordance; clicking it removes the line and fires onRemove.
onCrossfunctionFired when a closed bar crosses the line.
onMovefunctionFired after a drag completes.
onRemovefunctionFired when removed via the close affordance.
metaanyArbitrary payload, returned by getPriceLine/getPriceLines.

Line colors default from the theme's trading-overlay palette and are re-read on every re-apply, so a theme switch recolors them automatically.

Interactivity

Both interactive behaviors are opt-in per line.

Draggable lines

apexStock.addStopLoss({
  price: 95,
  draggable: true,
  onMove: ({ id, price }) => {
    console.log(`Stop-loss ${id} moved to ${price}`)
  },
})

Dragging repositions the line live; onMove fires once, on drop.

Closable lines

apexStock.addTakeProfit({
  price: 104,
  closable: true,
  onRemove: ({ id }) => console.log(`Removed ${id}`),
})

Crossing alerts

onCross is edge-triggered: it fires when a newly-closed bar (from appendData) crosses the line relative to the previous bar's close. The callback receives { id, type, price, direction, bar }:

apexStock.addAlert({
  price: 100,
  onCross: ({ direction, price }) => {
    // direction reflects which way the close moved through the line
    notify(`Price crossed ${price} (${direction})`)
  },
})

Updating and removing

// Reprice or relabel an existing line
apexStock.updatePriceLine(entryId, { price: 97 })

// Remove one line by id
apexStock.removePriceLine(entryId)

// Remove every line
apexStock.clearPriceLines()

updatePriceLine and removePriceLine return false if no line with that id exists. An auto-generated label tracks the new price/type/side on update; a custom label is preserved unless you pass a new one.

Reading lines back

const line = apexStock.getPriceLine(entryId) // a copy of one line's config, or null
const all = apexStock.getPriceLines()        // copies of every line's config

Both return copies (with any meta you attached), so mutating the result does not affect the live overlays.