IF Not OnMarket OR (ShortOnMarket AND LongOnMarket[1]) OR (ShortOnMarket[1] AND LongOnMarket) THEN
StartTrail = 0
StartPrice = 0
ENDIF
IF c2 and Flag THEN
EXITSHORT 1 CONTRACT AT MARKET
Flag = 0
//set the flags forthe start of the Trailing Stop
StartTrail = 1
StartPrice = tradeprice(1)
ENDIF
//
IF StartTrail THEN
//trailing stop function
trailingstart = 0 //trailing will start @trailinstart points profit
trailingstep = 5 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-StartPrice>=trailingstart*pipsize THEN
newSL = StartPrice+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND StartPrice-close>=trailingstart*pipsize THEN
newSL = StartPrice-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
ENDIF