EA Opens more trades than expected

 

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();
            }
      }
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2021.03.10
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

Example.... Half of those are from the initial trade, half from the martin order conditions (censored comments cuz they contained expressions that violate forum rules but expressed my inner anger at the time, and now).


Image attached.

 
Pantelis Pap.:

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?

When you check the signal, you are resetting first_trade to false if there is no signal, therefore it is now free to place another trade

it is normally best practice to monitor and control the item you want controlled, so if you want to limit trades, monitor and control the trades to ensure you don't open more than desired.

At present you are monitoring a signal and trying to control a flag from that to decide if a trade is allowed or not,  you are not actually monitoring and controlling the trade situation itself.

 
Paul Anscombe:

When you check the signal, you are resetting first_trade to false if there is no signal, therefore it is now free to place another trade

it is normally best practice to monitor and control the item you want controlled, so if you want to limit trades, monitor and control the trades to ensure you don't open more than desired.

At present you are monitoring a signal and trying to control a flag from that to decide if a trade is allowed or not,  you are not actually monitoring and controlling the trade situation itself.

So how you suggest I fix this situation as it's not really a good EA. I don't want to make it restart proof (to recognise past trades etc). I basically only want to make it backtestable. At this point can I patch this easily or do I have to rebuild it the proper way? Also thanks for the tips so far!

 
Pantelis Pap.:

So how you suggest I fix this situation as it's not really a good EA. I don't want to make it restart proof (to recognise past trades etc). I basically only want to make it backtestable. At this point can I patch this easily or do I have to rebuild it the proper way? Also thanks for the tips so far!

you could use the freelance section to get somebody to code it properly for you 

https://www.mql5.com/en/job

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2021.03.10
  • www.mql5.com
The largest freelance service with MQL5 application developers