BreakEven & Trailing Profit: complete function

Viewing 15 posts - 46 through 60 (of 138 total)
  • Author
    Posts
  • #164055 quote
    Paul
    Participant
    Master

    I made some changes, again. Goal is use of percentage, splitted long/short and working on different markets. That combo is tricky.

    GraHal and Kovit thanked this post
    #164306 quote
    deleted23092025
    Participant
    New

    By running the main code and testing with 1m bars is not working so good as some trades run super long back in time.

    #165005 quote
    DreaC
    Participant
    Average

    Hello Roberto,

    I am trying to understand your great code. I have the following 2 questions:

    1. SellPriceX = max(max(SellPriceX,SellPrice),q1)      What is the meaning of SellPriceX?
    2. ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))      Should the ‘100’ not be ‘1’?

    Best regards,

    Dré

    #165009 quote
    robertogozzi
    Moderator
    Master
    1. SellPriceX = max(max(SellPriceX,SellPrice),q1)      What is the meaning of SellPriceX?
      it makes sure that whatever the calculations are, the PRICE  at which the position is closed is NEVER less than the previous one
    2. ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))      Should the ‘100’ not be ‘1’?
      yes, it should be 1, you can change it. I actually was reported by another user last year, but I always forgot to change it as it doesn’t affect calculations
    Kovit and DreaC thanked this post
    #165134 quote
    DreaC
    Participant
    Average

    Hello Roberto,

    Thank you for your quick response. I understand the code now better.

    I have the following remarks:

    1.   r1 = ((PositionPrice – SrcL) * ProfitPerCent)   I think it should be:      r1 = PositionPrice – ((PositionPrice – SrcL) * ProfitPerCent)

    2. ExitPriceX should be inialized to 9999999 (ExitPriceX = 9999999) otherwise the ExitPrice will always be zero (0).

    Best regards,

    Dré

    #165141 quote
    robertogozzi
    Moderator
    Master

    Well spotted DreaC, you are right for both glitches.

    R1 must mirror Q1 (in the opposite direction) and ExitPriceX must be like ExitPrice.

    Thank you for sharing with us.

    #165577 quote
    Paul
    Participant
    Master

    Hi Roberto,

    Is there a way to set basepercent also to 0, with the result when the trailingstop kicks in and there is no progress followed by retracement, that the entryprice is the exitprice, so in the statistics it’s not a win?

    #165602 quote
    robertogozzi
    Moderator
    Master

    You should try setting a STEP of 9999.

    #165637 quote
    Paul
    Participant
    Master

    thnx it’s solved

    #165972 quote
    ScalpTrader
    Participant
    Junior

    Well spotted DreaC, you are right for both glitches.

    R1 must mirror Q1 (in the opposite direction) and ExitPriceX must be like ExitPrice.

    Thank you for sharing with us.

    Could you please post the correct code that should be in those lines?

    #165976 quote
    robertogozzi
    Moderator
    Master

    There you go:

    //------------------------------------------------------------------------------------------------------------------------------------------------
    ONCE UseCLOSE = 1                                 //1=use CLOSE,  0=use High/Low
    srcH = close                                      //defaults to CLOSE
    srcL = close                                      //defaults to CLOSE
    IF UseCLOSE = 0 THEN
       srcH = high
       srcL = low
    ENDIF
    DirectionSwitch = (LongOnMarket AND ShortOnMarket[1]) OR (LongOnMarket[1] AND ShortOnMarket)  //True when there's been a change in the direction (likely to be due to a Stop & Reverse)
    IF Not OnMarket OR DirectionSwitch THEN
       //
       // when NOT OnMarket or thare's been a change in direction, reset values to their default settings
       //
       TrailStart    = 30          //30     Start trailing profits from this point
       BasePerCent   = 0.200       //20.0%  Profit percentage to keep when setting BerakEven
       StepSize      = 10          //10     Pip chunks to increase Percentage
       PerCentInc    = 0.100       //10.0%  PerCent increment after each StepSize chunk
       RoundTO       = -0.5        //-0.5   rounds always to Lower integer,   +0.4 rounds always to Higher integer,     0 defaults PRT behaviour
       PriceDistance = 7 * pipsize //7      minimun distance from current price
       y1            = 0           //reset to 0
       y2            = 0           //reset to 0
       ProfitPerCent = BasePerCent //reset to desired default value
       //PositionCount = 0
       SellPrice     = 0
       SellPriceX    = 0
       ExitPrice     = 9999999
       ExitPriceX    = 9999999
    ELSE
       //------------------------------------------------------
       // --- Update Stop Loss after accumulating new positions
       //------------------------------------------------------
       //PositionCount = max(PositionCount,abs(CountOfPosition))
       //
       // update Stop Loss only when PositionPrice has changed (actually when increased, we don't move it if there's been some positions exited)
       //
       //IF PositionCount <> PositionCount[1] AND (ExitPrice + SellPrice)<>9999999 THEN //go on only if Trailing Stop had already started trailing
       IF PositionPrice <> PositionPrice[1] AND (ExitPrice + SellPrice) <> 9999999 THEN //go on only if Trailing Stop had already started trailing
          IF LongOnMarket THEN
             q1         = PositionPrice + ((srcH - PositionPrice) * ProfitPerCent)      //calculate new SL
             SellPriceX = max(max(SellPriceX,SellPrice),q1)
             SellPrice  = max(max(SellPriceX,SellPrice),PositionPrice + (y1 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
          ELSIF ShortOnMarket THEN
             r1         = PositionPrice - ((PositionPrice - srcL) * ProfitPerCent)      //calculate new SL
             ExitPriceX = min(min(ExitPriceX,ExitPrice),r1)
             ExitPrice  = min(min(ExitPriceX,ExitPrice),PositionPrice - (y2 * pipsize)) //set exit price to whatever grants greater profits, comopared to the previous one
          ENDIF
       ENDIF
    // --- Update END
    ENDIF
    //
    IF LongOnMarket AND srcH > (PositionPrice + (y1 * pipsize)) THEN                              //LONG positions
       //
       // compute the value of the Percentage of profits, if any, to lock in for LONG trades
       //
       x1 = (srcH - PositionPrice) / pipsize                                     //convert price to pips
       IF x1 >= TrailStart THEN                                                //    go ahead only if N+ pips
          Diff1         = abs(TrailStart - x1)                                 //difference from current profit and TrailStart
          Chunks1       = max(0,round((Diff1 / StepSize) + RoundTO))           //number of STEPSIZE chunks
          ProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCent
          ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))         //make sure ProfitPerCent doess not exceed 100%
          y1 = max(x1 * ProfitPerCent, y1)                                     //y1 = % of max profit
       ENDIF
    ELSIF ShortOnMarket AND srcL < (PositionPrice - (y2 * pipsize)) THEN                             //SHORT positions
       //
       // compute the value of the Percentage of profits, if any, to lock in for SHORT trades
       //
       x2 = (PositionPrice - srcL) / pipsize                                     //convert price to pips
       IF x2 >= TrailStart THEN                                                //      go ahead only if N+ pips
          Diff2         = abs(TrailStart - x2)                                 //difference from current profit and TrailStart
          Chunks2       = max(0,round((Diff2 / StepSize) + RoundTO))           //number of STEPSIZE chunks
          ProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCent
          ProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent))         //make sure ProfitPerCent doess not exceed 100%
          y2 = max(x2 * ProfitPerCent, y2)                                     //y2 = % of max profit
       ENDIF
    ENDIF
    //------------------------------------------------------------------------------
    //                    manage actual Exit, if needed
    //------------------------------------------------------------------------------
    IF y1 THEN                                                                 //Place pending STOP order when y1 > 0   (LONG positions)
       SellPrice = max(SellPrice,PositionPrice + (y1 * pipsize))                  //convert pips to price
       //
       // check the minimun distance between ExitPrice and current price
       //
       IF abs(close - SellPrice) > PriceDistance THEN
          //
          // place either a LIMIT or STOP pending order according to current price positioning
          //
          IF close >= SellPrice THEN
             SELL AT SellPrice STOP
          ELSE
             SELL AT SellPrice LIMIT
          ENDIF
       ELSE
          //
          //sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
          //
          SELL AT Market
       ENDIF
    ENDIF
    IF y2 THEN                                                                 //Place pending STOP order when y2 > 0   (SHORT positions)
       ExitPrice = min(ExitPrice,PositionPrice - (y2 * pipsize))                  //convert pips to price
       //
       // check the minimun distance between ExitPrice and current price
       //
       IF abs(close - ExitPrice) > PriceDistance THEN
          //
          // place either a LIMIT or STOP pending order according to current price positioning
          //
          IF close <= ExitPrice THEN
             EXITSHORT AT ExitPrice STOP
          ELSE
             EXITSHORT AT ExitPrice LIMIT
          ENDIF
       ELSE
          //
          //ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price
          //
          EXITSHORT AT Market
       ENDIF
    ENDIF
    Roger and Fabien6658 thanked this post
    #165989 quote
    nonetheless
    Participant
    Master

    Here’s a very minor mod, if you want to use % instead of points

    ts = (tradeprice*pc)/100   // % trailing start
    TrailStart    = ts          // Start trailing profits from this point

    Thanks again Roberto for all your work on this!

    Roger thanked this post
    #165993 quote
    Roger
    Participant
    Veteran

    I would have written

    ts = (close*pc)/100   // % trailing start

    because you may be out of the market at that time

    #165994 quote
    nonetheless
    Participant
    Master

    Hmm … I’d say that the trailing stop has an extremely low chance of starting if you’re not onmarket. 😁

    But it does make me think that perhaps it should be positionprice, to allow for cumulative orders ??

    #165995 quote
    Roger
    Participant
    Veteran

    I personally struggle to fully understand Roberto’s code 🙂

    But from what I read the trailing stop is set up when you are not on market or when there is a direction switch. By the way I dont really understand why, but there must be a logic behind this!

Viewing 15 posts - 46 through 60 (of 138 total)
  • You must be logged in to reply to this topic.

BreakEven & Trailing Profit: complete function


ProOrder support

New Reply
Author
Summary

This topic contains 137 replies,
has 23 voices, and was last updated by Wim
5 months, 1 week ago.

Topic Details
Forum: ProOrder support
Language: English
Started: 03/18/2019
Status: Active
Attachments: 25 files
Logo Logo
Loading...