Hi everyone,
I need some advice from you.
I am running a loop to calculate a linear regression and check how many times the b parameter is positive from all the iterations depending on some criteria.
I want the days on which the linear regression is run to be dynamic every time. What I mean here is that once a signal comes (i.e. the RSI of the stock > 70), then it counts how many days it takes for it to come and do the regression on that number of days.
However, for some reason when I try to do it I get an error saying "Index out of bounds".
This is the code I've got so far:
vix_df = odo(vix, pd.DataFrame)
vix_df.index = vix_df.asof_date
symbol = "SPY"
start = "2004-01-01"
end = "2017-01-20"
prices = get_pricing(symbol, start_date=start, end_date=end, fields='close_price')
prices.index.tz = None
sym_date=list()
sym_prices=list()
sym_vixstartdate=list()
vixinc = dict()
vixdec = dict()
totals = dict()
vixper = dict()
selldays = dict()
previous=[30]
after=[0]
# define a bottom: no close price lower than bottom in the previous x days and next y days.
for y in after:
for s in previous:
R=np.array([0,]*30)
#R = list() # Vector for the RSI Index
vixincrease = 0
vixdecrease = 0
days = 0
for i in range(len(prices.index)-30):
for j in range(s+1):
if prices[i] < prices[i-j-1]:
continue
else: break
if j == s:
for k in range(y+1):
if prices[i] < prices[i+k+1]:
continue
else: break
if k == y:
sym_prices.append(prices[i])
sym_date.append(prices.index[i])
sym_vixstartdate.append(prices.index[i+y])
R=rsi[i+y+1:i+y+30+1] # RSI vector
# Count the number of days until the sell signal comes
RSI = []
for r in R:
if r < 70:
r = 1
else: break
RSI.append(r)
days = sum(RSI) + 1
selldays[(s,y)] = days
b=linreg(range(days),vix_df.close.loc[prices.index[i+y:i+y+days]])
plt.xlabel('Days')
plt.ylabel('VIX')
plt.grid(False)
plt.text(1, 90,'(Bottom: no close price lower than bottom in the previous ' + str(previous) +\
' days and next ' + str(y) + ' day)')
if b>0:
vixincrease=vixincrease+1
else:
vixdecrease=vixdecrease+1
plt.axis([None, None, 0, 100])
vixinc[(s,y)] = vixincrease
vixdec[(s,y)] = vixdecrease
totals[(s,y)] = vixinc[(s,y)]+vixdec[(s,y)]
vixper = {(s,y): float(vixdec[(s,y)])/totals[(s,y)] for (s,y) in totals}
sym_bottom_data = {
'price': sym_prices,
'date': sym_date,
'vixstartdate': sym_vixstartdate
}
print 'percentage:', vixper, 'totals:', totals, 'Signal days:', selldays
The variable "days" should change for every iteration depending on how soon the signal comes.
The error is in the line:
b=linreg(range(days),vix_df.close.loc[prices.index[i+y:i+y+days]])
Where, If I replace the variable "days" with an integer, the code runs smoothly, however, this is not the idea.
Could someone please share some light on this issue?
Thank you all very much in advance.
Cheers!
Sebastian