I'm having issues retreiving the opening price for a changing list of stocks. Below is a snippet of my algo;
def CoarseSelectionFunction(self, universe):
self.treemodels = {}
self.selected = []
self.universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)
self.universe = [c for c in self.universe if c.Price > 10][:10]
for security in self.universe:
symbol = security.Symbol
sec_history = self.History(symbol, 45000, Resolution.Minute)
try:
self.treemodels[symbol] = TreeModel(sec_history)
if self.treemodels[symbol].Predict() == 1:
self.selected.append(symbol)
except:
pass
return self.selected
def OpeningBar(self):
self.portfolio_current = []
for symbol in self.selected:
open_price = self.Securities[symbol].Open
volatility = self.treemodels[symbol].PreviousVolatility()
close_price = self.treemodels[symbol].PreviousClose()
deviation = (open_price - close_price)/volatility
if deviation < -2:
self.portfolio_current.append(symbol)
if len(self.portfolio_current) != 0:
percent = 1 / (len(self.portfolio_current))
for security in self.portfolio_current:
self.SetHoldings(security,percent)
def ClosePositions(self):
self.Liquidate()
The code follows on from the Fading the Gap example in Boot camp but uses a tree model to decide which out of a universe to invest in.
def Initialize(self):
self.SetStartDate(2020, 10, 19)
self.SetEndDate(2020, 11, 27)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
self.AddEquity("SPY", Resolution.Minute)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 1), self.OpeningBar)
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 45), self.ClosePositions)
In the above OpeningBar(self) for all symbol self.Securities[symbol].Open = 0.0. I then noticed that to get a value for this I have to AddEquity(symbol) for each one of these symbols. For this I put AddEquity()s inside the coarse selection as this is when the universe is determined. Below is the amended code with the AddEquity()s;
def CoarseSelectionFunction(self, universe):
self.treemodels = {}
self.selected = []
universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True)
universe = [c for c in universe if c.Price > 10][:10]
for security in universe:
symbol = security.Symbol
#if security not in self.Securities:
self.AddEquity(symbol, Resolution.Minute)
#self.totalsecurities.append(symbol)
sec_history = self.History(symbol, 45000, Resolution.Minute)
try:
self.treemodels[symbol] = TreeModel(sec_history)
if self.treemodels[symbol].Predict() == 1:
self.selected.append(symbol)
except:
pass
return self.selected
def OpeningBar(self):
self.portfolio_current = []
for symbol in self.selected:
open_price = self.Securities[symbol].Open
volatility = self.treemodels[symbol].PreviousVolatility()
close_price = self.treemodels[symbol].PreviousClose()
deviation = (open_price - close_price)/volatility
if deviation < -2:
self.portfolio_current.append(symbol)
self.RemoveSecurity(symbol)
if len(self.portfolio_current) != 0:
percent = 1 / (len(self.portfolio_current))
for security in self.portfolio_current:
self.SetHoldings(security,percent)
This however resulted in errors for each stock being untradable and nothing was traded (errors shown at the bottom). It may be a timing issue but even SPY which i used in my Initialization was untradable. Is there an easier way to get the opening prices for a changing list at a specific time (OpeningBar needs to be ran as close to opening as possible)? If not how should i approach adding the securities and why are the below stocks untradable?
2020-10-19 00:00:00 :Launching analysis for e84f03c261b72aa99430d5d4946f7e27 with LEAN Engine v2.4.0.0.98832020-10-21 09:31:00 :The security with symbol 'NFLX' is marked as non-tradable.2020-10-26 09:31:00 :The security with symbol 'TSLA' is marked as non-tradable.2020-10-26 09:31:00 :The security with symbol 'INTC' is marked as non-tradable.2020-10-26 09:31:00 :The security with symbol 'MSFT' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'SPY' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'AAPL' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'TSLA' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'FB' is marked as non-tradable.2020-10-30 09:31:00 :The security with symbol 'AAPL' is marked as non-tradable.
Shile Wen
Hi Connor,
I recommend not using AddEquity inside a coarse filter. Furthermore, I answered your question in this thread. For future reference. please do not open duplicate threads on the same issue.
Best,
Shile Wen
Connor Watts
Thanks for the reply- It could be missing data but then when I use AddEquity() for each stock (in this thread) there is data. In this strategy I'm comparing the opening with close- I'm going to experiment with self.SetSecurityInitializer, is there a way to only get the opening price instead of the last known price?
Also, I've seen a few other threads talking about the non-tradable error at the end, is there somewhere this has been resolved?
Thanks again
Shile Wen
Hi Connor,
The reason there is data when using AddEquity is because AddEquity was used with Minute Resolution, while the Universe Selection has Daily Resolution. You should use Universe Selection with Minute data as you have Scheduled Events. Furthermore, when the market opens, the open price will be set to security. SetSecurityInitializer will be called before this data is available.
As for the non-tradable error, this is a sign that the algorithm is trying to trade a security that was removed from the Universe.
Best,
Shile Wen
Connor Watts
Thank you for your help! I changed the Universe Resolution and and all the above issues were solved
Apologies for the delayed response
Connor Watts
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!