Hi Everyone,
I am new to QuantConnect and am trying to implement a general version of the Fading the Gap Strategy (i.e. easily expandable for several stocks, uses a helper data class), but am having trouble doing so.
- My scheduled functions lead to runtime errors because self.CurrentSlice is None. Why is this happening?
- Even when I begin my functions checking if self.CurrentSlice is None, I still get runtime errors that a “NoneType is not callable”. Why is this?
I apologize for the rather basic questions, but any help would be greatly appreciated.
Thanks!
Initialize() Function Snippet
SYMBOLS = ["AAPL"]
NUM_DEVIATIONS = -3
# Subscribe to data for our stocks, schedule event handlers, and create
# data class instances
self.symbolData = {}
for symbol in SYMBOLS:
self.AddEquity(symbol, Resolution.Minute)
self.symbolData[symbol] = SymbolData(symbol)
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.BeforeMarketClose(symbol, 0),
self.ClosingBar(symbol))
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.AfterMarketOpen(symbol, 1),
self.OpeningBar(symbol, NUM_DEVIATIONS))
self.Schedule.On(self.DateRules.EveryDay(),
self.TimeRules.AfterMarketOpen(symbol, 60),
self.ClosePositions(symbol))
Scheduled Functions
def OpeningBar(self, symbol, k):
# ADDED FOR DEBUGGING
if self.CurrentSlice is None:
return
data = self.symbolData[symbol]
data.UpdateWindow(self.CurrentSlice)
if not data.IsReady():
return
if data.CalculateDeviations() < k:
self.SetHoldings(symbol, 1)
def ClosingBar(self, symbol):
# ADDED FOR DEBUGGING
if self.CurrentSlice is None:
return
self.symbolData[symbol].UpdateWindow(self.CurrentSlice)
Helper Class
class SymbolData(Object):
def __init__(self, symbol):
self.symbol = symbol
self.window = RollingWindow[TradeBar](2)
self.volatility = StandardDeviation(self.symbol, 100)
def UpdateWindow(self, slice):
if self.symbol in slice.Bars:
self.window.Add(slice[symbol])
def UpdateStandarDeviation(self, time, slice):
if slice[self.symbol] is not None:
self.volatility.Update(self.Time, slice[self.symbol].Close)
def CalculateDeviations(self):
delta = self.window[0].Open - self.window[1].Close
deviations = delta / self.volatility.Current.Value
return deviations
def IsReady(self):
return self.window.IsReady and self.volatility.IsReady
Vladimir
Chase Feng
I could not find how you define
self.ClosePositions(symbol)
So I decided to recreate your algorithm using QuantConnect indicators and their extensions.
If you are satisfied with my answer, please accept it and don't forget to like it.
Chase Feng
Thanks for your response, Vladimir! My mistake for not including the rest of the code – OnData(self, slice) just updates the standard deviation with the new slice, and ClosePositions(symbol) just liquidates all holdings of the given symbol. The rest of the code is attached now.
I really appreciate your solution, but for this project, I was hoping to get it working using my original approach. Specifically, if possible, I'd like to figure out where I am going wrong in my code because I fear I am missing something in my understanding.
Yuri Lopukhov
Chase Feng you are missing the big point that in scheduled function third argument has to be a function, not a result of a function call. You were getting this error, because those functions were called immediately in the Initialize, when there is no data yet.
I.e. you should not use parenthesis and you definitely should not use a cycle over all symbols like that. You could cycle over symbols inside scheduled functions instead. You could also transfer indicator's code from Vladimir's solution to make it simpler.
Vladimir
Chase Feng
Here is another working version of the code very similar to yours.
If you are satisfied with my answer, please accept it and don't forget to like it.
Chase Feng
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!