DEFPARAM CumulateOrders = FALSE
Media = average[63,0](close)
Sopra = close > Media
Sotto = close < Media
IF Sopra AND Not OnMarket THEN
BUY 1 CONTRACT AT MARKET
ENDIF
IF Sotto AND Not OnMarket THEN
SELLSHORT 1 CONTRACT AT MARKET
ENDIF
//************************************************************************
// trailing stop function
TrailingStart = 30 * pipsize //20 trailing will start @trailinstart points profit
TrailingStep = 10 * pipsize //10 trailing step to move the "stoploss"
MainSL = 10 * pipsize //10 initial SL
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL = 0
InitialSL = 0
ENDIF
//
IF LongOnMarket THEN
IF NewSL = 0 THEN
MyProfit = close - TradePrice
IF MyProfit >= TrailingStart THEN
SLunits = max(1,round(((MyProfit - TrailingStart) / TrailingStep) - 0.5) + 1)
NewSL = TradePrice + (SLunits * TrailingStep) - MainSL
ENDIF
ELSE
MyProfit = close - NewSL
IF MyProfit >= TrailingStart THEN
SLunits = round(((MyProfit - TrailingStart) / TrailingStep) - 0.5)
NewSL = NewSL + (SLunits * TrailingStep)
ENDIF
ENDIF
ELSIF ShortOnMarket THEN
IF NewSL = 0 THEN
MyProfit = TradePrice - close
IF MyProfit >= TrailingStart THEN
SLunits = max(1,round(((MyProfit - TrailingStart) / TrailingStep) - 0.5) + 1)
NewSL = (SLunits * TrailingStep) + MainSL - TradePrice
ENDIF
ELSE
MyProfit = NewSL - close
IF MyProfit >= TrailingStart THEN
SLunits = round(((MyProfit - TrailingStart) / TrailingStep) - 0.5)
NewSL = NewSL - (SLunits * TrailingStep)
ENDIF
ENDIF
ENDIF
//
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF