如何解决在协整策略中按天将循环中的哪个值附加到数据帧
我正在尝试将协整策略应用于 sp500 的几对股票公司。我想将总回报、净回报和累积回报存储在数据框中,以便我可以使用这些值来优化投资组合。
但是当我尝试将这些值存储在数据帧上时,我最终得到了一个没有值的数据帧。 我做错了什么?
导入数据
#specifying parameters and importing data
stocks = ['MMM','MO','KO']
start = '2020-06-22'
end = '2021-06-22'
fee = 0.001
window = 252
t_threshold = -2.5
#retrieving data
data = pd.DataFrame()
returns = pd.DataFrame()
for stock in stocks:
prices = yf.download(stock,start,end)
data[stock] = prices['Close'].fillna(method='bfill')
returns[stock] = np.append(data[stock][1:].reset_index(drop=True)/data[stock][:-1].reset_index(drop=True) - 1,0)
创建循环 这个循环将创建一个优化器并运行到所有具有交易策略的货币对。当您进行交易时,会显示有关该交易和结果的一些信息。我想将循环中的哪个值附加到数据帧,以便我可以处理结果。
results = pd.DataFrame(data = {'gross_return':[],'net_return':[],'cumulative':[]})
comprimento = len(stocks)
for i in range(0,comprimento):
gross_returns = np.array([])
net_returns = np.array([])
t_s = np.array([])
stock1 = stocks[i]
stock2 = stocks[i+1]
#moving through the sample
for t in range(window,len(data)):
#defining the unit root function: stock2 = a + b*stock1
def unit_root(b):
a = np.average(data[stock2].fillna(method='bfill')[t-window:t] - b*data[stock1].fillna(method='bfill')[t-window:t])
fair_value = a + b*data[stock1].fillna(method='bfill')[t-window:t]
diff = np.array(fair_value - data[stock2].fillna(method='bfill')[t-window:t])
diff_diff = diff[1:] - diff[:-1]
reg = sm.OLS(diff_diff,diff[:-1])
res = reg.fit()
return res.params[0]/res.bse[0]
#optimising the cointegration equation parameters
res1 = spop.minimize(unit_root,data[stock2].fillna(method='bfill')[t]/data[stock1].fillna(method='bfill')[t],method='Nelder-Mead')
t_opt = res1.fun
b_opt = float(res1.x)
a_opt = np.average(data[stock2].fillna(method='bfill')[t-window:t] - b_opt*data[stock1].fillna(method='bfill')[t-window:t])
#simulating Trading
fair_value = a_opt + b_opt*data[stock1].fillna(method='bfill')[t]
if t == window:
old_signal = 0
if t_opt > t_threshold:
signal = 0
gross_return = 0
else:
signal = np.sign(fair_value - data[stock2].fillna(method='bfill')[t])
gross_return = signal*returns[stock2].fillna(method='bfill')[t] - signal*returns[stock1].fillna(method='bfill')[t]
fees = fee*abs(signal - old_signal)
net_return = gross_return - fees
gross_returns = np.append(gross_returns,gross_return)
net_returns = np.append(net_returns,net_return)
t_s = np.append(t_s,t_opt)
#interface: reporting daily positions and realised returns
print('day '+str(data.index[t]))
print('')
if signal == 0:
print('no Trading')
elif signal == 1:
print('long position on '+stock2+' and short position on '+stock1)
else:
print('long position on '+stock1+' and short position on '+stock2)
print('gross daily return: '+str(round(gross_return*100,2))+'%')
print('net daily return: '+str(round(net_return*100,2))+'%')
print('cumulative net return so far: '+str(round(np.prod(1+net_returns)*100-100,2))+'%')
print('')
#try to store the daily results on a dataframe
for i in range(window,len(data)):
results.loc[i] = [str(round(gross_return*100,2))],[str(round(net_return*100,[str(round(np.prod(1+net_returns)*100-100,2))]
old_signal = signal
#plotting equity curves
plt.plot(np.append(1,np.cumprod(1+gross_returns)))
plt.plot(np.append(1,np.cumprod(1+net_returns)))
尝试打印数据框
result
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。