Strategy Sandbox
Python source · Trusted-code only · Isolated browser worker
Python strategy API & execution conventions
Paste Python defining on_bar(ctx), with WARMUP_BARS = 2…501. Standard-library imports, custom indicators and ctx.state run in a separate opaque-origin WebAssembly worker. Only run code you trust. Browser memory cannot be hard-limited: hostile code can freeze or crash your tab. No server secrets or other users’ files are supplied. JavaScript is not supported.
Read ctx.history, ctx.close, ctx.position, ctx.account; use ctx.sma(period), ctx.atr(period), or calculate your own indicators. Return ctx.enter("LONG" or "SHORT", stop_fraction=…, stop_distance=…, or stop_price=…, and optional target_fraction/target_distance/target_price). Return ctx.exit() or ctx.manage(stop=…, target=…, reduce_fraction=…) for custom position management. Use Decimal strings/Decimal arithmetic for financial thresholds.
Optional completed-timeframe context: declare CONTEXT_TIMEFRAMES = {"15m": 50, "1h": 21} and use ctx.candles("15m") / ctx.candles("1h"). Only complete UTC-aligned aggregates of verified 1m data are supplied (2–64 bars each). Pre-window coverage is required; tests are never shortened to warm up. ctx.last_exit supplies the latest symbol-specific close timestamp and fill window, or None. These observations are read-only copies; they cannot change trades.
All observations are completed candles. Entries, reductions and stop changes execute no earlier than the next open; old protective stops remain active during the trigger candle. Stops cannot loosen. reduce_fraction applies to remaining quantity. Ordinary Spot SHORT is rejected. Saving/testing never activate trading. No server worker, orders or strategy activation. Checkpoints preserve ctx.state, completed histories and serializable global data. User-defined class state, clock/random/OS/JS module state, mutable closures, mutable function defaults, native handles and unpicklable state cannot be safely resumed and are explicitly rejected at a checkpoint. A device may pause background tabs; keep the page visible. Strategy recovery bytes are restored only inside the opaque strategy worker, never the financial interpreter.