Modifying Stops and Trailing Stops
Author: bblidner
Creation Date: 10/11/2010 9:58 AM
profile picture

bblidner

#1
I am currently attempting to code a monthly, single position strategy using Trailing Stops (10% loss) and Stops (1% loss) as sell conditions that are based on monthly closing prices. Could you please tell/show me how to modify the following code so that these sell conditions are based on end-of-month closing prices rather than the within month prices that are lower than the the closing price.

CODE:
Please log in to see this code.


Thank you for your help.
B. Blidner

profile picture

Eugene

#2
First of all, please take a moment to go through this KB article:

Introductory | Bars, Loops, and Bar + 1

The strategy is using "bar" for your exits while in reality you must to use "bar+1". It's like having found the high not valid until the bar closes (i.e. month ends), calculating the retracement for the trailing stop, and exiting at the beginning of the month based on what was happening throughout the month (i.e. impossible).
profile picture

bblidner

#3
Understood - peeking is not good.

Please confirm that the following addresses your input. If so, please help with modifying the code to use end of month closing prices.

CODE:
Please log in to see this code.


Thanks again.
B. Blidner
profile picture

Eugene

#4
Assuming you're running the strategy on Monthly data, a trailing stop (not necessarily -loss) based on a 10% excursion from the highest close price would be:
CODE:
Please log in to see this code.

N/A to the stop since it's based on the entry price.
profile picture

bblidner

#5
It appears that this code also triggers a "sell" when prices that occur during the month that are lower than the closing price for the month. Specifically, if a purchase is made at $40 and the closing price moves up to $50 over the next few months, then the Trailing Stop should be $45 using a 10% trailing stop loss. If the price of the stock the following month has a high of $51, a low of $44 and a close of $46, the stock should not be sold because it closed at $46 (an 8% excursion). The coding provided sells the stock at $45 because it hit this value within the month - this is what I've been trying to avoid.

Thanks,
B. Blidner
profile picture

Eugene

#6
The code can't do that. Only the Close price is considered.

Have you replaced your code with this and compiled?
profile picture

bblidner

#7
I replaced the code (as follows) and compiled it

CODE:
Please log in to see this code.


I'm still getting the same results - sell prices below the closing price

I have output charts for this code in both .pdf and .jpg format. Unfortunately, I can't figure out how to "paste" either of these images under the IMG function.

Have I installed the correct code??

B. Blidner


profile picture

Eugene

#8
*TrailingStop will exit below the closing price by design - it's a stop order.

PDF, DOC are absolutely awful for taking screenshots, BMP and JPG are slightly better, PNG is ideal. Windows Paint is built in every copy of Windows (3.0 from 199x had it), and it supports saving to PNG. Probably like on any forum in existence, a direct image link uploaded to a free image hoster is placed in between the [IMG /IMG] tag. Free image hosting services like imageshack or tinypic can be found by using search engines.
profile picture

bblidner

#9
Is there some other function or code that will enable selling only if the closing monthly price hits a negative 10% excursion from the highest monthly close?

B. Blidner
profile picture

Eugene

#10
You wouldn't want to post a screenshot, but want another function?
profile picture

bblidner

#11
I tried to post a screenshot to help explain the problem I was seeing, however, that is not the function that prompted my initial request for assistance.

I would like to code WLP with two sell functions based on monthly closing prices. The first sell function would trigger a sell signal if the monthly closing price falls 1% below the purchase price. The second sell function would trigger a sell signal if the monthly closing price falls 10% below the highest closing price in the months following the purchase. The code that I posted on 10/11/10 at 11:12 AM does not function as I intend as it triggers sells below monthly closing prices.

I have performed multiple backtest calculations by hand and am reasonably convinced that these sell conditions will improve my results when coupled with the buy conditions I am using. I would like to be able to backtest using WLP to more rigorously determine if my current impressions are correct. I would, therefore, greatly appreciate your help in identifying how two code these sell functions.

B. Blidner
profile picture

Eugene

#12
OK if you refuse to show a proof that the code isn't working like you think it should, then I give up, good luck to you.
profile picture

bblidner

#13
I'm not refusing to show proof - I just couldn't figure out how to post the screenshot and didn't think it was necessary. Getting a little help from friends, here's the screenshot you want:



I would really like your assistance to resolve the coding problem I'm having.

Thanks

B. Blidner
profile picture

Eugene

#14
Thank you. As they say, a picture is worth a thousand words.

Let's take your 3rd trade marked as "NO", from June 2005.

Entry at close ~$24.56
Next highest close ~$24.88
10% excursion, AtStop order = $22.39

So why "no"? The code is working exactly like it was designed. One thing that comes to mind is, maybe you didn't want a stop? i.e.
CODE:
Please log in to see this code.

Maybe your original intention was an AtClose order triggering only if this month's close is below the 10% from the highest close? Replaces the line of code above:
CODE:
Please log in to see this code.

But that's not a stop order. What if your intention was the following?
CODE:
Please log in to see this code.
profile picture

bblidner

#15
My sell intentions are 1) an AtClose order triggered only if this month's close is below the 10% from the highest close (as you've indicated above) and 2) an AtClose order triggered only if this month's close is 1% below the entry price.

Sticking with intention #1 for now, here's the code I've tried and a link to a screenshot of the output I'm getting:

CODE:
Please log in to see this code.



[IMG]
http://yfrog.com/4wsphwith10tslmod2p
[/IMG]

Thanks for your continuing help.

B. Blidner
profile picture

bblidner

#16
OOPS. Here's the link.

profile picture

Eugene

#17
Oh my. The "high" was overwritten with p.EntryPrice on each bar, causing trouble. Sorry about that.
CODE:
Please log in to see this code.
profile picture

bblidner

#18
Perfect.

Thank you very, very much.

B. Blidner
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).