Do you want to get informed about new posts via Email?

Rules Based Rebalance Strategy

Author: lspinv

Creation Date: 11/19/2012 10:42 AM

I am trying to backtest a simple rebalancing strategy, but cannot figure out how to do it using Rules (I am not a programmer).

What I'd like to do is the following: A portfolio that consists of 3-symbols (say A, B & C). Initially put 30% of equity into A, 30% equity into B, and 40% equity into C. Rebalance every month.

Will you please tell me how to do this using Rules. Thank you very much.

What I'd like to do is the following: A portfolio that consists of 3-symbols (say A, B & C). Initially put 30% of equity into A, 30% equity into B, and 40% equity into C. Rebalance every month.

Will you please tell me how to do this using Rules. Thank you very much.

True rebalancing is a complex task in Wealth-Lab because of the way you have to manage a Position. (You can't add shares to a Position, you have to buy another Positions. Likewise, you can't sell just some shares from a Position, you have to split it into 2 Positions and sell one of the Positions.)

The best and easiest way to manage this is to exit all positions at the end of the month and buy them back again, 30% each. In Rules, use Date/Time > Date is Last Trading day of Month to trigger your rebalancing.

The best and easiest way to manage this is to exit all positions at the end of the month and buy them back again, 30% each. In Rules, use Date/Time > Date is Last Trading day of Month to trigger your rebalancing.

Thank you for the quick response.

Your recommended methodology sounds good; and I understand it. However, when I "sell and repurchase" all of my positions at the end of each month, I will not want the new purchases to all be the same percentage of equity. How do I set the position sizes for new purchases to be different percentages of equity (i.e. - Position-A be 25% of equity, Position-B be35% of equity, and Position-C be 40% of equity)?

Your recommended methodology sounds good; and I understand it. However, when I "sell and repurchase" all of my positions at the end of each month, I will not want the new purchases to all be the same percentage of equity. How do I set the position sizes for new purchases to be different percentages of equity (i.e. - Position-A be 25% of equity, Position-B be35% of equity, and Position-C be 40% of equity)?

So you're rebalancing the rebalance? i.e., the allocations are changing each time? How are the percentages determined?

When you need rules to size a position, you need PosSizers. And, if allocations depend on how other stocks are doing in the portfolio, this is getting complex.

When you need rules to size a position, you need PosSizers. And, if allocations depend on how other stocks are doing in the portfolio, this is getting complex.

No, the allocations are not changing each time.

If I understand Cone's method correctly, I will rebalance my equities on a monthly basis as follows:

For the sake of this example, let's say I own three equities.

1. At Close on the Last Trading Day of each Month, I will Exit all three equities.

2. After the Exit, I will have a certain amount of Cash from the sale of the equities. This is the value of my portfolio at that time.

3. At Market the next Trading Day of each month I will Buy back these same three equities. The formula for the amounts of each equity that I will Buy back each month is the same each month. The formula is as follows: The amount of Equity-A that I want to buy back each month is calculated by the amount of Cash in #2 above times 25% divided by the current price of Equity-A. The amount of Equity-B that I want to buy back each month is calculated by the amount of Cash in #2 above times 35% divided by the current price of Equity-B. The amount of Equity-C that I want to buy back each month is calculated by the amount of Cash in #2 above times 40% divided by the current price of Equity-C.

So, at the start of each month, the value of Equity-A will be 25% of the value of the total portfolio; the value of Equity-B will be 35% of the value of the total portfolio; and the value of Equity-C will be 40% of the value of the total portfolio. The value of each equity then will change during the month as the price of each equity changes, and therefore the value of each equity as a percentage of the total value of the portfolio will change; but at the end of the month the three equities will be sold, and the next day the same three equities will be bought back in the original percentage ratio (25%, 35% and 40%) of the total value of the portfolio at that time.

If I understand Cone's method correctly, I will rebalance my equities on a monthly basis as follows:

For the sake of this example, let's say I own three equities.

1. At Close on the Last Trading Day of each Month, I will Exit all three equities.

2. After the Exit, I will have a certain amount of Cash from the sale of the equities. This is the value of my portfolio at that time.

3. At Market the next Trading Day of each month I will Buy back these same three equities. The formula for the amounts of each equity that I will Buy back each month is the same each month. The formula is as follows: The amount of Equity-A that I want to buy back each month is calculated by the amount of Cash in #2 above times 25% divided by the current price of Equity-A. The amount of Equity-B that I want to buy back each month is calculated by the amount of Cash in #2 above times 35% divided by the current price of Equity-B. The amount of Equity-C that I want to buy back each month is calculated by the amount of Cash in #2 above times 40% divided by the current price of Equity-C.

So, at the start of each month, the value of Equity-A will be 25% of the value of the total portfolio; the value of Equity-B will be 35% of the value of the total portfolio; and the value of Equity-C will be 40% of the value of the total portfolio. The value of each equity then will change during the month as the price of each equity changes, and therefore the value of each equity as a percentage of the total value of the portfolio will change; but at the end of the month the three equities will be sold, and the next day the same three equities will be bought back in the original percentage ratio (25%, 35% and 40%) of the total value of the portfolio at that time.

Step 1. Install the MS123 PosSiser Library Extension

Step 2. Open a New Strategy Window and replace the code in the editor with this:

Step 3. Replace "AA", "AXP", "BA" with your instruments, corresponding to the sizing order 25, 35, 40%.

Step 4. Save the script

Step 5. Make sure the Scale is Daily and set your Data Range for testing

Step 6. In the PosSizer control, choose PosSizer and then "Position Options" from the dropdown menu. Then choose Configure... Click the Position.Tag button to highlight it.

Step 7. Finally, since you're using 100% of equity, you should allow for a little margin. Set the margin control to 1.05 to 1. (See User Guide > Strategy Window > Backtesting Strategies > 100% of Equity Sizing.

Step 8. Backtest on a DataSet that contains your 3 symbols.

Step 2. Open a New Strategy Window and replace the code in the editor with this:

CODE:

Please log in to see this code.

Step 3. Replace "AA", "AXP", "BA" with your instruments, corresponding to the sizing order 25, 35, 40%.

Step 4. Save the script

Step 5. Make sure the Scale is Daily and set your Data Range for testing

Step 6. In the PosSizer control, choose PosSizer and then "Position Options" from the dropdown menu. Then choose Configure... Click the Position.Tag button to highlight it.

Step 7. Finally, since you're using 100% of equity, you should allow for a little margin. Set the margin control to 1.05 to 1. (See User Guide > Strategy Window > Backtesting Strategies > 100% of Equity Sizing.

Step 8. Backtest on a DataSet that contains your 3 symbols.

Your instructions and the downloaded code worked fine; I presume it will work when I edit the symbols.

Thank you very much.

I am not a progrmmer, but would like to play around with the strategy a little bit. Will the following edits to the code work?

1. If I want to change the frequency of the rebalances, can I replace "DateRules.IsLastTradingDayOFMonth" with "... Week" or "... Quarter" or "...Year"?

2. If I want to backtest more than three symbols, can I just add symols and corresponding percentages to the code and DataSet? If I want to backtest only two symbols, would it be OK to delete one symbol and percentage from the code, or should I leave the third symbol in the code but just change its percentage to 0% (or 1%)?

Thanks again for your help.

Thank you very much.

I am not a progrmmer, but would like to play around with the strategy a little bit. Will the following edits to the code work?

1. If I want to change the frequency of the rebalances, can I replace "DateRules.IsLastTradingDayOFMonth" with "... Week" or "... Quarter" or "...Year"?

2. If I want to backtest more than three symbols, can I just add symols and corresponding percentages to the code and DataSet? If I want to backtest only two symbols, would it be OK to delete one symbol and percentage from the code, or should I leave the third symbol in the code but just change its percentage to 0% (or 1%)?

Thanks again for your help.

1 - Only ...Quarter is currently available. Everything else is not, and has yet to be programmed.

2 - Yes, but the number of symbols in the arrays must match each other and be equal to the number of symbols in your DataSet i.e. two symbols = two percentages, fifteen symbols = fifteen percentages.

2 - Yes, but the number of symbols in the arrays must match each other and be equal to the number of symbols in your DataSet i.e. two symbols = two percentages, fifteen symbols = fifteen percentages.

Cone, your suggestion of exiting and re-entering a position means that a large commission will have to be paid for the full turn-over of the position, as opposed to a small % of the full position.

Do you have any ideas to make the rebalance trade entail a smaller transaction price in the backtest?

Thanks

Do you have any ideas to make the rebalance trade entail a smaller transaction price in the backtest?

Thanks

The extra commission costs could be considered as execution slippage. Or, you could reduce commissions for the purpose of the simulation. No, I don't see commissions as a big factor here considering the greatly reduced complexity the strategy offers. The answer to your question is in my first post. True rebalancing would increase the complexity of a script by an order (at least) due to position management - splitting positions to reduce size and tracking multiple positions when adding size... and then think about the operations of reducing size from an instrument that has 3, 4, or 10 individual Positions! It can be done, but I'd rather think sbout other things.

Hi,

Regarding the above code, I am trying to avoid skipping trades and make a first trade on the second available bar. In order to accomplish that, I have modified this part of the code

to this

However, I can still see that strategy places its first trade on the 20th bar. Why is it so? How do I make it the strategy to trade immediately on the first available bar and not wait 19 bars?

Thank you.

P.S. I am catching up with my coding, which is something I am not really good at.

Regarding the above code, I am trying to avoid skipping trades and make a first trade on the second available bar. In order to accomplish that, I have modified this part of the code

CODE:

Please log in to see this code.

to this

CODE:

Please log in to see this code.

However, I can still see that strategy places its first trade on the 20th bar. Why is it so? How do I make it the strategy to trade immediately on the first available bar and not wait 19 bars?

Thank you.

P.S. I am catching up with my coding, which is something I am not really good at.

Hi,

It's because the strategy rebalances (making both entries and exits) at the last trading day of month:

It's because the strategy rebalances (making both entries and exits) at the last trading day of month:

CODE:

Please log in to see this code.

Got it, thanks.

Eugene, you mentioned above that only Monthy and Quarterly for IsLastTradingDayOF function available.

Do you have any examples in mind where I can see how to code entries and exits on specific dates? Specifically, I am interested in testing semi-annual and annual rebalancing.

Thank you.

Eugene, you mentioned above that only Monthy and Quarterly for IsLastTradingDayOF function available.

Do you have any examples in mind where I can see how to code entries and exits on specific dates? Specifically, I am interested in testing semi-annual and annual rebalancing.

Thank you.

QUOTE:

Do you have any examples in mind where I can see how to code entries and exits on specific dates?

Sure we do. For code-based Strategies:

Sell in May and go away (downloadable Strategy)

Buy stock on given date with Trailing stop

Using calendar date conditions as setup rather than buy signal (intermediate)

For Rule-based Strategies:

Seasonal Strategy

QUOTE:

Specifically, I am interested in testing semi-annual and annual rebalancing.

Vitaly, why not comment out the