Back

How to calculate the current ratio of securities

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:

  1. I use self.SetHoldings() to hold SPY with 50% of my account equity.
  2. I will calculate the value of my holdings monthly
  3. I will rebalance it and always balance to 50%
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. Those calculation is around line 27-28.Thank you very much.
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.


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

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.


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.

0

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.

2

Make sure you set some sort of BrokerageModel for fees to work (or at least define FeeModel.GetOrderFee() for the securities)

1

Thanks  Adam W 

0

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