Backtesting Dual Thrust and Reviewing Returns with Pyfolio
Summary
The document demonstrates a workflow for backtesting a Dual Thrust strategy and reviewing its performance with Pyfolio. It configures a CTA backtest engine, sets the date range and storage location, and creates a strategy instance with a futures contract, five-minute bars, a historical lookback, and upper and lower boundary coefficients. The strategy is then run and the engine is released afterward.
For analysis, the example reads daily account data from a CSV file, converts dates, adds starting capital to the dynamic balance, and calculates percentage changes as returns. Those returns are passed to Pyfolio to generate a full tear sheet. The document includes output images, but supplies no readable performance values or interpretation of the charts. It is a practical demonstration rather than an assessment of whether the strategy is profitable; it does not explain the Dual Thrust rules, data quality, transaction costs, or other assumptions needed to evaluate the results.
Key ideas
- A CTA engine can be configured to run a Dual Thrust strategy over a specified backtest period.
- The example sets the strategy’s contract, bar interval, lookback, and boundary coefficients.
- Daily dynamic balance is combined with initial capital before calculating percentage returns.
- Pyfolio can generate a performance tear sheet from the resulting return series.
- The document shows a workflow but does not interpret results or establish strategy performance.
Tags
Full text
# backtest and pyfolio analyze
```python
from wtpy import WtBtEngine,EngineType
from Strategies.DualThrust import StraDualThrust
```
```python
#创建一个运行环境,并加入策略
engine = WtBtEngine(EngineType.ET_CTA)
engine.init('../common/', "configbt.yaml")
engine.configBacktest(201909100930,201912011500)
engine.configBTStorage(mode="csv", path="../storage/")
engine.commitBTConfig()
```
```python
'''
创建DualThrust策略的一个实例
name 策略实例名称
code 回测使用的合约代码
barCnt 要拉取的K线条数
period 要使用的K线周期,m表示分钟线
days 策略算法参数,算法引用的历史数据条数
k1 策略算法参数,上边界系数
k2 策略算法参数,下边界系数
isForStk DualThrust策略用于控制交易品种的代码
'''
straInfo = StraDualThrust(name='pydt_IF', code="CFFEX.IF.HOT", barCnt=50, period="m5", days=30, k1=0.1, k2=0.1, isForStk=False)
engine.set_cta_strategy(straInfo)
```
```python
#开始运行回测
engine.run_backtest(bAsync=False)
```
```python
def analyze_with_pyfolio(fund_filename:str, capital:float=500000):
import pyfolio as pf
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
# 读取每日资金
df = pd.read_csv(fund_filename)
df['date'] = df['date'].apply(lambda x : datetime.strptime(str(x), '%Y%m%d'))
df = df.set_index(df["date"])
# 将资金转换成收益率
ay = df['dynbalance'] + capital
rets = ay.pct_change().fillna(0).tz_localize('UTC')
# 调用pyfolio进行分析
pf.create_full_tear_sheet(rets)
# 如果在jupyter,不需要执行该语句
plt.show()
```
```python
analyze_with_pyfolio("./outputs_bt/pydt_IF/funds.csv",500000)
```
```python
engine.release_backtest()
```
```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.