Merge pull request #1817 from 1122455801/portafolio_md

[Add] 投资组合使用文档
This commit is contained in:
vn.py 2019-06-11 22:19:13 +08:00 committed by GitHub
commit ce7c4ea6a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -637,7 +637,7 @@ calculate_statistics函数是基于逐日盯市盈亏情况DateFrame格式
 
### 回测引擎使用示例
### 单策略回测示例
- 导入回测引擎和CTA策略
- 设置回测相关参数品种、K线周期、回测开始和结束日期、手续费、滑点、合约规模、起始资金
@ -675,6 +675,89 @@ engine.show_chart()
 
### 投资组合回测示例
投资组合回测是基于单策略回测的其关键是每个策略都对应着各自的BacktestingEngine对象下面介绍具体流程
- 创建回测函数run_backtesting()这样每添加一个策略就创建其BacktestingEngine对象。
```
from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting
from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import AtrRsiStrategy
from vnpy.app.cta_strategy.strategies.boll_channel_strategy import BollChannelStrategy
from datetime import datetime
def run_backtesting(strategy_class, setting, vt_symbol, interval, start, end, rate, slippage, size, pricetick, capital):
engine = BacktestingEngine()
engine.set_parameters(
vt_symbol=vt_symbol,
interval=interval,
start=start,
end=end,
rate=rate,
slippage=slippage,
size=size,
pricetick=pricetick,
capital=capital
)
engine.add_strategy(strategy_class, setting)
engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
return df
```
 
- 分别进行单策略回测得到各自的DataFrame(该DataFrame包含交易时间、今仓、昨仓、手续费、滑点、当日净盈亏、累计净盈亏等基本信息但是不包括最大回撤夏普比率等统计信息),然后把DataFrame相加并且去除空值后即得到投资组合的DataFrame。
```
df1 = run_backtesting(
strategy_class=AtrRsiStrategy,
setting={},
vt_symbol="IF88.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,
)
df2 = run_backtesting(
strategy_class=BollChannelStrategy,
setting={'fixed_size': 16},
vt_symbol="RB88.SHFE",
interval="1m",
start=datetime(2019, 1, 1),
end=datetime(2019, 4, 30),
rate=1/10000,
slippage=1,
size=10,
pricetick=1,
capital=1_000_000,
)
dfp = df1 + df2
dfp =dfp.dropna()
```
 
- 创建show_portafolio()函数同样也是创建新的BacktestingEngine对象对传入的DataFrame计算如夏普比率等统计指标并且画图。故该函数不仅能显示单策略回测效果也能展示投资组合回测效果。
```
def show_portafolio(df):
engine = BacktestingEngine()
engine.calculate_statistics(df)
engine.show_chart(df)
show_portafolio(dfp)
```
 
## 参数优化
参数优化模块主要由3部分构成