boucle 10 derniers trades

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #242201 quote
    Michel_I
    Participant
    Junior

    Bonjour à tous ! Je me heurte à une difficulté concernant les boucles for … do … next, que je découvre.

    Dans un backtest, je souhaite coder le % de trades gagnants au cours des 10 derniers trades.

    Pour cela , j’intègre dans mon code la partie ci dessous :

    // Compteur de positions gagantes / perdantes
    if LONGONMARKET[1] then
    gainTradeLong = (tradeprice – tradeprice(2))*2-1.40 // le*2-1.40 correspond au levier (2$ le point) et aux frais de transaction (1.40)
    ENDIF
    IF SHORTONMARKET[1] then
    gainTradeShort = (TRADEPRICE(2) – tradeprice) * 2 -1.40
    ENDIF

    // compte les gains cumulés :
    CumulGainTradesLong = CumulGainTradesLong + gainTradeLong

    CumulGainTradesShort = CumulGainTradesShort + gainTradeShort

    // Compte le nombre de trades gagnants par type de trade
    once totalTrades = 0          // je mets à 0 les variables
    once winningTradesLong = 0
    once winningTradesShort = 0

    // compte les trades longs gagnants
    if LONGONMARKET [1] and not LONGONMARKET and gainTradeLong > 0 then
    winningTradesLong = winningTradesLong + 1
    endif
    // compte les trades shorts gagnants
    if SHORTONMARKET [1] and not SHORTONMARKET and gainTradeShort > 0 then
    winningTradesShort = winningTradesShort + 1
    endif
    // compte le nombre total de trades
    If ONMARKET and NOT ONMARKET [1] then
    TotalTrades = TotalTrades + 1
    ENDIF
    // calcule le ratio de trades gagnants
    nbTradesGagnants = winningTradesLong + winningTradesShort
    ratioTradesGagnants =(nbTradesGagnants/totalTrades ) * 100 // donne le % de trades nbTradesGagnants
    // on peut aussi détailler le ratio trades longs ou shorts.

    // Tout le code ci dessus fonctionne bien, mais maintenant je souhaite limiter cela aux 10 derniers trades
    //tableau de résultat des trades
    // Résultat des 10 derniers trades
    Result10dernierstrades = 0
    for i = 0 to 10 DO
    Result10dernierstrades = Result10dernierstrades + nbTradesGagnants[i]
    NEXT

    // j’ai essayé cette boucle mais elle ne fonctionne pas. Je ne sais pas comment trouver ce résultat, pourtant simple a priori…

    Merci de votre aide précieuse !

    Michel

    #242213 quote
    Michel_I
    Participant
    Junior

    Peut être pour simplifier ma question : comment coder le taux de réussite des 10 derniers trades (qu’ils soient longs ou shorts) ? peu m’importe le montant du gain ou de la perte, je voudrais juste connaitre le % de trades gagnants sur les 10 derniers trades.

    Merci de votre aide !

    Michel

    #242231 quote
    robertogozzi
    Moderator
    Master

    Voilà:

    //initialization of the array
    IF BarIndex = 0 THEN
       FOR i = 1 TO 10
          $Trade[i] = 0
       NEXT
    ENDIF
    // tell a winning trade from a losing one and update the 10-element array, shifting the elements leftwards
    // one place, to make room for the new element (which is always element 1 of the array)
    IF StrategyProfit <> StrategyProfit[1] THEN
       // shift elements leftwards one place to make element 1 ready for the new result
       FOR i = 10 DOWNTO 2
          $Trade[i] = $Trade[i - 1]
       NEXT
       $Trade[1] = 1                               //assume it's a winning trade
       IF StrategyProfit < StrategyProfit[1] THEN  //check if it's a losing trade, instead
          $Trade[1] = -1
       ENDIF
       //tally winning trades to calculate the ratios
       Wins   = 0
       FOR i = 1 TO 10    //check the last 10 trades
          Wins   = Wins   + ($Trade[i] > 0)
       NEXT
       WinRatio  = Wins * 10         //calculate the % of winning trades
       LossRatio = 100 - WinRatio    //the % of losing trades is the difference between 100 and winning trades %
    ENDIF
    // Entry
    Sma20 = average[20,0](close)
    IF Not OnMarket THEN
       IF close CROSSES OVER  Sma20 THEN
          BUY 1 Contract at Market
       ELSIF close CROSSES UNDER Sma20 THEN
          SELLSHORT 1 Contract at Market
       ENDIF
    ENDIF
    SET STOP   pLOSS   100
    SET TARGET pPROFIT 200
    //
    graph WinRatio  coloured("Blue")  //% of winning trades
    graph LossRatio coloured("Red")   //% of losing trades
    Iván González thanked this post
    #242232 quote
    Michel_I
    Participant
    Junior

    Cela fonctionne parfaitement ! Un très grand merci !!!

    Michel

    robertogozzi thanked this post
    #242271 quote
    Michel_I
    Participant
    Junior

    J’ai cependant une question : le code retourne les bons pourcentages de trades gagnants/perdants, mais inversés ! …
    En effet quand je regarde le résultat, j’ai 7 trades gagnants sur les 10 derniers trades et le code retourne 7 trades perdants pour 3 gagnants.

    J’ai regardé votre code (qui me fait bien progresser dans les fonctions tableau) mais je ne vois pas où il y aurait une erreur.

    #242274 quote
    Michel_I
    Participant
    Junior

    Je crois avoir trouvé la solution en modifiant la ligne 9 comme cela  :

    IF NOT ONMARKET AND StrategyProfit <> StrategyProfit[1] THEN
    (en effet la fonction Strategyprofit se modifie dès qu’un trade est ouvert. En ajoutant le IF NOT ONMARKET, cette fonction ne prend effet qu’à la cloture du trade !)
    Le résultat est correct !
    Encore merci !
    Michel
    robertogozzi thanked this post
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

boucle 10 derniers trades


ProOrder : Trading Automatique & Backtests

New Reply
Author
author-avatar
Michel_I @michel_i Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by Michel_I
1 year, 1 month ago.

Topic Details
Forum: ProOrder : Trading Automatique & Backtests
Language: French
Started: 01/05/2025
Status: Active
Attachments: No files
Logo Logo
Loading...