增加交易策略、交易指标、量化库代码等文件夹
This commit is contained in:
4542
5.课程代码/2.Option_spread_strategy/使用文档/31/31_demo.ipynb
Normal file
4542
5.课程代码/2.Option_spread_strategy/使用文档/31/31_demo.ipynb
Normal file
File diff suppressed because one or more lines are too long
100
5.课程代码/2.Option_spread_strategy/使用文档/31/buy_straddle_strategy.py
Normal file
100
5.课程代码/2.Option_spread_strategy/使用文档/31/buy_straddle_strategy.py
Normal file
@@ -0,0 +1,100 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from vnpy.trader.object import TickData, BarData
|
||||
|
||||
from elite_optionstrategy import (
|
||||
StrategyTemplate,
|
||||
Parameter,
|
||||
PortfolioData,
|
||||
ChainData,
|
||||
OptionData
|
||||
)
|
||||
|
||||
|
||||
class BuyStraddleStrategy(StrategyTemplate):
|
||||
"""持续做多跨式价差的策略"""
|
||||
|
||||
author: str = "用Python的交易员"
|
||||
|
||||
option_portfolio: str = Parameter("IO") # 期权产品代码
|
||||
fixed_size: int = Parameter(1) # 交易的手数
|
||||
percent_add: float = Parameter(0.05) # 委托超价比例
|
||||
rolling_days: int = Parameter(5) # 移仓剩余天数
|
||||
|
||||
def on_init(self):
|
||||
"""策略初始化"""
|
||||
self.write_log("策略初始化")
|
||||
|
||||
self.subscribe_options(self.option_portfolio)
|
||||
|
||||
def on_start(self):
|
||||
"""策略启动"""
|
||||
self.write_log("策略启动")
|
||||
|
||||
def on_stop(self):
|
||||
"""策略停止"""
|
||||
self.write_log("策略停止")
|
||||
|
||||
def on_tick(self, tick: TickData):
|
||||
"""Tick推送"""
|
||||
pass
|
||||
|
||||
def on_bars(self, bars: dict[str, BarData]):
|
||||
"""K线推送"""
|
||||
# 获取期权组合对象
|
||||
portfolio: PortfolioData = self.get_portfolio(self.option_portfolio)
|
||||
|
||||
# 默认交易当月期权链
|
||||
chain_level: int = 0
|
||||
|
||||
# 如果已有目标则检查是否要移仓
|
||||
if self.target_data:
|
||||
# 获取期权到期时间
|
||||
for vt_symbol in self.target_data.keys():
|
||||
option: OptionData = portfolio.get_option(vt_symbol)
|
||||
expiry: datetime = option.contract.option_expiry
|
||||
break
|
||||
|
||||
# 获取当前时间
|
||||
for bar in bars.values():
|
||||
now: datetime = bar.datetime.replace(tzinfo=None) # 移除时区
|
||||
break
|
||||
|
||||
# 计算剩余时间
|
||||
time_left: timedelta = expiry - now
|
||||
|
||||
# 如果尚未到时间,则继续持仓
|
||||
if time_left.days > self.rolling_days:
|
||||
return
|
||||
|
||||
# 需要交易次月期权链
|
||||
chain_level = 1
|
||||
|
||||
# 更新最新期权价格到组合
|
||||
price_data: dict[str, float] = {}
|
||||
for bar in bars.values():
|
||||
price_data[bar.vt_symbol] = bar.close_price
|
||||
|
||||
portfolio.update_price(price_data)
|
||||
|
||||
# 获取目标期权链
|
||||
chain: ChainData = portfolio.get_chain_by_level(chain_level)
|
||||
if not chain:
|
||||
self.write_log("无法获取对应期权链,请检查是否正确添加了期权合约")
|
||||
return
|
||||
|
||||
# 计算平值期权
|
||||
chain.calculate_atm()
|
||||
|
||||
# 清空当前目标
|
||||
self.clear_targets()
|
||||
|
||||
# 买入跨式价差
|
||||
atm_call = chain.get_option_by_level(cp=1, level=0)
|
||||
atm_put = chain.get_option_by_level(cp=-1, level=0)
|
||||
|
||||
self.set_target(atm_call.vt_symbol, self.fixed_size)
|
||||
self.set_target(atm_put.vt_symbol, self.fixed_size)
|
||||
|
||||
# 执行具体的委托交易
|
||||
self.execute_trading(price_data, self.percent_add)
|
||||
Reference in New Issue
Block a user