Skip to content

Commit ce7f92e

Browse files
committed
构建计算仓单“开平”字段的函数
1 parent b083a6c commit ce7f92e

11 files changed

+503
-0
lines changed

i2005_quote_data_bt_202002080145.xlsx

1.89 MB
Binary file not shown.
1.89 MB
Binary file not shown.

m2005_quote_data_bt_202002072318.xlsx

1.69 MB
Binary file not shown.
2.83 MB
Binary file not shown.

opt.py

Lines changed: 389 additions & 0 deletions
Large diffs are not rendered by default.

opt_arb_20200207.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from tqsdk import TqApi, TqSim, TqBacktest, TqReplay
2+
from tqsdk.lib import TargetPosTask
3+
from tqsdk.tafunc import time_to_datetime
4+
from tqsdk.exceptions import BacktestFinished
5+
from datetime import datetime, date
6+
from contextlib import closing
7+
from opt import TqOption, OptionTrade
8+
9+
#api = TqApi(TqSim())
10+
api = TqApi(TqSim(init_balance=300000), backtest=TqBacktest(
11+
start_dt=datetime(2020, 1, 1, 9), end_dt=datetime(2020, 2, 7, 16)))
12+
#api = TqApi(backtest=TqReplay(date(2020,2,5)), web_gui=True)
13+
kq_m = api.get_quote("[email protected]")
14+
opt_api = TqOption(api, future_product_id="SR", option_product_id="SR_o")
15+
main_contract_id = kq_m.underlying_symbol
16+
future = api.get_quote(main_contract_id)
17+
future_id, opts = opt_api.get_future_opt_symbols(
18+
strike_year=future.delivery_year, strike_month=future.delivery_month)
19+
trade = OptionTrade(api, opt_api, future_id, opts, save_data=True,
20+
long_call_threshold=-5, long_put_threshold=-5)
21+
# future_id, opts = opt_api.get_future_opt_symbols(opt_api.strike_dates[0]) # 选一个2020-3合约
22+
23+
print("期货代码:" + future_id)
24+
25+
# 期货行情订阅
26+
future_quote = api.get_quote(future_id)
27+
# 期权-期货套利任务准备
28+
for opt in opts.values():
29+
trade.parity_quote_task(opt, future_quote)
30+
api.wait_update()
31+
# 主线程
32+
if api._backtest is None:
33+
while True:
34+
api.wait_update()
35+
if trade.save_data and (datetime.now().minute == 0 or datetime.now().minute == 30) and datetime.now().second == 0:
36+
print('ts:{}'.format(datetime.now()))
37+
trade.quote_df.to_excel('quote_data_rt_{}.xlsx'.format(
38+
datetime.now().strftime('%Y%m%d%H')))
39+
else:
40+
with closing(api):
41+
while True:
42+
try:
43+
api.wait_update()
44+
if trade.save_data and datetime.now().second == 0:
45+
print('ts:{}'.format(time_to_datetime(
46+
api._backtest._current_dt)))
47+
except BacktestFinished:
48+
trade.quote_df.to_excel('quote_data_bt_{}.xlsx'.format(
49+
datetime.now().strftime('%Y%m%d%H%M')))
50+
exit()

opt_arb_gui_20200209.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import asyncio
2+
import sys
3+
import PySimpleGUI as sg
4+
from tqsdk import TqApi, TqSim, TqAccount, TqBacktest
5+
from tqsdk.lib import TargetPosTask
6+
from tqsdk.tafunc import time_to_datetime
7+
from tqsdk.exceptions import BacktestFinished
8+
from datetime import datetime, date
9+
from contextlib import closing
10+
from opt import TqOption, OptionTrade
11+
12+
13+
window = sg.Window('请输入策略运行参数', layout = [[sg.Text('交易账户')],
14+
[sg.Text('期货公司'), sg.Input("快期模拟", key="broker_id")],
15+
[sg.Text('账号'), sg.Input("jincheng", key="user_id")],
16+
[sg.Text('密码'), sg.Input("123456", key="password", password_char='*')],
17+
[sg.Text('策略参数')],
18+
[sg.Text('期货主连代码'), sg.Input("[email protected]", key="future_kq_id")],
19+
[sg.Text('期货product_id'), sg.Input("SR", key="future_product_id")],
20+
[sg.Text('期权product_id'), sg.Input("SR", key="option_product_id")],
21+
[sg.Text('最大long call值'), sg.Input(-5, key="long_call_threshold")],
22+
[sg.Text('最大long put值'), sg.Input(-5, key="long_put_threshold")],
23+
[sg.Text('最小行权价'), sg.Input(5400, key="min_strike")],
24+
[sg.Text('最大行权价'), sg.Input(5800, key="max_strike")],
25+
[sg.OK()]])
26+
27+
# 读取用户输入值
28+
event, input_values = window.Read()
29+
print(event, input_values)
30+
window.close()
31+
32+
33+
loop = asyncio.get_event_loop()
34+
api = TqApi(loop=loop, account=TqAccount(input_values["broker_id"], input_values["user_id"], input_values["password"]) if len(input_values["broker_id"])> 2 else TqSim())
35+
opt_api = TqOption(api, future_product_id=input_values["future_product_id"], option_product_id=input_values["option_product_id"])
36+
kq_m = api.get_quote(input_values["future_kq_id"])
37+
main_contract_id = kq_m.underlying_symbol
38+
future = api.get_quote(main_contract_id)
39+
future_id, opts = opt_api.get_future_opt_symbols(
40+
strike_year=future.delivery_year, strike_month=future.delivery_month,
41+
min_strike=float(input_values["min_strike"]), max_strike=float(input_values["max_strike"]))
42+
43+
# 期货行情订阅
44+
future_quote = api.get_quote(future_id)
45+
#quote_layout = [[sg.Text("time=", size=(5,1)), sg.Text("%H:%m:%S", key="dt", size=(10,1))], [sg.Text("strike",size=(5,1)), sg.Text("call",size=(6,1)), sg.Text("put",size=(6,1))]]
46+
#quote_layout.extend([[sg.Text(opt["K"],size=(5,1)), sg.Text("nan", key="{}-call_premium".format(opt["K"]),size=(6,1)), sg.Text("nan", key="{}-put_premium".format(opt["K"]),size=(6,1))] for opt in opts.values()])
47+
#quote_window = sg.Window(future_id, quote_layout, size=(300,300))
48+
#async def new_quote_window():
49+
# event, _ = quote_window.Read(timeout=0)
50+
# if event is None or event == 'Exit':
51+
# sys.exit(0)
52+
#api.create_task(new_quote_window())
53+
54+
trade = OptionTrade(api, opt_api, future_id, opts, can_trade=True,
55+
long_call_threshold=float(input_values["long_call_threshold"]),
56+
long_put_threshold=float(input_values["long_put_threshold"]))
57+
58+
for opt in opts.values():
59+
trade.parity_quote_task(opt, future_quote)
60+
61+
# 主线程
62+
while True:
63+
api.wait_update()

quote_data_bt_202002080152.xlsx

4.43 KB
Binary file not shown.

quote_data_bt_202002081303.xlsx

4.44 KB
Binary file not shown.

quote_data_bt_202002092351.xlsx

4.43 KB
Binary file not shown.

0 commit comments

Comments
 (0)