How to create custom exits with stop and limit orders
Code trailing stops and targets with market orders
Stop and Limit Orders
This lesson assumes that you know how to code stop and limit orders in Easylanguage. If you aren't familiar with this, please take the lessons in the next module and then come back.Â
Â
While you may use the setpercenttrailing function to set a trailing stop, you may want to have some more flexibility.
Perhaps you want to use a moving average or some other measure to trail price. In that case, you may simply use a stop or limit order together to create your own trailing stops and targets.
Let’s cover some examples!
Example 1: Moving Average Trailing Stop
If you’d like to use a moving average as the trailing stop level, you just set a stop order for the next bar at the moving average, like below
Sell next bar at average(close,20) stop;
Â
Now the stop level will be updated each time as a new bar form, and a stop order will be placed for the next bar.
Example 2: Moving Average Profit Target
To use a moving average that’s above the price as a profit target, you’ll have to place a sell limit order at the moving average.
Sell next bar at average(close,20) limit;
Use Market Orders Instead!
Sometimes you may want to use market orders instead, that are executed if the trailing stop or target level is reached.
While this means that you’ll have to wait for the market to close above or below the stop or target level, it may work better sometimes. In addition, it’s a simpler order type that leaves less margin for errors.
Below you see how we exit a trade if it closes above the 20-period moving average
If close crosses over average(close,20) then sell next bar open;