Skip to content

Chapter 5: Strategy Development in Practice

This chapter is currently maintained in Chinese first.

TA-Lib Backend Usage

  • backend="auto" defaults to rust.
  • For baseline alignment with legacy strategies, explicitly use backend="python".
  • To override auto globally, set AKQUANT_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])
  • title is display-only (defaults to the class name); overlay defaults to False (the script's own sub-pane); id defaults to the snake_case class name.
  • self.I plots by default inside aq.Indicator (default pane is 1, or 0 with overlay=True) and does not plot inside aq.Strategy; plot=True/False overrides. Chart arguments such as pane= or color= no longer imply plot=True.
  • Indicator scripts cannot trade: any trading call raises aq.IndicatorCannotTradeError.
  • replaces_price=True (indicator scripts only, requires overlay=True) hints that the script replaces the price candles; candle-shaped output uses render_type="candle" with outputs=("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's pane and plot without importing or executing the file; values it cannot determine are reported in dynamic.

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) - 指标组合实战手册