I don’t even bother to ask because so much has already been written here about trailing stops. But I still don’t quite get it. Please can someone help me again.
I have this trailing stop code and would like to add to it the minimum stop of IG and a points-to-keep in % of the price.
How can this look like? In gray I have made the minimum stop as I think it would be correct. But the points-to-keep function?
//a = 1
//b = 0.1
//c = 0.1
set stop %loss a
trailingPercent = b
stepPercent = c
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) // - 6*pipsize
trailingstep = tradeprice(1)*(stepPercent/100) // - 6*pipsize
endif
IF NOT ONMARKET THEN
newSL=0
ENDIF
IF LONGONMARKET THEN
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
IF SHORTONMARKET THEN
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
There you go (not tested):
//a = 1
//b = 0.1
//c = 0.1
MinStop = 10 //10 pips minimum stop loss required
Points2Keep = 0.01 //0.01% points to keep
set stop ploss max(MinStop,TradePrice(1) * a / 100 / pipsize)
trailingPercent = b
stepPercent = c
p2k = TradePrice(1) * Points2Keep / 100
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) // - 6*pipsize
trailingstep = tradeprice(1)*(stepPercent/100) // - 6*pipsize
endif
IF NOT ONMARKET THEN
newSL=0
ENDIF
IF LONGONMARKET THEN
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep+p2k
ENDIF
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
IF SHORTONMARKET THEN
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep+p2k
ENDIF
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
Thank you very much.
I will test it.