Implicit Execution & Proxies
The BacktestX script engine natively supports Pine Script-style "Implicit Execution". You can run code inside an automatic loop using ctx.run() and access previous historical data using native array bracket syntax like close[1].
Using ctx.run()
Instead of manually writing for(let i = bounds.startIndex; i <= bounds.endIndex; i++), you can wrap your time-series logic inside a ctx.run() callback. The engine will loop over all bars for you.
ctx.run((bar, state) => {
// Logic executes on every bar!
});
State Persistence (The 'var' keyword)
In Pine Script, you use the var keyword to initialize a variable once and persist it across bars. In BacktestX, you use the state object.
ctx.run((bar, state) => {
// Initialize only if undefined
state.crossCount = state.crossCount ?? 0;
if (ctx.ta.crossover(bar.close, ctx.ta.sma(ctx.bars.map(b => b.close), 14))[bar.index]) {
state.crossCount++;
}
});
JavaScript Proxies (close[1])
The bar object passed into the callback contains properties for open, high, low, close, and volume. These are not standard numbers; they are ES6 Proxy Objects.
This allows you to access historical data relative to the current bar by passing a lookback integer into the brackets.
bar.close[0]: The current bar's close price.bar.close[1]: The previous bar's close price.bar.close[10]: The close price 10 bars ago.