Strategies

Strategies Overview

Turn a kScript into a backtest. Declare strategy(), place orders with strategy.*, and get trades, an equity curve, and performance stats.

Since engine 3.0.61; verified against engine 3.0.61. Every example on this page is an executed probe against that build.

What a strategy script is

A strategy script is a kScript that declares strategy(...) instead of define(...) and places simulated orders with strategy.* methods. The engine replays your rules bar by bar through a broker emulator and returns:

  • a trades list (entries, exits, per-trade PnL, fees, exit reason),
  • an equity curve and drawdown series,
  • performance stats (net profit, win rate, profit factor, Sharpe, Sortino, max drawdown, exposure, and more),
  • open trades and pending orders as of the last bar.

Results render in the Strategy Tester panel under the chart: trade markers on the price series, the equity curve, a performance table, and a run-details popover that reports exactly how fills were simulated.

scripts/probes/strategies/ma-cross.ks
//@version=2
strategy(title="MA Cross Strategy", initialCapital=10000, qtyType="percentOfEquity", qtyValue=50, commissionPercent=0.05, slippageBps=2)

timeseries bars = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries fast = ema(source=bars.close, period=9)
timeseries slow = ema(source=bars.close, period=21)

if (crossover(fast, slow)) {
  strategy.entry("L", "long")
}
if (crossunder(fast, slow)) {
  strategy.closeAll()
}

plotLine(value=fast, width=1, colors=["#4f8cff"], label=["Fast EMA"], desc=["9-period EMA of close"])
plotLine(value=slow, width=1, colors=["#f59e0b"], label=["Slow EMA"], desc=["21-period EMA of close"])

A script contains either define() or strategy(), never both. Calling any strategy.* method without a strategy() declaration is a compile-time error. In the editor, the first-line keyword is clickable: you can toggle a draft between indicator and strategy, and strategy-only declaration params are stripped automatically when converting back.

Core guarantees

The simulator is built around three properties, each locked by tests:

  • Deterministic. The same script on the same data produces byte-identical output, every run.
  • Lookahead-free. Orders placed while bar N is computing fill no earlier than bar N+1's open. Running on only the first K bars reproduces exactly the full run's trades that entered before bar K.
  • Honest accounting. Where a single chart bar cannot settle which of two levels traded first, the engine resolves the question with finer data when it is available, and otherwise settles it against you by default and counts it. The run details always disclose the resolution quality. See Fill simulation.

Running a backtest

Use the Backtest action in the script editor. A backtest run is granted its own replay depth and compute budget, separate from normal chart-window economics:

TierReplay depthCompute budgetRuns per day
Free1,000 bars10 s20
Plus20,000 bars20 s200

Fine-grained fill resolution (sub-bar walks) and order-book slippage data are loaded for plus-tier backtests. Free-tier runs use the same engine with chart-bar resolution and the declared flat slippage; the run details say so explicitly, so a shared result is never mistaken for a higher-fidelity one.

Reruns of an unchanged strategy over unchanged data return identical results, and cached results are segregated by the fidelity the run actually had (never served across tiers or data availability).

What the backtester is, and is not

This is a retail research tool built for fast iteration and honest reporting, not an execution simulator for microstructure research. It does not model queue position, partial fills at a level, replenishment, latency, or venue fees beyond your declared commission. Every simplification that can affect a fill is either resolved with real finer-grained data or disclosed in the run details with a count.

Section contents

  • Writing strategies: the strategy() declaration, the order API, position and sizing rules.
  • Fill simulation: the execution model, fill models, sub-bar resolution, and precision reporting.
  • Slippage and costs: commission, flat slippage, and the order-book slippage estimate.