Problème sur Stop Suiveur

 

Hello la commu !


J'ai codé un bot en mql5 qui trade sur de la correlation de 3 paires.


Les trades s'ouvrent, le SL est bien en place mais mon suiveur ne fonctionne pas...


Des pistes ?


Merci !

Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
  • 2023.04.06
  • www.mql5.com
MQL5: linguagem de estratégias de negociação inseridas no Terminal do Cliente MetaTrader 5. A linguagem permite escrever seus próprios sistemas automáticos de negócios, indicadores técnicos, scripts e bibliotecas de funções
 

Bonjour Alain,


Avec mes excuses.

Le voici comme demandé :

//+------------------------------------------------------------------+
//|                                                    EMACorrel.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade trade;

input string Pair1 = "EURJPY";
input string Pair2 = "USDJPY";
input string Pair3 = "GBPJPY";

input int emaPeriod1 = 10;
input int emaPeriod2 = 20;
input double SLPercentage = 0.5;       //Taille du SL en pourcent
input double updateTrailingSL = 0.2;   //Distance minimum pour mettre à jour le trailing Stop en pourcent

int emaHandle1, emaHandle2, emaHandle3, emaHandle4, emaHandle5, emaHandle6;
double emaBuffer1[], emaBuffer2[], emaBuffer3[], emaBuffer4[], emaBuffer5[], emaBuffer6[];

double Ask, Bid, SL, TP;
string lastTradeType = "";
bool canLong, canShort;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   emaHandle1 = iMA(Pair1, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle2 = iMA(Pair1, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle3 = iMA(Pair2, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle4 = iMA(Pair2, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle5 = iMA(Pair3, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle6 = iMA(Pair3, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
      
   ArraySetAsSeries(emaBuffer1, true);
   ArraySetAsSeries(emaBuffer2, true);
   ArraySetAsSeries(emaBuffer3, true);
   ArraySetAsSeries(emaBuffer4, true);
   ArraySetAsSeries(emaBuffer5, true);
   ArraySetAsSeries(emaBuffer6, true);

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CopyBuffer(emaHandle1, 0, 0, 1, emaBuffer1);
   CopyBuffer(emaHandle2, 0, 0, 1, emaBuffer2);
   CopyBuffer(emaHandle3, 0, 0, 1, emaBuffer3);
   CopyBuffer(emaHandle4, 0, 0, 1, emaBuffer4);
   CopyBuffer(emaHandle5, 0, 0, 1, emaBuffer5);
   CopyBuffer(emaHandle6, 0, 0, 1, emaBuffer6);

   
   Ask = SymbolInfoDouble(Pair1, SYMBOL_ASK);
   Bid = SymbolInfoDouble(Pair1, SYMBOL_BID);
   
   
   //Symbol Pair1
   if(emaBuffer1[0] > emaBuffer2[0]
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer1[0] < emaBuffer2[0]
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
   //Symbol Pair2
   if(emaBuffer3[0] > emaBuffer4[0]
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer3[0] < emaBuffer4[0]
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
   //Symbol Pair3
   if(emaBuffer5[0] > emaBuffer6[0]
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer5[0] < emaBuffer6[0]
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
  
   // Conditions
   if(canLong && emaBuffer1[0] > emaBuffer2[0] )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, _Symbol, Ask, SL, TP);
      lastTradeType = "Long";
   }
   if(canLong && emaBuffer3[0] > emaBuffer4[0]  )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, _Symbol, Ask, SL, TP);
      lastTradeType = "Long";
   }
   if(canLong && emaBuffer5[0] > emaBuffer6[0] )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, Pair1, Ask, SL, TP);
      lastTradeType = "Long";
   }
   
   if(canShort && emaBuffer1[0] < emaBuffer2[0] )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short";
   }
   if(canShort &&  emaBuffer3[0] < emaBuffer4[0]  )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short";
   }
   if(canShort && emaBuffer5[0] < emaBuffer6[0] )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short";
   }
    if(PositionsTotal () == 1)
   {
      if(lastTradeType == "Long" && Ask - SL > SLPercentage / 100 * Ask + updateTrailingSL / 100 * Ask)
      
      {
       SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
       trade.PositionModify(_Symbol, SL, 0);
      }
   }
   
   if(PositionsTotal () == 1)
   {
      if(lastTradeType == "Short" && SL - Bid > SLPercentage / 100 * Bid + updateTrailingSL / 100 * Bid)
      
      {
       SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
       trade.PositionModify(_Symbol, SL, 0);
      }
   }
  }
//+------------------------------------------------------------------+
 
phenix33 #:

Bonjour Alain,


Avec mes excuses.

Le voici comme demandé :

J'avais demandé de mofifier (éditer) le message initial, mais bon ce n'est pas grave.

Pour identifier le problème :

  • Utiliser le "debugger" (Comment on dit ça en français ? mdr).
  • Utiliser Print() pour vérifier les valeurs de vos variables.
 

Bonjour Alain,


Oui je n'avais pas vu que le bouton éditer était dans un petit coin à droite...

Debugger, tu veux dire, compiler ?

Merci pour les infos.

 

J'ai compilé et je n'ai aucune erreur sur ce code :

//+------------------------------------------------------------------+
//|                                                    EMACorrel.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade trade;

input string Pair1 = "EURJPY";
input string Pair2 = "USDJPY";
input string Pair3 = "GBPJPY";

input int emaPeriod1 = 10;
input int emaPeriod2 = 20;
input double SLPercentage = 0.5;       //Taille du SL en pourcent
input double updateTrailingSL = 0.2;   //Distance minimum pour mettre à jour le trailing Stop en pourcent

int emaHandle1, emaHandle2, emaHandle3, emaHandle4, emaHandle5, emaHandle6;
double emaBuffer1[], emaBuffer2[], emaBuffer3[], emaBuffer4[], emaBuffer5[], emaBuffer6[];

double Ask, Bid, SL, TP;
string lastTradeType = "";
bool canLong, canShort;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   emaHandle1 = iMA(Pair1, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle2 = iMA(Pair1, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle3 = iMA(Pair2, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle4 = iMA(Pair2, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle5 = iMA(Pair3, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle6 = iMA(Pair3, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
      
   ArraySetAsSeries(emaBuffer1, true);
   ArraySetAsSeries(emaBuffer2, true);
   ArraySetAsSeries(emaBuffer3, true);
   ArraySetAsSeries(emaBuffer4, true);
   ArraySetAsSeries(emaBuffer5, true);
   ArraySetAsSeries(emaBuffer6, true);

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CopyBuffer(emaHandle1, 0, 0, 1, emaBuffer1);
   CopyBuffer(emaHandle2, 0, 0, 1, emaBuffer2);
   CopyBuffer(emaHandle3, 0, 0, 1, emaBuffer3);
   CopyBuffer(emaHandle4, 0, 0, 1, emaBuffer4);
   CopyBuffer(emaHandle5, 0, 0, 1, emaBuffer5);
   CopyBuffer(emaHandle6, 0, 0, 1, emaBuffer6);

   
   Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); 
   
   
   //Symbol Pair1
   if(emaBuffer1[0] > emaBuffer2[0] 
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer1[0] < emaBuffer2[0] 
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
   //Symbol Pair2
   if(emaBuffer3[0] > emaBuffer4[0] 
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer3[0] < emaBuffer4[0] 
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
   //Symbol Pair3
   if(emaBuffer5[0] > emaBuffer6[0] 
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer5[0] < emaBuffer6[0] 
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
   
   // Conditions
   if(canLong && emaBuffer1[0] > emaBuffer2[0] )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, _Symbol, Ask, SL, TP);
      lastTradeType = "Long"; 
   }
   else if(canLong && emaBuffer3[0] > emaBuffer4[0]  )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, _Symbol, Ask, SL, TP);
      lastTradeType = "Long"; 
   }
   else if(canLong && emaBuffer5[0] > emaBuffer6[0] )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, _Symbol, Ask, SL, TP);
      lastTradeType = "Long"; 
   }
   
   else if(canShort && emaBuffer1[0] < emaBuffer2[0] )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short"; 
   }
   else if(canShort &&  emaBuffer3[0] < emaBuffer4[0]  )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short"; 
   }
   else if(canShort && emaBuffer5[0] < emaBuffer6[0] )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short"; 
   }
    if(PositionsTotal () == 1)
   {
      if(lastTradeType == "Long" && Ask - SL > SLPercentage / 100 * Ask + updateTrailingSL / 100 * Ask)
      
      {
       SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
       trade.PositionModify(_Symbol, SL, 0);
      }
   }
   
   if(PositionsTotal () == 1)
   {
      if(lastTradeType == "Short" && SL - Bid > SLPercentage / 100 * Bid + updateTrailingSL / 100 * Bid)
      
      {
       SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
       trade.PositionModify(_Symbol, SL, 0);
      }
   }
  }
//+------------------------------------------------------------------+

Je ne comprend vraiment pas pourquoi le SL suiveur ne s'active pas.

J'avais codé avec juste une paire et ce code fonctionne avec le SL suiveur pourtant....

//+------------------------------------------------------------------+
//|                                                    EMADouble.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade trade;

input int emaPeriod1 = 9;
input int emaPeriod2 = 20;
input double SLPercentage = 0.5;       //Taille du SL en pourcent
input double updateTrailingSL = 0.1;   //Distance minimum pour mettre à jour le trailing Stop en pourcent

int emaHandle1, emaHandle2;
double emaBuffer1[], emaBuffer2[];

double Ask, Bid, SL, TP;
string lastTradeType = "";
bool canLong, canShort;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   emaHandle1 = iMA(_Symbol, _Period, emaPeriod1, 0, MODE_EMA, PRICE_CLOSE);
   emaHandle2 = iMA(_Symbol, _Period, emaPeriod2, 0, MODE_EMA, PRICE_CLOSE);
      
   ArraySetAsSeries(emaBuffer1, true);
   ArraySetAsSeries(emaBuffer2, true);

   
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CopyBuffer(emaHandle1, 0, 0, 1, emaBuffer1);
   CopyBuffer(emaHandle2, 0, 0, 1, emaBuffer2);

   
   Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); 
   
   if(emaBuffer1[0] > emaBuffer2[0] 
       && PositionsTotal () == 0)
   {
         canLong = true;
   }
   else
   {
         canLong = false;
   }
   
   if(emaBuffer1[0] < emaBuffer2[0] 
    && PositionsTotal () == 0)
   {
         canShort = true;
   }
   else
   {
         canShort = false;
   }
   
   if(canLong && emaBuffer1[0] > emaBuffer2[0] )
   {
      SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
      TP = 0;
      trade.Buy(1.0, _Symbol, Ask, SL, TP);
      lastTradeType = "Long"; 
   }
   
   if(canShort && emaBuffer1[0] < emaBuffer2[0] )
   {
      SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
      TP = 0;
      trade.Sell(1.0, _Symbol, Bid, SL, TP);
      lastTradeType = "Short"; 
   }
   
   if(PositionsTotal () == 1)
   {
      if(lastTradeType == "Long" && Ask - SL > SLPercentage / 100 * Ask + updateTrailingSL / 100 * Ask)
      
      {
       SL = NormalizeDouble(Ask - SLPercentage / 100 * Ask, _Digits);
       trade.PositionModify(_Symbol, SL, 0);
      }
   }
   
   if(PositionsTotal () == 1)
   {
      if(lastTradeType == "Short" && SL - Bid > SLPercentage / 100 * Bid + updateTrailingSL / 100 * Bid)
      
      {
       SL = NormalizeDouble(Bid + SLPercentage / 100 * Bid, _Digits);
       trade.PositionModify(_Symbol, SL, 0);
      }
   }
  }
//+------------------------------------------------------------------+

Je sèche complet :/

 
phenix33 #:

Bonjour Alain,


Oui je n'avais pas vu que le bouton éditer était dans un petit coin à droite...

Debugger, tu veux dire, compiler ?

Merci pour les infos.

Je veux dire déboguer : https://www.metatrader5.com/fr/metaeditor/help/development/debug
Déboguage du code - Développer des programmes - Aide MetaEditor
  • www.metatrader5.com
MetaEditor a un débogueur intégré vous permettant de vérifier l'exécution d'un programme pas à pas (par...
Raison: