Hi,
For reference, I'm new to coding and Quantconnect, though have completed most of the bootcamp equities tutorials.
I'm trying to build an algo (as part of a more complicated strategy) which returns a dynamic universe of stocks with the following criteria:
- price between $2-$10; and
- average volume over the last 30 days greater than 100k shares
Once the daily universe of stocks has been selected, I want to log a list of each security in the universe for that day.
I have built the attached backtest (its mainly based on the algorithm built in the 200-50 EMA Momentum Universe tutorial here) to achieve the above, however I'm running into the following issues:
- Whenever I try to use self.Log to log the list of securities currently in the universe (or anything other than “Hello”), I get the following error:
“Runtime Error: Algorithm took longer than 10 minutes on a single time loop. CurrentTimeStepElapsed: 10.0 minutes in Isolator.cs:line 175”
For reference, I was trying to log a list of securities in the universe by using self.Log(selected). I understand this may be incorrect.
2. The algorithm is temperamental. Sometimes it will seemingly run fine (as per the attached backtest), other times it will spit out the above error at me, despite the fact I haven't changed the code in any way.
Does anyone have any idea why I might be running into the above issues? Is there an obvious error in my code that I'm missing? Is the only solutions to upgrade my backtest node?
I would be very grateful for any advice at all.
Kind regards,
Eoin
Lucas
Hi Eoin.
As the error describes, it is thrown when the algorithm takes too long to finish a loop. I do not know why you got this error specifically, as I did not encounter this error, even though I ran the algorithm multiple times. Maybe you are entering a loop that takes to long to finish, due to the high amount of stocks being picked?
To answer the first question, you can log the added and removed stocks with the following code:
Lucas
Eoin
Hi Lucas,
Many thanks for your advice - I entered your OnSecuritiesChanged method for logging universe changes and actually managed to get it to backtest without getting the error as described in my original post, though it still took a number of tries.
I suspect you're right when you say “Maybe you are entering a loop that takes to long to finish, due to the high amount of stocks being picked?” . However, I just don't understand why the error is throw only some of the time?
May I ask what backtest node type you're using? I'm using B-Micro and what to see if a more powerful node might solve my issue, if I can't find another workaround.
Successful backtest attached for reference.
Kind regards,
Eoin
Lucas
Hi Eoin
I use the B2-8 note (the one that cost 14 bucks a month). Yes, it is faster than the free note, but it should not take more than 10 minutes to finish a loop. It takes 50 seconds for me from when i hit “backtest”, to the backtest is completed. It should be noted that a lot of securities is going through the algorithm, which might hit some RAM limits on the free notes.
A workaround (if you are willing to change the algorithm) would be to have tighter constrains in the algorithm, before enterering the first for loop. The history request is what is most likely causing the trouble. Try taking the 100 most liquid stocks (or something like that) and see if that help. You could do something like this
and then sort by price after, or what else you wanna
Lucas
Fred Painchaud
Hi Eoin,
The attached should be stable. I certainly used Lucas' code for OnSecuritiesChanged as it was proper.
Your strategy to calculate volume averages for all assets in (2$, 10$) inherently takes time and memory…
Fred
Eoin
Hi Lucas, Fred,
Thank you both very much for your input, it's been a great help.
I've implemented both of your changes and limited the amount of securities returned by the coarse filter to 50, in the interests of quicker backtesting. Attached is a recent successful backtest for reference.
I also upgraded my backtest node to the B2-8 (mainly out of curiosity) and am pleasantly surprised by how much faster it is, even after scaling the algorithm up a bit.
I am, however, running into the following issues:
I'd be grateful for any advice either of you (or anyone else happening upon this thread) might be able to give me.
I'll slow down on the questions for a while after that, I swear.
Eoin
Fred Painchaud
Hi Eoin,
In that segment, you write self.Log() after the return - it will never be executed. In fact, selected does not exist anymore there.
Use this:
In:
selected is not defined. It is defined in CoarseSelectionFunction but it is then not visible in OnSecuritiesChanged. You want to use your self.averages dict:
Fred
Eoin
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!