Simple Portfolio Rebalancing Model In Python

Intuition:

Rebalancing a portfolio can be thought of as resetting the weights of individual assets chosen. For example, we could have a portfolio of four different assets with an even 25% allocated in each. These weightings can change dramatically over time as the assets may fluctuate up and down in price. An investor may choose to rebalance the weightings of the assets over time to their original weight settings. This can be done by either buying or selling more shares of the assets. A portfolio can be rebalanced in a multitude of different ways. For example, some investors try to have a set allocation in certain sectors and or industries. An example of this strategy would be setting the combined industry weights of a portfolio similar to the S&P 500 index. The main point to be taken home by rebalancing a portfolio is that an investor is attempting to maintain their original asset allocations.

When to Rebalance:

The answer to this question can vary from investor to investor. Some individuals may only want to rebalance after an asset allocation deviates from its original weighting by more than 5%-10%. Some individuals may take a different periodic approach known as calendar rebalancing. With this approach, the investor would examine the portfolio at predetermined intervals in time and rebalance accordingly. For example, some investors rebalance at the end of each year. This question of when to rebalance is extremely subjective and dependent on the investors’ original asset allocations, and the random nature of the stock market over time.

Creating a Simple Model to Rebalance a Portfolio:

We can use Python to create a simple model to reset the original weightings of selected assets. We will need a few inputs to make this dynamic model.

• Start date for the allocation period
• End date for recent specified prices
• Series of symbols that constitute the portfolio
• The number of shares purchased for each asset

After the inputs are entered, we can attempt to rebalance the portfolio to its original weightings for each asset. We can write a few loops to solve for a variable which will be the number of shares to obtain the original weighing in the asset. We know the weight of the asset is obtained by dividing the equity position by the total fund value. We also can infer the equity position is derived from the number of shares purchased multiplied by the share price of the asset.

• Equity Position = num shares * share price
• Wi = \$invested in Asset i / \$Value of Portfolio

We will loop through each of our assets and solve for the correct share amount. Note that many programs and services such as Quantopian offer the option to rebalance your portfolio over a prespecified time frame. Or, you could create your own algorithm to rebalance a portfolio in a multitude of ways!

Step One: Dependencies and Data

Step Two: Calculating the Weights

Step 3: Solve for New Share Amounts

Step 4: Output Results

programmingforfinance

Hi, I'm Frank. I have a passion for coding and extend it primarily within the realm of Finance.

View all posts

• Ed says:

Hi, Great post! I have a couple of questions about step 3, (1) could you explain the lines `e = x * sp` and `x += 0.01`?, (2) why does that block need to go inside a `while` loop?

Quick typo note: I noticed a few occurrences of `>`, which (if I’m right) should just be `>` eg. `if w >= dw` should be `if w >= dw`. Thanks again.

• Ed says:

Apologies about the type, I was trying to type “% g t ;” (eg. step 2, line 28) which (in the Python code) should be `>`.

• programmingforfinance says:

The > was a result of HTML character conversion that I had to fix. The reason that e = x * sp and x += 0.01 go back into the while loop is that we are trying to solve for a specific amount of shares to match the desired weights (dw) or the original weightings of the portfolio. We do this by creating a while loop and then breaking out when that weight is obtained. In this case we solve incrementally by adding 0.01 (x += .01) to the share amount.

• James says:

Great. Thanks, that all makes sense! Cheers.