Download MetaTrader 5
To add comments, please log in or register
Having troubles understanding the code? Articles can help you!
flexea
120
flexea 2016.06.20 22:01 

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...?

Drazen Penic
2404
Drazen Penic 2016.06.20 23:36  

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

Check function GlobalVariableSetOnCondition() 

whroeder1
13602
whroeder1 2016.06.21 11:57  
My mutex attached, untested.
Files:
mutex.mqh 4 kb
flexea
120
flexea 2016.07.06 19:27  
WHRoeder:
My mutex attached, untested.

Thanks WHRoeder,

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

Fernando Carreiro
1439
Fernando Carreiro 2016.07.06 23:37  
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.

flexea
120
flexea 2016.07.06 23:59  
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...
Fernando Carreiro
1439
Fernando Carreiro 2016.07.07 00:18  
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!
flexea
120
flexea 2016.07.07 02:19  
     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?

Fernando Carreiro
1439
Fernando Carreiro 2016.07.07 03:39  
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.

whroeder1
13602
whroeder1 2016.07.07 17:14  
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()
/
To add comments, please log in or register