"Sell in May" Seasonal Strategy supported by MACD
Author: rmpwealth
Creation Date: 2/10/2013 7:13 PM
profile picture

rmpwealth

#1
I would like to backtest the following seasonal strategy (a version of "Sell in May and Go Away"). I tried to find existing code, but could not; tried to build the strategy using Rules but could not. Please help.

Strategy:

BUY: The first day after October 16th that the MACD is positive.
SELL: The first day after April 20th that the MACD is negative.
profile picture

Eugene

#2
QUOTE:
tried to build the strategy using Rules but could not

A picture is worth a thousand words:

profile picture

rmpwealth

#3
Ahhhh (slap head with hand!). I failed to notice the Day and Month parameters.

Thank you very much.
profile picture

rmpwealth

#4
Hi, again, Eugene.

Something is not working properly. I entered the Rules, and set the following parameters: Symbol=SPY; Scale=Daily; DataRange=6-yrs; PosSize=$10,000 (RPM); and MarginFactor=1.1:1.

Instead of my backtest showing a BUY of a position each fall and a corresponding SELL of that same position each following spring (i.e. -- Long during the winter; Cash during the summer), my backtest showed several BUY/SELL cycles some years and none during other years. Specifically the trade cycles were as follow:

10/08/07 - 11/06/07
12/11/07 - 12/18/07
12/27/07 - 12/31/07
Nothing in 08
10/29/09 - 11/06/09
11/10/09 - 05/07/10
10/19/10 - 05/25/11
10/18/11 - 11/23/11
12/06/11 - 12/20/11
12/22/11 - 04/18/11
10/18/12 - 10/24/12
12/06/12 - Open

I can understand there possibly being no trades during a given calendar cycle if the MACD would never be positive between October and the following April (although that would seem to be unlikely); but there should not be multiple trade cycles between October and the following April. Basically, the strategy should be: Buy a position in the fall, Hold the position all winter, and Sell the position in the spring; repeat each year.

Can you help?
profile picture

Eugene

#5
QUOTE:
but there should not be multiple trade cycles between October and the following April.

What makes you think so? :) Isn't this your own rule?

SELL: The first day after April 20th that the MACD is negative.

The rule is ambiguous and allows to sell at any date in the late spring, summer, fall, and winter. This explains the re-entries. Adding one more condition for the exit as follows will make it work as you planned:


profile picture

rmpwealth

#6
This may be more complicated than it first seemed.

I added the additional condition per the above, and at first glance the strategy appeared to execute as intended. However, when comparing the tabulated results to the chart, there still are problems.

The tabulated Trades were as follow:

10/18/07 - 06/09/08
Nothing for winter 08 to spring 09
10/20/09 - 05/07/10
10/19/10 - 05/25//11
10/18/11 - 04/24/12
10/18/12 - Open

Per the Chart, the following:

On 10/18/07, MACD was negative; MACD didn't turn positive until 11/29/07. Therefore, the BUY signal should have been 11/29/07, not 10/18/07.

MACD turned negative on 05/02/08. Therefore, the SELL should have been on 05/02/08, not 06/09/08.

On 10/29/08, MACD was positive, but no BUY signal was shown. There should have been a Long cycle from 10/29/08 to 04/22/09, but none was shown.

The next three cycles appear to be OK.

On the 10/18/12, MACD still was negative; MACD didn't turn positive until 11/21/12. Therefore, the Buy signal should have been on 11/21/12 instead of 10/18/12.

It appears that something still is not as it should be.
profile picture

Eugene

#7
You're confusing MACD with MACD Histogram. On 10/18/07, MACD was positive.
profile picture

rmpwealth

#8
??????

I thought the MACD Histogram was a graphical representation of the MACD. If that is not true, what is the difference between MACD and MACD Histogram?
profile picture

Eugene

#9
Take another look at the MACD group of conditions. Why would the developers put different rules for MACD and MACD Histogram then?

I'm absolutely sure you'll find the answer in the MACD article in the Wealth-Lab Wiki.
profile picture

rmpwealth

#10
Got it. Thanks.
profile picture

rmpwealth

#11
I modified the above Rules to implement a rigid Winter/Summer trading strategy, but encountered a problem.

First, I removed all references to MACD, and set the trading dates criteria as follow: Buy At Market / Date After Calendar Date: 10/31. Sell At Market / Date After Calendar Date: 04/30 AND Date Before Calendar Date 10/31. It seems to work -- it triggers a Buy each November, and Closes each respective trade the following May.

I then reversed the date criteria so as to trade the summer months instead of winter months, as follows: Buy At Market / Date After Calendar Date: 4/30. Sell At Market / Date After Calendar Date: 10/31 AND Date Before Calendar Date 4/30. However, this set of Rules did not work -- it opened one trade on the first May trading date, and never closed that trade. Any idea why the first set of Rules works but the second set does not?

profile picture

Eugene

#12
QUOTE:
Buy At Market / Date After Calendar Date: 4/30. Sell At Market / Date After Calendar Date: 10/31 AND Date Before Calendar Date 4/30. However, this set of Rules did not work -- it opened one trade on the first May trading date, and never closed that trade.


That's right: this set of rules should not work as desired. No calendar date can satisfy both criteria at the same time, because this time span crosses the new year's date.

This will work:

Buy At Market
Date After Calendar Date: 4/30
Date Before Calendar Date 10/31

Sell At Market
Date After Calendar Date: 10/31
Date Before Calendar Date 12/31
OR
Date After Calendar Date: 1/1
Date Before Calendar Date 4/30



Note the extra "Date Before" condition attached to your entry.
profile picture

rmpwealth

#13
Thank you. It works.

I'm trying to understand why the first set of Rules worked OK for the "Winter" test but not for the "Summer" test (the additional Conditions included in the second set of Rules were required for the "Summer" test). Apparantly the change-of-year affects one set of dates but not the other, but I am not sure I understand why. Please explain in more detail. Thanks.
profile picture

Eugene

#14
A desk calendar could help visualize it:

QUOTE:
Date After Calendar Date: 04/30 AND Date Before Calendar Date 10/31

The first exit condition appears valid: there are two hundred calendar dates within the same year satisfying these conditions.

QUOTE:
Date After Calendar Date: 10/31 AND Date Before Calendar Date 4/30.

Now, is there any calendar date within the same year that satisfies both criteria at the same time? No way. There is no April in this year that follows October. Any given year ends strictly on 12/31, and next year is not considered by the computer.

As can be seen, it's not related to C# altogether, and not a Wealth-Lab thing either.
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).