Hi everyone
Does anyone know how to code the following condition which must only apply after I have been in a trade for x bars to prevent premature exits:
EXIT IF price is halfway (or lower) between the ENTRYPRICE, and the highest price reached during the trade?
I do not want to use a trailing stop loss because there is a bug in ProRealTime which is causing trailing stop losses to get pushed back in the opposite direction.
Many thanks in advance for your help.
Hi! here you have an example:
// ===========================
// 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")
This is fantastic, thank you Ivan!