0

Everything seems fine. But EA usually opens multiple trades in the same second... The way I built it is very linear and i can't seem to spot the logical mistake. It is basically a random martingale EA to test stuff out. Any indicator (that's why I called it random, haven't decided myself) can be put in there.

Basic idea is that it has an upper and lower threshold which determines when it is in buy zone and when at sell zone. Once it is in either zone, if trend goes against it (determined by indicator's value, not symbol's price) it opens another trade with the same SL/TP of the initial order. Also it checks whether initial trade still runs so it does not open other ones and once the initial trade is open. After that the criterias about the rest of the trades (that go against the trade are different).

The problem is that it opens multiple trades at times that it shouldn't, or like 3-4 trades within the same second or two. Any idea why this happens?

#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int stepValue = 5;
input double lotsize = 0.01;
input int stoploss = 2000;
input int takeprofit = 140;
input int slippage = 10;
input double upper_border = 60.0;
input double lower_border = 40.0;

const string EAComment = "Xind";

string mode = "";
bool first_trade = false;
int InitTicket = 1;
double X = 0.0;
double X_Last = 0.0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {
//---
      first_trade = false;
//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
  {
//---

  }
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void OnTick()
  {
//---
      SearchSignal();
      if (mode == "Buy")
         {
            if (first_trade == false)
            {
               Buy();
            }
            if (first_trade == true)
            {
               MartinCheck();
               CloseCheck();
            }
         }
      if (mode == "Sell")
         {
            if (first_trade == false)
            {
               Sell();
            }
            if (first_trade == true)
            {
               MartinCheck();
               CloseCheck();
            }
         }
  }
//+------------------------------------------------------------------+
void Buy()
  {
      X_Last = X;
      first_trade = true;
      InitTicket = OrderSend(NULL,OP_BUY,lotsize,Ask,slippage,Ask-stoploss*Point,Ask+takeprofit*Point,EAComment,1,0,clrDarkBlue);
  }
//---
void Sell()
  {
      X_Last = X;
      first_trade = true;
      InitTicket = OrderSend(NULL,OP_SELL,lotsize,Bid,slippage,Bid+stoploss*Point,Bid-takeprofit*Point,EAComment,1,0,clrDarkRed);
  }
//---
void MartinBuy()
  {
      if (OrderSelect(InitTicket, SELECT_BY_TICKET) == true)
         {      
            double new_SL = OrderStopLoss();
            double new_TP = OrderTakeProfit();
            int dont_care = OrderSend(NULL,OP_BUY,lotsize,Ask,slippage,new_SL,new_TP,EAComment+" martin",1,0,clrDarkBlue);
         }
  }
//---
void MartinSell()
  {
      if (OrderSelect(InitTicket, SELECT_BY_TICKET) == true)
         {      
            double new_SL = OrderStopLoss();
            double new_TP = OrderTakeProfit();
            int dont_care = OrderSend(NULL,OP_SELL,lotsize,Bid,slippage,new_SL,new_TP,EAComment+" martin",1,0,clrDarkRed);
         }
  }
//---
void SearchSignal()
{
   X = 0.0; //where 0.0, put here the iCustom for external indicators, or some built-in indicator
   if (X >= upper_border)
      {
         mode = "Sell";
      }
   else if (X <= lower_border)
      {
         mode = "Buy";
      }
   else
      {
         mode = "";
         first_trade = false;
         InitTicket = 1;
         X_Last = 0.0;
      }
}
//---
void CloseCheck()
{
    if (OrderSelect(InitTicket, SELECT_BY_TICKET))
    {

       if (OrderCloseTime() == 0)
          {
             first_trade = true;
          }
       else if (OrderCloseTime() != 0)
          {
             first_trade = false;
          }
       else
          {
             return;
          }
    }
}
//---
void MartinCheck()
{
   if (mode == "Buy")
      {
         if ((X_Last - stepValue) >= X)
            {
               X_Last = X;
               MartinBuy();
            }
      }
   if (mode == "Sell")
      {
         if ((X_Last + stepValue) <= X)
            {
               X_Last = X;
               MartinSell();
            }
      }
}

1 Answers1

1

The layout of your code makes it possible for several processes to happen in sequence all on the same tick which I assume you do not want. Try changing your code initially to this and work from there:

void OnTick()
{
    SearchSignal();
    if(mode=="Buy")
    {
       if(!first_trade) Buy();
       else
       {
          MartinCheck();
          CloseCheck();
       }
    }
    else if(mode=="Sell")
    {
       if(!first_trade) Sell();
       else
       {
           MartinCheck();
           CloseCheck();
       }
    }
}

Remember to use if(...) else to stop executing all functions when it should only be an either/or situation.

PaulB
  • 1,262
  • 1
  • 6
  • 17