Back

Error not in index

I keep getting an error message saying VIXY is not in the index.  Is there a way to go about fixing this? Thank you.

import numpy as np
from datetime import datetime
import pandas as pd
import math


class VolTrading(QCAlgorithm):

def __init__(self):
self.previous = None
self.position = None




def Initialize(self):

self.SetStartDate(2007,1,1) #Set Start Date
#self.SetEndDate(2018,10,29) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.AddEquity("SPY", Resolution.Minute)
self.AddEquity("VIXY", Resolution.Minute)
self.SetWarmUp(440)


def OnData(self, data):
history_s = self.History(["SPY"], 2, Resolution.Minute)
last_minute_close_s = history_s.loc["SPY"]["close"][-2]

history_v = self.History(["VIXY"], 2, Resolution.Minute)
last_minute_close_v = history_v.loc["VIXY"]["close"][-2]

self.SPercent = math.log((self.Identity("SPY").Current.Value)/
(last_minute_close_s))
self.VPercent = math.log((self.Identity("VIXY").Current.Value)/
(last_minute_close_v))


if self.IsWarmingUp:
return

if SPercent <= -.01:
if self.position == None:
self.SetHoldings("VIXY", 1)
elif self.position == "SPY":
self.Liquidate("SPY")
self.SetHoldings("VIXY", 1)
self.position = "VIXY"


elif VPercent <= -.01:
if self.position == None:
self.SetHoldings("SPY", 1)
elif self.position == "VIXY":
self.Liquidate("VIXY")
self.SetHoldings("SPY", 1)
self.position = "SPY"
Update Backtest







Hi Ken, thanks for attaching the backtest! After looking at your code, it looks like there is a more efficient way to implement this algorithm. Instead of requesting history, a RollingWindow will be more efficient. A RollingWindow is an array of data that allows for reverse list access semantics, Since you're only requesting the last minute price, we can just store it from the last iteration of OnData().

Also, when looking at QuantConnect's Data Explorer, it can be seen that the VIXY data is only available after 1/4/2011. The algorithm above started in 2007, so there wasn't any data being fed into OnData().

The attached backtest below has the aforementioned changes. Hope this helps!

0


Thank you for the information. What I am trying to do exaclty is take advantage of the fact that the VIX and S&P 500  are accordion-like (inverse correlated) and that there is a litle bit of a lag between the two. Am I on the right track? Thanks again.

0

Hi Ken,

I think VIX and S&P 500 do exist the inverse correlated relationship and the lag in theoretical can give you time to expoit that relationship. However, I think you may want to try some different theshold for determining how does the corresponding index move. I may examine the R-square for different regression model to see which one is the most robust and then based on that model's coefficients to set threshold. 

0

Thank you for the response and ideas. I changed when to buy and sell a little but, I am still getting some errors. I am getting errors in the Spercent ahd Vpercent lines. Also,  I believe that my if statements are not working correctly to buy and sell right when the if statements become true. Any help would be much apperciated. Thanks.

 import numpy as np
from datetime import datetime
import pandas as pd



class VolTrading(QCAlgorithm):

def __init__(self):
self.previous = None
self.position = None




def Initialize(self):

self.SetStartDate(2018,1,1) #Set Start Date
#self.SetEndDate(2018,1,1) #Set End Date
self.SetCash(10000) #Set Strategy Cash
self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
self.vixy = self.AddEquity("VIXY", Resolution.Minute).Symbol
self.SetWarmUp(440)


def OnData(self, data):

if data.Bars.ContainsKey("SPY") or data.Bars.ContainsKey("VIXY"):

#if data.Bars.ContainsKey("VIXY"):
# return
# if data.Bars.ContainsKey("SPY"):
# spy_open = self.History([self.spy], 2, Resolution.Minute)
#elif data.Bars.ContainsKey("SPY"):
# vixy_open = self.History([self.vixy], 2, Resolution.Minute)


#if not self.spy_open.empty and not vixy_open.empty:
# spy_open_one = spy_open.loc[str(self.spy)]["open"]
# vixy_open_one = vivy_open.loc[str(self.vixy)]["open"]
#else:
# return
#history_s = self.History(["SPY"], 1, Resolution.Minute)
#last_minute_close_s = history_s.loc["SPY"]["close"][-1]
#history_v = self.History(["IEF"], 1, Resolution.Minute)
#last_minute_close_v = history_v.loc["IEF"]["close"][-1]

#if self.position == None:
# return messes everything up
##add if statement here if none skip it ##self.Securities["SPY"].Price
#self.SPercent = float(np.log(((float(self.Identity(self.spy).Current.Value))/(float(data[self.spy].Open)))))
#self.VPercent = float(np.log(((float(self.Identity(self.vixy).Current.Value))/(float(data[self.vixy].Open)))))

self.SPercent = float(np.log(((float((float(
self.Securities[self.spy].Price))/(float((data["SPY"].Open)))))
self.VPercent = float(np.log(((float((float(
self.Securities[self.vixy].Price))/(float((data["VIXY"].Open)))))


if self.IsWarmingUp:
return


if self.SPercent <= -.005:
#or VPercent >=.01
if self.position == None:
self.SetHoldings("VIXY", 1)
elif self.position == "SPY":
self.Liquidate("SPY")
self.SetHoldings("VIXY", 1)
self.position = "VIXY"


if self.SPercent >= .005:
#or VPercent <=-.01
if self.position == None:
self.SetHoldings("SPY", 1)
elif self.position == "VIXY":
self.Liquidate("VIXY")
self.SetHoldings("SPY", 1)
self.position = "SPY"

##1. run time error getting NoneType with different time periods
##2. when trades are excuted
## 3.error with not buying anything other than vixy may be do to the if statement


#####
#self.MarketOrders()
#MarketOrder(Symbol symbol, decimal quantity, bool asynchronous=false, string tag="")
0

Could you be more specific about the errors?
Please share an algorithm (use the attach backtest button below) that shows the issue you are experiencing.
This procedure helps us, the community, to answer your questions quickly and effectively.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


You initialize self.position as None in Initialize(), so self.position is always None before you assign the value to this variable. "if self.position == None: return"  this should always be true and the code after this statement will not run all the time. You should remove this line.

self.SetWarmUp is not required if you don't use indicator in the algorithm.

Please see the attached algorithm

0


Thank you Alexandre and Jing for the responses and the help. I really appreciate it. I will look over this and get back to you two if I have any further questions. Thanks again.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed