Skip to content
All library documents

Building CTA Strategies with vn.py: Lifecycle, Indicators, Orders, and Backtesting

Article vn.py community

Summary

This Chinese-language guide explains how to build a Commodity Trading Advisor strategy for the vn.py framework. It outlines the strategy class structure, the roles of configurable parameters and persistent state, and the event callbacks used for market data, orders, trades, and local stop orders. A moving-average example shows how bars can be formed from ticks, indicators warmed up, signals checked, and orders submitted through the framework’s long and short entry and exit methods.

The guide also describes the BarGenerator and ArrayManager utilities, strategy initialization and shutdown, order handling, and the distinction between live and backtesting engines. It highlights implementation hazards such as acting before indicators are initialized, modifying framework-maintained position state, leaving stale orders active, and allowing callback exceptions to stop a strategy. Its contribution is practical framework documentation, not evidence that the sample signal is profitable; execution, fees, slippage, and stop-order fills may differ between simulation and live trading.

Key ideas

  • A vn.py CTA strategy implements trading behavior through lifecycle and market-event callbacks.
  • Parameters are externally adjustable, while listed variables represent strategy state that can be displayed and persisted.
  • BarGenerator can convert tick data into bars, and ArrayManager provides indicator calculations after sufficient history is collected.
  • The framework updates position after fills, so strategy code should not alter position state directly.
  • Backtesting and live trading differ in order simulation, costs, and supported engine operations.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.