Hello,
I'm trying to get the current portfolio holdings of a security that I've purchased but It doesn't seem to be working.
Is there a better way to get the current portfolio holdings quantity?
public void OnData(TradeBars data, ISecurityProvider _securityProvider)
var holdingQuantity = _securityProvider.GetHoldingsQuantity(symbol);
Log("q" + holdingQuantity);
Thanks!
Michael Handschuh
Hey Travis, You can't change the signature of your OnData method. The engine looks for specific methods of the form OnData(DataType), in this case, OnData(TradeBars), so by adding the ISecurityProvider parameter it confuses the engine and it never gets called! If you want to get your holdings for a particular symbol you can use the following:
var holdings = Portfolio[symbol]; var holdingsQuantity = holdings.Quantity
Travis Teichelmann
Hey Micheal, Thanks for the quick response, I was also trying to declare private readonly ISecurityProvider _securityProvider; before Initialize. The console would say that there was no object reference set. When I backtest Log("Quantity: " + holdingsQuantity); the console returns 2016-03-07 09:30:46 Quantity: 0 2016-03-07 09:30:47 Quantity: 0 2016-03-07 09:30:48 Quantity: 0
Michael Handschuh
If you declare the a variable in your algorithm it needs to be initialized. So if you have the following:
private readonly ISecurityProvider _securityProvider;
Then it will always have the default value of null unless you explicitly set a value to that variable. The ISecurityProvider interface is used internally by the engine and brokerage implementation. The Portfolio object implements the interface, so you could add this to Initialize to get it to work:_securityProvider = Portfolio;
But typically it's just easier to directly access the holdings via Portfolio[symbol].QuantityTravis Teichelmann
Great! Things are going well now. I needed to declare
var holdings = Portfolio[symbol].Quantity;
AFTER the algorithm buys. Thank you for helping me brainstorm Micheal.Michael Handschuh
Absolutely correct Travis. If you like, you can save off the SecurityHoldings object and reference it whenever you like using:
var securityHoldings = Portfolio[symbol];
Then any time you access securityHoldings.Quantity you should get the correct answer. If you're interested in the reasoning behind all of this, check out this link describing the different between reference types and value types.Travis Teichelmann
Thanks Micheal That resource really sheds some new light on the difference between value and reference types. I like how constructors and memory allocation are explained. I've gotten pretty far in the MVA course but Bob still hasn't gone over what I just read. FYI, I did end up offloading the security holdings to a variable and I'm super close to a fully functioning strategy.
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!