期权基本使用

o10 - 获取期权实时行情

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'ringo'

from tqsdk import TqApi, TqAuth

# 创建API实例,传入自己的信易账户
api = TqApi(auth=TqAuth("信易账户", "账户密码"))

# 获取大商所豆粕期权行情
quote_m = api.get_quote("DCE.m1807-C-2450")

# 获取中金所股指期权行情
quote_IO = api.get_quote("CFFEX.IO2002-C-3550")

# 输出 m1807-C-2450 的最新行情时间和最新价
print(quote_m.datetime, quote_m.last_price)

# 关闭api,释放资源
api.close()

o20 - 查询符合要求的期权

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'ringo'

from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("信易账户", "账户密码"))

ls = api.query_options("SHFE.au2012")
print(ls)  # 标的为 "SHFE.au2012" 的所有期权

ls = api.query_options("SHFE.au2012", option_class="PUT")
print(ls)  # 标的为 "SHFE.au2012" 的看跌期权

ls = api.query_options("SHFE.au2012", option_class="PUT", expired=False)
print(ls)  # 标的为 "SHFE.au2012" 的看跌期权, 未下市的

ls = api.query_options("SHFE.au2012", strike_price=340)
print(ls)  # 标的为 "SHFE.au2012" 、行权价为 340 的期权

ls = api.query_options("SSE.510300", exchange_id="CFFEX")
print(ls)  # 中金所沪深300股指期权

ls = api.query_options("SSE.510300", exchange_id="SSE")
print(ls)  # 上交所沪深300etf期权

ls = api.query_options("SSE.510300", exchange_id="SSE", exercise_year=2020, exercise_month=12)
print(ls)  # 上交所沪深300etf期权, 限制条件 2020 年 12 月份行权

api.close()

o30 - 查询平值/虚值/实值期权

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'ringo'

from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("信易账户", "账户密码"))

quote = api.get_quote("SHFE.au2012")

# 预计输出的为以au2012现在最新价来比对的认购的平值期权,当没有符合的平值期权时返回为空,如果有返回则格式为 ["SHFE.au2012C30000"]
ls = api.query_atm_options("SHFE.au2012", quote.last_price, 0, "CALL")

# 预计输出的为au2012,以开盘价来比对的认购的实值3档,实值2档,实值1档期权,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如 [None,None,"SHFE.au2012C30000"]
ls = api.query_atm_options("SHFE.au2012", quote.open, [3, 2, 1], "CALL")

# 预计输出的为au2012,以开盘价来比对的认购的实值1档,平值期权,虚值1档,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如
ls = api.query_atm_options("SHFE.au2012", quote.open, [1, 0, -1], "CALL")

# 预计输出的为au2012,以现在最新价来比对的认购的虚值1档期权
ls = api.query_atm_options("SHFE.au2012", quote.last_price, -1, "CALL")

# 预计输出沪深300股指期权,2020年12月的虚值1档期权
ls = api.query_atm_options("SSE.000300", quote.last_price, -1, "CALL", exercise_year=2020, exercise_month=12)

# 预计输出 上交所 沪深300股指ETF期权,2020年12月的虚值1档期权
ls = api.query_atm_options("SSE.510300", quote.last_price, -1, "CALL", exercise_year=2020, exercise_month=12)

api.close()

o40 - 计算期权的希腊字母

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'ringo'

from tqsdk import TqApi, TqAuth
from tqsdk.ta import OPTION_GREEKS

api = TqApi(auth=TqAuth("信易账户", "账户密码"))

# 获取指定期权行情
quote = api.get_quote("SHFE.cu2006C44000")

# 获取期权和其对应标的的多合约的 kline 数据
klines = api.get_kline_serial(["SHFE.cu2006C44000", "SHFE.cu2006"], 24 * 60 * 60, 30)

# 运行 OPTION_GREEKS 希腊值计算函数
greeks = OPTION_GREEKS(klines, quote, 0.025)

# 输出希腊字母
print(list(greeks["delta"]))
print(list(greeks["theta"]))
print(list(greeks["gamma"]))
print(list(greeks["vega"]))
print(list(greeks["rho"]))

api.close()

o41 - 计算期权隐含波动率和历史波动率

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'ringo'

from tqsdk import TqApi, TqAuth
from tqsdk.ta import OPTION_IMPV

api = TqApi(auth=TqAuth("信易账户", "账户密码"))

# 获取指定期权行情
quote = api.get_quote("SHFE.cu2006C50000")

# 获取期权和对应标的的多合约 kline
klines = api.get_kline_serial(["SHFE.cu2006C50000", "SHFE.cu2006"], 24 * 60 * 60, 20)

# 通过 OPTION_IMPV 函数计算隐含波动率,设置无风险利率为 0.025
impv = OPTION_IMPV(klines, quote, 0.025)

print(list(impv["impv"] * 100))

api.close()

o60 - 获取期权波动率曲面

from tqsdk import TqApi, TqAuth
from tqsdk.ta import VOLATILITY_CURVE

api = TqApi(auth=TqAuth("信易账户", "账户密码"))

# 获取 m2112 的看跌期权
underlying = "DCE.m2101"
options = api.query_options(underlying_symbol=underlying, option_class="PUT", expired=False)

# 批量获取合约的行情信息, 存储结构必须为 dict, key 为合约, value 为行情数据
quote = {}
for symbol in options:
    quote[symbol] = api.get_quote(symbol)
options.append(underlying)

klines = api.get_kline_serial(options, 24 * 60 * 60, 20)

# 使用 VOLATILITY_CURVE 函数计算波动率曲面
vc = VOLATILITY_CURVE(klines, quote, underlying, r=0.025)

print(vc)

api.close()

o70 - 期权套利策略

from tqsdk import TqApi, TqAuth

'''
如果买入看涨期权构建多头期货的价格小于卖出期货价格
存在套利机会则发出双边挂单
'''
api = TqApi(auth=TqAuth("信易账户", "账号密码"))

# 获取行权价为2950的MA109看涨期权的quote数据
quote_option = api.get_quote('CZCE.MA109C2950')

# 获取期权对应标的期货,即MA109的quote数据
quote = api.get_quote(quote_option.underlying_symbol)

# 套利机会尝试次数
times = 0

while True:
    api.wait_update()
    # 以对手价来计算买入看涨期权然后行权后的期货成本价格
    option_buy = quote_option.strike_price + quote_option.ask_price1
    # 判断当期货的买入1档,即卖出期货价格大于买入看涨期权的期货成本价格,形成套利空间时进行限价下单
    if quote.bid_price1 < option_buy and times == 0:
        times += 1
        # 以现在卖1档价格买入看涨期权
        order_opiton = api.insert_order('CZCE.MA109C2950', "BUY", "OPEN", 1, quote_option.ask_price1)
        # 以现在买1档的价格卖出期货
        order_future = api.insert_order(quote.underlying_symbol, "SELL", "OPEN", 1, quote.bid_price1)
        print("存在期货,期权套利空间尝试买入")

o71 - 获取一组期权和其对应行权价

from tqsdk import TqApi, TqAuth

'''
获取标的对应看涨期权的期权和行权价对应列表
'''
api = TqApi(auth=TqAuth("信易账户", "账号密码"))

# 获取沪深300股指期权的认购在市合约
ls = api.query_options("SSE.000300", "CALL", expired=False)

# 批量获取这些合约的quote合约信息
quote_ls = api.get_quote_list(ls)

option_ls = {}

# 遍历quote合约信息,将合约和其对应行权价组装成字典
for i in quote_ls:
    option_ls[i.instrument_id] = i.strike_price

print(option_ls)

api.close()

o72 - 查询标的对应期权按虚值平值实值分类

from tqsdk import TqApi, TqAuth
from datetime import datetime
from tqsdk.tafunc import time_to_datetime

'''
查询标的对应期权按虚值平值实值分类
'''
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("信易账户", "账户密码"))
quote = api.get_quote("SHFE.au2112")
in_money_options, at_money_options, out_of_money_options = api.query_all_level_options("SHFE.au2112", quote.last_price, "CALL")
print(in_money_options)  # 实值期权列表
print(at_money_options)  # 平值期权列表
print(out_of_money_options)  # 虚值期权列表

api.close()