I would like to calculate the current ratio of those holdings securites so that I will rebalance if the ratio is not what I want.
Attached backtest:
- I use self.SetHoldings() to hold SPY with 50% of my account equity.
- I will calculate the value of my holdings monthly
- I will rebalance it and always balance to 50%
Shile Wen
Hi Sulfred,
An easier way to rebalance would be to count the number of securities in our portfolio, then rebalance each security to have (.5 / (number of securities)) allocation. I’ve shown how to do this in the attached backtest.
Best Regards,
Shile Wen
Sulfred
Hi Shile,
Thank you for your reply, I don't want to reblance the portfolio in an equal ratio. I just want to keep the desired ratio. For example, SPY is 50%, APPl is 20%. So, I may need to know the current security ratio and do the rebalance when there is a need.
I would like to ask if the way I used to calculate the security ratio is correct or not, for example, is the TotalCloseProfit() included the fee.
Thank you very much.
Adam W
TotalCloseProfit() includes an estimate of the fees:
/// <summary> /// Profit if we closed the holdings right now including the approximate fees in units of the account's currency. /// </summary> /// <remarks>Does not use the transaction model for market fills but should.</remarks> public virtual decimal TotalCloseProfit() { if (Quantity == 0) { return 0; } // this is in the account currency var marketOrder = new MarketOrder(_security.Symbol, -Quantity, _security.LocalTime.ConvertToUtc(_security.Exchange.TimeZone)); var orderFee = _security.FeeModel.GetOrderFee( new OrderFeeParameters(_security, marketOrder)).Value; var feesInAccountCurrency = _currencyConverter. ConvertToAccountCurrency(orderFee).Amount; var price = marketOrder.Direction == OrderDirection.Sell ? _security.BidPrice : _security.AskPrice; return (price - AveragePrice) * Quantity * _security.QuoteCurrency.ConversionRate * _security.SymbolProperties.ContractMultiplier - feesInAccountCurrency; }
Though you may want to keep in mind that if you are rebalancing frequently based on this ratio, the fees may start adding up so the original target of 50% SPY becomes deflated to 0 over time.
Adam W
Make sure you set some sort of BrokerageModel for fees to work (or at least define FeeModel.GetOrderFee() for the securities)
Sulfred
Thanks  Adam WÂ
Sulfred
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!