A股量化交易回测工具,基于AKQuant(Rust引擎)和AKShare数据构建。当用户要求"回测股票策略"、"测试交易算法"时使用。
High-performance quantitative backtesting for Chinese stocks using Rust-powered AKQuant framework.
Use this skill when you need to:
User says: "Backtest double MA strategy on 贵州茅台"
Actions:
bash
python3 scripts/run_backtest.py 600519 10 30
Result: Returns total return, trade count, equity curve
User says: "Compare 5-day vs 20-day MA on 平安银行"
Actions:
bash
# Fast MA = 5, Slow MA = 20
python3 scripts/run_backtest.py 000001 5 20
# Compare with default 10/30
python3 scripts/run_backtest.py 000001 10 30
Result: Compare returns to find optimal parameters
User says: "Analyze which tech stocks performed best with momentum strategy in 2024"
Actions:
python3 scripts/run_backtest.py 300750 10 30 (宁德时代)python3 scripts/run_backtest.py 002594 10 30 (比亚迪)Common A-Share Symbols:
SymbolCompanySector600519贵州茅台消费000001平安银行金融300750宁德时代新能源002594比亚迪汽车000858五粮液消费
Find symbol: Use AKShare or search "股票代码 + 公司名称"
Double MA Strategy (金叉买入,死叉卖出):
bash
python3 scripts/run_backtest.py <symbol> <fast_period> <slow_period>
Recommended combinations:
Key metrics to review:
Interpretation:
text
Return > 0% → Strategy beats buy-and-hold
Return < 0% → Strategy underperforms
Trade count > 20 → Consider commission impact
Logic: Fast MA crosses above slow MA → Buy; Crosses below → Sell
Code example:
python
from double_ma_strategy import run_double_ma_backtest
result = run_double_ma_backtest(
symbol="000001",
fast_period=10,
slow_period=30,
initial_capital=100000,
start_date="20240101",
end_date="20241231"
)
print(f"Return: {result['return_pct']:.2f}%")
print(f"Trades: {len(result['trades'])}")
RSI Strategy Template:
python
import akquant as aq
class RsiStrategy:
def __init__(self, period=14, oversold=30, overbought=70):
self.rsi = aq.RSI(period)
self.oversold = oversold
self.overbought = overbought
def on_bar(self, bar):
self.rsi.update(bar['close'])
if self.rsi.value < self.oversold:
return 'BUY' # 超卖买入
elif self.rsi.value > self.overbought:
return 'SELL' # 超买卖出
return 'HOLD'
IndicatorUsageSignalaq.SMA(n)Trend followingPrice > SMA → uptrendaq.EMA(n)Faster trendMore responsive than SMAaq.RSI(n)Momentum<30 oversold, >70 overboughtaq.MACD()Trend + momentumCrossover signalsaq.BollingerBands(n, k)VolatilityPrice touches bandsaq.ATR(n)Risk sizingPosition sizing based on volatility
Example:
python
import akquant as aq
# Multi-indicator strategy
sma = aq.SMA(20)
rsi = aq.RSI(14)
for price in prices:
sma.update(price)
rsi.update(price)
# Buy: Price > SMA AND RSI < 40 (uptrend but not overbought)
if price > sma.value and rsi.value < 40:
signal = 'BUY'
python
import akshare as ak
# Daily price data (qfq = 前复权)
df = ak.stock_zh_a_hist(
symbol="000001",
period="daily",
start_date="20240101",
end_date="20241231",
adjust="qfq"
)
# Columns: 日期, 开盘, 收盘, 最高, 最低, 成交量
python
# Current prices
df = ak.stock_zh_a_spot_em()
Cause: Dependencies not installed Solution:
bash
source /root/.openclaw/venv/bin/activate
pip install akquant akshare pandas numpy
Cause: Wrong symbol format Solution:
000001 not SZ000001)Causes:
start_date < end_dateCauses:
Wrong parameter order - fast_period should be < slow_period bash
# Wrong: fast > slow
python3 scripts/run_backtest.py 000001 30 10
# Correct: fast < slow
python3 scripts/run_backtest.py 000001 10 30
Too many trades - High commission costs
Wrong signal logic - Check buy/sell conditions
Solutions:
fast_period >= 5 to reduce calculationCause: AKShare data updates (recent days) Solution:
bash
# Test multiple combinations
for fast in 5 10 15; do
for slow in 20 30 60; do
echo "Testing $fast/$slow:"
python3 scripts/run_backtest.py 000001 $fast $slow
done
done