期权制作回测数据

28 篇文章 24 订阅
3 篇文章 4 订阅

将指定的档位的期权,指定阶段剩余到期日的期权数据合并,用于回测

import pandas as pd
import numpy as np
import akshare as ak
pd.set_option("display.max_rows",None)
pd.set_option("display.max_columns",None)

nh_price = ak.nh_price_index('RM')
nh_price.head()

nh_return = ak.nh_return_index('RM')
nh_return.head()

nh_volatility = ak.nh_volatility_index('RM')
nh_volatility.head()


# rm_fu = ak.get_futures_daily(start_date='20180101',end_date='20210624',market='CZCE')
# rm_fu = rm_fu.loc[rm_fu['symbol'].str.contains('RM')]
import datetime
rm_fu = pd.read_csv('rm_fu.csv')
rm_fu['date'] = rm_fu['date'].apply(lambda x:datetime.datetime.strptime(str(x),'%Y-%m-%d'))
rm_fu = rm_fu.iloc[:,6:]
#rm_fu.to_csv('rm_fu.csv')
rm_fu.head()

rm = pd.read_csv('RM.csv')
import datetime
rm['date'] = pd.to_datetime(rm['date'])
rm = rm.iloc[:,6:]
#rm.to_csv('RM.csv')
rm.head()

#rm['date'] = rm['date'].apply(lambda x:datetime.datetime.strptime(x, '%Y-%m-%d').strftime('%Y-%m-%d %H:%M:%S')).tolist()

rm.rename(columns={'合约系列':'symbol'},inplace=True)
last = pd.merge(rm,rm_fu,how = 'left',on=['date','symbol'])
last.head()


#根据长度,制作合约剩余交易日
#寻找每一个合约
"""
def fun1(df):
    for j in range(len(df['date'].unique())):
        df.iloc[j,-1] = len(df['date'])-j
    return df
"""
from tqdm import tqdm
final = pd.DataFrame()
for i in tqdm(last['品种代码   '].unique()):
    #print(last['品种代码   '].head(1))
    #print(i)
    last_1 = last.loc[last['品种代码   '] == i]
    #print(last_1)
    last_1 = last_1.sort_values(by='date')
    last_1['rt'] = 0
    #print(last_1.date.head())
    #寻找每一个日期
    for j in tqdm(range(len(last_1['date'].unique()))):
        #print('j',j)
        #print(range(len(last_1['date'].unique())))
        if len(last_1['date'])-j-1 == 0:
            print('特殊')
            print(len(last_1['date']))
            print(j)
            print(last_1.iloc[j,:])
        else:
            last_1.iloc[j,-1] = len(last_1['date'])-j-1
    final = final.append(last_1)
#print(final)
final.to_csv('final_1.csv')


#数据长度小于20日的删除
from tqdm import tqdm
final = pd.DataFrame()
for i in tqdm(last['品种代码   '].unique()):
    #print(last['品种代码   '].head(1))
    #print(i)
    last_1 = last.loc[last['品种代码   '] == i]
    #print(last_1)
    last_1 = last_1.sort_values(by='date')
    last_1['rt'] = 0
    #print(last_1.date.head())
    #寻找每一个日期
    for j in tqdm(range(len(last_1['date'].unique()))):
        #print('j',j)
        #print(range(len(last_1['date'].unique())))
        if len(last_1['date'])< 20:
            pass
        else:
            last_1.iloc[j,-1] = len(last_1['date'])-j-1
    final = final.append(last_1)
#print(final)
final.to_csv('final_2.csv')


#选择某一天的合约,
print(last['品种代码   '].head(1))

#选取某一天的全部合约
final = pd.read_csv('final_1.csv')
date = '2020-01-16'
test = final.loc[final['date']==date]
test.head()

#根据CP,距离期货价格数据长短,判定合约(也可以根据希腊字母,价格等)
final['date'][0]==date
final.to_csv('E:/final.csv')

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
卖出50ETF跨式期权策略是一种期权交易策略,它涉及卖出一个较低行权价的认购期权合约,同时买入一个较高行权价的认购期权合约。这种策略可以用于投资者预期标的资产价格在一段时间内保持相对稳定或略微上涨的情况下获利。 以下是一个简单的使用Python进行卖出50ETF跨式期权策略回测的示例代码: ```python import pandas as pd import numpy as np # 假设有以下数据 underlying_price = pd.Series([100, 105, 102, 98, 101]) # 标的资产价格 strike_price_short_call = 105 # 卖出认购期权的行权价 strike_price_long_call = 110 # 买入认购期权的行权价 premium_short_call = 2 # 卖出认购期权的权利金 premium_long_call = 1 # 买入认购期权的权利金 # 计算策略收益 def calculate_strategy_profit(underlying_price, strike_price_short_call, strike_price_long_call, premium_short_call, premium_long_call): short_call_payoff = np.where(underlying_price <= strike_price_short_call, premium_short_call, -premium_short_call) long_call_payoff = np.where(underlying_price <= strike_price_long_call, -premium_long_call, premium_long_call) strategy_payoff = short_call_payoff + long_call_payoff return strategy_payoff # 执行策略回测 strategy_payoff = calculate_strategy_profit(underlying_price, strike_price_short_call, strike_price_long_call, premium_short_call, premium_long_call) # 输出策略报 print("策略报:", strategy_payoff) ``` 在上述代码中,我们首先定义了标的资产价格(underlying_price)、卖出认购期权的行权价(strike_price_short_call)、买入认购期权的行权价(strike_price_long_call)、卖出认购期权的权利金(premium_short_call)和买入认购期权的权利金(premium_long_call)等参数。 然后,我们定义了一个名为`calculate_strategy_profit`的函数,用于计算策略的收益。该函数根据标的资产价格和期权行权价,计算出卖出认购期权和买入认购期权的收益,并返策略的收益。 最后,我们调用`calculate_strategy_profit`函数,并将标的资产价格等参数传入,得到策略的收益,并将其打印输出。 希望以上代码能够帮助你进行卖出50ETF跨式期权策略回测。如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神出鬼没,指的就是我!

必须花钱,数据超好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值