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!
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.
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;
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;