Back

Semantics of SetHoldings(...)

Hello

It may be that I didn't do my homework on this, but I have a question regarding the SetHoldings(...) function.

It appears that the "buying power" referred to in the documentation is calculated taking only the current cash amount into account, not any other security holdings.

In particular, let's say we trigger a first order with

SetHoldings("MSFT", 0.5).

Then, some time later, I'd expect

SetHoldings("MSFT", 1.0)

to trigger an order of approximately the same size (assuming that the price of the security stays the same, approximately). But this is not what happens. Isn't this a bit counter-intuitive?

Adding "liquidateExistingHoldings = true" doesn't help either, since the cash amount determining the buying power is locked in before the liquidation.

Any thoughts on this?

Cheers
Update Backtest








Hey @YoshimiTakano :) Welcome to QC. We just updated the cash management system where previously it was total buying power and only updated with closed trades, now cash is updated instantly when a trade is made - resulting in the weird behavior you noticed.

We just tested a new CustomSetHoldings in the Indian Algorithm to use the "TotalPortfolioValue" when calculating the fraction for the holdings. It was a test run for the new version and it worked OK. The problem is it will always be an approximation as the market value will change and the transaction fees are not factored in. Additionally if you need to liquidate it will take 2 data cycles to sell the current holdings then buy new ones.

You're right liquidate won't help if you're holding near 100% buying power as the orders need to be processed to update the cash balance to free the capital to buy the next asset: e.g.
Holding 100% IBM.
SetHoldings("MSFT", 1, true); <-- rejected because IBM liquidate order not processed

There seems like only one way to deal with this -- we need to will change the way we process orders in backtests to remove the pre-filters and check the cash balance and buying power immediately before processing the order. E.g. This would mean you can queue up 1000 orders all cancelling each other out to net 100% buying power.

We'll put it in the plan for today/tomorrow. We'll test and deploy on Wednesday :)
0

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.


Thanks for all your efforts, Jared! I agree that such a "invest a given factor of the current total portfolio value" is always an approximation in real market conditions. My use case was simple: I just wanted to be invested either 100% long or 100% short in one single asset, re-investing all accumulated profits/losses ("compounding"). SetHoldings("MSFT", [-]100%) implied exactly that to me. Anyway, I'll keep an eye on upcoming changes to QC. Good luck :)
0

Hey @YoshimiTakano -- we just deployed the new synchronous order processing system. By default the portfolio is updated as soon as the order is placed (vs asynchronously before). This is more intuitive to what you'd expect. With live trading this will mean a 0.3s-0.5s lag while the order fills and returns but it can be set to asynchronously with an optional true flag:

int Order(string symbol, int quantity, OrderType type = OrderType.Market, bool asynchronous = false)

I think very few people will need to process asynchronously. But if they do there's also a new event handler - OnOrderEvent() which will fire order status changes when the portfolio is updated.

There's one gotcha that might catch some people -- E.g. in the test code attached we have set short IBM 50% in Sept, and then short 50% SPY in October. In the month since the price of IBM changed to more than 50% of the portfolio, meaning SPY order fails because of insufficient funds. You'd need to reset IBM to 50% of portfolio (which would mean selling a few more shares in this case), and that sale would re-balance the 50% for the following SPY order to successfully process.
0

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.


Update Backtest





0

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.


Loading...

This discussion is closed