Trading Systématique 게시글

Fermez vos Ordres par Objectif ou Limite de Perte - Un Outil Indispensable pour MetaTrader 4

첨부파일
34194.zip (995 bytes, 다운로드 0회)
Nous utilisons cet Expert Advisor (EA) comme un outil de trading efficace.

Voici les 3 paramètres à définir :

  • Objectif de Profit
  • Limite de Perte
  • Numéro Magique
extern    double         inTargetProfitMoney     = 10;       //Objectif de Profit (€)
extern    double         inCutLossMoney          = 0.0      //Limite de Perte (€)
extern    int            inMagicNumber           = 0        //Numéro Magique


Lorsque cet EA est exécuté, il appellera d'abord la fonction OnInit(). C'est ici que nous vérifions les paramètres et l'initialisation des variables.

int OnInit()
  {
//---
   if(inTargetProfitMoney <= 0)
     {
      Alert("Entrée invalide");
      return(INIT_PARAMETERS_INCORRECT);
     }

   inCutLossMoney = MathAbs(inCutLossMoney) * -1;

//---
   return(INIT_SUCCEEDED);
  }


À chaque mouvement de prix (tick), la fonction OnTick() sera appelée.

void OnTick()
  {
//---

   double   tFloating = 0.0;
   int tOrder  = OrdersTotal();
   for(int i=tOrder-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderMagicNumber() == inMagicNumber)
           {
            tFloating   += OrderProfit()+OrderCommission() + OrderSwap();
           }
        }
     }

   if(tFloating >= inTargetProfitMoney || (tFloating <= inCutLossMoney && inCutLossMoney < 0))
     {
      fCloseAllOrders();
     }

  }

Dans la fonction OnTick(), nous continuons à calculer le profit ou la perte totale. Ensuite, nous fermerons tous les ordres qui atteignent l'objectif ou la limite de perte maximale.

void fCloseAllOrders()
  {
   double   priceClose = 0.0;
   int tOrders = OrdersTotal();
   for(int i=tOrders-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderMagicNumber() == inMagicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL))
           {
            priceClose  = (OrderType()==OP_BUY)?MarketInfo(OrderSymbol(), MODE_BID):MarketInfo(OrderSymbol(), MODE_ASK);
            if(!OrderClose(OrderTicket(), OrderLots(), priceClose, slippage, clrGold))
              {
              Print("AVERTISSEMENT : Échec de la fermeture");
              }
           }
        }
     }
  }


Pour plus d'informations détaillées et de partage de code mql4, rejoignez notre groupe Telegram t.me/codeMQL




연관 포스트

댓글 (0)