Trailing Stop MFE avec TP partiel et stop ajusté

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #239275 quote
    bertrandpinoy
    Participant
    Veteran

    Bonsoir, je sollicite une aide pour parvenir a faire fonctionner une version du MFE avec clôture partielle et stop loss (positif) en cas de retracement après la prise du premier TP.

    • Premier TP à 50 pips : Nous devons fermer exactement 50% de la position à 50 pips de profit.
    • Niveau de stop pour la seconde moitié : Une fois le premier TP atteint, le nouveau stop loss doit être fixé à +10 pips par rapport au prix d’entrée initial, et non au niveau du premier TP ou au prix actuel.

    Jai développé cette version mais en cas de retracement après le premier TD j obtiens UN second TP à -25 pips.  Alors que je vis + 10…

    • // — Trailing Stop MFE avec TP partiel et stop ajusté —
      trailingstop = 120
      initialStopLoss = 100 * pipsize // Stop loss initial à 100 pips
      takeProfitTrigger = 50 * pipsize // Seuil pour le premier TP à 50 pips (qui donnera 25 pips puisque 0.5 contrat arreté)
      adjustedStopLossAfterTP = 10 * pipsize // Stop loss ajusté à +10 pips après le premier TP (qui donnera 5 pips puisque 0.5 contrat restant arreté)

      // Variables pour la gestion des positions
      isPartialProfitTaken = 0 // Indicateur pour savoir si le TP partiel a été pris
      newSL = 0 // Stop loss dynamique
      currentProfit = 0
      partialProfitExitPrice = 0 // Enregistrer le prix au moment du TP partiel
      initialTradePrice = 0 // Prix d’entrée initial
      positionClosedAtAdjustedStop = 0 // Indicateur de fermeture de la deuxième moitié

      // — Réinitialisation des variables si aucune position n’est ouverte —
      IF NOT ONMARKET THEN
      MAXPRICE = 0
      MINPRICE = close
      priceexit = 0
      isPartialProfitTaken = 0 // Réinitialisation du TP partiel
      initialTradePrice = 0 // Réinitialiser le prix d’entrée initial
      positionClosedAtAdjustedStop = 0
      ENDIF

      // — Gestion des positions SHORT —
      IF SHORTONMARKET THEN
      MINPRICE = MIN(MINPRICE, close) // Sauvegarder le plus bas (MFE) de la position

      // 1. Si le profit atteint 50 pips et que le TP partiel n’a pas été pris, fermer la moitié
      IF tradeprice(1) – MINPRICE >= takeProfitTrigger AND isPartialProfitTaken = 0 THEN
      SELL 0.5 CONTRACT AT MARKET // Fermer la moitié de la position
      isPartialProfitTaken = 1 // Marquer que le TP partiel a été pris
      initialTradePrice = tradeprice(1) // Enregistrer le prix d’entrée initial
      ENDIF

      // 2. Si le TP partiel a été pris, ajuster le stop loss pour la deuxième moitié à +10 pips du prix d’entrée initial
      IF isPartialProfitTaken = 1 AND positionClosedAtAdjustedStop = 0 THEN
      IF tradeprice(1) – close <= adjustedStopLossAfterTP THEN
      EXITSHORT AT MARKET // Fermer la position restante avec au moins +10 pips de profit
      positionClosedAtAdjustedStop = 1 // Marquer que la seconde moitié est fermée
      ENDIF
      ENDIF

      // 3. Activation du trailing stop une fois que le profit dépasse le trailingstop
      IF tradeprice(1) – MINPRICE >= trailingstop * pointsize THEN
      priceexit = MINPRICE + trailingstop * pointsize // Définir le prix de sortie (trailing stop)
      ENDIF
      ENDIF

      // — Gestion des positions LONG —
      IF LONGONMARKET THEN
      MAXPRICE = MAX(MAXPRICE, close) // Sauvegarder le plus haut (MFE) de la position

      // 1. Si le profit atteint 50 pips et que le TP partiel n’a pas été pris, fermer la moitié
      IF MAXPRICE – tradeprice(1) >= takeProfitTrigger AND isPartialProfitTaken = 0 THEN
      SELL 0.5 CONTRACT AT MARKET // Fermer la moitié de la position
      isPartialProfitTaken = 1 // Marquer que le TP partiel a été pris
      initialTradePrice = tradeprice(1) // Enregistrer le prix d’entrée initial
      ENDIF

      // 2. Si le TP partiel a été pris, ajuster le stop loss pour la deuxième moitié à +10 pips du prix d’entrée initial
      IF isPartialProfitTaken = 1 AND positionClosedAtAdjustedStop = 0 THEN
      IF close – tradeprice(1) <= adjustedStopLossAfterTP THEN
      SELL AT MARKET // Fermer la position restante avec au moins +10 pips de profit
      positionClosedAtAdjustedStop = 1 // Marquer que la seconde moitié est fermée
      ENDIF
      ENDIF

      // 3. Activation du trailing stop une fois que le profit dépasse le trailingstop
      IF MAXPRICE – tradeprice(1) >= trailingstop * pointsize THEN
      priceexit = MAXPRICE – trailingstop * pointsize // Définir le prix de sortie (trailing stop)
      ENDIF
      ENDIF

      // — Application du stop loss initial (100 pips) —
      IF LONGONMARKET THEN
      SELL AT (tradeprice(1) – initialStopLoss) STOP // Stop loss pour les positions longues
      ENDIF

      IF SHORTONMARKET THEN
      EXITSHORT AT (tradeprice(1) + initialStopLoss) STOP // Stop loss pour les positions courtes
      ENDIF

      // — Exit on trailing stop price levels —
      IF ONMARKET AND priceexit > 0 THEN
      EXITSHORT AT priceexit STOP
      SELL AT priceexit STOP
      ENDIF

    #239288 quote
    Iván González
    Moderator
    Master

    Hola, voici un exemple pour les longs :

    ema1=average[10,1](close)
    ema2=average[50,1](close)
    
    trailingstop = 120 * pipsize 
    initialStopLoss = 100 * pipsize
    takeProfitTrigger = 50 * pipsize
    adjustedStopLossAfterTP = 10 * pipsize
    
    num=2
    
    if not onmarket and ema1 crosses over ema2 then
    buy num contract at market
    set stop ploss initialStopLoss
    endif
    
    if onmarket then
    
    if onmarket[1]=0 then
    buyprice=tradeprice
    stopLong=buyprice-initialStopLoss
    tp1Long=buyprice+takeProfitTrigger
    endif
    
    maxprice=max(maxprice,close)
    
    if isPartialProfitTaken=0 then
    sell 0.5*COUNTOFLONGSHARES contract at tp1Long limit
    if high crosses over tp1Long then
    isPartialProfitTaken=1
    stopLong=buyprice+adjustedStopLossAfterTP
    set stop price stopLong
    endif
    endif
    
    if maxprice-buyprice>=trailingstop then
    trailLong=maxprice-trailingstop
    set stop ptrailing trailingstop
    endif
    else
    isPartialProfitTaken=0
    maxprice=close
    trailLong=close
    buyprice=close
    tp1Long=close
    stopLong=close
    endif
    
    graphonprice buyprice coloured("green")
    graphonprice stopLong coloured("red")
    graphonprice tp1Long coloured("blue")
    graphonprice trailLong coloured("orange")
    graphonprice maxprice coloured("black")
    #239306 quote
    bertrandpinoy
    Participant
    Veteran

    merci, mais la seconde cloture =-50pips

    #239336 quote
    robertogozzi
    Moderator
    Master

    TRADEPRICE, après la première sortie, prend la valeur du prix de sortie, et non plus le prix d’entrée d’origine.

    Pour cette raison je l’ai remplacé par ENTRYPRICE, qui est détecté à la clôture de la première bougie d’entrée.

    Essayez ceci:

    // — Trailing Stop MFE avec TP partiel et stop ajusté —
    trailingstop            = 120
    initialStopLoss         = 100 * pipsize // Stop loss initial à 100 pips
    takeProfitTrigger       = 50  * pipsize // Seuil pour le premier TP à 50 pips (qui
    //                                         donnera 25 pips puisque 0.5 contrat arreté)
    adjustedStopLossAfterTP = 10  * pipsize // Stop loss ajusté à +10 pips après le premier
    //                                         TP (qui donnera 5 pips puisque 0.5 contrat
    //                                         restant arreté)
    
    // Variables pour la gestion des positions
    isPartialProfitTaken         = 0 // Indicateur pour savoir si le TP partiel a été pris
    newSL                        = 0 // Stop loss dynamique
    currentProfit                = 0
    partialProfitExitPrice       = 0 // Enregistrer le prix au moment du TP partiel
    initialTradePrice            = 0 // Prix d’entrée initial
    positionClosedAtAdjustedStop = 0 // Indicateur de fermeture de la deuxième moitié
    
    // — Réinitialisation des variables si aucune position n’est ouverte —
    IF NOT ONMARKET THEN
    IF close crosses under average[100,0](close) then
    sellshort 1 Contract at market
    elsIF close crosses over average[100,0](close) then
    buy 1 Contract at market
    endif
    MAXPRICE = 0
    MINPRICE = close
    priceexit = 0
    isPartialProfitTaken = 0 // Réinitialisation du TP partiel
    initialTradePrice = 0 // Réinitialiser le prix d’entrée initial
    positionClosedAtAdjustedStop = 0
    ENDIF
    
    // — Gestion des positions SHORT —
    IF SHORTONMARKET THEN
    IF Not OnMarket[1] THEN
    EntryPrice = TradePrice
    ENDIF
    MINPRICE = MIN(MINPRICE, close) // Sauvegarder le plus bas (MFE) de la position
    
    // 1. Si le profit atteint 50 pips et que le TP partiel n’a pas été pris, fermer la moitié
    IF EntryPrice - MINPRICE >= takeProfitTrigger AND isPartialProfitTaken = 0 THEN
    EXITSHORT 0.5 CONTRACT AT MARKET // Fermer la moitié de la position
    isPartialProfitTaken = 1 // Marquer que le TP partiel a été pris
    initialTradePrice = EntryPrice - adjustedStopLossAfterTP// Enregistrer le prix d’entrée initial
    ENDIF
    
    // 2. Si le TP partiel a été pris, ajuster le stop loss pour la deuxième moitié à +10 pips du prix d’entrée initial
    IF isPartialProfitTaken = 1 AND positionClosedAtAdjustedStop = 0 THEN
    IF close <= initialTradePrice THEN
    EXITSHORT AT MARKET // Fermer la position restante avec au moins +10 pips de profit
    positionClosedAtAdjustedStop = 1 // Marquer que la seconde moitié est fermée
    ENDIF
    ENDIF
    
    // 3. Activation du trailing stop une fois que le profit dépasse le trailingstop
    IF EntryPrice - MINPRICE >= trailingstop * pointsize THEN
    priceexit = MINPRICE + trailingstop * pointsize // Définir le prix de sortie (trailing stop)
    ENDIF
    ENDIF
    
    // — Gestion des positions LONG —
    IF LONGONMARKET THEN
    IF Not OnMarket[1] THEN
    EntryPrice = TradePrice
    ENDIF
    MAXPRICE = MAX(MAXPRICE, close) // Sauvegarder le plus haut (MFE) de la position
    
    // 1. Si le profit atteint 50 pips et que le TP partiel n’a pas été pris, fermer la moitié
    IF MAXPRICE - EntryPrice >= takeProfitTrigger AND isPartialProfitTaken = 0 THEN
    SELL 0.5 CONTRACT AT MARKET // Fermer la moitié de la position
    isPartialProfitTaken = 1 // Marquer que le TP partiel a été pris
    initialTradePrice = EntryPrice + adjustedStopLossAfterTP// Enregistrer le prix d’entrée initial
    ENDIF
    
    // 2. Si le TP partiel a été pris, ajuster le stop loss pour la deuxième moitié à +10 pips du prix d’entrée initial
    IF isPartialProfitTaken = 1 AND positionClosedAtAdjustedStop = 0 THEN
    IF close <= initialTradePrice THEN
    SELL AT MARKET // Fermer la position restante avec au moins +10 pips de profit
    positionClosedAtAdjustedStop = 1 // Marquer que la seconde moitié est fermée
    ENDIF
    ENDIF
    
    // 3. Activation du trailing stop une fois que le profit dépasse le trailingstop
    IF MAXPRICE - EntryPrice >= trailingstop * pointsize THEN
    priceexit = MAXPRICE - trailingstop * pointsize // Définir le prix de sortie (trailing stop)
    ENDIF
    ENDIF
    
    // — Application du stop loss initial (100 pips) —
    IF LONGONMARKET THEN
    SELL AT (EntryPrice - initialStopLoss) STOP // Stop loss pour les positions longues
    ENDIF
    
    IF SHORTONMARKET THEN
    EXITSHORT AT (EntryPrice + initialStopLoss) STOP // Stop loss pour les positions courtes
    ENDIF
    
    // — Exit on trailing stop price levels —
    IF ONMARKET AND priceexit > 0 THEN
    EXITSHORT AT priceexit STOP
    //SELL      AT priceexit STOP
    ENDIF
    //
    graphonprice TradePrice
    graphonprice priceexit coloured("Green")
    graph PositionPerf * PositionPrice / pipsize
    #239347 quote
    bertrandpinoy
    Participant
    Veteran

    merci Roberto mais ce n est pas tout a fait ça encore.

    La version que je poste ici est très proche de ce que je veux. Le seul problème est qu’après le premier TP partiel, si le prix redescend, j ai un second TP à – 25 pips. Et ceci précisément à 100 pips en dessous du niveau du premier TP. Je sèche.

    trailingstop = 183
    initialStopLoss = 100 * pipsize // Stop loss à 100 pips
    takeProfitTrigger = 50 * pipsize // Seuil pour le premier TP à 50 pips
    adjustedStopLossAfterTP = 10 * pipsize // Si le profit retombe à +10 pips, on coupe la position

    // Variables pour la gestion des positions
    isPartialProfitTaken = 0 // Indicateur pour savoir si le TP partiel a été pris
    currentProfit = 0
    partialProfitExitPrice = 0 // Enregistrer le prix au moment du TP partiel

    // Reset des variables quand aucune position n’est ouverte
    IF NOT ONMARKET THEN
    MAXPRICE = 0
    MINPRICE = close
    isPartialProfitTaken = 0 // Réinitialisation du TP partiel
    ENDIF

    // —- GESTION DES POSITIONS LONG —-
    IF LONGONMARKET THEN
    currentProfit = close – tradeprice(1)
    MAXPRICE = MAX(MAXPRICE, close) // Sauvegarder le plus haut (MFE) de la position

    // —- Première partie : Gestion du TP partiel —-
    IF MAXPRICE – tradeprice(1) >= takeProfitTrigger AND isPartialProfitTaken = 0 THEN
    SELL 0.5 CONTRACT AT MARKET // Fermer la moitié de la position à +50 pips
    isPartialProfitTaken = 1 // Marquer que le TP partiel a été pris
    partialProfitExitPrice = tradeprice(1) + adjustedStopLossAfterTP // Fixer le nouveau stop à +10 pips par rapport à l’entrée initiale
    ENDIF

    // —- Deuxième partie : Gestion du retracement —-
    IF isPartialProfitTaken = 1 THEN
    // Si le prix descend à +10 pips ou moins après la prise partielle, on coupe la position restante
    IF close <= partialProfitExitPrice THEN
    SELL AT MARKET // Fermer la position restante
    ENDIF
    ENDIF
    ENDIF

    // —- Application du Stop Loss initial —-
    IF LONGONMARKET THEN
    SELL AT (tradeprice(1) – initialStopLoss) STOP // Stop loss pour les positions longues
    ENDIF

    #239360 quote
    robertogozzi
    Moderator
    Master

    Où est écrit -25 ?

    #239363 quote
    bertrandpinoy
    Participant
    Veteran

    Nul part. C’est lors de l’exécution du BT. Bizarre.

    #244411 quote
    Madrosat
    Participant
    Master

    Bonjour Ivan

    Dans une simple stratégie comment  si le MFE est égal ou supérieur à 30 pips ajouter une sortie au breakeven au cas où les prix baisseraient

    j’ai déjà dans la stratégie une sortie en cas de perte : set stop price (indicator3)

    EN résumé substituer le breakeven  à l’autre sortie ( set stop price (indicator3) si mfe =>30 est ce faisable

    Cordialement

    Michel

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

Trailing Stop MFE avec TP partiel et stop ajusté


ProOrder : Trading Automatique & Backtests

New Reply
Author
Summary

This topic contains 7 replies,
has 4 voices, and was last updated by Madrosat
1 year ago.

Topic Details
Forum: ProOrder : Trading Automatique & Backtests
Language: French
Started: 10/20/2024
Status: Active
Attachments: No files
Logo Logo
Loading...