In this algo I am basically trying to do a coarse selection of stocks that have Price < 50 day SMA. I am using a helper class to calc the SMA as I've seen in other examples. Any ideas why no stocks get returned? Surely in the last couple months there have been stocks under their 50 day SMA.
Martin Molinero
Hi Mark Axmann, I believe the lack of trades is due to the size of the period of the slow moving average. The period number will be the amount of daily data it will need before it is set to Ready. Increasing the data range or reducing the period (for example to 20) made trades appear.
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.
Mark Axmann
Thanks Martin, If I were to go live, how could I ensure that I have the 50 day SMA? Thanks
Mark Axmann
I also just tried adding: SetWarmup(TimeSpan.FromDays(_slowPeriod));
Still no data. Surely there is a way to add a warmup when using universes?
Mark Axmann
I ended up finding my answer in this post. Basically I needed to run the history inside the constructor for my SelectionData class.
public SelectionData(QCAlgorithm algorithm, Symbol symbol)
{
Slow = new SimpleMovingAverage(_slowPeriod);
algorithm.AddSecurity(SecurityType.Equity, symbol);
var history = algorithm.History(symbol, _slowPeriod);
algorithm.RemoveSecurity(symbol);
foreach (TradeBar tradeBar in history)
{
Slow.Update(tradeBar.EndTime, tradeBar.Close);
}
}
Mark Axmann
This added another issue.. when calling AddSecurity() in SelectionData - this triggers the OnSecuritiesChanged() event in my algo , which is what I use to determine if I should add/remove it from the Portfolio. Now as it starts running the history for each stock, it triggers a Position to be added for all of them , alphabetically. Any ideas around this?
Martin Molinero
Hi Mark Axmann. The algorithm could check UniverseManager.Values and trade on the universe which is not UserDefinedUniverse (User defined universe has symbols from AddSecurity/AddEquity calls).
foreach (var universe in UniverseManager.Values) { // User defined universe has symbols from AddSecurity/AddEquity calls if (!(universe is UserDefinedUniverse)) { foreach (var security in _changes.AddedSecurities) { if (universe.Members.ContainsKey(security.Symbol)) { // Trade } } } }
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.
Mark Axmann
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!