Same EA on multiple charts (different currencies) + Conditions (to open an order) achieved same time in multiple charts. Problem: Send only one order and not one order per chart

 

Hello everyone,


I decided to write my problem here hopefully someone help me because I am literally blocked. I spent a lot of time without any good results.

As written in the title, I have the exact same EA (MQL4) run on multiple charts (different currencies). The EA send an order when X conditions are met. Part of that condition is that only one order be open in all currencies. Unfortunately, it opens multiple orders from the different currencies when the conditions are met in the exact same time.


EA code (Demonstration): (Conditions are: seconds over 0 -I will change it later on when I fix my problem-, Once5 variable so it only sends one order in the current currency, HELP_HERE_MAYBE is where I tried various solutions)

int Once5;

void OnTick()
{

int s = TimeSeconds(TimeCurrent());

if(s>=0 && Once5==0 && HELP_HERE_MAYBE){
Once5=1;
OrderSend(_Symbol,OP_BUY,0.01,Ask,0,0,0,B.P1,0,0,Green);
}

}


The various solution I tried are different functions starting with:

- CountingAllPositions(). Problem: Conditions are met the exact same time so it returns 0 and I end up with 6 orders from all the six currencies

int CountAllPositions()
{
int NumberOfAllPositions=0;
for (int v= OrdersTotal()-1;v>=0;v--) // we count all the orders
{
OrderSelect(v,SELECT_BY_POS,MODE_TRADES);
NumberOfAllPositions=NumberOfAllPositions+1;
}
return NumberOfAllPositions;
}

- OpenTrades().  Problem: Putting EA to sleep in a currency isn't efficient because sometimes it opens multiple orders because EA in EURUSD for instance runs exact same time as in other currencies.

int OpenSequences()
{

if(Symbol()=="GBPJPY."){Sleep(18000);}
else if(Symbol()=="AUDUSD."){Sleep(15000);}
else if(Symbol()=="USDCHF."){Sleep(12000);}
else if(Symbol()=="USDCAD."){Sleep(9000);}
else if(Symbol()=="USDJPY."){Sleep(6000);}
else if(Symbol()=="EURUSD."){Sleep(3000);}
else if(Symbol()=="GBPUSD."){/*Sleep(0000);*/}
else {Print("Failure FUNCTION");}

for ( v= OrdersTotal()-1;v>=0;v--) // we count all the orders
{
OrderSelect(v,SELECT_BY_POS,MODE_TRADES);
if((StringFind(OrderComment(),"P1",0)>=1)){
NumberOfSequences=1; 
}
else if((StringFind(OrderComment(),"P2",0)>=1)){
NumberOfSequences=2; 
}
}

if(OrdersTotal()==0){
NumberOfSequences=0;
}

Print("How many? ", NumberOfSequences);
return NumberOfSequences;


- Priority() in link with CountAllPositions. Problem: It doesn't work.. because it still opens multiple trades. Not 6 as before but at least 2.

int Priority()
{
if(Symbol()=="GBPJPY."){Sleep(6000);}
else if(Symbol()=="AUDUSD."){Sleep(5000);}
else if(Symbol()=="USDCHF."){Sleep(4000);}
else if(Symbol()=="USDCAD."){Sleep(3000);}
else if(Symbol()=="USDJPY."){Sleep(2000);}
else if(Symbol()=="EURUSD."){Sleep(1000);}
else if(Symbol()=="GBPUSD."){/*Sleep(0000);*/}
else {Print("Failure FUNCTION");}

return CountAllPositions();

}


Note: I read about MagicNumber however, I don't really see how it is useful in my case since it's different currencies charts and not same currency multiple charts.

Note2: I read something about Mutex but I found for MQL5 plus I really didn't understand how to use it.

Note3: I tried using GlobalVariables but things got really messed up because I didn't understand how do you set a global variable in the first place. (int X=GlobalVariableSet("X", 10);). Global Variable name is X and value is 10?!


I thank you in advance for taking the time to read my problem and direct me towards the solution.

Abdel



Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...