mon programme fonctionne en backtest mais pas en réel live Nouveau commentaire Sébastien Debiais 2026.08.01 21:37 //+------------------------------------------------------------------+ //| pyramiding3.mq5 | //| Copyright 2026, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2026, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #include <Trade/Trade.mqh> CTrade trade; //---------------------- PARAMETRES --------------------------------- double LotSize = 0.01; int StepPoints = 30; ulong MagicNumber = 123456; //---------------------- VARIABLES ---------------------------------- double LastTickPrice = 0; double LastBuyPrice = 0; double LastSellPrice = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { trade.SetExpertMagicNumber(MagicNumber); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Vérifie s'il existe un achat | //+------------------------------------------------------------------+ bool HasBuyPosition() { for(int i=0;i<PositionsTotal();i++) { ulong ticket=PositionGetTicket(i); if(PositionSelectByTicket(ticket)) { if(PositionGetString(POSITION_SYMBOL)==_Symbol && PositionGetInteger(POSITION_MAGIC)==MagicNumber && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) return(true); } } return(false); } //+------------------------------------------------------------------+ //| Vérifie s'il existe une vente | //+------------------------------------------------------------------+ bool HasSellPosition() { for(int i=0;i<PositionsTotal();i++) { ulong ticket=PositionGetTicket(i); if(PositionSelectByTicket(ticket)) { if(PositionGetString(POSITION_SYMBOL)==_Symbol && PositionGetInteger(POSITION_MAGIC)==MagicNumber && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) return(true); } } return(false); } //+------------------------------------------------------------------+ //| Profit total des positions du robot | //+------------------------------------------------------------------+ double TotalProfit() { double profit = 0.0; for(int i=0;i<PositionsTotal();i++) { ulong ticket = PositionGetTicket(i); if(PositionSelectByTicket(ticket)) { if(PositionGetString(POSITION_SYMBOL)==_Symbol && PositionGetInteger(POSITION_MAGIC)==MagicNumber) { profit += PositionGetDouble(POSITION_PROFIT); } } } return profit; } //+------------------------------------------------------------------+ //| Ferme toutes les positions du robot | //+------------------------------------------------------------------+ void CloseAllPositions() { for(int i=PositionsTotal()-1;i>=0;i--) { ulong ticket=PositionGetTicket(i); if(PositionSelectByTicket(ticket)) { if(PositionGetString(POSITION_SYMBOL)==_Symbol && PositionGetInteger(POSITION_MAGIC)==MagicNumber) { trade.PositionClose(ticket); } } } LastBuyPrice=0; LastSellPrice=0; } void TrailingStopAll() { for(int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(!PositionSelectByTicket(ticket)) continue; if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue; //if(PositionGetInteger(POSITION_MAGIC) != MagicNumber) // continue; double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); // BUY if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double newSL = NormalizeDouble(bid - 30 * _Point, _Digits); if(sl==0 || newSL>sl) trade.PositionModify(ticket, newSL, tp); } // SELL if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double newSL = NormalizeDouble(ask + 30 * _Point, _Digits); if(sl==0 || newSL<sl) trade.PositionModify(ticket, newSL, tp); } } } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- TrailingStopAll(); double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID); double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK); if(LastTickPrice==0) { LastTickPrice=bid; return; } //------------------- TENDANCE HAUSSIERE ------------------------- if(bid>LastTickPrice) { // Ferme les ventes si la tendance devient haussière if(HasSellPosition()) { //CloseAllPositions(); LastTickPrice=bid; return; } // Premier achat if(!HasBuyPosition()) { double lot = 0.01; double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double sl = NormalizeDouble(ask - 80 * _Point, _Digits); // Stop à 100 points if(trade.Buy(lot, _Symbol, ask, sl, 0)) LastBuyPrice=ask; } // Achat supplémentaire else { if(ask >= LastBuyPrice + StepPoints*_Point) { double lot = 0.01; double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double sl = NormalizeDouble(ask - 80 * _Point, _Digits); // Stop à 100 points if(trade.Buy(lot, _Symbol, ask, sl, 0)) LastBuyPrice=ask; } } } //------------------- TENDANCE BAISSIERE ------------------------- else if(bid<LastTickPrice) { // Ferme les achats si la tendance devient baissière if(HasBuyPosition()) { //CloseAllPositions(); LastTickPrice=bid; return; } // Première vente if(!HasSellPosition()) { double lot = 0.01; double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double sl = NormalizeDouble(bid + 80 * _Point, _Digits); // Stop à 60 points au-dessus if(trade.Sell(lot, _Symbol, bid, sl, 0)) LastSellPrice = bid; } // Vente supplémentaire else { if(bid <= LastSellPrice - StepPoints*_Point) { double lot = 0.01; double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double sl = NormalizeDouble(bid + 80 * _Point, _Digits); // Stop à 60 points au-dessus if(trade.Sell(lot, _Symbol, bid, sl, 0)) LastSellPrice = bid; } } } LastTickPrice=bid; } //+------------------------------------------------------------------+ Pouvez-vous m'aider à comprendre d'où vient l'erreur, pourquoi le programme fonctionne en backtest mais pas en réel live ? Nouveau commentaire
Pouvez-vous m'aider à comprendre d'où vient l'erreur, pourquoi le programme fonctionne en backtest mais pas en réel live ?