I was taught in the earlier bootcamps that "def OnData(self, data)" would be the primary entry point of the algo, and the codes for trade executions(e.g. SetHoldings()) were placed in this function in those bootcamps, so I kind of assumed that would always be the case.
Why is it that in the Liquid Universe bootcamp, the codes for trade execution go under the "def OnSecuritiesChanged()" function? Why is it that some algos have their SetHoldings code under some functions, and some algos have it in other functions? Does it strictly vary depending on what you're trying to build? Do the instructions for orders always go under def OnSecuritiesChanged() when building an algorithm that deals with a liquid universe?
Arthur Asenheimer
In addition to the bootcamps I recommend to work through the documentation as well.
RE your question: If you use the classic style algorithm then OnData is the primary entry point, meaning that you will there receive the data in the given resolution.
But if you use the Algorithm Framework you will no longer have use for the OnData() method since the Algorithm is splitted into modules (universe selection, alpha model, portfolio construction, execution, risk management). The corresponding method here is Update(self, algorithm, data) of your Alpha Model.
Vncne
Hi Arthur, thanks for the reply!
So you mean that in this case, SetHoldings/Liquidate basically goes under the Update() method under the Alpha Model class?
This is what I was trying to work on; I put the market order code within the OnSecuritiesChanged() method within the Universe Selection Model class
Arthur Asenheimer
If you want to transfer your code of your OnData() method to the Algorithm Framework style, then the Update() method of your AlphaModel class is probably the best place to paste the code. But you can submit orders in other methods, too.
Some comments to the attached backtest:
- I think your QCAlgorithm class (inherited) or your AlphaModel class (if available) is a better place for OnSecuritiesChanged() than your UniverseSelectionModel since UniverseSelection runs at midnight when markets are closed.
- In SelectFine you are trying to access the Symbol attribute twice like (f.Symbol).Symbol. This will throw an error.
- EarningYield > 7 ? Are you sure? EarningYield is given as a percentage here. There are no matches for the Top 100 Dollar Volume stocks.
- Why do you set self.changes = changes when there is no use of self.changes outside of OnSecuritiesChanged()
Adam W
> So you mean that in this case, SetHoldings/Liquidate basically goes under the Update() method under the Alpha Model class?
If using Alpha Model, you should just return a list of insights like:
def Update(self, data): insights = [ Insight.Price('SPY',*args), Insight.Price('GOOG',*args), ] return insights
this then passes to portfolio model then execution. SetHoldings/Liquidate is more suitable for classic style or within a universe selection-type trading logic like you have now.
Vncne
@Arthur, thanks! I followed your advice with placing OnSecuritiesChanged under the QCAlgorithm class, and it seems to be making orders now! My bad about the EarningYield > 7!
About the self.changes = changes, that part I copy/pasted from the bootcamp
Vncne
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!