EAs no mesmo ativo

 
Eu posso ter dois diferentes EAs no mesmo ativo? Obrigado.
 
hulemos01Eu posso ter dois diferentes EAs no mesmo ativo? Obrigado.

Se a conta for Netting, pode virar bagunça; se for Hedging, utilize diferentes Magic Numbers para os EAs (o tópico abaixo se refere a "diferentes ativos", mas a sugestão vale pro seu caso também):

Fórum de negociação, sistemas de negociação automatizados e testes de estratégias de negociação

[Dúvida] Como realizar operações em diferentes EA's, em diferentes ativos, no mesmo tempo?

Vinicius de Oliveira, 2022.10.18 15:34


Bom dia  Christian!!


Para os EAs poderem negociar simultaneamente e sem conflitos você deve utilizar um identificador para as operações. O identificador mais utilizado é o MAGIC NUMBER. Veja exemplos abaixo de como gerenciar suas ordens/posições utilizando o MN:


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

//=== Classes
#include <Trade\Trade.mqh> CTrade m_trade;    //--- Funções de negociação

//=== Define constants
#define MAGIC 11


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Set Magic Number
   m_trade.SetExpertMagicNumber(MAGIC);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- Local variables
   int   Cnt, pos_nr = 0, ord_nr = 0;
   ulong TICKET;


// . . .


//--- Checks orders
   for(Cnt = OrdersTotal() - 1; Cnt >= 0; Cnt --)
     {
      TICKET = OrderGetTicket(Cnt);
      if(TICKET == 0)
        {
         Print("Failed to get order ticket...");
         return;
        }
      if(OrderGetString(ORDER_SYMBOL) == _Symbol && OrderGetInteger(ORDER_MAGIC) == MAGIC)
        {
         //--- Increase the orders number
         ord_nr ++;
         if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_BUY_STOP)
           {
            //--- Checks if deletes/modify pending order
            // . . .
            //--- Decrease the orders number
            ord_nr --; //--- (Caso a ordem seja excluída)
           }
         else
           {
            if(OrderGetInteger(ORDER_TYPE) == ORDER_TYPE_SELL_STOP)
              {
               //--- Checks if deletes/modify pending order
               // . . .
               //--- Decrease the orders number
               ord_nr --; //--- (Caso a ordem seja excluída)
              }
           }
        }
     }


// . . .


//--- Checks positions
   for(Cnt = PositionsTotal() - 1; Cnt >= 0; Cnt --)
     {
      //--- Gets the ticket and selects the position
      TICKET = PositionGetTicket(Cnt);
      if(TICKET == 0)
        {
         Print("Failed to get position ticket...");
         return;
        }
      if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == MAGIC)
        {
         //--- Increase the positions number
         pos_nr ++;
         //--- Checks POSITION_TYPE_BUY
         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
           {
            //--- Checks if closes position
            if(CheckCloseLong())
              {
               // . . .
               //--- Decrease the positions number
               pos_nr --; //--- (Caso a posição seja encerrada)
              }
            else
              {
               //--- Checks trailing stop loss
               // . . .
              }
           }
         //--- POSITION_TYPE_SELL
         else
           {
            //--- Checks if closes position
            if(CheckCloseShort())
              {
               // . . .
               //--- Decrease the positions number
               pos_nr --; //--- (Caso a posição seja encerrada)
              }
            else
              {
               //--- Checks trailing stop loss
               // . . .
              }
           }
        }
     }


// . . .


//--- Open position - Buy
   if(pos_nr == 0)
     {
      if(!m_trade.Buy(Lot, _Symbol, ASK, SL, TP, COMMENT))
        {
         Print(m_trade.ResultRetcode(), " ", m_trade.ResultRetcodeDescription());
         return;
        }
     }


// . . .


//--- Open position - Sell
   if(pos_nr == 0)
     {
      if(!m_trade.Sell(Lot, _Symbol, BID, SL, TP, COMMENT))
        {
         Print(m_trade.ResultRetcode(), " ", m_trade.ResultRetcodeDescription());
         return;
        }
     }
  }
//+------------------------------------------------------------------+


... É comum também deixar o MN como parâmetro de entrada, ou ainda, se preferir, calcular automaticamente (em OnInit) de acordo com o timeframe que o EA está sendo executado...



EDIT.: Adicionadas as variáveis ord_nr e pos_nr ao código, como uma forma de contar ordens/posições do EA...


. . .


Razão: