Chapter 5: Strategy Development in Practice¶
This chapter is currently maintained in Chinese first.
- Chinese chapter: 第 5 章:策略开发实战 (Strategy Implementation)
- Lifecycle note: within a trading day
on_pre_open(1ns before the first session, 09:14:59.999… for A-shares) always precedeson_before_trading(1ns after continuous trading starts);on_after_tradingfires after the day-end settlement.daily_interestis the interest of this settlement, accrued over the calendar days since the previous settlement (3 days from Friday to Monday). - Textbook home: Chinese textbook index
- Practice links:
- Primary example: examples/textbook/ch05_strategy.py
- Extended example: examples/23_functional_callbacks_demo.py
- Framework hooks example: examples/50_framework_hooks_demo.py
- Day-boundary rebalance example: examples/strategies/05_stock_momentum_rotation_timer.py
- After-bar rebalance example: examples/strategies/09_stock_momentum_rotation_after_bar.py
- Class-style tick callback example: examples/51_class_tick_callbacks_demo.py
- Pre-open execution example: examples/52_pre_open_demo.py
- Staged next-day pre-open example: examples/53_timer_to_pre_open_demo.py
- Expiry callback example: examples/49_on_expiry_demo.py
- Indicator playbook example: examples/45_talib_indicator_playbook_demo.py
- Real-data mode:
python examples/45_talib_indicator_playbook_demo.py --data-source akshare --symbol sh600000 --start-date 20240101 --end-date 20260301 - Indicator script example: examples/74_indicator_scripts.py
- Functional twin example: examples/textbook/ch05_strategy_functional.py
- Guide: Strategy Guide
- Guide: Custom Indicator Guide
TA-Lib Backend Usage¶
backend="auto"defaults torust.- For baseline alignment with legacy strategies, explicitly use
backend="python". - To override
autoglobally, setAKQUANT_TALIB_AUTO_BACKEND=python|rust.
from akquant import talib as ta
close = df["close"].to_numpy()
high = df["high"].to_numpy()
low = df["low"].to_numpy()
volume = df["volume"].to_numpy()
rsi_py = ta.RSI(close, timeperiod=14, backend="python")
rsi_rs = ta.RSI(close, timeperiod=14, backend="rust")
mfi_rs = ta.MFI(high, low, close, volume, timeperiod=14, backend="rust")
tema_rs = ta.TEMA(close, timeperiod=20, backend="rust")
Indicator Selection Template¶
- Trend-following:
EMA + ADX + NATR - Mean-reversion:
BBANDS + RSI - Volume confirmation:
OBV + MFI + ROC - Trailing risk control:
SAR + ATR
import numpy as np
from akquant import talib as ta
ema_fast = ta.EMA(close, timeperiod=20, backend="rust")
ema_slow = ta.EMA(close, timeperiod=60, backend="rust")
adx = ta.ADX(high, low, close, timeperiod=14, backend="rust")
if np.isnan(ema_fast[-1]) or np.isnan(adx[-1]):
return
if ema_fast[-1] > ema_slow[-1] and adx[-1] >= 20:
self.buy(symbol, 100)
Indicator Scripts (aq.Indicator)¶
AKQuant mirrors TradingView's split between indicator() and strategy() scripts.
Script metadata goes on the class line:
import akquant as aq
class DualMA(aq.Indicator, title="Dual MA", overlay=True):
def on_start(self):
self.fast = self.I(aq.EMA(10), name="ema_fast") # plotted, main pane
self.slow = self.I(aq.EMA(30), name="ema_slow")
class Trend(aq.Strategy, title="Trend"):
def on_start(self):
self.fast = self.I(aq.EMA(10), name="ema_fast") # not plotted in a Strategy
result = aq.run_backtest(data=df, strategy=Trend, indicators=[DualMA])
titleis display-only (defaults to the class name);overlaydefaults toFalse(the script's own sub-pane);iddefaults to the snake_case class name.self.Iplots by default insideaq.Indicator(defaultpaneis 1, or 0 withoverlay=True) and does not plot insideaq.Strategy;plot=True/Falseoverrides. Chart arguments such aspane=orcolor=no longer implyplot=True.- Indicator scripts cannot trade: any trading call raises
aq.IndicatorCannotTradeError. replaces_price=True(indicator scripts only, requiresoverlay=True) hints that the script replaces the price candles; candle-shaped output usesrender_type="candle"withoutputs=("open", "high", "low", "close"), e.g.aq.HeikinAshi().aq.inspect_strategy(path_or_source)statically classifies every class (indicator / strategy / unknown) and lists each declared line'spaneandplotwithout importing or executing the file; values it cannot determine are reported indynamic.
For complete Chinese content including the full on_xxx callback map, warmup handling, pre-open execution semantics, staged next-day execution patterns, and migration tips:
- 第 5 章:策略开发实战 (Strategy Implementation)
- 指标组合实战手册