增加交易策略、交易指标、量化库代码等文件夹

This commit is contained in:
Win_home
2025-04-27 15:54:09 +08:00
parent ca3b209096
commit f57150dae8
589 changed files with 854346 additions and 1757 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,69 @@
from vnpy.trader.object import TickData, BarData
from elite_optionstrategy import (
StrategyTemplate,
Parameter,
PortfolioData,
)
class ShortStrangleStrategy(StrategyTemplate):
"""持续做空宽跨式价差的策略"""
author: str = "用Python的交易员"
option_portfolio: str = Parameter("IO") # 期权产品代码
fixed_size: int = Parameter(1) # 交易的手数
percent_add: float = Parameter(0.02) # 委托超价比例
otm_level: int = Parameter(2) # 宽跨式虚值档位
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)
# 更新最新期权价格到组合
price_data: dict[str, float] = {}
for bar in bars.values():
price_data[bar.vt_symbol] = bar.close_price
portfolio.update_price(price_data)
# 如果目标为空(尚未开仓或已经到期),则执行开仓
if not self.target_data:
# 获取当月期权链
front_chain = portfolio.get_chain_by_level(0)
if not front_chain:
self.write_log("无法获取当月期权链,请检查是否正确添加了期权合约")
return
# 计算平值期权
front_chain.calculate_atm()
# 卖出宽跨式价差
otm_call = front_chain.get_option_by_level(cp=1, level=self.otm_level)
otm_put = front_chain.get_option_by_level(cp=-1, level=self.otm_level)
self.set_target(otm_call.vt_symbol, -self.fixed_size)
self.set_target(otm_put.vt_symbol, -self.fixed_size)
# 执行具体的委托交易每次on_bars都执行避免某一轮委托未能成交导致偏差
self.execute_trading(price_data, self.percent_add)