//-------------------------------------------------------------------------
// Main code : SELL AROON 2 TD REV UP6
//-------------------------------------------------------------------------
DEFPARAM CumulateOrders = True // Cumulating positions deactivated
ATR = AverageTrueRange[168]
//take profit
SET TARGET pPROFIT TKPROFIT
IF NOT ONMARKET AND (PositionPerf(1) + PositionPerf(2) + PositionPerf(3) + PositionPerf(4)) < -100 OR STRATEGYPROFIT < -100 THEN
QUIT
ENDIF
//trailing stop function (make equal to absolute value when trailing stop and absolute value)
IF CountOfPosition < 2 AND ONMARKET AND PositionPerf(1) > 0 THEN
//trailing will start @trailinstart points profit
TRSTART = (ATR*0.50)*40
//trailing step to move the "stoploss"
TRSTEP = (ATR*0.50)*4
// Target profit = S*T
TKPROFIT = (ATR*0.50)*16
ENDIF
IF CountOfPosition < 2 AND ONMARKET AND PositionPerf(1) <= 0 THEN
//trailing will start @trailinstart points profit
TRSTART = (ATR*0.45)*40
//trailing step to move the "stoploss"
TRSTEP = (ATR*0.45)*4
// Target profit = S*T
TKPROFIT = (ATR*0.45)*16
ENDIF
//reset the stoploss value
IF NOT ONMARKET THEN
SPLOSS = 0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF SPLOSS = 0 AND close-TradePrice(1)>=TRSTART THEN
SPLOSS = TradePrice(1)+TRSTEP
ENDIF
//next moves
IF SPLOSS > 0 AND close-SPLOSS>TRSTEP THEN
SPLOSS = SPLOSS+TRSTEP
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF SPLOSS = 0 AND TradePrice(1)-close>=TRSTART THEN
SPLOSS = TradePrice(1)-TRSTEP
ENDIF
//next moves
IF SPLOSS> 0 AND SPLOSS-close>TRSTEP THEN
SPLOSS = SPLOSS-TRSTEP
ENDIF
ENDIF
//stop order to exit the positions
IF SPLOSS > 0 THEN
SELL AT SPLOSS STOP
EXITSHORT AT SPLOSS STOP
ENDIF
//put the first stoploss
IF ONMARKET AND SPLOSS = 0 THEN
SET STOP pTRAILING TRSTART
ENDIF