Hi, I can’t seem to change the code to work in percent, and I don’t know how to change the variables. thank you
once trailinstop= ActiveTs
trailingstart = TS
trailingstep = TsStep
once breakeaven = ActiveBE
startBreakeven = StartBe
PointsToKeep = PointBe
if trailinstop>0 then
IF NOT ONMARKET THEN
newSL=0
ENDIF
IF LONGONMARKET THEN
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
IF SHORTONMARKET THEN
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif
if breakeaven>0 then
IF NOT ONMARKET THEN
breakevenLevel=0
ENDIF
IF LONGONMARKET AND close-tradeprice(1)>=startBreakeven*pipsize THEN
breakevenLevel = tradeprice(1)+PointsToKeep*pipsize
ENDIF
IF breakevenLevel>0 THEN
SELL AT breakevenLevel STOP
ENDIF
IF SHORTONMARKET AND tradeprice(1)-close>startBreakeven*pipsize THEN
breakevenLevel = tradeprice(1)-PointsToKeep*pipsize
ENDIF
IF breakevenLevel>0 THEN
EXITSHORT AT breakevenLevel STOP
ENDIF
ENDIF
thank you, I had already seen, but the problem I can not change in the variables of the file, and I deleted them, if someone could do it. Thanks
This a modified version of Nicolas’ code to use percentages:
//*********************************************************************************
// https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
// (lines 17- 56)
// using Percentages
//
//trailing stop function
//
StartPerCent = 0.5 //Trailing Stop will activate after 0.5% gain
StepPerCent = 0.1 //Trailing Steps will occur at any further 0.1% gain
//
trailingstart = PositionPrice * StartPerCent / 100 / PipSize
trailingstep = PositionPrice * StepPerCent / 100 / PipSize
//
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-PositionPrice>=trailingstart*pipsize THEN
newSL = PositionPrice+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = max(newSL,newSL+trailingstep*pipsize)
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND PositionPrice-close>=trailingstart*pipsize THEN
newSL = PositionPrice-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = min(newSL,newSL-trailingstep*pipsize)
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//*********************************************************************************
//graphonprice PositionPrice coloured("Blue")
//graphonprice newSL coloured("Red")
//graph TrailingStart
//graph TrailingStep
thanks, Roberto, my concern is to modify in the variables. Even the file I couldn’t attach it so bad I suck. i found here thank.
Don’t fool us, telling you are not the author of that file! 😆
It’s a pity for you to waste our time, just to disguise your advertising, and by using mutliple profiles on the website to hide yourself not being sufficiently good at math to convert points distance in percent. It is a shame.