Strategy Authoring Overview
How to build PyneScript strategies without leaking logic across stages. Covers stage split, canonical flow, and safe authoring patterns.
How to Build Strategies Without Leaking Logic Across Stages#
This handbook focuses on the most common PyneScript strategy mistakes in ATK: putting logic in the wrong stage,
treating trade-frame fields as an afterthought, and sneaking MTF or risk calculations into build_trade_frame(...).
The safe pattern is simple once the stage ownership is explicit.
| Core rule | Signals first, mapping second |
|---|---|
| MTF rule | request.security stays in signal stage |
| Risk rule | SL/TP/trailing are strategy intent |
| Starter examples | Strategy starter and MTF confirmation |
Separate Strategy Thinking from Trade Mapping#
| Stage | Owns | Must not do |
|---|---|---|
build_signal_frame | Indicators, filters, entries, exits, MTF context, canonical trade-frame fields. | Defer strategy intent into later stages. |
build_trade_frame | Thin normalization, usually build_mapped_trade_frame(signal_df) or equivalent canonical normalizer. | Compute TA, call request.security, or invent stop/target/sizing logic. |
Practical test: if you can explain your strategy only by reading build_signal_frame, the design is healthy.
If the behavior is split across both builders, the contract is already drifting.
PyneScript Strategy to Backtest to Live: One Canonical Flow#
build_signal_frame(...)
-> emit entry_* / sl / tp / trail_offset
-> build_trade_frame(...) returns build_mapped_trade_frame(signal_df)
-> normalize_strategy_trade_frame(...)
-> execution core builds OrderIntent
-> ExecutionPolicyEngine pre_trade_check(...)
-> batch backtest / replay / live session submit path
-> unified order primitives and reportsWhat is unified already#
The strategy layer uses one canonical trade-frame schema, one normalizer, and one execution-domain intent model before dispatch. That is the core contract shared by batch simulation, replay, and live signal execution.
What to keep explicit#
PyneScript strategy authors should target the canonical trade-frame schema first, not adapter-specific order factories. That keeps strategy code portable across backtest, replay, and live execution paths.