|
| 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() |
0 commit comments