Debug an algorithm
See what your code sees.
Write to the log
Section titled “Write to the log”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.
Draw it on the chart
Section titled “Draw it on the chart”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.
Read the errors
Section titled “Read the errors”- 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.
Usual mistakes
Section titled “Usual mistakes”- A variable without
var,letorconst. Undeclared variables are not allowed. - Timers and promises.
setTimeout,asyncand promises are not available. UseonTickandonIntervalClose. - Trading on every tick. A condition checked in
onTickcan be true many times a bar. CheckOPEN_POSITIONS_COUNT, or trade inonIntervalClose. - Too many positions. A run stops if it asks for 50 positions within a minute, or after 500 in total.