Hi ,
I am new to PRorealtime. Thank you GURU’s for reading the topic.
I am trying to build a code where in when MACD crossover happens with 26 & 52 period EMA.
// Conditions to enter long positions
defparam CUMULATEORDERS = false
//macdline
c2 = MACDline[ 26 , 52, 12 ](close)
//signalline
c3 = ExponentialAverage[12](c2)
//histogram heigt
histbar = c2 – c3
c5 = histbar crosses over 0
c6 = histbar crosses under 0
IF NOT LongOnMarket AND c5 THEN
BUY 1 CONTRACTS AT MARKET
sl = lowest[3](low)
set stop loss sl
tgt = ( POSITIONPRICE – SL ) * 1.5
set target $profit tgt
ENDIF
// Conditions to exit long positions
// Conditions to enter short positions
IF NOT ShortOnMarket AND c6 THEN
SELLSHORT 1 CONTRACTS AT MARKET
sl = highest[3](high)
set stop loss sl
tgt = ( sl – POSITIONPRICE ) * 1.5
set target $profit tgt
ENDIF
The above code is not setting up the Stop loss & targets. The stop loss should be lowest of last three candles when going long & highest when shorting.
Also i notice that order is place after 2 candles are formed delaying the entry of the order.
Can anyone help what is wrong in the code.
SET STOP LOSS and SET TARGET PROFIT require a difference between prices (Close – Low), not a price (Low).
Replace lines 16-19 as follows:
sl = abs(close-lowest[3](low))
set stop loss sl
tgt = SL * 1.5
set target profit tgt
do the same for the Short side using HIGHEST.
Do not use $ for PROFIT, it’s intended for currency values, such as 400 etc…