Hey! Every backtest I run keeps ordering "SPY" even when I have a filter that seems to work correctly. EX. stocks with a value under $10. Not sure why this is happening or how I can stop it. The code I'm using is pretty much identical to the post that Micheal created. https://www.quantconnect.com/forum/discussion/918
Jared Broad
SPY is the benchmark symbol in this case - the algorithm buys it on the first time its added. You can either set your own benchmark, set another symbol as a benchmark, or manually check if its SPY when your code is buying the securities.
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.
Travis Teichelmann
Makes sense. Thank you for the help.
Travis Teichelmann
If anybody was wondering I used this to stop the algorithm from purchasing SPY.
if (security.Symbol != "SPY")
Stephen Oehler
Hah. Just had this same problem. Glad you brought this forward for discussion, Travis. So is it adding SPY in our coarse universe selector? I.e. if we're "Take(5)" in our universe, is it filling one of those five slots with SPY at all times? If so, I guess we should always do a Take(6) to account for it?
Travis Teichelmann
Stephen - Since "SPY" is a symbol we should think about the same way as any other symbol that our algorithms may purchase. When we run a coarse universe for some reason it will occupy one of our spaces when we select symbols. You could account for it in your own strategies but I find it easier to just leave the symbol out altogether. There is a SetBenchmark(""); method that I've used to compare the algorithm to any symbol. Check out the algorithm I linked and you'll see that I compared my performance to the performance of Exxon "XOM" "An investor could buy the SPY as a core portfolio holding to provide exposure to the U.S. stock market. Alternatively, an investor could combine it with other ETFs such as a small cap ETF, value-based ETF, or sector ETF to further customize the exposure to U.S. stocks. An active trader could also use this ETF to actively trade because it is exceptionally liquid, making it easy to buy and sell with little cost." Read more: Exchange-Traded Funds: SPDR S&P 500 ETF | Investopedia http://www.investopedia.com/university/exchange-traded-fund/etf3.asp#ixzz41bdIshpL Follow us: Investopedia on Facebook
Stephen Oehler
Great post Travis, thanks. I have a side-topic question to ask you, since you've been working in the coarse universe for a bit. How does the AddedSecurities and RemovedSecurities thing work? Let's assume I am told that a security has been added to my universe due to it satisfying some metric. I then attempt to buy that security. The attempt succeeds. Does that security then get removed from the "Added Securities" enumerable so that you don't attempt to buy it again the next day when the universe resets? Conversely, let's say the attempt to buy fails. Is the AddedSecurities enumerable aware of this and keeps the security so you can attempt it again later? Do we need to manually keep track of the success/failure of buy/sell orders? The reason I ask is because I'm noticing that my coarse universe is exhibiting all manner of bizarre behavior: 1.) Buying the same security multiple times a day (notably, not during the scheduled event I ask for) 2.) Failing to sell a security day after day 3.) others that are less important I'm baffled as to where to begin.
Stephen Oehler
So I take that back; apparently a lot of my mistakes were due to not resetting the _SecurityChanges enum back to .None after applying my trading logic! Vast majority of my errors disappeared after that. Now I'm battling what's left: the occasional persistent invalid market order!
Travis Teichelmann
Hey Stephen, Glad you sorted that problem out! The addition and removal of securities based on time can be controlled by resetting the changes back to none just like you said. There is the .Add and .Remove methods to manually inject a symbol into your algo if the coarse universe selection did not. Are you still having trouble with invalid market orders?
Stephen Oehler
There is the .Add and .Remove methods to manually inject a symbol into your algo if the coarse universe selection did not.
Ah I did not know that! Thanks for the advice :-) As for the invalid orders, yes I did figure those out. I was having so many invalid orders that I was maxing out my memory usage with just six years of backtesting! So for those who come across this same problem, here's what I did (copy-pasting from an email I had sent to Jared): 1.) Switching from the previous build of the LEAN engine to the MASTER build removed about 25% of the errors 2.) Adding in checks to prevent my universe from selecting securities with less than $1Million in trade volume removed another 10% of the errors 3.) Setting my universe resolution to minute or hour removed the rest of the errors, because its no longer having to convert every order to Market On Open.Travis Teichelmann
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!