Hi,
I really like the AssetClassmomentum strategy (https://www.quantconnect.com/tutorials/strategy-library/asset-class-momentum) but would like to modify it in order to avoid some lack of liquidity situtation in which it tries to buy more shares that avaiable margin permits.
In order to do that I need the last price of the symbol being traded from inside the Rebalance method however I don't know how to call the OnData method and get the tradebar data back or if there is a better way to do it.
I have been through the tutorials but so far I have not found any examples in which tradebar data is accessed outside of OnData.
In the attached backtest I am simply setting a global variable to the first symbol in self.symbols just to have some price value but this does not provide the solution I need.
Any help on this matter will be truly appreciated.
Cheers,
Andres
Fred Painchaud
Hi Andres,
You cannot “call the OnData” method to get a TradeBar. LEAN calls the OnData method and passes data to it based a simulation of time passing while backtesting and based on real-time during live trading.
I don't fully understand what you are trying to do but I use that sentence in your post “In order to do that I need the last price of the symbol being traded from inside the Rebalance method”.
I think you want to have access to the previous close of all traded symbols.
If so, to do that, the best data structure is a dictionary.
So I would add it to the Initialize method:
self.LastPrices = {}
and then in OnData, I would iterate over your symbols to get the price for each of them:
Then, in Rebalance, you can do:
self.LastPrices[some_symbol]
to get the last price of “some_symbol” (from your list of course).
Fred
Andres Arizpe
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!