Configuring an ATR-RSI Futures Backtest and Parameter Search
Summary
This code example outlines a vn.py workflow for backtesting an ATR-RSI strategy on a Chinese equity index futures contract. It configures the instrument, minute interval, historical dates, commissions, slippage, contract size, tick size, and starting capital, then loads data, runs the backtest, calculates results and statistics, and displays a chart.
It also demonstrates parameter optimization against the Sharpe ratio, varying ATR lookback settings through both genetic-algorithm and brute-force searches. The example provides a framework and sample configuration, not evidence that the strategy is profitable: it reports no outcome metrics or comparison with a benchmark. It also does not discuss data quality, parameter overfitting, out-of-sample validation, or whether the assumed trading costs and settings reflect actual execution. Those omissions limit conclusions that can be drawn from the snippet.
Key ideas
- The example configures a minute-level historical backtest for an ATR-RSI index futures strategy.
- The setup includes trading costs, slippage, contract specifications, and initial capital.
- Results and statistics are calculated after loading market data and running the strategy.
- The Sharpe ratio is used as the target for parameter searches using two optimization methods.
- The snippet gives no performance results or safeguards against overfitting.
Tags
Full text
# backtesting demo
```python
from datetime import datetime
from vnpy.trader.optimize import OptimizationSetting
from vnpy_ctastrategy.backtesting import BacktestingEngine
from vnpy_ctastrategy.strategies.atr_rsi_strategy import AtrRsiStrategy
```
```python
engine = BacktestingEngine()
engine.set_parameters(
vt_symbol="IF888.CFFEX",
interval="1m",
start=datetime(2019, 1, 1),
end=datetime(2019, 4, 30),
rate=0.3/10000,
slippage=0.2,
size=300,
pricetick=0.2,
capital=1_000_000,
)
engine.add_strategy(AtrRsiStrategy, {})
```
```python
engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
engine.calculate_statistics()
engine.show_chart()
```
```python
setting = OptimizationSetting()
setting.set_target("sharpe_ratio")
setting.add_parameter("atr_length", 25, 27, 1)
setting.add_parameter("atr_ma_length", 10, 30, 10)
engine.run_ga_optimization(setting)
```
```python
engine.run_bf_optimization(setting)
```
```python
```Shown in full with attribution under the source's licence. Licence: MIT
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.