Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Rebalancing the portfolio

I'm new and learning about how the portfolio is rebalanced. I have taken a code sample from one of the tutorials and placed it below.

def my_rebalance(context, data):  
    lng_secs = context.output[context.output['longs']].index  
    lng_wghts = 0.5/len(lng_secs)

    shrt_secs = context.output[context.output['shorts']].index  
    shrt_wghts = -0.5/len(shrt_secs)  


    for security in lng_secs:  
        if data.can_trade(security):  
            order_target_percent(security, lng_wghts)

    for security in shrt_secs:  
        if data.can_trade(security):  
            order_target_percent(security, shrt_wghts)

    for security in context.portfolio.positions:  
        if data.can_trade(security) and security not in lng_secs and security not in shrt_secs:  
            order_target_percent(security, 0)  

Based on the above, I have two questions:

  1. In the last part of the code, we see that securities that are not in the new portfolio are removed. Should this not come before we purchase new securities i.e. should we not liquidate our old positions first and then purchase our new ones? In the above case, this is done afterwards. To me, it makes sense to liquidate first so that we have the cash to purchase new securities. Or is the order not relevant i.e. does Quantopian fix this automatically?
  2. In the above, we only liquidate our positions if the security can trade. What if it can't? how does our new portfolio weights get affected? By example, let's say we have two securities A & B in our portfolio with a 50/50 split. Then let's say our new strategy tells us that we need to rebalance to a 50/50 split of B & C. What happens when A can't trade? Does C stay at zero i.e. no rebalancing takes place?

Thanks ahead.

1 response

First off, welcome. Good questions.

  1. "should we not liquidate our old positions first and then purchase our new ones? "
    In the real world, probably yes. The Quantopian framework however assumes one has unlimited borrowing power. So, if you open some positions and don't have the available cash (eg you haven't closed some other positions first) then the backtester will simply 'lend' you the money (at no charge). This functionality makes a first pass at an algorithm much easier by not having to worry about exact number of shares and available cash etc. In most cases, this timing won't impact performance. If you want to live trade an algorithm, then yes, you will want to control when you open and close positions and check your available cash and borrowing power first.

  2. "What happens when A can't trade? "
    Check out this post https://www.quantopian.com/posts/delisted-securities-now-removed-from-portfolio. The only reason a security can't trade is if it's delisted (it doesn't mean nobody wants to buy it). Quantopian handles this much like your broker would. You can't trade the shares (because they are no longer listed) so the shares stay in your account for a few days. Then, magically, you will see the the shares disappear and some cash appear. In the code above, your re-balance won't be affected. Remember that Quantopian will lend you whatever money you need. If A can't trade, then the code will still go ahead and adjust shares so B and C are 50% of the portfolio value.

The takeaway here is that one needs to explicitly implement cash management. Typically, just check if one has enough cash before opening new positions. This isn't generally necessary when testing and doing initial development, but for live trading it's a must.