基于tushare 数据用Python写一个skew的套利策略
时间: 2024-06-01 22:03:46 浏览: 152
Skew是指期权隐含波动率曲线的偏斜程度,通常表现为“左偏”或“右偏”。在套利策略中,我们可以利用不同到期时间或执行价格的期权之间的skew差异来寻找套利机会。下面是一个基于tushare数据用Python写的skew套利策略的示例代码:
```python
import tushare as ts
import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
# 获取期权数据
option_data = ts.get_option_data(exchange='SSE', call_put='C', expiration='2021-12-16')
# 筛选符合条件的期权
option_data = option_data[(option_data['strike'] % 100 == 0) & (option_data['volume'] > 0) & (option_data['oi'] > 0)]
# 计算期权隐含波动率曲线
option_data['mid_price'] = (option_data['ask'] + option_data['bid']) / 2
option_data['skew'] = np.nan
for date in option_data['trade_date'].unique():
data = option_data[option_data['trade_date'] == date]
atm_option = data.iloc[abs(data['strike'] - data['pre_close']).idxmin()]
iv_atm = atm_option['implied_volatility']
skew = (data['implied_volatility'] - iv_atm) / iv_atm
option_data.loc[option_data['trade_date'] == date, 'skew'] = skew
# 按照到期时间和执行价格分组
grouped_data = option_data.groupby(['expire_date', 'strike'])
# 计算每个组的skew差异
skew_diff = pd.DataFrame(columns=['expire_date', 'strike', 'skew_diff'])
for group in grouped_data.groups:
data = grouped_data.get_group(group)
if len(data) > 1:
skew_diff.loc[len(skew_diff)] = [group[0], group[1], data['skew'].diff().abs().sum()]
# 筛选skew差异最大的期权组合
max_skew_diff = skew_diff.loc[skew_diff['skew_diff'].idxmax()]
max_skew_data = option_data[(option_data['expire_date'] == max_skew_diff['expire_date'])
& (option_data['strike'].isin([max_skew_diff['strike'], max_skew_diff['strike'] + 100]))]
# 计算期权价格和payoff
max_skew_data['price'] = (max_skew_data['ask'] + max_skew_data['bid']) / 2
max_skew_data['payoff'] = max_skew_data['price'] - max_skew_data['pre_close']
# 画出payoff图像
fig, ax = plt.subplots()
ax.plot(max_skew_data['strike'], max_skew_data['payoff'])
ax.set_xlabel('Strike Price')
ax.set_ylabel('Payoff')
ax.set_title('Maximum Skew Difference Option Strategy')
plt.show()
```
以上代码中,我们首先使用tushare获取上证交易所的期权数据,并筛选出符合条件的期权。然后,我们计算每个期权的隐含波动率曲线,并按照到期时间和执行价格分组,计算每个组的skew差异。接着,我们筛选skew差异最大的期权组合,并计算期权价格和payoff。最后,我们画出payoff图像,以便我们更好地理解和分析策略的效果和风险。
阅读全文