// ===========================
// apply after x bars in trade to prevent premature exits
// ===========================
// ----- General settings
DEFPARAM CumulateOrders = False
rrmultiple = 3
x = 20
// ===========================
// Example
// ===========================
atr = averagetruerange[14](close)
sma10 = average[10](close)
sma60 = average[60](close)
setupLong = sma10 crosses over sma60
// ===========================
// Entry rules
// ===========================
IF NOT longonmarket and setupLong THEN
BUY 1 CONTRACTS AT MARKET
stopprice = close - 1.5*atr
targetdist = rrmultiple * (close - stopprice)
targetprice = close + targetdist
entrybar = barindex
highestPrice = close
SET STOP pRICE stopprice
SET TARGET pRICE targetprice
ENDIF
// ===========================
// Exit after X bars if price < limit
// ===========================
if longonmarket then
bars=bars+1
highestPrice = max(high,highestPrice)
if bars>x then
stopprice = 0.5*(highestPrice+tradeprice)
SET STOP pRICE stopprice
endif
else
bars = 0
highestPrice = close
endif
// ===========================
// Graphs for monitoring
// ===========================
GRAPH bars COLOURED("red")
GRAPH x coloured("blue")
GRAPHONPRICE stopprice COLOURED("red")
GRAPHONPRICE targetprice COLOURED("green")
GRAPHONPRICE highestPrice COLOURED("blue")
GRAPHONPRICE sma10 COLOURED("fuchsia")
GRAPHONPRICE sma60 COLOURED("orange")