How a script runs
On this page
The lifecycle
- Register. CodeLab builds the script once to collect its inputs and plot layers. That is why plots must be called unconditionally, in the same order.
- Terse form on the chart. The
indicator()body runs once per recompute with the full history loaded. Series hold every bar;close.valueis the newest bar only. - Class form on the chart.
onBar()runs once per bar, oldest to newest. - Backtest. Bars are fed one at a time. Orders queued on a bar fill at the next bar’s open.
Debugging
The Console under the editor shows compile errors and runtime warnings, such as a script that drew nothing. To check the numbers themselves, put them on the chart:
export default indicator('Debug', { placement: 'subpane' }, () => {
// Put the numbers you are checking on the chart itself:
const rsi = ta.rsi(close, 14);
plot(rsi, { title: 'RSI' });
table({ position: 'top-right', cells: [
{ row: 0, col: 0, text: 'RSI now', color: '#787b86' },
{ row: 0, col: 1, text: rsi.value.toFixed(2), color: '#ffffff' },
{ row: 1, col: 0, text: 'RSI 1 bar ago', color: '#787b86' },
{ row: 1, col: 1, text: rsi[1].toFixed(2), color: '#ffffff' },
]});
});