Breakeven & Trailing Stop (fixed)

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #101015 quote
    Luce
    Participant
    New

    Hi Paul – I wonder wether you can help here.

    I’ve got this far, can you advise on the next step..?

    I’ve coded the below as far as I can get but seem to be just missing one minor detail! I’m Still working on a way to combine a trailing STOP with a trailing STEP that becomes a fixed STOP – i.e Trade is a buy open with a 10pt stop, if it moves 2 up, your stop is now 8, if it moves another 2, it’s now 6 but when it gets to 10, so break even on a 1-1, it becomes fixed. Currently I only get a trailing to stop at breakeven, so still risking the full 10.

    // Conditions to enter long positions
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    positionsize=2
    
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    
    noEntryBeforeTime = 080000
    
    timeEnterBefore = time >= noEntryBeforeTime
    
    // Conditions to enter long positions
    
    indicator1 = SuperTrend[2.1,21]
    
    c1 = (close CROSSES OVER indicator1)
    
    IF c1 AND timeEnterBefore THEN
    
    BUY positionsize contract AT MARKET
    SET STOP pLOSS 13
    
    ENDIF
    
    // Conditions to exit long positions
    
    indicator2 = SuperTrend[2.1,21]
    
    c2 = (close CROSSES UNDER indicator2)
    
    IF c2 THEN
    
    SELL AT MARKET
    
    ENDIF
    
    // Conditions to enter short positions
    
    indicator3 = SuperTrend[2.1,21]
    
    c3 = (close CROSSES UNDER indicator3)
    
    IF c3 AND timeEnterBefore THEN
    
    SELLSHORT positionsize contract AT MARKET
    SET STOP pLOSS 13
    
    ENDIF
    
    // Conditions to exit short positions
    
    indicator4 = SuperTrend[2.1,21]
    
    c4 = (close CROSSES OVER indicator4)
    
    IF c4 THEN
    
    EXITSHORT AT MARKET
    
    ENDIF
    
    //************************************************************************
    //trailing stop function
    trailingstart = 10 //trailing will start @trailinstart points profit
    trailingstep = 0 //trailing step to move the “stoploss”
    
    //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-tradeprice(1)>=trailingstart*pipsize THEN
    newSL = tradeprice(1)+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 tradeprice(1)-close>=trailingstart*pipsize THEN
    newSL = tradeprice(1)-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
    //************************************************************************
    
    GRAPH newSL as “trailing”
    #101018 quote
    Luce
    Participant
    New

    I’m sure it’s a simple add in but just can’t seem to work it out..!!

    #101021 quote
    robertogozzi
    Moderator
    Master

    To write code, please use the <> “insert PRT code” button, to make code easier to read.

    Thank you.

    #101022 quote
    robertogozzi
    Moderator
    Master

    I created another topic since this one has nothing to do with the original post, it’s a completely different code.

    Who’s the PAUL you are referring to?

    #101023 quote
    robertogozzi
    Moderator
    Master

    As to what you asked, you may search the forum, but I think all code snippets/examples deal with breakeven first, not really a true trailing stop!

    So what you want is to trail stop loss until breakeven, then leave it fixed till the next trade, is that correct?

    #101025 quote
    Vonasi
    Moderator
    Master

    Luce – you have posted an identical question here:

    Hard coded Stop Loss becoming Trailing Stop Loss

    One of the forum rules is that you do not double post as it leads to wasted effort and time for those trying to answer you.

    Please refrain from double posting in future.

    #101027 quote
    Luce
    Participant
    New

    Hi Roberto

    Yes basically – I have managed to code it so it trails to the 10 point target point which then becomes fixed, that part is now fine.

    What I’m trying to achieve however is whilst it is reaching the trailing stop target (10) it steps up by 2 each time until it reaches the target. So, you enter the trade, you have a trailing stop for 10 but it triggers immediately by a step of 2 if it moves in your favour by 2, then this becomes 8. If it moves another 2 then the target is now 6. It it moves another 2 the target is now 4 etc until it hits 10, and then it ALL stops and the stop is now fixed.

    Does that make sense..?

    #101029 quote
    robertogozzi
    Moderator
    Master

    This is the trailing stop updated so that it doesn’t move once at breakeven, you’ll have to add a further value, InitialSL which must be the same one used throughout the code (13 in your case):

    //************************************************************************
    //              trailing stop function (fixed after breakeven)
    //reset the stoploss value
    IF NOT ONMARKET THEN
       trailingstart = 6        //6   trailing will start @trailinstart points profit
       trailingstep  = 2        //2   trailing step to move the "stoploss"
       initialSL     = 13       //13  initial SL when a trade is entered
       newSL         = 0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
       CurrentProfit = close - tradeprice(1)
       //first move
       IF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THEN
          newSL     = tradeprice(1) + (trailingstep * pipsize) - (initialSL * pipsize)
          InitialSL = initialSL - (trailingstep * pipsize)
       ENDIF
       //next moves
       IF newSL > 0 AND (close - newSL) >= (trailingstep * pipsize) AND initialSL > 0 THEN
          newSL     = newSL + min(trailingstep * pipsize,initialSL * pipsize)
          initialSL = max(0,initialSL - (trailingstep * pipsize))
       ENDIF
    ENDIF
    
    //manage short positions
    IF SHORTONMARKET THEN
       CurrentProfit = tradeprice(1) - close
       //first move
       IF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THEN
          newSL     = tradeprice(1) - (trailingstep * pipsize) + (initialSL * pipsize)
          InitialSL = initialSL - (trailingstep * pipsize)
       ENDIF
       //next moves
       IF newSL > 0 AND (newSL - close) >= (trailingstep * pipsize) AND initialSL > 0 THEN
          newSL     = newSL - min(trailingstep * pipsize,initialSL * pipsize)
          initialSL = max(0,initialSL - (trailingstep * pipsize))
       ENDIF
    ENDIF
    
    //stop order to exit the positions
    IF newSL > 0 THEN
       SELL      AT newSL STOP
       EXITSHORT AT newSL STOP
    ENDIF

    InitialSL, your SL, is used as a counter and decremented each TRAILINGSTEP pips till it reaches 0, then it stops working (NewSL, the updated SL price still operates till it, or the target price, is hit).

    #101030 quote
    robertogozzi
    Moderator
    Master

    This your complete code:

    // Conditions to enter long positions
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    positionsize=2
    // Prevents the system from creating new orders to enter the market or increase position size before the specified time
    noEntryBeforeTime = 080000
    timeEnterBefore = time >= noEntryBeforeTime
    // Conditions to enter long positions
    indicator1 = SuperTrend[2.1,21]
    c1 = (close CROSSES OVER indicator1)
    IF c1 AND timeEnterBefore THEN
    BUY positionsize contract AT MARKET
    SET STOP pLOSS 13
    ENDIF
    // Conditions to exit long positions
    indicator2 = SuperTrend[2.1,21]
    c2 = (close CROSSES UNDER indicator2)
    IF c2 THEN
    SELL AT MARKET
    ENDIF
    // Conditions to enter short positions
    indicator3 = SuperTrend[2.1,21]
    c3 = (close CROSSES UNDER indicator3)
    IF c3 AND timeEnterBefore THEN
    SELLSHORT positionsize contract AT MARKET
    seT STOP pLOSS 13
     ENDIF
    // Conditions to exit short positions
    indicator4 = SuperTrend[2.1,21]
    c4 = (close CROSSES OVER indicator4)
    IF c4 THEN
    EXITSHORT AT MARKET
    ENDIF
    //************************************************************************
    //              trailing stop function (fixed after breakeven)
    //reset the stoploss value
    IF NOT ONMARKET THEN
    trailingstart = 6        //6   trailing will start @trailinstart points profit
    trailingstep  = 2        //2   trailing step to move the "stoploss"
    initialSL     = 13       //13  initial SL when a trade is entered
    newSL         = 0
    ENDIF
     
    //manage long positions
    IF LONGONMARKET THEN
    CurrentProfit = close - tradeprice(1)
    //first move
    IF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THEN
    newSL     = tradeprice(1) + (trailingstep * pipsize) - (initialSL * pipsize)
    InitialSL = initialSL - (trailingstep * pipsize)
    ENDIF
    //next moves
    IF newSL > 0 AND (close - newSL) >= (trailingstep * pipsize) AND initialSL > 0 THEN
    newSL     = newSL + min(trailingstep * pipsize,initialSL * pipsize)
    initialSL = max(0,initialSL - (trailingstep * pipsize))
    ENDIF
    ENDIF
    
    //manage short positions
    IF SHORTONMARKET THEN
    CurrentProfit = tradeprice(1) - close
    //first move
    IF newSL = 0 AND CurrentProfit >= (trailingstart * pipsize) THEN
    newSL     = tradeprice(1) - (trailingstep * pipsize) + (initialSL * pipsize)
    InitialSL = initialSL - (trailingstep * pipsize)
    ENDIF
    //next moves
    IF newSL > 0 AND (newSL - close) >= (trailingstep * pipsize) AND initialSL > 0 THEN
    newSL     = newSL - min(trailingstep * pipsize,initialSL * pipsize)
    initialSL = max(0,initialSL - (trailingstep * pipsize))
    ENDIF
    ENDIF
    
    //stop order to exit the positions
    IF newSL > 0 THEN
    SELL      AT newSL STOP
    EXITSHORT AT newSL STOP
    ENDIF
    //
    graph OnMarket
    graph Tradeprice(1)
    graph CurrentProfit
    graph trailingstart
    graph trailingstep
    graph initialSL
    graph newSL

    I tested it a bit and it seems to be working.

    Be warned that tight stops and targets may imply your orders being rejected or entered at market, due to the broker’s policy about minimum distance.

    Luce and sgilles33 thanked this post
    #101062 quote
    Luce
    Participant
    New

    Wow HUGE thanks Roberto – That’s amazing skills – Hugely appreciated!!

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

Breakeven & Trailing Stop (fixed)


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Luce @lucien_butler Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by Luce
6 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/19/2019
Status: Active
Attachments: No files
Logo Logo
Loading...