Trailing stop – aggiunta steps breakeven + steps incremento

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #207881 quote
    Simon
    Participant
    New

    Buongiorno Roberto,

    sarebbe possibile aggiungere al tuo codice di trailing stop un primo step che porta a breakeven  (es. al raggiungimento  di 30 punti di guadagno = sl a breakeven +1 punto, lasciando la variabile  PointsToKeep )

    successivamente solo al raggiungimento step2 (es. + 60 punti) inizia il trailing stop  con step di n. punti  (es.  20 punti)

    successivamente al raggiungimento step3 (es. + 120 punti) modifica il trailing stop  con step di n. punti  (es.  10 punti)

    Ti ringrazio anticipatamente per l’aiuto e per il grande lavoro per il forum.

     

    //trailing stop function

    IF NOT ONMARKET THEN

    TrailingStart = 20  //20 trailing will start @trailinstart points profit

    TrailingStep  = 5   //5  trailing step to move the “stoploss”

    Distance      = 7   //7  pips Distance from caurrent price (if required by the broker)

    PointsToKeep  = 1   //1  pips to be gained when breakeven is set

    //reset the stoploss value

    newSL=0

    ENDIF

    IF (BarIndex – TradeIndex) >= 0 THEN                                 //0

    //manage long positions

    IF LONGONMARKET THEN

    //first move (breakeven)

    IF newSL=0 AND high-TradePrice(1)>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN

    newSL = TradePrice(1)+TrailingStep*PipSize+PointsToKeep*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)-low>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN

    newSL = TradePrice(1)-TrailingStep*PipSize+PointsToKeep*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

    IF LongOnMarket THEN

    IF (close + Distance) > newSL THEN

    SELL AT newSL STOP

    ELSIF (close – Distance) < newSL THEN

    SELL AT newSL LIMIT

    ELSE

    SELL AT Market

    ENDIF

    ELSIF ShortOnmarket THEN

    IF (close + Distance) < newSL THEN

    EXITSHORT AT newSL STOP

    ELSIF (close – Distance) > newSL THEN

    EXITSHORT AT newSL LIMIT

    ELSE

    EXITSHORT AT Market

    ENDIF

    ENDIF

    ENDIF

    ENDIF

    //**************************************

    #208065 quote
    robertogozzi
    Moderator
    Master

    Eccolo (non l’ho provato):

    //trailing stop function
    IF NOT ONMARKET THEN
       TrailingStart = 20  //20 trailing will start @trailinstart points profit
       TrailingStep  = 5   //5  trailing step to move the "stoploss"
       Distance      = 7   //7  pips Distance from caurrent price (if required by the broker)
       PointsToKeep  = 1   //1  pips to be gained when breakeven is set
       newSL         = 0   //reset the stoploss value
       Step2         = 0
       Step3         = 0
    ELSE
       Pips          = PositionPerf * PositionPrice / PipSize
       IF (Pips >= 60) AND (Step2 = 0) THEN  //15 punti di step dopo 60 di profitto
          TrailingStep = 15
          Step2        = 1
       ENDIF
       IF (Pips >= 120) AND (Step3 = 0) THEN //30 punti di step dopo 120 di profitto
          TrailingStep = 30
          Step3        = 1
       ENDIF
    ENDIF
    IF (BarIndex - TradeIndex) >= 0 THEN                                 //0
       //manage long positions
       IF LONGONMARKET THEN
          //first move (breakeven)
          IF newSL=0 AND high-TradePrice(1)>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN
             newSL = TradePrice(1)+TrailingStep*PipSize+PointsToKeep*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)-low>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN
             newSL = TradePrice(1)-TrailingStep*PipSize+PointsToKeep*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
          IF LongOnMarket THEN
             IF (close + Distance) > newSL THEN
                SELL AT newSL STOP
             ELSIF (close - Distance) < newSL THEN
                SELL AT newSL LIMIT
             ELSE
                SELL AT Market
             ENDIF
          ELSIF ShortOnmarket THEN
             IF (close + Distance) < newSL THEN
                EXITSHORT AT newSL STOP
             ELSIF (close - Distance) > newSL THEN
                EXITSHORT AT newSL LIMIT
             ELSE
                EXITSHORT AT Market
             ENDIF
          ENDIF
       ENDIF
    ENDIF
    //**************************************
    #208180 quote
    Simon
    Participant
    New
    IF TS<>0 THEN
    //trailing stop function
    
    IF NOT ONMARKET THEN
    TrailingStart = TS  //20 trailing will start @trailinstart points profit
    TrailingStep  = 5   //5  trailing step to move the "stoploss"
    Distance      = 50   //7  pips Distance from caurrent price (if required by the broker)
    PointsToKeep  = 1   //1  pips to be gained when breakeven is set
    newSL         = 0   //reset the stoploss value
    Step2         = stp2  //60
    Encr2         = Enc2  //15
    Step3         = Stp3  //90
    Encr3         = Enc3  //10
    ELSE
    Pips          = PositionPerf * PositionPrice / PipSize
    IF (Pips >= Step2)  THEN  //15 punti di step dopo 60 di profitto
    TrailingStep = Encr2
    ENDIF
    IF (Pips >= step3) THEN //30 punti di step dopo 120 di profitto
    TrailingStep = Encr3
    ENDIF
    ENDIF
    IF (BarIndex - TradeIndex) >= 0 THEN                                 //0
    //manage long positions
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND high-TradePrice(1)>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN
    newSL = TradePrice(1)+TrailingStep*PipSize+PointsToKeep*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)-low>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN
    newSL = TradePrice(1)-TrailingStep*PipSize+PointsToKeep*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
    IF LongOnMarket THEN
    IF (close + Distance) > newSL THEN
    SELL AT newSL STOP
    ELSIF (close - Distance) < newSL THEN
    SELL AT newSL LIMIT
    ELSE
    SELL AT Market
    ENDIF
    ELSIF ShortOnmarket THEN
    IF (close + Distance) < newSL THEN
    EXITSHORT AT newSL STOP
    ELSIF (close - Distance) > newSL THEN
    EXITSHORT AT newSL LIMIT
    ELSE
    EXITSHORT AT Market
    ENDIF
    ENDIF
    ENDIF
    ENDIF
    //**************************************
    ENDIF
    

    Grazie Roberto,

    gentilissimo!

    Allego codice modificato con variabili, lo sto testando.

    Buona giornata

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

Trailing stop – aggiunta steps breakeven + steps incremento


ProOrder: Trading Automatico & Backtesting

New Reply
Author
author-avatar
Simon @simontemp Participant
Summary

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

Topic Details
Forum: ProOrder: Trading Automatico & Backtesting
Language: Italian
Started: 01/22/2023
Status: Active
Attachments: No files
Logo Logo
Loading...