Hey all,
I am creating a new algorithm and I was wondering if there were any known caveats to this approach:
1) Retrieving X amount of stocks from a coarse selection filter
2) Creating indicators for each of X stocks and storing them into a dictionary
3) Wait until indicators are ready
3) Sort the stocks in the dictionary based on the indicators value
4) ???
I am current having issues with step #3 where I feel like the backtest is looping infinitely while I am checking if the indicator is ready. If it is not ready, all indicator values come out as 0.
Any help or suggestions would be awesome! Thanks!
for symbol in selectedStocks:
### Create dictionary of every symbol with standard deviation indicators
decodedSymbol = self.Symbol(symbol).Value
stdDict[decodedSymbol] = self.STD(symbol, 20, Resolution.Daily)
self.SetWarmUp(20, Resolution.Daily)
### Create another dictionary of every symbol with indicator values
stdValDict = {}
for key, value in stdDict.items():
while not value.IsReady:
pass
stdValDict[key] = value.Current.Value
Pcnpj
securitySTD[security] = StandardDeviation(security, 60) securitySTD[security].Update(self.Time, previousClose) while not securitySTD[security].IsReady: pass
Attempted with a manual StandardDeviation and .Update() as well for another algorithm. Backtest still throws me in for a (presumed) infinite loop.
Any advice?
Soumya sen
Here are a few tips and see if this helps:
1) If you want to end a while loop, you can use 'break'. You can refer to attached link for differences between break, continue and pass in a while loop in python.
https://www.tutorialspoint.com/python/python_loop_control.htm
2) Also are you sure you want to do a while loop here inside a for loop iterating over the list/dictionary. How about just checking with a If else statement.
Let me know if this helped. Good luck with your experimentation. Keep it up.
Pcnpj
Hey soumya sen
Thanks for the response. I'm not looking into breaking that loop because I want to have the indicator ready before proceeding because I intend to retrieve the indicator value after that check.
If the indicator is not ready and I attempt to retrieve the indicator value it will only return 0.
I've only created indicators in the Initialize portion, but now I am creating it within OnData()/Schedule function.
Any ideas on what is not making my indicator ready?
securitySTD[security] = StandardDeviation(security, 60) securitySTD[security].Update(self.Time, previousClose) while not securitySTD[security].IsReady: pass #do something with securitySTD[security].CurrentValue
Soumya sen
If the Indicator is not ready ( which is the warm up period) , then the while loop will continue to run in an infinite loop as it is evaluated as true ( while NOT (FALSE) is equivalent to while true ). The program will not move forward and cannot move past the warm period . You need to use IF and handle the case when warmup period is true. You can see some examples of other code in tutorial or sample code in discussion forums where the warmup period is used correctly and adopt that. Hope this helps.
Alexandre Catarino
Hi pcnpj ,
We often need the context of the code snippets. Where are they place in the algorithm? In the Initialize method?
Please attach the backtest, It makes the community support more efficient.
Rahul Chowdhury
Hey Pcnpj,
I restructured your algorithm to make the indicators work properly. I created a SymbolData class which holds the indicator data, previousClose data, and updates those fields with consolidated daily data. Follow the comments in the backtest for more details on how it works.
Hope that helped!
Rahul Chowdhury
Hey Pcnpj,
Orders aren't being placed because the symbol you're entering into SetHoldings is not any recognizable symbol.
The mistake is happening here
for security in self.SecuritiesList: symbol = self.Symbol(security).Value
security is actually a Symbol object because you set self.SecuritiesList to a list of Symbols.
filteredByPrice = [c.Symbol for c in sortedByDollarVolume if c.Price > self.minimumDollar and c.Price < self.maximumDollar] self.SecuritiesList = filteredByPrice[:self.topVolume]
If you account for the fact that security is actually a Symbol object in self.OpeningBar, trades will correctly be placed.
Pcnpj
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!