Roboquant
Roboquant engine

Roboquant Indicators

A custom chart indicator is a compiled .rq program that implements BarIndicator. It uses the same SDK as a strategy: the same #[strategy] and #[param] attributes, the same built-in indicators (init.add(...)), the same plots and the same drawings. What it lacks is the account and order surface: an indicator has no ctx.buy/ctx.sell, no position and no on_trade. It is attached to a RoboCharts chart and never places orders, so it cannot be backtested or deployed.

Because the chart code is identical, an indicator becomes a strategy by changing impl BarIndicator to impl Strategy, &mut ChartCtx to &mut Ctx, and adding orders. Plots and drawings stay as they are.

Minimal example

use rq_sdk::prelude::*;

#[strategy(name = "RSI Levels")]
pub struct RsiLevels {
    #[param(default = 14, min = 2, max = 200, title = "RSI length")]
    length: i64,
    #[param(default = 70, min = 50, max = 100, title = "Overbought")]
    upper: i64,
    #[param(default = 30, min = 0, max = 50, title = "Oversold")]
    lower: i64,
    rsi: Ind,
    line: PlotId,
    marks: PlotId,
}

impl BarIndicator for RsiLevels {
    fn on_init(&mut self, init: &mut Init) {
        self.rsi = init.add(Rsi::new(self.length as usize));
        self.line = init.plot_line("RSI").color("#A78BFA").pane("rsi").register();
        self.marks = init
            .plot_markers("Oversold")
            .shape(MarkerShape::TriangleUp)
            .color("#22C55E")
            .register();
    }

    fn on_bar(&mut self, ctx: &mut ChartCtx, bar: Bar) {
        if ctx.bar_index() == 0 {
            // Levels are drawings in the indicator's pane, created once.
            ctx.draw(DrawingSpecV2::hline(self.upper as f64).pane("rsi").color("#EF5350"));
            ctx.draw(DrawingSpecV2::hline(self.lower as f64).pane("rsi").color("#26A69A"));
        }
        let Some(r) = ctx.val(self.rsi) else { return }; // warm-up leaves a gap
        ctx.plot(self.line, r);
        if r < self.lower as f64 {
            ctx.marker(self.marks, bar.low);
            ctx.bar_color("#22C55E");
        }
    }
}

Shape

  • on_init(&mut self, init: &mut Init) runs once. Register built-in indicators with init.add(spec) -> Ind and plots with the builders below, and keep the handles in struct fields. Parameters are #[param] fields (i64, f64 or bool), read as self.x.
  • on_bar(&mut self, ctx: &mut ChartCtx, bar: Bar) receives one closed bar at a time, oldest first. Keep running state in fields (or a Rolling<f64> window) and never rescan history. ctx.bar(back) reads recent bars (0 is the current one), ctx.bar_index() the position, ctx.time() the current time, and ctx.val(ind) / ctx.val_back(ind, n) built-in values (None during warm-up).
  • The struct derives nothing itself: #[strategy] generates Default and the metadata, and the compiler adds the export for you.

Plots

Register each plot in on_init; every builder ends with .register() and returns a PlotId.

BuilderExtra modifiers
init.plot_line(name)
init.plot_histogram(name).baseline(v)
init.plot_band(name).opacity(f)
init.plot_markers(name).shape(MarkerShape::Circle | TriangleUp | TriangleDown | Cross)
init.plot_candles(name).up_color(css), .down_color(css)

Common modifiers: .color(css); .pane("price") overlays the candles, any other name opens a separate pane; .axis_label(true) prints the plot name on the price scale.

Feed plots from on_bar (or on_tick):

  • ctx.plot(id, value), ctx.band(id, upper, lower), ctx.marker(id, price), ctx.candle(id, open, high, low, close).
  • ctx.bar_color(css) recolors the current market bar without changing it.
  • A bar where a plot is not fed is a gap; that is how warm-up renders.

Drawings

Levels, zones and labels are drawings, the same calls a strategy uses: ctx.plot_zone(high, low), ctx.plot_level(price), ctx.plot_hline(price), ctx.plot_text(text, price) and ctx.draw(spec), then ctx.update, ctx.freeze, ctx.invalidate and ctx.delete on the returned DrawingId. See the Runtime Reference for the full drawing API.

Ticks and the order book

An indicator opts into ticks exactly like a strategy: implement fn wants_ticks(&self) -> bool { true } and fn on_tick(&mut self, ctx: &mut ChartCtx, tick: Tick). A bar's ticks are delivered before its on_bar, so per-tick state is complete when on_bar reads it. tick.side is the aggressor side (Aggressor::Buy, Sell or Unknown).

use rq_sdk::prelude::*;

#[strategy(name = "CVD")]
pub struct Cvd {
    delta: f64,
    cvd: f64,
    p_delta: PlotId,
    p_cvd: PlotId,
}

impl BarIndicator for Cvd {
    fn on_init(&mut self, init: &mut Init) {
        self.p_delta = init.plot_histogram("Delta").pane("cvd").register();
        self.p_cvd = init.plot_line("CVD").pane("cvd").register();
    }

    fn wants_ticks(&self) -> bool { true }

    fn on_tick(&mut self, _ctx: &mut ChartCtx, tick: Tick) {
        match tick.side {
            Aggressor::Buy => self.delta += tick.size,
            Aggressor::Sell => self.delta -= tick.size,
            Aggressor::Unknown => {}
        }
    }

    fn on_bar(&mut self, ctx: &mut ChartCtx, _bar: Bar) {
        self.cvd += self.delta;
        ctx.plot(self.p_delta, self.delta);
        ctx.plot(self.p_cvd, self.cvd);
        self.delta = 0.0;
    }
}

ctx.book() returns the bar's order-book snapshot (None when there is none), with the same values a strategy reads.

On a live CME chart, ticks and ctx.book() continue into the current session and refresh at each bar close. The book is a 10-level snapshot at minute resolution. The bar that is still forming, or any bar opened in the last ~30 seconds, may have no book yet; it appears at a later bar close. For about 15 seconds after each new hour starts, bars of that hour may have no book. When recent ticks are not available yet for part of the session, the chart says which period is missing, and values built from ticks can be off for it until it fills.

Indicators that read ticks also work on tick, volume, range and Renko charts. Ticks are matched to those bars by whole second, so bars that share one second share its ticks approximately.

Tick data on a chart covers at most 45 days and 5,000,000 ticks. When the visible window is wider, the indicator runs over the most recent part that fits, and its plots start there. On a live chart today's ticks count toward that limit, so a window close to it starts later.

Limits

An indicator shares the strategy plot budget: 24 plots and 1,000,000 points per run.

Older indicators

Indicators written with the older impl Indicator for trait (a whole-window compute) still run, but new indicators use BarIndicator. When you edit one, rewrite it to BarIndicator. Logic that looks ahead of the current bar, or normalizes over the whole visible window, needs a real rewrite: on_bar sees only bars up to the current one, so use a bounded Rolling window instead.

Existing Python indicators are covered in Python Indicators (Legacy).