boucle 10 derniers trades
Forums › ProRealTime forum Français › Support ProOrder › boucle 10 derniers trades
- This topic has 5 replies, 2 voices, and was last updated 9 months ago by Michel_I. 
- 
	
		
- 
		
			
				
01/05/2025 at 4:18 PM #242201Bonjour à 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 + gainTradeLongCumulGainTradesShort = 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 01/06/2025 at 10:57 AM #242213Peut ê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 01/06/2025 at 5:56 PM #242231Voilà: 123456789101112131415161718192021222324252627282930313233343536373839//initialization of the arrayIF BarIndex = 0 THENFOR i = 1 TO 10$Trade[i] = 0NEXTENDIF// 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 resultFOR i = 10 DOWNTO 2$Trade[i] = $Trade[i - 1]NEXT$Trade[1] = 1 //assume it's a winning tradeIF StrategyProfit < StrategyProfit[1] THEN //check if it's a losing trade, instead$Trade[1] = -1ENDIF//tally winning trades to calculate the ratiosWins = 0FOR i = 1 TO 10 //check the last 10 tradesWins = Wins + ($Trade[i] > 0)NEXTWinRatio = Wins * 10 //calculate the % of winning tradesLossRatio = 100 - WinRatio //the % of losing trades is the difference between 100 and winning trades %ENDIF// EntrySma20 = average[20,0](close)IF Not OnMarket THENIF close CROSSES OVER Sma20 THENBUY 1 Contract at MarketELSIF close CROSSES UNDER Sma20 THENSELLSHORT 1 Contract at MarketENDIFENDIFSET STOP pLOSS 100SET TARGET pPROFIT 200//graph WinRatio coloured("Blue") //% of winning tradesgraph LossRatio coloured("Red") //% of losing trades1 user thanked author for this post.01/06/2025 at 6:16 PM #242232Cela fonctionne parfaitement ! Un très grand merci !!! Michel 1 user thanked author for this post.01/07/2025 at 5:31 PM #242271J’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. 01/07/2025 at 7:12 PM #242274Je 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 !Michel1 user thanked author for this post.
- 
		AuthorPosts
			Find exclusive trading pro-tools on 

 
 
 
		 
		 
		