Conflict between two expert advisors during AutoTrading in mt4

 

Hi everyone.

I don't know MQL, I'm using a builder to create Expert Advisors.

I'm trying to use 2 Expert Advisors in 2 different symbols to execute automatic orders.

EAs are similar but with opposite conditions and different magic numbers.

When one of the two executes an order automatically, for example a Buy order, the other one is unable to execute Buy orders but can only go short, and vice versa.

I don't understand what the problem could be.

Can someone help me understand what's going on please?

This is the code of one of the two EAs:

int minutesTimeR1C2O1[60] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
int hoursTimeR1C2O1[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
int daysOfWeekTimeR1C2O1[7] = {0, 1, 2, 3, 4, 5, 6};
int daysOfMonthTimeR1C2O1[31] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

int minutesTimeR2C2O1[60] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
int hoursTimeR2C2O1[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
int daysOfWeekTimeR2C2O1[7] = {0, 1, 2, 3, 4, 5, 6};
int daysOfMonthTimeR2C2O1[31] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

#property copyright "Created with: ©2024 Visual Strategy Builder"
#property link "https://tools.forextester.com"
#property description ""
#property strict

datetime currentBar = 0;
int logFileHandle = INVALID_HANDLE;

datetime lastActR1 = 0;
datetime lastActR2 = 0;

input bool DebugMode = false; //Write strategy actions to logs
input bool WriteDebugLogToFile = false;
extern string ___R1 = "Rule 'BUY'";

extern string _______________________PriceR1C1O1="Price";


extern string _______________________PriceR1C1O2="Price";


extern string ___R2 = "Rule 'BUY'";

extern string _______________________TimeR1C2O1="Time";


extern string _______________________="CurrentTime";


extern string _______________________R1A1="OpenMarketOrder";
extern double Volume________R1A1 = 0.01;
extern int TakeProfit________R1A1 = 0;
extern int StopLoss________R1A1 = 0;
extern int Slippage________R1A1 = 3;

extern string _______________________R1A2="CloseOrdersByMagicNumber";

extern string ___R3 = "Rule 'SELL'";

extern string _______________________PriceR2C1O1="Price";


extern string _______________________PriceR2C1O2="Price";


extern string ___R4 = "Rule 'SELL'";

extern string _______________________TimeR2C2O1="Time";




extern string _______________________R2A1="OpenMarketOrder";
extern double Volume________R2A1 = 0.01;
extern int TakeProfit________R2A1 = 0;
extern int StopLoss________R2A1 = 0;
extern int Slippage________R2A1 = 3;

extern string _______________________R2A2="CloseOrdersByMagicNumber";


extern bool UseTrailingStop = false;
extern double TrailingStart = 20;
extern double TrailingStep = 20;
extern bool MoveTakeProfit = false;

void OnInit()
{
   if(DebugMode && WriteDebugLogToFile)
     logFileHandle = FileOpen(StringConcatenate(MathRand(), "-vsb.log"), FILE_WRITE|FILE_TXT|FILE_SHARE_READ);
}

void OnDeinit(const int reason)
{
   if(logFileHandle != INVALID_HANDLE)
     FileClose(logFileHandle);
}

void WriteDebugLog(string logMessage)
{
   Print(logMessage);

   if (logFileHandle != INVALID_HANDLE && WriteDebugLogToFile)
     FileWriteString(logFileHandle, StringConcatenate(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS), " " ,logMessage, "\r\n"));
}

void OnTick()
{
   currentBar = iTime(NULL, 0, 0);

   if(DebugMode)
          WriteDebugLog("Process new tick");

      double PriceR1C1O1 = iHigh(Symbol(), 0, 0);
   double PriceR1C1O2 = iHigh(Symbol(), 0, 1);
   bool TimeR1C2O1 = CheckTime(minutesTimeR1C2O1, hoursTimeR1C2O1, daysOfWeekTimeR1C2O1, daysOfMonthTimeR1C2O1);

   if(DebugMode)
   {
      WriteDebugLog("Process rule: BUY");
      WriteDebugLog(StringConcatenate("PriceR1C1O1(iHigh(Symbol(), 0, 0)) = ", PriceR1C1O1));
      WriteDebugLog(StringConcatenate("PriceR1C1O2(iHigh(Symbol(), 0, 1)) = ", PriceR1C1O2));
      WriteDebugLog(StringConcatenate("TimeR1C2O1(CheckTime(minutesTimeR1C2O1, hoursTimeR1C2O1, daysOfWeekTimeR1C2O1, daysOfMonthTimeR1C2O1)) = ", TimeR1C2O1));
      WriteDebugLog("Rule structure: PriceR1C1O1 > PriceR1C1O2 &&  TimeR1C2O1 ");
      WriteDebugLog(StringConcatenate("Rule values: ","",PriceR1C1O1," > ",PriceR1C1O2,""," && ", TimeR1C2O1));
   }

   if(PriceR1C1O1 > PriceR1C1O2 && TimeR1C2O1 )
   {
      if(DebugMode)
      {
         WriteDebugLog("Condition of rule \"BUY\" met");
         WriteDebugLog("Performing actions...");
      }
      if(lastActR1 != currentBar)
      {
         lastActR1 = currentBar;
      
         if(DebugMode)
            WriteDebugLog(StringConcatenate("Try to open Market Order with : Symbol:",Symbol(), " Type:","BUY"," Volume:",Volume________R1A1," Slippage:",Slippage________R1A1," SL:",CalculateStopLoss(Symbol(), 0, StopLoss________R1A1)," TP:",CalculateTakeProfit(Symbol(), 0, TakeProfit________R1A1), "MagicNumber:",24681));
         if(!ExistsOrdersWithIdentifier("R1A1"))
         {
            if(OrderSend(Symbol(), 0, Volume________R1A1, SymbolInfoDouble(Symbol(), SYMBOL_ASK), Slippage________R1A1, CalculateStopLoss(Symbol(), 0, StopLoss________R1A1), CalculateTakeProfit(Symbol(), 0, TakeProfit________R1A1), ";R1A1", 24681, 0, 65280))
            {
               if(DebugMode)
                  WriteDebugLog("Order opened");
            }
            else
            {
               if(DebugMode)
                  WriteDebugLog("Failed to open order");
            }
         }
   
      
         CloseAllOrdersByMagicNumber(24682);
      }
   }
   else
   {
      if (DebugMode)
         WriteDebugLog("Condition of rule \"BUY\" not met");
   }
      double PriceR2C1O1 = iLow(Symbol(), 0, 0);
   double PriceR2C1O2 = iLow(Symbol(), 0, 1);
   bool TimeR2C2O1 = CheckTime(minutesTimeR2C2O1, hoursTimeR2C2O1, daysOfWeekTimeR2C2O1, daysOfMonthTimeR2C2O1);

   if(DebugMode)
   {
      WriteDebugLog("Process rule: SELL");
      WriteDebugLog(StringConcatenate("PriceR2C1O1(iLow(Symbol(), 0, 0)) = ", PriceR2C1O1));
      WriteDebugLog(StringConcatenate("PriceR2C1O2(iLow(Symbol(), 0, 1)) = ", PriceR2C1O2));
      WriteDebugLog(StringConcatenate("TimeR2C2O1(CheckTime(minutesTimeR2C2O1, hoursTimeR2C2O1, daysOfWeekTimeR2C2O1, daysOfMonthTimeR2C2O1)) = ", TimeR2C2O1));
      WriteDebugLog("Rule structure: PriceR2C1O1 < PriceR2C1O2 &&  TimeR2C2O1 ");
      WriteDebugLog(StringConcatenate("Rule values: ","",PriceR2C1O1," < ",PriceR2C1O2,""," && ", TimeR2C2O1));
   }

   if(PriceR2C1O1 < PriceR2C1O2 && TimeR2C2O1 )
   {
      if(DebugMode)
      {
         WriteDebugLog("Condition of rule \"SELL\" met");
         WriteDebugLog("Performing actions...");
      }
      if(lastActR2 != currentBar)
      {
         lastActR2 = currentBar;
      
         if(DebugMode)
            WriteDebugLog(StringConcatenate("Try to open Market Order with : Symbol:",Symbol(), " Type:","SELL"," Volume:",Volume________R2A1," Slippage:",Slippage________R2A1," SL:",CalculateStopLoss(Symbol(), 1, StopLoss________R2A1)," TP:",CalculateTakeProfit(Symbol(), 1, TakeProfit________R2A1), "MagicNumber:",24682));
         if(!ExistsOrdersWithIdentifier("R2A1"))
         {
            if(OrderSend(Symbol(), 1, Volume________R2A1, SymbolInfoDouble(Symbol(), SYMBOL_BID), Slippage________R2A1, CalculateStopLoss(Symbol(), 1, StopLoss________R2A1), CalculateTakeProfit(Symbol(), 1, TakeProfit________R2A1), ";R2A1", 24682, 0, 65280))
            {
               if(DebugMode)
                  WriteDebugLog("Order opened");
            }
            else
            {
               if(DebugMode)
                  WriteDebugLog("Failed to open order");
            }
         }
   
      
         CloseAllOrdersByMagicNumber(24681);
      }
   }
   else
   {
      if (DebugMode)
         WriteDebugLog("Condition of rule \"SELL\" not met");
   }
   ProcessTrailingStop();}


double PipPoint(string symbol)
{
       double calcPoint = 0.0;
       int digits = (int)MarketInfo(symbol, MODE_DIGITS);
       if (digits == 2 || digits == 3)
          calcPoint = 0.01;
       if (digits == 4 || digits == 5)
          calcPoint = 0.0001;

       return calcPoint;
}


double CalculateLot(string symbol, double percent, int orderType, double slPips, double entryPrice = 0)
{
    if (entryPrice == 0)
        entryPrice = SymbolInfoDouble(symbol, orderType == 0 ? SYMBOL_ASK : SYMBOL_BID);

    const int iPipMult[] = {1, 10, 10, 10, 1, 10, 100};
    int digits = (int)MarketInfo(symbol, MODE_DIGITS);
    double minLot = MarketInfo(symbol, MODE_MINLOT);
    double maxLot = MarketInfo(symbol, MODE_MAXLOT);
    double tickValue = MarketInfo(symbol, MODE_TICKVALUE);
    double riskMoney = AccountBalance() * (percent / 100);
    double stopLoss = CalculateStopLoss(symbol, orderType, slPips, entryPrice);
    double riskPrice = MathAbs(entryPrice - stopLoss) * MathPow(10, digits - 1);
    double lot = riskMoney / (riskPrice * tickValue * iPipMult[digits]);
    if(lot < minLot) lot = minLot;
    if(lot > maxLot) lot = maxLot;
    return NormalizeLot(symbol, lot);
}


double NormalizeLot(string symbol, double lot)
{
    int lotStep = 0;
    if(MarketInfo(symbol, MODE_LOTSTEP) == 0.1)
        lotStep = 1;
    if(MarketInfo(symbol, MODE_LOTSTEP) == 0.01)
        lotStep = 2;
    if (lotStep == 0)
        return lot;
    return NormalizeDouble(lot, lotStep);
}


double CalculateStopLoss(string symbol, int orderType, double slPips, double entryPrice = 0)
{
    if (slPips == 0)
        return 0;

    if (entryPrice == 0)
        entryPrice = SymbolInfoDouble(symbol, orderType == 0 ? SYMBOL_ASK : SYMBOL_BID);

    int digits = (int)MarketInfo(symbol, MODE_DIGITS);
    double slPrice = PipPoint(symbol) * slPips;

    if (orderType == 0 || orderType == 2 || orderType == 4)
        return NormalizeDouble(entryPrice - slPrice, digits);
    else
        return NormalizeDouble(entryPrice + slPrice, digits);
}


double CalculateTakeProfit(string symbol, int orderType, double tpPips,  double entryPrice = 0)
{
    if (tpPips == 0)
        return 0;

    if (entryPrice == 0)
        entryPrice = SymbolInfoDouble(symbol, orderType == 0 ? SYMBOL_ASK : SYMBOL_BID);

    int digits = (int)MarketInfo(symbol, MODE_DIGITS);
    double tpPrice = PipPoint(symbol) * tpPips;

    if (orderType == 0 || orderType == 2 || orderType == 4)
        return NormalizeDouble(entryPrice + tpPrice, digits);
    else
        return NormalizeDouble(entryPrice - tpPrice, digits);
}


bool ExistsOrdersWithIdentifier(string identifier)
{
   for(int i = 0; i < OrdersTotal(); i++)        
   { 
           if (OrderSelect(i,SELECT_BY_POS) == false)
                  continue;   
           if (StringFind(OrderComment(), identifier) == -1)
                  continue; 
                  
           if(DebugMode)
           {
                  WriteDebugLog("Order from this action is still open. Skip action");
           }
           return true;                                 
   }
   
   return false;
}


bool CheckTime(int& minutes[], int& hours[], int& daysOfWeek[], int& daysOfMonth[])
{
        datetime currentTime = TimeCurrent();
        int minutesSize = ArraySize(minutes);
        int hoursSize = ArraySize(hours);
        int daysOfWeekSize = ArraySize(daysOfWeek);
        int daysOfMonthSize = ArraySize(daysOfMonth);

        if (minutesSize > 0)
        {
                bool passMinutes = false;
                for (int i = 0; i < minutesSize; i++)
                {
                        if (TimeMinute(currentTime) == minutes[i])
                        {
                                passMinutes = true;
                                break;
                        }
                }
                if (!passMinutes)
                        return false;
        }

        if (hoursSize > 0)
        {
                bool passHours = false;
                for (int i = 0; i < hoursSize; i++)
                {
                        if (TimeHour(currentTime) == hours[i])
                        {
                                passHours = true;
                                break;
                        }
                }
                if (!passHours)
                        return false;
        }

        if (daysOfWeekSize > 0)
        {
                bool passDaysOfWeek = false;
                for (int i = 0; i < daysOfWeekSize; i++)
                {
                        if (TimeDayOfWeek(currentTime) == daysOfWeek[i])
                        {
                                passDaysOfWeek = true;
                                break;
                        }
                }
                if (!passDaysOfWeek)
                        return false;
        }

        if (daysOfMonthSize > 0)
        {
                bool passDaysOfMonth = false;
                for (int i = 0; i < daysOfMonthSize; i++)
                {
                        if (TimeDay(currentTime) == daysOfMonth[i])
                        {
                                passDaysOfMonth = true;
                                break;
                        }
                }
                if (!passDaysOfMonth)
                        return false;
        }

        return true;
}


void CloseAllOrdersByMagicNumber(int magicNumber)
{
    if(DebugMode)
        WriteDebugLog(StringConcatenate("Try to close orders with magic number: ",magicNumber));

    for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (!OrderSelect(i, SELECT_BY_POS) || OrderMagicNumber() != magicNumber)
            continue;

        if(DebugMode)
            WriteDebugLog(StringConcatenate("Try to close order with ticket: ", OrderTicket()));

        RefreshRates();

        bool closed;
        if (OrderType() < 2)
            closed = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3);
        else
            closed = OrderDelete(OrderTicket());

        if (DebugMode)
        {
            if (closed)
                WriteDebugLog("Order closed");
            else
                WriteDebugLog("Order not closed");
        }
    }
}


void ProcessTrailingStop()
{
   if (!UseTrailingStop) return;

   for(int i = 0; i < OrdersTotal(); i++)
   {
      if(!OrderSelect(i, SELECT_BY_POS))
         continue;

      RefreshRates();

      int digits = (int)MarketInfo(OrderSymbol(), MODE_DIGITS);
      double pip = 10 * MarketInfo(OrderSymbol(), MODE_POINT);
      double bid = SymbolInfoDouble(OrderSymbol(), SYMBOL_BID);
      double ask = SymbolInfoDouble(OrderSymbol(), SYMBOL_ASK);

      double tsStart = NormalizeDouble(TrailingStart * pip, digits);
      double tsStep = NormalizeDouble(TrailingStep * pip, digits);

      double newSL = OrderStopLoss();
      double newTP = OrderTakeProfit();

      if(OrderType() == OP_BUY)
      {
         if(bid - OrderOpenPrice() <= tsStart) continue;
         if(bid - tsStep > OrderStopLoss() || OrderStopLoss() == 0)
         {
            newSL = bid - tsStep;
            if (newSL > OrderTakeProfit() && OrderTakeProfit() != 0)
               continue;
            if (MoveTakeProfit && OrderTakeProfit() > 0 && OrderStopLoss() > 0)
               newTP = OrderTakeProfit() + (newSL - OrderStopLoss());
         }
      }
      if(OrderType() == OP_SELL)
      {
         if(OrderOpenPrice() - ask <= tsStart) continue;
         if(ask + tsStep < OrderStopLoss() || OrderStopLoss() == 0)
         {
            newSL = ask + tsStep;
            if (newSL < OrderTakeProfit() && OrderTakeProfit() != 0)
               continue;
            if (MoveTakeProfit && OrderTakeProfit() > 0 && OrderStopLoss() > 0)
               newTP = OrderTakeProfit() - (OrderStopLoss() - newSL);
         }
      }

      if (newSL == OrderStopLoss())
         continue;

      if(OrderModify(OrderTicket(), OrderOpenPrice(), newSL, newTP, 0))
      {
         if(DebugMode)
            WriteDebugLog(StringConcatenate("TrailingStop modified order ", OrderTicket(), " New SL ", newSL, " New TP ", newTP));
      }
      else
      {
         if(DebugMode)
            WriteDebugLog(StringConcatenate("TrailingStop failed to update order ", GetLastError()));
      }
   }
}

 
augh hgua:I don't know MQL, I'm using a builder to create Expert Advisors.

ChatGPT (the worst), “Bots Builder”, “EA builder”, “EA Builder Pro”, EATree, “Etasoft forex generator”, “Forex Strategy Builder”, ForexEAdvisor (aka. ForexEAdvisor STRATEGY BUILDER, and Online Forex Expert Advisor Generator), ForexRobotAcademy.com, forexsb, “FX EA Builder”, fxDreema, Forex Generator, FxPro, “LP-MOBI”, Molanis, “Octa-FX Meta Editor”, Strategy Builder FX, “Strategy Quant”, “Visual Trader Studio”, “MQL5 Wizard”, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

Since you haven't learned MQL4/5, therefor there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.

We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

ChatGPT
  1. Even it says do not use it for coding. * 
  2. Mixing MT4 and MT5 code together.
  3. Creating multiple OnCalculate/OnTick functions.
  4. OnCalculate returning a double.
  5. Filling buffers with zero in OnInit (they have no size yet). Setting buffer elements to zero but not setting Empty Value to correspond.
  6. Calling undefined functions.
  7. Calling MT4 functions in MT5 code.
  8. Sometimes, not using strict (MT4 code).
  9. Code that will not compile.
  10. Creating code outside of functions. * 
  11. Creating incomplete code. * 
  12. Initialization of Global variables with non-constants. * 
  13. Assigning a MT5 handle to a double or missing the buffer and bar indexes in a MT4 call. * 
  14. Useing MT4 Trade Functions without first selecting an order. * 
  15. Uses NULL in OrderSend. * 
bot builder Creating two OnInit() functions. * 
EA builder
  1. Counting up while closing multiple orders.
  2. Not useing time in new bar detection.
  3. Not adjusting for 4/5 digit brokers, TP/SL and slippage. * 
  4. Not checking return codes.
EATree Uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)
ForexEAdvisor
  1. Non-updateing global variables.
  2. Compilation errors.
  3. Not checking return codes.
  4. Not reporting errors.
FX EA Builder
  1. Not checking return codes.
  2. Loosing open tickets on terminal restart. No recovery (crash/power failure.)
  3. Not adjusting stops for the spread. * 
  4. Using OrdersTotal directly.
  5. Using the old event handlers.