Strategy does not close many trades for years
Author: rickraphael
Creation Date: 8/26/2009 8:03 AM
profile picture

rickraphael

#1
Below is code from two strategies called MA 5 and MA 5B. Both enter the position when several SMAs are above each other (5 day above 10 day, 10 day above 20 day, 20 Day above 50 day, 50 day above 300 day and 32 day is above 56 day) and the stock makes a new 19 day high, and if its price is below $17.50.
MA 5B adds one other entry condition, the P/E ratio must be greater than 1 and less than 15.
Exits are taken either when the 32 day SMA crosses under the 56 day SMA or the price makes a new 68 day low.
The problem is that, in back testing, MA 5 (from which MA 5B is derived) executes as expected, but MA 5B generally fails to exit on the moving average cross under when it should. The result is that the positions in MA 5B stay open for years after they were expected to exit.
I have tested this under identical date ranges, both in RP and portfolio simulation. In all tests, MA 5B shows that approximately 75% of the original trades are open after 5 years. Looking at the charts it is clear that many exit signal are being missed.
This code was tested on a large data set (991 stocks). The first trades enter in July 2004 and the test runs through to today.
Can you offer any guidance?
Thanks, Rick

MA 5:
CODE:
Please log in to see this code.



MA 5B (problem code):
CODE:
Please log in to see this code.


profile picture

rickraphael

#2
I forgot to mention that I am running WL 5.4.20.0
profile picture

Cone

#3
In the 5B code all these conditions must be true just to execute the exit logic -

(maFast[bar] > maSlow[bar]) and (maFast_1[bar] > maSlow_2[bar]) and (maFast_3[bar] > maSlow_4[bar])

whereas in MA 5, only this one has to be true for the exit logic to execute.

(maFast[bar] > maSlow[bar])
profile picture

rickraphael

#4
I'm sure you're right because that's what's happening, but I can't see where it is happening in the MA 5B code. The code looks identical to me with the exception of the following in MA 5B:
CODE:
Please log in to see this code.


How can I change MA 5B to exit the same as MA 5?

Thanks
profile picture

Eugene

#5
CODE:
Please log in to see this code.

It seems to me that this is not necessarily a viable idea for backtesting. In this case, your rule needs to take the price at its pre-split level to be actually valid for backtesting.

Add this line to the top of the strategy:
CODE:
Please log in to see this code.

Adjust the High like this:
CODE:
Please log in to see this code.

Finally, change your comparison:
CODE:
Please log in to see this code.

profile picture

rickraphael

#6
Ok, I'll give that a try later this morning. But will it also resolve the exit problem in 5B that I asked about?

Thanks
profile picture

rickraphael

#7
Also, these strategies were developed from Rules (I am not a programmer). The only modifications I made were to the sliders and the appearance of the data series (color and style). If the strategy needed to adjust for splits, shouldn't the Rules based encoding have taken that into account?
profile picture

Cone

#8
Everything contained in a set of { these } are included in the statement. It's very clear that you've [probably mistakenly] put the exit logic within many of the same conditions used for the entry logic. Was that you're intention? We can only assume that it was your intention, because that's the way it's code. If it's not, it's very simple to move blocks of code around by cutting and pasting.
profile picture

rickraphael

#9
No, it was not my intention at all.
MA 5, the 1st version, exits as intended on a 32 day - 56 day moving average cross under or a new 68 day low:

CODE:
Please log in to see this code.



MA 5B, has exactly the same exit code:
CODE:
Please log in to see this code.


But, you indicate that
QUOTE:
In the 5B code all these conditions must be true just to execute the exit logic -

(maFast[bar] > maSlow[bar]) and (maFast_1[bar] > maSlow_2[bar]) and (maFast_3[bar] > maSlow_4[bar])

whereas in MA 5, only this one has to be true for the exit logic to execute.

(maFast[bar] > maSlow[bar])


What I don't understand is how the same code can have different executions? It's not clear to me how MA 5B can have multiple AND statements or where they originate from. Cutting and pasting isn't a problem but what to cut and paste am unsure of.
Again, MA 5B should exit on either a 32-56 day Moving Avenge Cross-under, or a new 68 day Low. Any clarification would be appreciated.

Also, I'm still not sure why
QUOTE:
your rule needs to take the price at its pre-split level to be actually valid for backtesting.
the adjusted split value wasn't wasn't added from the Rules when the code was created. Is that something that Rules based code does not do automatically.

Thanks very much
profile picture

Cone

#10
QUOTE:
What I don't understand is how the same code can have different executions?
It's because you've put in in a different place. Look, you've got your entry and exit conditions mixed together. If this is not clear to you, then please review basic code syntax on any C# for beginners site. The block code should make these things obvious.

Here's MA 5 corrected:
CODE:
Please log in to see this code.


And 5B -
CODE:
Please log in to see this code.



And, so you know, this is equivalent code for 5B with a little more saner way to combine all those entry conditions -
CODE:
Please log in to see this code.
profile picture

Eugene

#11
QUOTE:
Also, I'm still not sure why the adjusted split value wasn't wasn't added from the Rules when the code was created

The Rules do have these conditions but their application isn't automatic. See "Price (or Volume) Action", the 2 rules marked as "backtest". Play with it and open the code in a new window. You typically need them when you refer to "hard coded" price levels like $17.50 in your strategy.
profile picture

rickraphael

#12
Thanks very much for your help with this. I will look more closely at this later today and if I have any further questions I'll get back to you.
profile picture

rickraphael

#13
Cone: Yes, I can see the error now. I misplaced the brackets in a cut and paste operation. That was very helpful information to avoid this mistake in future. Thank you.
Eugene: If I add the Reverse adjust the High DataSeries code to the strategy for backtesting, do I then need to remove it for trading? Seems to work fine with it in. Thanks.
profile picture

Eugene

#14
Rick,

You don't need to remove the rule for trading.
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).