Modifying Sell Rules: sell if close falls 8% below "Benchmark value"
Author: bblidner
Creation Date: 10/30/2010 9:58 AM
profile picture

bblidner

#1
Please help me modify the monthly trading strategy shown below by incorporating the following additional sell conditions:

1) If the sell price at the sell signal date is less than the entry price, execute the sale.

2) If the sell price at the sell signal date is equal to or greater than the entry price, do not execute the sale. Instead, use the sell price indicated at this date as a “Benchmark Value” and only sell if a subsequent closing value is less than 92% of the Benchmark Value.


CODE:
Please log in to see this code.


Using AAPL as a reference and running the code provided, the first buy occurs on 10/30/92 @ $13.13 and the first sale occurs on 2/26/93 @ $13.35. However, according to the paper and pencil calculations I’ve done, the successful incorporation of the new sell conditions into the code provided will result in the first sale taking place on 6/30/93 @ $9.88 because 1) the sale price on 2/29/93 ($13.35) is greater than the buy price of $13.13 (which negates this sale) and 2) the first time the price subsequently crosses below 0.92 * Benchmark Value (0.92 * $13.35 = $12.28) is on 6/30/93

In a similar manner, the modified code should show the following two results:

1) An AAPL buy should occur on 1/31/94 @ $8.19 and this should be sold on 6/30/94 @ $6.93 (the price is less than the entry price).

2) An APPL buy should take place on 7/29/94 @ $8.42 (as also indicated by the code provided) and this should be sold on 12/29/95 @ $7.97, not on 3/31/95 @ $8.81 as indicated by the code provided: The new sell conditions should a) negate the 3/31/95 sale because the closing price at this date ($8.81) is above the buy price ($8.42) and b)
set a Benchmark Value of $8.81 and then sell if subsequent prices cross below $8.81 * 0.92 (or $8.10). The price first crosses below $8.10 on 12/29/95.

I hope my description of the additional sell conditions and the expected output differences between the code provided and the code desired aren’t too long winded. Thank you for your patience and your help.

B. Blidner
profile picture

Eugene

#2
Try this (and note that you should set the start bar at 3 times the EMA period, NOT at bar 8 - WealthScript Guide for more on "Stability of Indicators"):
CODE:
Please log in to see this code.
profile picture

Eugene

#3
Your rule #2 is ambiguous if you give it a thought. It just occurred to me that there could be another variation. My first code sets the “Benchmark Value” only once, when this condition becomes true for the first time in trade. However, it's easily possible that your MACD/Stoch rules trigger further trading signals but the exit still never comes. It's easier to code it than to keep clarifying so here's a code variation that acts more like a "SellAtTrailingStop but AtClose":
CODE:
Please log in to see this code.
profile picture

bblidner

#4
Thank you for your excellent help. The addition of Benchmark information to the chart makes the output very user friendly, which I appreciate.

You are quite correct in anticipating that the Benchmark Value needs to be periodically reset for the strategy to be useful. I should have included this information in my initial request. In this regard, would you please revise the code you provided on 10/30 at 12:39 PM to reflect the following rules for resetting the Benchmark:

Rule 1: Set a Benchmark when the initial condA or condB occurs and the sale price at this bar is equal to or greater than the EntryPrice (if the sales price is lower than the EntryPrice, execute the sale and do not set a Benchmark).

Rule 2: Following setting of a Benchmark a) if any subsequent closing price is below Benchmark * 0.92, sell at the closing bar or b) if a second cond1 or cond2 occurs before a Benchmark sale is triggered, then void the Benchmark.

Rule 3: If a second sell subsequently appears as evoked by condA or condB, either a) sell if the indicated sales price is less than the actual EntryPrice (not the closing price seen with the re-occurrence of the cond1 or cond2) or b) reset the Benchmark to the closing price seen with the reappearance of condA or condB criteria.

Rule 4: Repeat Rule 2 if there is a 3rd occurrence of cond1 or cond2.
Rule 5: Repeat Rule 3 if there is a 4th occurrence of condA or condB.
Rule 6: Continue voiding and re-establishing Benchmarks until a Sell occurs. Once a Sale occurs, restart at Rule 1.

Also, using AAPL and looking at the outputs from your code and mine using 8 bars(I will convert to the 3X the EMA period), your code correctly identifies the first Benchmark of $13.25 (Exit $12.19) on 2/26/93, however, the code provided then Sells on 3/31/93 at $12.88. I’m not sure why this happened. If you revise the code to reflect the Benchmark Rules stated above, then the $13.25 Benchmark would be in place through 4/30/93 (no closing price was lower than $12.19) and then the Benchmark would be voided on 5/28/93 (Rule 2b). On 6/30/93, the closing price was $9.88. Since $9.88 is lower than the Entry Price, the position should be sold (Rule 3a). Note: if the closing price on 6/30/93 was greater than $9.88 (let’s say $12.00 for purposes of this discussion, then $12.00 would be the reset Benchmark per Rule 3b).

I hope these rules and examples provide the information you need. I look forward to your continued help with this project and appreciate your support.

B. Blidner
profile picture

Eugene

#5
Sorry but evolving this code for your new rules would have required a design beyond the quick help I had intended to provide.
profile picture

bblidner

#6
Understood.

The 100+ paper and pencil backtests I've conducted indicate that modifying the sell rules for my strategy, as outlined above, results in significant improvements. That said, would it be permissible/possible for you to recommend someone (and provide contact information) who could assist me.

Thanks.
profile picture

bblidner

#7
Is it appropriate to request support for this project in the user forum?

profile picture

Cone

#8
Sure you can. We're working on a solution (a Market Place) to make this easier in the future.
profile picture

bblidner

#9
Is anyone within the community willing to help me with this coding effort or provide contact information for someone who can. Please respond to this post.

Thank you.
profile picture

Eugene

#10
Please don't post email addresses directly or you can get spammed.
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).