I have one question, pls help me.

 

Hello all, I would like to know how to solve following problem.

1. One EA working on 20 Pair. EA open position with EAMagicNumber=5000. EA open only one position on lifetime. For example, If EA open one position ( EAMagicNumber=5000 ) on XAUUSD, then other pair can't open position with EAMagicNumber=5000. EA don't allow open position on other pair

I attach following code. But that code doesn't work. How do I solve that problem?

for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      int chkA=OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
      if(OrderMagicNumber()==EAMagicNumber)
      {
         notAllow=true;
      }
   }
   for(int j=OrdersTotal()-1;j>=0;j--)
   {
      int chkB=OrderSelect(j, SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==EAMagicNumber)
      {
         notAllow=true;
      }
   }
 
Erdenebayar Lamjav:

Hello all, I would like to know how to solve following problem.

1. One EA working on 20 Pair. EA open position with EAMagicNumber=5000. EA open only one position on lifetime. For example, If EA open one position ( EAMagicNumber=5000 ) on XAUUSD, then other pair can't open position with EAMagicNumber=5000. EA don't allow open position on other pair

I attach following code. But that code doesn't work. How do I solve that problem?

is this a separate function that returns the notAllow ? 

 
Lorentzos Roussos:

is this a separate function that returns the notAllow ? 

No, I think notAllow is public variable. So it doesn't require return value.

void FirstPositions()
{
   //Alert(StringFind(OrderComment(),"Martingale",0));
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      int chkA=OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
      if(OrderMagicNumber()==EAMagicNumber)
      {
         notAllow=true;
      }
   }
   for(int j=OrdersTotal()-1;j>=0;j--)
   {
      int chkB=OrderSelect(j, SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==EAMagicNumber)
      {
         notAllow=true;
      }
   }
}
 
So, once the global variable is set to true, it remains so forever.
 
Yes, EA of other pair must disagree open more position. But still open position on all pair. I don't need multiple position each on of pairs. I need one single position during all pair on lifetime.
 
William Roeder:
So, once the global variable is set to true, it remains so forever.

this .

try 

void FirstPositions()
{
static bool Allow=true;
   //Alert(StringFind(OrderComment(),"Martingale",0));
   //note : does OrdersHistoryTotal return entire history  ? 
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      bool select=OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
      if(select){
      if(OrderMagicNumber()==EAMagicNumber)
      {
      Allow=false;
      }
      }
   }
   for(int j=OrdersTotal()-1;j>=0;j--)
   {
      bool select=OrderSelect(j, SELECT_BY_POS,MODE_TRADES);
      if(select){
      if(OrderMagicNumber()==EAMagicNumber)
      {
      Allow=false;
      }
      }
   }
}
for note : https://docs.mql4.com/trading/ordershistorytotal
OrdersHistoryTotal - Trade Functions - MQL4 Reference
OrdersHistoryTotal - Trade Functions - MQL4 Reference
  • docs.mql4.com
The number of closed orders in the account history loaded into the terminal. The history list size depends on the current settings of the "Account history" tab of the terminal.
 
Lorentzos Roussos:

this .

try

  1. Same thing; once the static is set to false, it remains that way forever. The static is internal to the function and can't ever be seen, thus is useless.
  2. Delete the static and make the function return the result.
 
William Roeder:
  1. Same thing; once the static is set to false, it remains that way forever. The static is internal to the function and can't ever be seen, thus is useless.
  2. Delete the static and make the function return the result.

indeed . 

I did not know that .

The fact i could not use it outside its function should have given me a hint . 

Thanks 

 
Lorentzos Roussos:

indeed . 

I did not know that .

The fact i could not use it outside its function should have given me a hint . 

Thanks 

You search orders in history :)

so you can Just open One order with this magic number.

you have to search magic number in your opening orders/positions

for ( int i = OrdersTotal() - 1; i >= 0; i--)
       {  
        ...
        ...
 
Tick movement very fast, so many position opened synchronization. Above code work only tick movement slowly. But I guess thank you your responses.
 
Faeze Bakhshayesh:

You search orders in history :)

so you can Just open One order with this magic number.

you have to search magic number in your opening orders/positions

one loop ?

Reason: