// SuperTrend
//
//https://www.prorealcode.com/topic/ritracciamento-supertrend-2-minuti/page/2/#post-185761
//
DEFPARAM CumulateOrders = false
Defparam Flatbefore = 010000
Defparam Flatafter = 220000
Timeframe(4h,UpdateOnClose)
H4close = close[1]
//
Timeframe(4h,default)
ONCE IncrocioST = 0
ONCE IncrocioST2 = 0
ST = Supertrend[2,20] //2,20
ST2 = Supertrend[8,10] //8,10
ST2rialzista = close > ST2
ST2ribassista = close < ST2
IF close CROSSES OVER ST2 OR close CROSSES UNDER ST2 THEN
IncrocioST2 = close
ENDIF
IF close CROSSES OVER ST OR close CROSSES UNDER ST THEN
IncrocioST = close
ENDIF
CondL1 = Not OnMarket
CondL2 = close > H4close
CondL3 = IncrocioST < IncrocioST2
CondL4 = ST2rialzista
CondL5 = Not OnMarket
CondL6 = close CROSSES OVER ST
CondL = CondL1 AND CondL2 AND CondL3 AND CondL4 AND CondL5 AND CondL6
IF CondL THEN
BUY 1 Contract AT Market
ENDIF
//
CondS1 = CondL1
CondS2 = close < H4close
CondS3 = IncrocioST > IncrocioST2
CondS4 = ST2ribassista
CondS5 = CondL5
CondS6 = close CROSSES UNDER ST
CondS = CondS1 AND CondS2 AND CondS3 AND CondS4 AND CondS5 AND CondS6
IF CondS THEN
SELLSHORT 1 Contract AT Market
ENDIF
//
Timeframe(default)
//SET STOP pLOSS 50
//
//************************************************************************
//trailing stop function
trailingstart = 30 //30 trailing will start @trailinstart points profit
trailingstep = 0 //0 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-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+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 tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-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
//************************************************************************
//graphonprice ST coloured(255,0,0,255) AS "SuperTrend Veloce"
//graphonprice ST2 coloured(0,0,255,255) AS "SuperTrend Lento"
//graphonprice NewSL AS "StopLoss"
//graph positionprice*positionperf/pipsize AS"Profitto"