EA opens multible trades instead of 1

 

Hi all

My EA should open one trade if the ADX crosses a level from below. Most time it works fine but sometimes it opens multible Trades. All at the same time in same symbol. I think the problem occours in this part of code. This part should just watch if the adx crossed the adx_low_level from below and return a TRUE or FALSE. If there was a TRUE then a trade would be openend and the already_sent bool would turn to true. This boolean should help to send just one "TRUE" to caller if there's fluctuation in adx calculation. The boolean would be resettet when the adx value is +3 to the low level. In my opionon +3 should not be possible to acchive by just fluctuation. Therefore it should not open new trades but this happens.

bool already_sent = FALSE;

double ADX_crossed_level_from_below(string symbol){
   
   double ADX_crossed_low_level_upwards = FALSE;
   
   if(get_ADX(symbol)>ADX_LOW_LEVEL){
     // check if ADX 2 bars ago was under ADX_LOW_LEVEL
     if((iADX(NULL,PERIOD_M30,14,PRICE_CLOSE,MODE_MAIN,2)<ADX_LOW_LEVEL) && !already_sent){
         ADX_crossed_low_level_upwards = TRUE;
         already_sent = true;
         return ADX_crossed_low_level_upwards;
     }   
     else{
         ADX_crossed_low_level_upwards = FALSE;
     }
     if(get_ADX(symbol) > (ADX_LOW_LEVEL+3)){
         already_sent = false;
    }
     
   }
   else{
      ADX_crossed_low_level_upwards = FALSE;
     
   }
   
   
   return ADX_crossed_low_level_upwards;
 }

What am I doing wrong?

Thanks for input.

Best regards

Remu

 
Remu Mure: but sometimes it opens multible Trades. … What am I doing wrong?
  1. The word is spelled multiple.
  2. You are not checking if you already have an open order.
 
William Roeder:
  1. The word is spelled multiple.
  2. You are not checking if you already have an open order.

As William wrote - you're not checking what's already open.

Try this one out

void OnTick()
 {
  if(CheckForExistingOrder()==0)//Example to call the function,check and if no orders exists and do stuff....
  {
   //...do stuff here
  }
 }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CheckForExistingOrder()
  {
   int check=0;
   for(int x = OrdersTotal() - 1; x >= 0; x--)
     {
      if(!OrderSelect(x, SELECT_BY_POS))
         break;
      if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=MagicNumber)
         continue;
      if((OrderCloseTime() == 0) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType() == OP_BUY||OrderType() == OP_SELL)
            check = 1;
         if(!(OrderType() == OP_BUY||OrderType() == OP_SELL))
            check = -1;
        }
     }
   return(check);
  }
 

Okay, thank you very much.

Do I have to check the magic number? I only want to open one trade in each symbol. So I assume that I only need to check if there is an open trade in the corresponding symbol and I don't need to check the magic number? Or is there an advantage if I check the magic number?

 
Remu Mure:

Okay, thank you very much.

Do I have to check the magic number? I only want to open one trade in each symbol. So I assume that I only need to check if there is an open trade in the corresponding symbol and I don't need to check the magic number? Or is there an advantage if I check the magic number?

Magic number is the actual orders identification number and should absolutely be used. The code i gave you checks and identify your order and what symbol it's placed on plus it makes it not interfere with other robots you may use. Simply make an external setting in your ea with a magic number input;

extern int MagicNumber= 1234567;

and now this work with the function itself i gave you...you don't need to change the magic for each chart you trade,not necessary at all!

 

Hi everybody,

Could I ask if this code work for mt4? In the case I am on right forum.

I think I have the same problem: I have to run Multi EAs on different  pairs, the same EA on different pairs or more EAs on the same pair (the same timeframe or different ones)

Regarding the code that Kenneth Parling suggested where I have to write this part of code:

void OnTick()
 {
  if(CheckForExistingOrder()==0)//Example to call the function,check and if no orders exists and do stuff....
  {
   //...do stuff here
  }
 }

and where this one:

int CheckForExistingOrder()
  {
   int check=0;
   for(int x = OrdersTotal() - 1; x >= 0; x--)
     {
      if(!OrderSelect(x, SELECT_BY_POS))
         break;
      if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=MagicNumber)
         continue;
      if((OrderCloseTime() == 0) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType() == OP_BUY||OrderType() == OP_SELL)
            check = 1;
         if(!(OrderType() == OP_BUY||OrderType() == OP_SELL))
            check = -1;
        }
     }
   return(check);
  }

Finally, Do I need to specify before OrderSend condition example MA1>MA2&&ect... also something else?

Thanks a lot

 
Kenneth Parling:

Magic number is the actual orders identification number and should absolutely be used. The code i gave you checks and identify your order and what symbol it's placed on plus it makes it not interfere with other robots you may use. Simply make an external setting in your ea with a magic number input;

and now this work with the function itself i gave you...you don't need to change the magic for each chart you trade,not necessary at all!

Ok I missunderstood the power of magic number. After some googling and fixes in the code it seems to work fine now. It runs since 2 days and never had a multiple trade again. Thank you very much for help.