trailing stop with cumulative orders

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #146926 quote
    nonetheless
    Participant
    Master

    I tested a new system today in 2 versions with cumulative orders true and false. The latter opened one position which closed in profit as the price retraced to the trailing stop.

    The one with cumulative orders opened multiple positions, but none of them closed out as the price fell — ie the trailing stop had no effect at all.

    Is there any way to make a trailing stop work with cumulative orders?

    #146932 quote
    GraHal
    Participant
    Master

    I guess the last trade to open did not achieve the starting level of the trailing start and so therefore this non-started trailing stop was applied (i.e. not applied) to all cumulated trades?

    #146945 quote
    robertogozzi
    Moderator
    Master

    If you are using a code snippet as a trailing stop, replace TRADEPRICE with POSITIONPRICE, which is the average price used by ProOrder with multiple cumulated positions.

    This should make things better, but previously saved profits would require to be updated after accumulating positions, to fully make it work as expected.

    #146947 quote
    nonetheless
    Participant
    Master

    Thanks Roberto, that seems to have made a big difference. What do you mean though when you say, previously saved profits would require to be updated after accumulating positions ?

    #146950 quote
    robertogozzi
    Moderator
    Master
    I mean that if you buy (say 1 lot) at 1000 with a trailing start at 20 and a trailing step of 10, after +32 points you’ll have:

    Current Price                                       1032              PositionPrice (former TradePrice) is 1000

    Profits locked in                                      10              (you will exit at 1010 in case price retraces)

    after cumulating 1 more lot at, say 1034,  PositionPrice  is 1017    ((1000 + 1034) / 2):

    Profits previously locked in                  10              (you will still exit at 1010 in case price retraces, but you will lose because PositionPrice is now 1017)

    update profits to be locked in          1027             1017 (current PositionPrice) + 10 pips (first step), so you will be able to exit profitaby at 1027.

    It’s not that straightforward and needs some extra coding, but it’s worth doing it.
    #146951 quote
    robertogozzi
    Moderator
    Master
    This is Nicolas’code updated to accomodate for cumulative positions (the attached pics highlights changes, apart from PositionPrice):
    //*********************************************************************************
    // https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
    //
    // trailing stop function
    //-------------------------------------------------------------
    //reset the stoploss value
    IF NOT ONMARKET THEN
       NewSL           = 0
       MyPositionPrice = 0
    ENDIF
    //-------------------------------------------------------------
    // Update locked in profit, if any, after comulating positions
    PositionCount = abs(CountOfPosition)
    IF NewSL > 0 THEN
       IF PositionCount > PositionCount[1] THEN
          IF LongOnMarket THEN
             NewSL = max(NewSL,PositionPrice * NewSL / MyPositionPrice)
          ELSE
             NewSL = min(NewSL,PositionPrice * NewSL / MyPositionPrice)
          ENDIF
       ENDIF
    ENDIF
    //-------------------------------------------------------------
    trailingstart = 20     //trailing will start @trailinstart points profit
    trailingstep  = 5      //trailing step to move the "stoploss"
    //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 = 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 = newSL-trailingstep*pipsize
       ENDIF
    ENDIF
    //stop order to exit the positions
    IF newSL>0 THEN
       SELL AT newSL STOP
       EXITSHORT AT newSL STOP
    ENDIF
    MyPositionPrice = PositionPrice
    //*********************************************************************************
    nonetheless and Paul thanked this post
    x-3.jpg x-3.jpg
    #146956 quote
    nonetheless
    Participant
    Master
    that’s great – thanks Roberto
    #147037 quote
    GraHal
    Participant
    Master
    Link to Nicolas Code above for Cum TS added as Log 253 here … Snippet Link Library
    robertogozzi, Nicolas and ZeroCafeine thanked this post
    #213589 quote
    ZeroCafeine
    Participant
    Senior
    Is it better to replace lines 50 and 51 with the new Set Stop Price X function ?
    #213611 quote
    robertogozzi
    Moderator
    Master
    Yes, it’s a code snippet written years before those new instructions were added. It’s easy to replace them, just replace both of those lines with this one:
    Set Stop Price NewSL
    ZeroCafeine thanked this post
    #213850 quote
    ZeroCafeine
    Participant
    Senior
    Yes, it’s a code snippet written years before those new instructions were added. It’s easy to replace them, just replace both of those lines with this one:
    Is it possible to use set stop price for exit from a position but with 2 different level and of course different qantity ?
    Set Stop Price PriceA // 10% of position at level A
    
    Set Stop Price PriceB // 90% of position at level B
    #213852 quote
    GraHal
    Participant
    Master
    You could set up a Test Algo on your Demo Account …
    If barindex > 0 Then
    Buy 10 Contracts at Market
    Endif
    
    If LongonMarket then
    Sell 1 Contracts at TradePrice - 25*pipsize Stop
    Sell 9 Contracts at YradePrice - 75*pipsize Stop 
    Endif
    #213853 quote
    GraHal
    Participant
    Master
    It works, but doesn’t drop to 0 positions … thats for you to figure out! 😉
    DEFPARAM CUMULATEORDERS = True
    If Time = 080000 Then
    Buy 10 Contracts at Market
    Endif
     
    If LongonMarket then
    Sell 1 Contracts at TradePrice - 25*pipsize Stop
    Sell 9 Contracts at TradePrice - 50*pipsize Stop
    Endif
    ZeroCafeine thanked this post
    Screenshot-2023-04-29-165816.png Screenshot-2023-04-29-165816.png
    #213855 quote
    GraHal
    Participant
    Master
    Sorted! It’s more fun trying and just doing it?
    DEFPARAM CUMULATEORDERS = True
    If Time = 080000 Then
    Buy 10 Contracts at Market
    Endif
     
    If LongonMarket then
    Sell 1 Contracts at Average[20](close) - 25*pipsize Stop
    Sell 9 Contracts at Average[20](close) - 50*pipsize Stop
    Endif
    
    ZeroCafeine thanked this post
    Screenshot-2023-04-29-170416.png Screenshot-2023-04-29-170416.png
    #213859 quote
    GraHal
    Participant
    Master
    It’s more fun trying and just doing it?
    I was only jesting! 🙂 Coding Algos can be frustrating and very time consuming. We should all try to help each other more, even though at times it feels like the blind leading the blind! 😉
    ZeroCafeine thanked this post
Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.

trailing stop with cumulative orders


ProOrder: Automated Strategies & Backtesting

New Reply
Author
Summary

This topic contains 16 replies,
has 4 voices, and was last updated by ZeroCafeine
2 years, 9 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/09/2020
Status: Active
Attachments: 3 files
Logo Logo
Loading...