Limit trading to a single pair.

 

I have my ea on all the fx pairs that my broker has, but I want to limit trading to only a single pair only eg I want each ea to check that there it is allowed to open a trade before sending the order. If my ea on GBPUSD has 1 or more trades open, then only that ea is allowed to open a new trade.

I have found that if multiple eas has a signal, on the same tick, when there is 0 trades open, then, i can have a trade open, on multiple pairs. This is exactly what I want to avoid from happening.

This is the 2 lines of code that I have in the OnTick() function.

   if(buys + sells < 1 && PositionsTotal() >= 1)
      return;

As i said, this seems to work for the most part, but the issue i have is that if there is 0 trades open, and multiple eas have a signal, then, each ea will open a trade.

Can you suggest a better way?

 
Would adding those 2 lines into the OnTimer() function fix this?
 
  1. OnTimer changes nothing.
  2. Your problem is you need a mutex for the test (PositionsTotal) and then open, to be atomic across multiple threads (EAs).
              Prevent EA from opening trades at the same time across 2 or more pairs? - MQL4 programming forum - Page%nbsp;2 #11 (2022)

 
William Roeder #:
  1. OnTimer changes nothing.
  2. Your problem is you need a mutex for the test (PositionsTotal) and then open, to be atomic across multiple threads (EAs).
              Prevent EA from opening trades at the same time across 2 or more pairs? - MQL4 programming forum - Page%nbsp;2 #11 (2022)

will check it out. thanks.

 
William Roeder #:
  1. OnTimer changes nothing.
  2. Your problem is you need a mutex for the test (PositionsTotal) and then open, to be atomic across multiple threads (EAs).
              Prevent EA from opening trades at the same time across 2 or more pairs? - MQL4 programming forum - Page%nbsp;2 #11 (2022)

sometimes i use code from eabuilder and that will often open multiple trades. Q... will the above avoid this from happening, also? i mean: if i add the mySend function from eabuilder... to the function at the bottom.

Reason: