Prevent EA from opening trades at the same time across 2 or more pairs?

 

I've got an EA that runs on many currency pairs/charts, and sometimes 2 or more charts will trigger trades at nearly the exact same time. I have filters in place that is supposed to prevent too many charts from having open trades at the same time, but this can be bypassed when trades are opened at the same time.


I am using      int randSleep = 5000+ MathRand()/32768;   Sleep(randSleep);    before triggering an order, but this doesn't seem to help stagger trades.


I'm sure there is a better way to do this...?

 

You can create sort of semaphore by using terminal global variables.

Check function GlobalVariableSetOnCondition() 

 
My mutex attached, untested.
Files:
mutex.mqh  4 kb
 
WHRoeder:
My mutex attached, untested.

Thanks WHRoeder,

Is there any way of doing this without requiring an external file?

 
kinitex:

Thanks WHRoeder, Is there any way of doing this without requiring an external file?

Well, you seem to be ignoring the first reply post by drazen64, which suggests the use of Terminal Global Variables. I personally also use them, but remember to always Flush the results after each change (this is very important).

Another alternative is to use Win32 API and the Windows Registry, but that is more complex and not always ideal.

 
I did try the first method using global vars, but it still seems to get by when two or more instances of the EA trigger trades at the exact same time. I've even tried waiting a random number of ticks before the check but still no dice...
 
kinitex:
I did try the first method using global vars, but it still seems to get by when two or more instances of the EA trigger trades at the exact same time. I've even tried waiting a random number of ticks before the check but still no dice...
Then you are not coding your EA properly to handle the situation. The problem is not the Terminal Global Variables. They work correctly and I have used them in my EAs for several years for a variety of uses and it has never failed me!
 
     if(max_ticks_to_wait=0)
     max_ticks_to_wait = MathRand()%20;


   if (current_tick_count <= max_ticks_to_wait && max_ticks_to_wait>0) 
   {
      current_tick_count++;
      DontBuy=true;

      if(symbolcount()>=Max_Charts)
      {//too many charts have trades already, wait until there is room}
   }
   else
   {DontBuy=false;current_tick_count = 0;max_ticks_to_wait=0;}


   if(symbolcount()<=MaxChartsAllowedToTrade && DontBuy=false)
   //do trade stuff here



//Count how many pairs have open trades
int symbolcount()
{
string symbol; int count=1;

for (int ii=0;ii<OrdersTotal();ii++)   
 
  {
   if (!OrderSelect(ii,SELECT_BY_POS,MODE_TRADES)) continue;
    if (OrderSymbol()!=symbol && OrderMagicNumber()==MagicNumber)
    {symbol=OrderSymbol(); 
     count++;
    }

  
 }
  return(count);
}


How should I incorporate global vars to solidify this?

 
kinitex: How should I incorporate global vars to solidify this?

I thought you said that you had already incorporated Terminal Global Variables but that it was not working as expected. So where is that code? If you use the Terminal Global Variables, you will not need the "random tick count wait", so just remove it.

Your code sample is just an "unstructured jumble" with no real logic to it. Is that really your code, or did you just type it out instead of copy/pasting it from your real code?

There are several problems with it, for example you have used an assignment inside an "if" instead of a comparison:

if(max_ticks_to_wait=0)  // This is incorrect
if(max_ticks_to_wait==0) // This is a comparison

There is more, but I prefer waiting until you submit properly structured code. Do proper filtering by MagicNumber and Symbol and if needed, also incorporate Time-Frame in the Magic Numbering.

Describe to us in detail, what the conditions are that you want to test to prevent multiple orders opening. In other words, are each of the EAs running on different Symbols or on the same Symbol but on different Time-frames, or combinations thereof.

Depending on your description, we may just come to the conclusion that you don't even need Terminal Global Variables, and that all you just need is to properly filter your orders.

 
kinitex: How should I incorporate global vars to solidify this?
Why don't you look at the file I provided. The class just uses GlobalVariableSetOnCondition()
 
William Roeder:
My mutex attached, untested.

There are some missing header files being used in your file, can you provide them?

Reason: