Coding stop loss 2% below SMA
Author: brucetravali
Creation Date: 6/6/2011 6:03 PM
profile picture

brucetravali

#1
I'm new to WL, so I apologize if this is a basic question but here it goes....

I'm trying to set a sell order at certain percentage below a moving average. I'm using weekly bars. My problem is that I want the sell order to trigger exactly at 2% below the 22 bar sma.

If I enter the "sell at close" option, it will always trigger at the closing price on that day. If I enter "sell at market", it will trigger at the open on the next bar.

Does anyone know how to have the sell order trigger as soon as it hits (crosses over) a certain indicator, in this case 2% below the 22 sma?

Thanks!
profile picture

Eugene

#2
The nature of your question suggests you're not coding - because you'd just pick SellAtStop and settle with that - but using the rule wizard. Problem is, such type of order isn't currently available in Rules.

Below is an illustration of two of the possible approaches: 1) define the SMA offset by 2% and 2) simply subtract a percentage from the 22-week SMA:

CODE:
Please log in to see this code.
profile picture

Tobey

#3
Hi Eugene,

I'd like to back test with different stops.
I'd like to test from 2% to 12% in Half % increments. From looking at the code above, it looks like the code below could be inserted into any strategy to backtest setting a stop.

Once again thanks for the help
Tobey

CODE:
Please log in to see this code.

profile picture

Eugene

#4
Hi Tobey,

Yes it can be insterted. See example below tailored for your increments.

CODE:
Please log in to see this code.
profile picture

Tobey

#5
Hi Eugene, Thanks so much! Tobey
profile picture

Eugene

#6
You're welcome.
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).