Create and Optimize a Simple Rotation Strategy?
Author: rmpwealth
Creation Date: 2/18/2011 2:24 PM
profile picture

rmpwealth

#1
I would like to create, backtest and optimize the following simple strategy, but am unable to do so using Rules, and am not a programmer. Would anyone care create a Code that I could study and perhaps modify?

Simple Symbol Rotation Strategy:

Using a pre-defined database of, say, 10 symbols,

- Select and BuyAtMarket on Monday morning the 3 symbols that had the highest percent gains during the previous week.

- Rebalance (rotate?) every Monday morning (i.e. - Every Monday morning, Sell the existing 3 symbols; replace with the 3 best performing symbols from the prior week).

Additional Question: Once the strategy is coded, could the W-L Optimizing feature be used to Optimize one or more of the following variables, or would each variable have to be optimized manually?

1. Optimize the number of symbols (i.e. -- 3 may or may not be optimum)?
2. Optimize the rebalancing period (i.e. -- weekly may not be optimum; bi-weekly or monthly may be better)?
3. Optimize the day-of-week (i.e. -- Monday may not be the best day to rebalance)?

Thank you for your help.
profile picture

Eugene

#2
You don't have to be a programmer.

Click the "Open Strategy" icon, click on "Download..." there, don't specify a date to download all available strategies, then open the "Weak-stock Rotation" strategy from the "Symbol Rotation" folder in your WLP6.

Switch your chart to Weekly bar scale to take care of Monday entries and weekly gains. Uncomment this line of code by removing the slashes to turn it from week-stock rotation into a highest weekly % change rotation:
CODE:
Please log in to see this code.

Click "Compile", then "Save" (or vice versa), then "Execute". Keep in mind that rotational systems should be run on a single symbol only, not on a DataSet.

Optimizing the number of symbols is handled by an existing Strategy parameter. That should give a working strategy to start with (sans #2,#3).
profile picture

rmpwealth

#3
Thanks, Eugene.

QUOTE:
Keep in mind that rotational systems should be run on a single symbol only, not on a DataSet.
I'm not sure I understand this. If I run the code on a single symbol only, with what will the symbol "rotate"? How can the code select the best 3-of-10 symbols each week if I don't specify the 10 symbols?

profile picture

Eugene

#4
Yes it will. The design of rotational systems is specific, looping through DataSetSymbols directly, that's why they should be run in single symbol mode.

But of course you can (and should) select a portfolio simulation position sizing option like "% Equity" -- this will work. Just don't run them on a DataSet.
profile picture

rmpwealth

#5
Thanks, Eugene. I did what you instructed, and the code does seem to work on the entire DataSet even though I selected only one symbol (as you said it would <G>). Also, very clever how you appear to have incorporated into the code the flexibility to use the same code for different purposes by varying which features are "turned on."

However, the code as I am working with it doesn't seem to be doing what I wanted (i.e. -- using the screening criteria to rebalance my positions each week).

My input was as follows:

Scale: Weekly
Data Range: 5-Yrs
PosSize: I first tried Raw Profit = $10,000; then also tried 30% of Equity (for 3 symbols)
Parameters: Set n Symbols to be 3; left ROC Period = 14 and Keep >=N Days = 5 as defaulted

Here's the problem:

The first day's trades purchased 3-symbols, as desired. However, in subsequent weeks, the code sometimes purchased 3-symbols, but more often than not purchased only 2-, 1-, or zero symbols. At first I chalked this up to maybe the strategy was holding symbols that it already owned if those symbols also met the screening criteria for the following week. But in studying the Trades data, I noticed that the code was holding Long positions longer than 1-week; and frequently held more than 3 Long positions.

The strategy I was trying to implement was to rebalance every week (i.e. -- Sell all 3 of the previous week's Long positions, and immediately replace them with the 3 symbols that currently are selected by the screen).

If its not too much trouble, would you please tell me how to further modify the code to do that?

NOTE: I modified the original code by deleting the two slashes on the line of code you quoted above; but only that one line of code -- was I supposed to have removed the two slashes from all the lines of code in that grouping?

Thanks.

profile picture

Eugene

#6
Here's some leftover code in charge of the trades held for at least 5 weeks (as per the system's rules):
CODE:
Please log in to see this code.

Try removing this code block, than click Compile/Go or Execute.
QUOTE:
I modified the original code by deleting the two slashes on the line of code you quoted above; but only that one line of code

Right. Other commented out code blocks are irrelevant in the current context.
profile picture

rmpwealth

#7
Thanks, Eugene. Deleting that line of code does make the code work better; but I am not able to determine whether the code actually is implementing my original strategy (i.e. -- Hold 3 positions; rebalance every week).

If I set nSymbols = 3, the code does always seem to hold only three symbols; however, the code does not seem to literally rebalance the entire portfolio every calendar week (at least not that I can tell from reviewing the Trades List -- the Trades List does not show 3 Sells and 3 Buys each week). Instead, the code appears to hold each symbol until some other Sell criteria is generated, then replaces the Sold symbol with a symbol that presumeably best meets the current Buy criteria. The question is: What is the Sell criteria that the code is using?

The following is my guess, and hopefully this is is what is happening; but I need you to please confirm:

Since the code does not appear to actually sell and replace each position each week, does the code simply continue to hold any symbols that were held the previous week that also meet the current week's criteria (i.e. -- the code doesn't sell a position until there is a symbol that better meets the current Buy criteria than does one of the 3 existing Long positions, and replaces the inferior position at that time)? If that is the case, then the code does implement my original strategy. Please advise.

If on the other hand the code's Sell criteria is something else, please advise what the Sell criteria is.

Thank you again for your patience, Eugene.
profile picture

Eugene

#8
QUOTE:
The question is: What is the Sell criteria that the code is using?

Please see the Description box of the strategy's window for a checklist with its buy and sell rules. If you have removed the leftover code block and are executing the strategy on Weekly data, then there should be no more exit rules.
QUOTE:
If that is the case, then the code does implement my original strategy. Please advise.

Your understanding is correct. If the criteria is still in place, no need to sell a stock just to buy it back.
profile picture

rmpwealth

#9
Thanks, Eugene.

1. If I set my Scale to Weekly as you suggested (in order to nominally trade weekly), does that change all the parameter units to weeks as well? If so, and if I want to nominally trade on a weekly basis, should I change the "Parameter: Keep >=N Days" from 5 to 1?

2. As you state in the Strategy Summary, the MaxDrawdown of this strategy is quite high. I tried to ameliorate this by including in my DataSet some Short Funds and a Money Market Fund, hoping that one or both of these would have "rotated in" during the prolonged market decline of a few years ago, but for whatever reason, that didn't happen. Since the strategy seems to work well only in up markets, would you please give me a code modification that would allow Buys to occur only if the S&P500 would be above its 100-day SMA (I can include SPY in my DataSet, if that would help, instead of having to separately reference the S&P500)?

Thanks again for your help, Eugene.
profile picture

Eugene

#10
1. Yes.

Note: the mentioned parameter is inactive (it has been removed, see my post from 2/22/2011 5:22 AM).

2. There's no doubt that the inverted "weak stock" approach, i.e. "highest percent gains during the previous week", will work well in bull markets.

See the WealthScript Programming Guide > DataSeries > Accessing Secondary Symbols.

First you need to define the external symbol -- before the main (trading) loop:
CODE:
Please log in to see this code.

The buys should be filtered with this additional condition:
CODE:
Please log in to see this code.

profile picture

masiegert

#11
Hello rmpwealth,

would you be so kind to share your code here. I am interessted in testing a portfolio of ETF`s.

Thanks

Martin
profile picture

Eugene

#12
By the way, just a kind reminder: feel free to take a 30-day trial of Wealth-Lab Developer 6. /Otherwise it's not clear to me how would you backtest that code. Which version and build are you using?/
profile picture

rmpwealth

#13
1. Maseigert: I'm not sure it would be a good idea to post my code. I am not a programmer; I'm simply trying to implement Eugene's instructions from above. Probably better if you follow Eugene's instructions from the beginning.

2. Eugene:
QUOTE:
By the way, just a kind reminder: feel free to take a 30-day trial of Wealth-Lab Developer 6. /Otherwise it's not clear to me how would you backtest that code. Which version and build are you using?/

I am using Wealth-Lab Pro, v 6.1.27.0; obtained from Fidelity (I am a "qualified trader"). Do I need to have "Developer" in order to use the above code?

3. Eugene: Thank you for the code lines on 02/17. I'm so embarrassed to say I can't figure out exactly where to insert them. Would you please direct me (give the lines between which each item should be inserted)? Thanks.

4. Eugene: From reading your original strategy code, it appears that it contains several different screening criteria that could be used (i.e. -- comment lines preceeded by "//". If I want to try the original code using one of those other criteria, how would I do that -- just delete the "//" that preceeds the comment of the criteria I want to try? If so, is there a built-in criteria (i.e. -- lowest ROC) that I would have to insert a "//" in front of in order to not have a conflict? Having said that, is it possible to run multiple criteria, or does the code only support running one at a time?

profile picture

Eugene

#14
Hi Richard,
QUOTE:
Do I need to have "Developer" in order to use the above code?

Sorry for the confusion, that reply was addressed to the new user masiegert exclusively.

Re: #3
Count me in. As this discussion have started on 02/18, I'm not sure which code do you mean. Having searched your posts, nothing came from 02/17 as well.

Re: #4
A single uncommented rule at any time is how the strategy was designed to work. Two criterias uncommented -- aside from will it make sense from the trading strategy point of view or not -- will give the effect of combining these two rules.
profile picture

rmpwealth

#15
QUOTE:
Re: #3,
Count me in. As this discussion have started on 02/18, I'm not sure which code do you mean. Having searched your posts, nothing came from 02/17 as well.


My bad; I'm as bad a typist as I am a programmer. The date should have read: 02/27 (the code regarding defining an external symbol, and using the external symbal as a BUY filter). Sorry for the confusion.

Richard

profile picture

Eugene

#16
The 1st block of code should be pasted before the trading loop that starts with this line:
CODE:
Please log in to see this code.

The 2nd block precedes this line:
CODE:
Please log in to see this code.

Then, click Compile/Go or Execute.
profile picture

rmpwealth

#17
It works.

Thank you for your help and patience, Eugene.

With the original code, plus the above modifications, plus what you've taught me above regarding how to further modify the code on my own, I now have a week's worth of Rotation Strategy combinations and permutations to play with.

My wife thanks you as well :-)
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).