Rotation Strategy based on Highest ROC that on weekly data and external symbol
Author: rmpwealth
Creation Date: 5/17/2011 1:30 PM
profile picture

rmpwealth

#1
Eugene;

I have tried to incorporate your earlier suggestions to code a Rotation Strategy based on Highest ROC that (a) evaluates/trades on a weekly basis, and (b) is long only when SPY is above its 200-day SMA (i.e. – only Buy if SPY >200-day SMA; Sell immediately if SPY is <200-day SMA).

Below is the code. Something is wrong, because the code executes trades during the autumn of 2008 even though SPY was less than its 200-day SMA during that time.

My input criteria were as follow:

DataBase = A group of 10 diverse Spider ETF's.
Scale = Daily
DataRange = 3-Yrs.
PosSize = Fixed Denomination = $10,000.
Parameters = Default

Can you please fix this code? Thanks.

CODE:
Please log in to see this code.

profile picture

Eugene

#2
To start with, there's a 100-day SMA of SPY - not 200-day.
profile picture

rmpwealth

#3
QUOTE:
To start with, there's a 100-day SMA of SPY - not 200-day.


Sorry. The code I attached apparently was a trial-and-error version wherein I had been evaluating different SMA values.

However, a 100-day SMA criteria should have caused the Strategy to stop trading even earlier than the original criteria, but it did not. The code seems to be not properly evaluating the SMA of SPY.
profile picture

Eugene

#4
Note: since your only modification to my TSL article code was the addition of an external symbol's SMA, the discussion should be started in the "Weak stock rotation" strategy's forum under "Strategies & Trading". Each uploaded strategy has a place specifically designed to discuss them. Please keep this in mind with regard to modifying uploaded strategies, thank you.

First thing to fix is to make sure you're not starting the main (trading) loop prematurely:
CODE:
Please log in to see this code.

The Period is 14 by default while the SPY SMA is a 100-day (or 200-day) one. Consequently, the SPY SMA has a 0 value bar # 100 (or 200, depending on its period) and for jumping the gun, you'll get trades taken well before bar #100 (200) i.e. trades will be created before all indicators are valid. Here's how to fix that:
CODE:
Please log in to see this code.

CODE:
Please log in to see this code.

Now to your question. To verify that the SPY SMA is absolutely correct here and no trades are being taken when the SPY is below its SMA (assuming that you've already fixed the loop start bar), you could use this plot:
CODE:
Please log in to see this code.
profile picture

rmpwealth

#5
Thanks for the help, Eugene.

I had never explored the Strategies & Trading area. Thanks for the advice.

Re: The above code modifications, I am not a programmer -- where should I insert the above suggested code (i.e. -- lines #2 and #3); immediately after above quoted line #1? Also, where insert the three lines of chart/plot code?

Thanks again.
profile picture

Eugene

#6
The suggested line (either #2 or #3) replaces the existing line #1 which became inappropriate following your introduction of the SPY SMA.

The three lines are inserted below the SPY SMA by analogy with the existing ChartPane creation code block.
profile picture

rmpwealth

#7
Eugene;

I insterted the above code lines where indicated (I think), and the result is below.

I am getting bizarre results.

For example using the following parameters:
Scale: Daily
DataRange: 5-Yrs
PosSize: Portfolio Simulator: Percent of Equity = 20%; Margin Factor = 2:1
DataSet: Source=Fidelity: XLB, XLE, XLF, XLI, XLK, XLP, XLU, XLV, XLY
and FSLXX (my Fidelity cash account)
n Symbols: 5 (default)
ROC Period: 14 (default)
Keep > Days: 5 (default)

1. This morning I get 10 Alerts, but all 10 are the same symbol (Buy XLE).
2. If I view Trades, I notice that it shows symbols have traded multiple times on the same dates.
3. If I view Equity Curve, it shows the Cash symbol on the same dates as the Equity symbol, and shows the Cash value to have drawn down to almost 100% below zero.
4. When I first tried the code, I remember seeing a chart showing SPY price and SMA=100, but no longer can find it.

Can you help?

When we started this thread, it was suggested to modify the existing Rotation Strategy code rather than coding from scratch, but would it be better to start from scratch?


CODE:
Please log in to see this code.
profile picture

Eugene

#8
QUOTE:
1. This morning I get 10 Alerts, but all 10 are the same symbol (Buy XLE).
2. If I view Trades, I notice that it shows symbols have traded multiple times on the same dates.

Wealth-Lab Wiki FAQ :: Strategies and WealthScript :: Rotation strategies

A Rotation strategy isn't working like it should.
profile picture

rmpwealth

#9
Doh!! I remember reading that. Sorry.

However, still one glitch: SPY crossed below its SMA=100 on about 06/09/08, and the Strategy did in fact stop Buying on that date; however, the Strategy did not Sell all open positions AtMarket when SPY crossed below SMA=100 -- it held some open positions until as late as August. Does there need to be another code line to Sell all open positions if SPY crosses below SMA=100?
profile picture

rmpwealth

#10
I still am trying to modify the Weak-Stock Rotation strategy as follows:

1. Use symbols with the highest ROC instead of lowest ROC.

2. Buy symbols only if SPY price is above its SMA=100.

3. Sell all open positions if SPY price is below its SMA=100.

In other words, only implement the Rotation Strategy when SPY price is above its SMA=100; be 100% in cash when SPY price is below its SMA=100.

My modified code is below. As stated above, I am not a programmer – I used the coding tips provided by Eugene as best I could. The code seems to work OK for applying the Highest ROC condition and the Buy condition; but it does not Sell all open positions when SPY price is below its SMA=100.

Can you please help? Thanks.

CODE:
Please log in to see this code.
profile picture

Eugene

#11
Hope this helps:
CODE:
Please log in to see this code.
profile picture

rmpwealth

#12
Thank you very much.