hi,
i am trying to add a stop loss when going long when the candle crosses the 200ma. the stop loss is meant to be under the lowest of the last 5 candles when i have gone long. somehow it is not placing any stop loss.
indicator1 = Average[200](close)
longc1 = (low CROSSES UNDER indicator1)
longc2 = (close CROSSES UNDER indicator1)
bullcandle = (open<close)
period = 5
for a = 0 to period -1
IF (longc1[a] or longc2[a]) and not onmarket[a] then
if bullcandle and close crosses over indicator1 then
buy 1 contracts at market
longstoploss = (tradeprice - lowest[5](low) + 0.002)*pipsize
set stop ploss longstoploss
endif
break
ENDIF
next
Replace TRADEPRICE with CLOSE in line 11, since it takes a whole candle for it to be known. Using CLOSE may not exactly the same, due to slippage (if any), but it’s thfe only known entry price at that moment. TRADEPRICE is either 0 (if it’s the first trade) or the exit price of the prior trade.
Next candle, if you want to be more accurate, you can recalculate your SL. But this is usually not necessary.
i changed the tradeprice to close and whenever i open a position it is closing immediately
longstoploss is always zero but when i check the individual values of close, and lowest[5](low) and do the math myself, its not equal to zero.
i solve it. line 11 replace with
longstoploss = (close - lowest[5](low)+0.0002)*10000
it was pipsize command. i thought it converts the number in the bracket to pips.
Yes, you are right. You have to divide by PipSize:
longstoploss = (tradeprice - lowest[5](low) + 0.002)/pipsize