How do I implement a trailing stop in a trading system?

Assuming you have a system that goes both long (with BUY action) and short (with SELLSHORT action). And let's also assume we want to implement a trailing stop on both the long and short trades.

Create the following two signals:

Set_Long_Trail_Stop
IF(POS_STATE = POS_LONG) THEN (SET(STOP, HI - 1))

Set_Short_Trail_Stop
IF(POS_STATE = POS_SHORT) THEN (SET(STOP, LO + 1))

And assign these signals to two rules with actions of "NONE" (in this case the rule is just designed to adjust the trailing stop). For a long position, the STOP value will only increase, and for a short position, the STOP value will only decrease. The SET does not act otherwise. The STOP never moves in the direction opposite the trade.

Now, in the exit rule of the long position (SELL rule), you would need to include the expression OR CL <= STOP

and in the exit rule for the short position (COVERSHORT rule), you would need to include the expression OR CL >= STOP

If the system's only exit condition is a trailing stop, then the OR would of course not be needed.