增加交易策略、交易指标、量化库代码等文件夹
This commit is contained in:
176
5.课程代码/2.Option_spread_strategy/使用文档/19/19_demo.ipynb
Normal file
176
5.课程代码/2.Option_spread_strategy/使用文档/19/19_demo.ipynb
Normal file
@@ -0,0 +1,176 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 加载功能模块\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"from vnpy.trader.constant import Interval\n",
|
||||
"\n",
|
||||
"from elite_optionstrategy import BacktestingEngine\n",
|
||||
"\n",
|
||||
"from buy_option_strategy import BuyOptionStrategy"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 创建回测引擎\n",
|
||||
"engine = BacktestingEngine()\n",
|
||||
"\n",
|
||||
"engine.set_parameters(\n",
|
||||
" interval=Interval.MINUTE,\n",
|
||||
" start=datetime(2023, 1, 1),\n",
|
||||
" end=datetime(2023, 6, 30),\n",
|
||||
" rate=0,\n",
|
||||
" slippage=0.6 + (16 / 100),\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"engine.add_strategy(BuyOptionStrategy, {})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" 1%| | 2/180 [00:00<00:16, 10.53it/s]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2024-03-09 09:42:27.361177\t触发异常,回测终止\n",
|
||||
"2024-03-09 09:42:27.362176\tTraceback (most recent call last):\n",
|
||||
" File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\backtesting.py\", line 114, in run_backtesting\n",
|
||||
" File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\backtesting.py\", line 397, in _run_intraday\n",
|
||||
" File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\backtesting.py\", line 442, in _new_bars\n",
|
||||
" File \"D:\\Gitee_Code\\trading_strategy\\VNPY_Code\\Option_spread_strategy\\使用文档\\19\\buy_option_strategy.py\", line 66, in on_bars\n",
|
||||
" portfolio: PortfolioData = self.get_portfolio(self.option_portfolio)\n",
|
||||
" File \"C:\\veighna_elite_simulation\\lib\\site-packages\\elite_optionstrategy\\template.py\", line 247, in get_portfolio\n",
|
||||
"KeyError: 'IO'\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 历史数据回放\n",
|
||||
"engine.run_backtesting()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2024-03-09 09:28:32.115670\t开始计算逐日盯市盈亏\n",
|
||||
"2024-03-09 09:28:32.115670\t逐日盯市盈亏计算完成\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 计算每日盈亏\n",
|
||||
"engine.calculate_result()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 统计绩效结果\n",
|
||||
"result = engine.calculate_statistics()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 显示资金图表\n",
|
||||
"engine.show_chart()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 打印逐笔成交\n",
|
||||
"for trade in engine.all_trades.values():\n",
|
||||
" print(trade.datetime, trade.vt_symbol, trade.direction.value, trade.offset.value, trade.volume, \"@\", trade.price)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for result in engine.daily_results.values():\n",
|
||||
" print(result.today_close_data[\"IO2201-C-4900.CFFEX\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.9"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "1b43cb0bd93d5abbadd54afed8252f711d4681fe6223ad6b67ffaee289648f85"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
152
5.课程代码/2.Option_spread_strategy/使用文档/19/buy_option_strategy.py
Normal file
152
5.课程代码/2.Option_spread_strategy/使用文档/19/buy_option_strategy.py
Normal file
@@ -0,0 +1,152 @@
|
||||
from vnpy.trader.constant import Interval
|
||||
from vnpy.trader.utility import ArrayManager, BarGenerator
|
||||
from vnpy.trader.object import TickData, BarData
|
||||
|
||||
from elite_optionstrategy import (
|
||||
StrategyTemplate,
|
||||
Variable,
|
||||
Parameter,
|
||||
PortfolioData,
|
||||
)
|
||||
|
||||
|
||||
class BuyOptionStrategy(StrategyTemplate):
|
||||
"""基于均线信号买入期权的策略"""
|
||||
|
||||
author: str = "用Python的交易员"
|
||||
|
||||
option_portfolio: str = Parameter("IO") # 期权产品代码
|
||||
underlying_symbol: str = Parameter("IFJQ00.CFFEX") # 标的合约代码
|
||||
fast_window: int = Parameter(20) # 快速均线周期
|
||||
slow_window: int = Parameter(120) # 慢速均线周期
|
||||
fixed_size: int = Parameter(1) # 交易的手数
|
||||
percent_add: float = Parameter(0.02) # 委托超价比例
|
||||
|
||||
ma_signal: int = Variable(0) # 当前信号多空
|
||||
|
||||
def on_init(self):
|
||||
"""策略初始化"""
|
||||
self.write_log("策略初始化")
|
||||
|
||||
self.subscribe_options(self.option_portfolio)
|
||||
self.subscribe_data(self.underlying_symbol)
|
||||
|
||||
# 标的信号对象
|
||||
self.factor = MaFactor(
|
||||
self.underlying_symbol,
|
||||
self.fast_window,
|
||||
self.slow_window
|
||||
)
|
||||
|
||||
# 加载标的历史数据初始化
|
||||
bars = self.load_bars(self.underlying_symbol, 10, Interval.MINUTE)
|
||||
for bar in bars:
|
||||
self.factor.update_bar(bar)
|
||||
|
||||
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线推送"""
|
||||
# 回测首先计算标的信号
|
||||
underlying_bar: BarData = bars.pop(self.underlying_symbol, None)
|
||||
if underlying_bar:
|
||||
self.factor.update_bar(underlying_bar)
|
||||
|
||||
# 获取期权组合对象
|
||||
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)
|
||||
|
||||
# 获取当月期权链
|
||||
front_chain = portfolio.get_chain_by_level(0)
|
||||
if not front_chain:
|
||||
self.write_log("无法获取当月期权链,请检查是否正确添加了期权合约")
|
||||
return
|
||||
|
||||
# 计算平值期权
|
||||
front_chain.calculate_atm()
|
||||
|
||||
# 获取当前均线多空信号
|
||||
ma_signal: int = self.factor.get_signal()
|
||||
|
||||
# 如果均线多头排列,且尚未做多
|
||||
if ma_signal > 0 and self.ma_signal <= 0:
|
||||
# 清空之前的目标
|
||||
self.clear_targets()
|
||||
|
||||
# 做多平值call
|
||||
atm_call = front_chain.get_option_by_level(cp=1, level=0)
|
||||
self.set_target(atm_call.vt_symbol, self.fixed_size)
|
||||
# 如果均线空头排列,且尚未做空
|
||||
elif ma_signal < 0 and self.ma_signal >= 0:
|
||||
# 清空之前的目标
|
||||
self.clear_targets()
|
||||
|
||||
# 做多平值put
|
||||
atm_put = front_chain.get_option_by_level(cp=-1, level=0)
|
||||
self.set_target(atm_put.vt_symbol, self.fixed_size)
|
||||
|
||||
# 缓存均线多空信号
|
||||
self.ma_signal = ma_signal
|
||||
|
||||
# 执行具体的委托交易
|
||||
self.execute_trading(price_data, self.percent_add)
|
||||
|
||||
|
||||
class MaFactor:
|
||||
"""标的物均线因子(基于均线输出多空信号)"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
vt_symbol: str,
|
||||
fast_window: int,
|
||||
slow_window: int
|
||||
) -> None:
|
||||
"""构造函数"""
|
||||
self.vt_symbol: str = vt_symbol
|
||||
self.fast_window: int = fast_window
|
||||
self.slow_window: int = slow_window
|
||||
|
||||
self.bg: BarGenerator = BarGenerator(self.update_bar)
|
||||
self.am: ArrayManager = ArrayManager(slow_window + 10)
|
||||
|
||||
self.signal: int = 0
|
||||
|
||||
def update_tick(self, tick: TickData) -> None:
|
||||
"""Tick更新"""
|
||||
self.bg.update_tick(tick)
|
||||
|
||||
def update_bar(self, bar: BarData) -> None:
|
||||
"""K线更新"""
|
||||
self.am.update_bar(bar)
|
||||
|
||||
# 计算均线
|
||||
self.fast_ma = self.am.sma(self.fast_window)
|
||||
self.slow_ma = self.am.sma(self.slow_window)
|
||||
|
||||
# 判断信号
|
||||
if self.fast_ma > self.slow_ma:
|
||||
self.signal = 1
|
||||
elif self.fast_ma < self.slow_ma:
|
||||
self.signal = -1
|
||||
else:
|
||||
self.signal = 0
|
||||
|
||||
def get_signal(self) -> int:
|
||||
"""获取当前多空信号"""
|
||||
return self.signal
|
||||
139
5.课程代码/2.Option_spread_strategy/使用文档/19/download_data.ipynb
Normal file
139
5.课程代码/2.Option_spread_strategy/使用文档/19/download_data.ipynb
Normal file
@@ -0,0 +1,139 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 配置迅投研数据服务\n",
|
||||
"from vnpy.trader.setting import SETTINGS\n",
|
||||
"\n",
|
||||
"SETTINGS[\"datafeed.name\"] = \"xt\"\n",
|
||||
"SETTINGS[\"datafeed.username\"] = \"token\"\n",
|
||||
"SETTINGS[\"datafeed.password\"] = \"4aff6f3b0dcfc990ec9476213ba784e17c34e757\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 加载功能模块\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"from vnpy.trader.datafeed import get_datafeed\n",
|
||||
"from vnpy.trader.object import HistoryRequest, Exchange, Interval\n",
|
||||
"\n",
|
||||
"from vnpy_sqlite import Database as SqliteDatabase\n",
|
||||
"from elite_database import Database as EliteDatabase"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 初始化数据服务\n",
|
||||
"datafeed = get_datafeed()\n",
|
||||
"datafeed.init()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"数据长度 174959\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 查询期权历史数据\n",
|
||||
"req = HistoryRequest(\n",
|
||||
" symbol=\"IFJQ00\", # 加权指数 \n",
|
||||
" # symbol=\"IF00\", # 主力连续(未平滑)\n",
|
||||
" exchange=Exchange.CFFEX,\n",
|
||||
" start=datetime(2015, 1, 1),\n",
|
||||
" end=datetime.now(),\n",
|
||||
" interval=Interval.MINUTE\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"bars = datafeed.query_bar_history(req)\n",
|
||||
"print(\"数据长度\", len(bars))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# 创建Elite数据库实例并写入数据\n",
|
||||
"db2 = EliteDatabase()\n",
|
||||
"db2.save_bar_data(bars)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.9"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "1b43cb0bd93d5abbadd54afed8252f711d4681fe6223ad6b67ffaee289648f85"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
184
5.课程代码/2.Option_spread_strategy/使用文档/19/update_data.py
Normal file
184
5.课程代码/2.Option_spread_strategy/使用文档/19/update_data.py
Normal file
@@ -0,0 +1,184 @@
|
||||
from multiprocessing import Process
|
||||
from datetime import datetime
|
||||
|
||||
from vnpy.trader.database import BarOverview
|
||||
from vnpy.trader.datafeed import get_datafeed
|
||||
from vnpy.trader.object import ContractData, BarData, HistoryRequest
|
||||
from vnpy.trader.constant import Exchange, Product, OptionType, Interval
|
||||
from vnpy.trader.setting import SETTINGS
|
||||
|
||||
from elite_database import EliteDatabase
|
||||
|
||||
|
||||
# 配置迅投研数据服务
|
||||
SETTINGS["datafeed.name"] = "xt"
|
||||
SETTINGS["datafeed.username"] = "token"
|
||||
SETTINGS["datafeed.password"] = "4aff6f3b0dcfc990ec9476213ba784e17c34e757"
|
||||
|
||||
|
||||
# 交易所映射关系
|
||||
EXCHANGE_XT2VT = {
|
||||
"SH": Exchange.SSE,
|
||||
"SZ": Exchange.SZSE,
|
||||
"BJ": Exchange.BSE,
|
||||
"SF": Exchange.SHFE,
|
||||
"IF": Exchange.CFFEX,
|
||||
"INE": Exchange.INE,
|
||||
"DF": Exchange.DCE,
|
||||
"ZF": Exchange.CZCE,
|
||||
"GF": Exchange.GFEX
|
||||
}
|
||||
|
||||
|
||||
def update_history_data() -> None:
|
||||
"""更新历史合约信息"""
|
||||
# 在子进程中加载xtquant
|
||||
from xtquant.xtdata import download_history_data
|
||||
|
||||
# 初始化数据服务
|
||||
datafeed = get_datafeed()
|
||||
datafeed.init()
|
||||
|
||||
# 下载历史合约信息
|
||||
download_history_data("", "historycontract")
|
||||
|
||||
print("xtquant历史合约信息下载完成")
|
||||
|
||||
|
||||
def update_contract_data(sector_name: str) -> None:
|
||||
"""更新合约数据"""
|
||||
# 在子进程中加载xtquant
|
||||
from xtquant.xtdata import (
|
||||
get_stock_list_in_sector,
|
||||
get_instrument_detail
|
||||
)
|
||||
|
||||
# 初始化数据服务
|
||||
datafeed = get_datafeed()
|
||||
datafeed.init()
|
||||
|
||||
# 查询中金所历史合约代码
|
||||
vt_symbols: list[str] = get_stock_list_in_sector(sector_name)
|
||||
|
||||
# 遍历列表查询合约信息
|
||||
contracts: list[ContractData] = []
|
||||
|
||||
for xt_symbol in vt_symbols:
|
||||
# 拆分XT代码
|
||||
symbol, xt_exchange = xt_symbol.split(".")
|
||||
|
||||
# 筛选期权合约合约
|
||||
if "-" in symbol:
|
||||
data: dict = get_instrument_detail(xt_symbol, True)
|
||||
|
||||
type_str = data["InstrumentID"].split("-")[1]
|
||||
if type_str == "C":
|
||||
option_type = OptionType.CALL
|
||||
elif type_str == "P":
|
||||
option_type = OptionType.PUT
|
||||
|
||||
option_underlying: str = data["InstrumentID"].split("-")[0]
|
||||
|
||||
contract: ContractData = ContractData(
|
||||
symbol=data["InstrumentID"],
|
||||
exchange=EXCHANGE_XT2VT[xt_exchange.replace("O", "")],
|
||||
name=data["InstrumentName"],
|
||||
product=Product.OPTION,
|
||||
size=data["VolumeMultiple"],
|
||||
pricetick=data["PriceTick"],
|
||||
min_volume=data["MinLimitOrderVolume"],
|
||||
option_strike=data["ExtendInfo"]["OptExercisePrice"],
|
||||
option_listed=datetime.strptime(data["OpenDate"], "%Y%m%d"),
|
||||
option_expiry=datetime.strptime(data["ExpireDate"], "%Y%m%d"),
|
||||
option_underlying=option_underlying,
|
||||
option_portfolio=data["ProductID"],
|
||||
option_index=str(data["ExtendInfo"]["OptExercisePrice"]),
|
||||
option_type=option_type,
|
||||
gateway_name="XT"
|
||||
)
|
||||
contracts.append(contract)
|
||||
|
||||
# 保存合约信息到数据库
|
||||
database: EliteDatabase = EliteDatabase()
|
||||
database.save_contract_data(contracts)
|
||||
|
||||
print("合约信息更新成功", len(contracts))
|
||||
|
||||
|
||||
def update_bar_data() -> None:
|
||||
"""更新K线数据"""
|
||||
# 初始化数据服务
|
||||
datafeed = get_datafeed()
|
||||
datafeed.init()
|
||||
|
||||
# 获取当前时间戳
|
||||
now: datetime = datetime.now()
|
||||
|
||||
# 获取合约信息
|
||||
database: EliteDatabase = EliteDatabase()
|
||||
contracts: list[ContractData] = database.load_contract_data()
|
||||
|
||||
# 获取数据汇总
|
||||
data: list[BarOverview] = database.get_bar_overview()
|
||||
|
||||
overviews: dict[str, BarOverview] = {}
|
||||
for o in data:
|
||||
# 只保留分钟线数据
|
||||
if o.interval != Interval.MINUTE:
|
||||
continue
|
||||
|
||||
vt_symbol: str = f"{o.symbol}.{o.exchange.value}"
|
||||
overviews[vt_symbol] = o
|
||||
|
||||
# 遍历所有合约信息
|
||||
for contract in contracts:
|
||||
# 如果没有到期时间,则跳过
|
||||
if not contract.option_expiry:
|
||||
continue
|
||||
|
||||
# 查询数据汇总
|
||||
overview: BarOverview = overviews.get(contract.vt_symbol, None)
|
||||
|
||||
# 如果已经到期,则跳过
|
||||
if overview and contract.option_expiry < now:
|
||||
continue
|
||||
|
||||
# 初始化查询开始的时间
|
||||
start: datetime = datetime(2018, 1, 1)
|
||||
|
||||
# 实现增量查询
|
||||
if overview:
|
||||
start = overview.end
|
||||
|
||||
# 执行数据查询和更新入库
|
||||
req: HistoryRequest = HistoryRequest(
|
||||
symbol=contract.symbol,
|
||||
exchange=contract.exchange,
|
||||
start=start,
|
||||
end=datetime.now(),
|
||||
interval=Interval.MINUTE
|
||||
)
|
||||
|
||||
bars: list[BarData] = datafeed.query_bar_history(req)
|
||||
|
||||
if bars:
|
||||
database.save_bar_data(bars)
|
||||
|
||||
start_dt: datetime = bars[0].datetime
|
||||
end_dt: datetime = bars[-1].datetime
|
||||
msg: str = f"{contract.vt_symbol}数据更新成功,{start_dt} - {end_dt}"
|
||||
print(msg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 使用子进程更新历史合约信息
|
||||
process: Process = Process(target=update_history_data)
|
||||
process.start()
|
||||
process.join() # 等待子进程执行完成
|
||||
|
||||
# 更新合约信息
|
||||
update_contract_data("中金所")
|
||||
update_contract_data("过期中金所")
|
||||
|
||||
# 更新历史数据
|
||||
# update_bar_data()
|
||||
Reference in New Issue
Block a user