转载一篇来自知乎 诸神黄昏的优秀回答,关于如何利用天勤量化来设计期权策略
#!/usr/bin/env python # -*- coding: utf-8 -*- #import asyncio from tqsdk import TqApi, TqAuth from contextlib import closing from time import time # 创建API实例. api = TqApi(auth=TqAuth("天勤账号", "账号密码")) async def taoli(Call='',Put='',Lr=3, Dlr=1): #默认3跳,1跳对手价平仓损耗,1跳抹除手续费,1跳纯利润 quote_C = api.get_quote(Call) quote_P = api.get_quote(Put) futer = quote_C.underlying_symbol quote_F = api.get_quote(futer) async with api.register_update_notify([quote_C,quote_P,quote_F]) as update_chan: # async for _ in update_chan: # D_time = (quote_C.last_exercise_datetime-time()) * Lr / (Dlr*86400) if quote_C.exercise_type == 'A': lr = quote_F.price_tick * Lr #行权盈利点数 if D_time <= Lr: dlr = lr else : dlr = quote_F.price_tick * D_time #价格变动百分比,到期盈利幅度 elif quote_C.exercise_type == 'E': if D_time <= Lr : lr = dlr = quote_F.price_tick * Lr else : lr = dlr = quote_F.price_tick * D_time Cb = quote_C.strike_price + quote_C.ask_price1 #K+C,构建期货多头价格 Pb = quote_P.strike_price - quote_P.ask_price1 #K-P,构建期货空头价格 CbPs_Fs = quote_P.strike_price + quote_C.ask_price1 - quote_P.bid_price1 #K+C-P,构建期货多头价格 PbCs_Fb = quote_P.strike_price + quote_C.bid_price1 - quote_P.ask_price1 #K+C-P,构建期货空头价格 Fb = quote_F.ask_price1 #期货买入价 Fs = quote_F.bid_price1 #期货卖出价 kai = False if Fs-Cb >= lr: print(Call,'-',futer,'K+C<F:多看涨期权+空期货', Cb, Fs, Cb<Fs,' 利润:',Fs-Cb) kai = True break if Pb-Fb >= lr : print(Put,'+',futer,'K-P>F:多看跌期权+多期货',Pb, Fb, Pb>Fb,' 利润:',Pb-Fb) kai = True break if Fs-CbPs_Fs >= dlr : print(Call,'-',Put,'-',futer,'K+C-P<F:多看涨期权+空看跌期权+空期货',CbPs_Fs, Fs, CbPs_Fs<Fs,' 利润:',Fs-CbPs_Fs) kai = True break if PbCs_Fb-Fb >= dlr : print(Put,'-',Call,'+',futer,'K+C-P>F:多看跌期权+空看涨期权+多期货',PbCs_Fb, Fb, PbCs_Fb>Fb,' 利润:',PbCs_Fb-Fb) kai = True break if not kai : print('监控中:') print(Call,'-',futer,'K+C<F:多看涨期权+空期货', Cb, Fs, Cb<Fs) print(Put,'+',futer,'K-P>F:多看跌期权+多期货', Pb, Fb, Pb>Fb) print(Call,'-',Put,'-',futer,'K+C-P<F:多看涨期权+空看跌期权+空期货', CbPs_Fs, Fs, CbPs_Fs<Fs) print(Put,'-',Call,'+',futer,'K+C-P>F:多看跌期权+空看涨期权+多期货', PbCs_Fb, Fb, PbCs_Fb>Fb) #break C = [] P = [] exchange = ['SHFE','DCE','CZCE','INE'] for e in exchange : quote = api.query_quotes(ins_class='OPTION',exchange_id=e,expired=False) for i in quote : q = api.get_quote(i) if q.option_class == 'CALL' : C.append([i,q.underlying_symbol,q.strike_price]) elif q.option_class == "PUT" : P.append([i,q.underlying_symbol,q.strike_price]) del quote Options = [] for i in C : for j in P : if i[1] == j[1] and i[2] == j[2]: Options.append([i[0],j[0]]) del C , P for i in Options : api.create_task(taoli(Call=i[0],Put=i[1],Lr=3,Dlr=7)) with closing(api): while True : api.wait_update()
代码解析:
先获取上海、大连、郑州、能源交易所全部期权,按看涨看跌分类,再把执行价相同的看涨看跌期权组对。
套利机会由异步函数taoli(Call=”,Put=”,Lr=3, Dlr=1)监控。
从上述期权对里获取看涨期权赋值给Call,获取看跌期权赋值给Put。
Lr是最小获利点数,默认3跳,因为套利对在平仓时,以对手价成交会损耗一点,手续费再损耗一点,剩一点为纯利润。
美式期权多头随时可以行权以获得利润,期权空头和欧式期权只能在到期时被行权,参数Dlr表示持有多少天获得Lr点利润可以接受,比如Lr=3, Dlr=7,表示持有7天获得3跳利润可以接受,如果期权到期日是21天,则需获得9跳利润,利润再低就可能不合适了,持有的时间长但获得的利润小有点不值当。
所有的价格判断都以对手价计算,当if语句检测到套利机会时,break语句可换成开仓函数,立即以对手价下单,并在成交后锁定计算的利润,美式期权多头可立即行权并平仓获得利润,期权空头和欧式期权需等到到期时行权再平仓。
期货期权套利机会分析的原理应用的是无风险套利关系式,如下:
1,K+C < F,即执行价K+权利金C<期货价格,此时买入看涨期权+卖空期货,左边等于低价买入期货,右边高价卖出期货,形成锁仓利润
2,K – P > F,即执行价K-权利金P>期货价格,此时买入看跌期权+买入期货,左边等于高价卖空期货,右边低价买入期货,形成锁仓利润
3,K + C – P < F,即执行价K+权利金C-权利金P<期货价格,此时买入看涨期权+卖出看跌期权+卖空期货,左边等于低价买入期货,右边高价卖出期货,形成锁仓利润
4,K + C – P > F,即执行价K+权利金C-权利金P>期货价格,此时买入看跌期权+卖出看涨期权+买入期货,左边等于高价卖空期货,右边低价买入期货,形成锁仓利润