Trailing Stop % From Close Price
Author: ronc
Creation Date: 11/16/2013 3:51 PM
profile picture

ronc

#1
Trying to implement a basic trailing stop but am thoroughly confused.

What I want to do (example:

Sell at stop loss, X% below entry price, adjust the stop price upwards as the closing price increases each day/bar.

WL documentation says: "The trailing stop price is maintained with the Position, and it is modified only when the specified stopPrice is above the current trailing stop price."

CODE:
Please log in to see this code.


I assume that "...trailing stop price is maintained with the Position..." means that WL is tracking and updating the trailing stop price as the symbol price changes, which is what I want. However, it says the trailing stop price "is modified only when the specified stopPrice is above the current trailing stop price" which seems completely backwards. If, for example, stopPrice = entryPrice * 0.95 (5% stop loss) then the trailing stop price should be modified (increased) every day (bar) that the closing price increases above the entry price, which implies that the current trailing stop price is above the original stopPrice - opposite from the documentation.

To do a 5% trailing stop loss, do I just set stopPrice = entryPrice * 0.95, and WL will continuously adjust the stop price upwards if the Close prices increase?
profile picture

Eugene

#2
For a long position, a trailing stop only goes up but never down. WL is tracking the stop price, ignoring any changes you send that would make the stop level fall. Vice versa for shorts. This is what maintaining the trailing stop price means. That simple.

QUOTE:
To do a 5% trailing stop loss, do I just set stopPrice = entryPrice * 0.95, and WL will continuously adjust the stop price upwards if the Close prices increase?

No, you adjust the stop price - WL only does not let it move back. Setting a trailing stop this way only makes it fixed. Create a rule-based strategy with a % trailing stop and do an "Open code in new strategy window" to see how it can be coded by binding to MFEAsOfBar().
profile picture

ronc

#3
If I understand correctly, MFE and MFEAsOfBar are the maximum intraday excursions, and there is no built-in function or automatic tracking of maximum total excursion since the position was opened? So, CoverAtTrailingStop works inside a single bar (day), but if I am only interested in covering at trailing stops at the open or close of each bar (day) then I need to code that myself, correct? In other words, I need to:
- Find the high and low since entry
- compute the trailing offsets from the high and low values
- close the position if price violates a trailing offset
Right track?
profile picture

Eugene

#4
That's right. You need to find the highest (lowest) Open or Close since entry (i.e. bar - Position.EntryBar) and compute the trailing offsets from these values.
profile picture

ronc

#5
If I use the Highest and Lowest indicators for this, do they consider the specified bar in addition to previous bars? E.g.

CODE:
Please log in to see this code.


... returns the highest value from bar or from bar-1?
profile picture

Eugene

#6
Hint: You could easily check that by inserting a PrintDebug() statement in your code.
profile picture

vumusic

#7
Likewise!

As a newbie I found it extremely confusing about WL's trailing stop.

1) The documentation for SellAtTrailingStop is not understandable. I hope someone would add more clarifications to help newbies.

2) It seems to me I have to input a stop price for WL trailing stop unlike in real life (broker orders) where a stop price is adjusted automatically based on new highs or lows. No "trailing" action can be seen in WL trailing stop commands.

3) So based on my understanding above, the only difference between SellAtStop and SellAtTraillingStop is the effect of the price direction. SellAtStop sells when price ascends and reaches the stopPrice where as SellAtTrailingStop sells when price descends and reaches the stopPrice (after ascending pass the stopPrice).

Am I missing something?
profile picture

Eugene

#8
What you describe as "SellAtStop sells when price ascends" is not how SellAtStop works in real life. This is how SellAtLimit operates i.e. limit orders.

The difference between SellAtStop and SellAtTraillingStop is that the former can go down while the latter will not. So unlike SellAtStop which is free to go in both directions Wealth-Lab will modify the trailing stop for the next bar only if it has ascended.

Here's a quick example. Run this code on 1 year of EOD data for CSCO, for instance. Compile, then drag the slider at the bottom which says "Stop = 0" from 0 (stop) to 1 (trailing stop) and note the dotted lines on the chart to see the difference in behavior:

CODE:
Please log in to see this code.

Hope that helps.
profile picture

vumusic

#9
Hi Eugene,

Your response speed is second to none. Thanks.

The chart did help me understand (by looking at the stopPrice plot). Thanks again.

About the SellAtTrailingStop, what that threw me off was the "specified StopPrice" in the instruction. It means you have to input a stopPrice for every bar. Once this part is understood, the rest of the instruction is clear.

Hope this clarification helps other new comers.
profile picture

Eugene

#10
Hi,

Your understanding is correct. Wealth-Lab's backtesting is bar-based so you have to reestablish the stop price every bar.