defparam cumulateorders = false
defparam preloadbars = 5000
once positionsize = 1
once tradetype = 2 // [1]long&short;[2]long;[3]short
//========= STRATEGY ===================================
//long
c10=low<lowest[2](low[1])
c11=close<low[1]
tradelong = c10 and c11
//short
c20=high>highest[2](high[1])
c21=close>high[1]
tradeshort = c20 and c21
//=======================================================
// entry
If (tradetype=1 or tradetype=2) and tradelong then
if not onmarket then
buy positionsize contract at market
endif
endif
if (tradetype=1 or tradetype=3) and tradeshort then
if not onmarket then
sellshort positionsize contract at market
endif
endif
//%trailing stop function
trailingPercent = .26 //org .26
stepPercent = .015 //org .014
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailinstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stop
endif
//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-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF