change points to trailing stop percentages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #205163 quote
    sublime06
    Blocked
    Senior

    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

    #205166 quote
    Nicolas
    Keymaster
    Master

    Use the default instruction for trailing stop in percent: https://www.prorealcode.com/documentation/percenttrailing/

    or look in this thread, there are many version of trailing stop: https://www.prorealcode.com/topic/trailing-stop-and-breakeven-codes/

    #205168 quote
    sublime06
    Blocked
    Senior

    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

    #205173 quote
    robertogozzi
    Moderator
    Master

    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
    #205174 quote
    sublime06
    Blocked
    Senior

    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.

    #205187 quote
    Nicolas
    Keymaster
    Master

    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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

change points to trailing stop percentages


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 5 replies,
has 3 voices, and was last updated by Nicolas
3 years, 2 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 12/02/2022
Status: Active
Attachments: No files
Logo Logo
Loading...