Example 01
OHLC bars
Bars with open and close ticks instead of candles. The chart draws no candles for a chart type: the script draws every bar.

- Draws each visible bar around its slot on the time axis.
- Reports high, low and close, so autoscale and the crosshair magnet follow the bars.
- Draws its own last price line and price label.
1import { defineScript, input } from "@tradingthings/tt-create";2 3/** Chart type: OHLC bars instead of candles, with the last price on the price axis. */4export default defineScript({5 name: "OHLC bars",6 surface: { kind: "ChartSeries" },7 inputs: {8 up: input.color({ default: "#26a69a", label: "Up" }),9 down: input.color({ default: "#ef5350", label: "Down" }),10 },11 subscribe: (ctx) => ({ bars: ctx.market.bars() }),12 state: () => ({}),13 draw: (_s, frame, g, ctx) => {14 const { time, open, high, low, close, length } = ctx.streams.bars;15 const { up, down } = ctx.inputs;16 const from = frame.barTimeAt(frame.visibleTime.from) ?? frame.visibleTime.from;17 const tick = Math.max(1, frame.barSpacing * 0.3);18 19 for (let i = 0; i < length; i += 1) {20 if (time[i]! < from || time[i]! > frame.visibleTime.to) continue;21 22 const x = frame.timeToX(time[i]!);23 24 if (x === null) continue;25 26 const color = close[i]! >= open[i]! ? up : down;27 28 // Autoscale and the crosshair magnet use these prices.29 g.priceValue(time[i]!, high[i]!);30 g.priceValue(time[i]!, low[i]!);31 g.priceValue(time[i]!, close[i]!);32 g.line(x, frame.priceToY(high[i]!), x, frame.priceToY(low[i]!), { color, width: 1 });33 g.line(x - tick, frame.priceToY(open[i]!), x, frame.priceToY(open[i]!), { color, width: 1 });34 g.line(x, frame.priceToY(close[i]!), x + tick, frame.priceToY(close[i]!), { color, width: 1 });35 }36 37 const last = length - 1;38 39 if (last < 0) return;40 41 const color = close[last]! >= open[last]! ? up : down;42 const y = frame.priceToY(close[last]!);43 44 // The chart draws no last price for a chart type: the script does.45 g.line(0, y, frame.width, y, { color, width: 1, dash: [2, 2] });46 g.priceLabel(close[last]!, frame.formatPrice(close[last]!), color, "#ffffff");47 },48});49 
