Skip to content

Guides

Debug an algorithm

See what your code sees.

function onIntervalClose() {
log("Fast", sma(5), "Slow", sma(20), "Open", OPEN_POSITIONS_COUNT);
}

Each log() line appears in the backtest’s Log, with the time of the bar. Log what a decision depends on, not everything: a long backtest with a line per tick is hard to read.

plot("RSI", rsi(14));
plot("Fast SMA", sma(5));

plot() draws a series on the backtest’s price chart, one point per bar. Comparing a line to the price is often faster than reading numbers.

  • A backtest stops at the first error your code throws, and the Log shows the message and the line.
  • Use error("message") to stop on purpose when something is wrong, such as a setting out of range.
  • Rejected orders do not stop the run. Look for lines that start [WARNING], such as a stop on the wrong side of the price.
  • A variable without var, let or const. Undeclared variables are not allowed.
  • Timers and promises. setTimeout, async and promises are not available. Use onTick and onIntervalClose.
  • Trading on every tick. A condition checked in onTick can be true many times a bar. Check OPEN_POSITIONS_COUNT, or trade in onIntervalClose.
  • Too many positions. A run stops if it asks for 50 positions within a minute, or after 500 in total.