Counting open positions within mt4 teminal

 

I have an ea which is placed  on  10  different charts in mt4. The problem is it opens many trades. I just want it to place a maximum of 3 positions. So for example if the chart 1 trade is buy EURUSD, chart 2 is buy USDJPY and chart 3 is Buy USD/CAD and  on chart4 there is a signal to buy usd/chf, then the EA does not buy usd/chf because there are 3 trades already opened. 


I know there is this counting code

 for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))  continue;  
      if(OrderSymbol()      != _Symbol)  continue;  
      if(OrderMagicNumber() != MagicNumber) continue;
      if(OrderType() !=OP_SELL) continue;



but this only counts orders within the EA but does not count orders opened by other EAs. I'm new on MQL$ so will appreciate your help.

 

Do you understand what these lines do:

      if(OrderSymbol()      != _Symbol)  continue;  
      if(OrderMagicNumber() != MagicNumber) continue;
      if(OrderType() !=OP_SELL) continue;

If you understand this you know the answer!

To learn to understand that just write in simple words what it means: if (a != b) continue;

otherwise place your cursor above if and press F1 and start reading!

 
Carl Schreiber:

Do you understand what these lines do:

If you understand this you know the answer!

To learn to understand that just write in simple words what it means: if (a != b) continue;

otherwise place your cursor above if and press F1 and start reading!

Yes i do understand , my question is, does order select , selects all orders in the terminal rather than the EA?

 
prweza:

I have an ea which is placed  on  10  different charts in mt4. The problem is it opens many trades. I just want it to place a maximum of 3 positions. So for example if the chart 1 trade is buy EURUSD, chart 2 is buy USDJPY and chart 3 is Buy USD/CAD and  on chart4 there is a signal to buy usd/chf, then the EA does not buy usd/chf because there are 3 trades already opened. 


I know there is this counting code

but this only counts orders within the EA but does not count orders opened by other EAs. I'm new on MQL$ so will appreciate your help.

If you want to count all open BUY orders you should use something like this:

for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))  continue;
      if(OrderType() !=OP_BUY) continue;

If you want to count all open BUY orders with a specific magic number you should use something like this:

for(int i=OrdersTotal()-1; i>=0; i--)           
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))  continue;
      if(OrderMagicNumber() != MagicNumber) continue;
      if(OrderType() !=OP_BUY) continue;
 
prweza: Yes i do understand , my question is, does order select , selects all orders in the terminal rather than the EA?
  1. Did you see anything in the documentation to the contrary?
  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

 
prweza:

I have an ea which is placed  on  10  different charts in mt4. The problem is it opens many trades. I just want it to place a maximum of 3 positions. So for example if the chart 1 trade is buy EURUSD, chart 2 is buy USDJPY and chart 3 is Buy USD/CAD and  on chart4 there is a signal to buy usd/chf, then the EA does not buy usd/chf because there are 3 trades already opened. 


I know there is this counting code

but this only counts orders within the EA but does not count orders opened by other EAs. I'm new on MQL$ so will appreciate your help.

This is very old legacy code. The reason these old programmers used the continue statement was because MQL would evaluate each expression in an if statement even if the first resolved to false so it was very inefficient. MQL has since fixed that so now you can instead combine those into one if statement like this:

for(int i=OrdersTotal()-1; i>=0; i--)           
   if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber)
      ...
 
SanjayBalraj:

This is very old legacy code. The reason these old programmers used the continue statement was because MQL would evaluate each expression in an if statement even if the first resolved to false so it was very inefficient. MQL has since fixed that so now you can instead combine those into one if statement like this:

for(int i=OrdersTotal()-1; i>=0; i--)           
   if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber)


ok the orders are selected, how then are they added  up?



Please help.

 
prweza:


ok the orders are selected, how then add them up?



Please help.

 

i have tried this code

but get this warning; return value of 'OrderSelect' should be checked


int OpenTradesForMNandPairType(int iMN, string sOrderSymbol, int iType)
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++) // for loop
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
       if (OrderSymbol()== sOrderSymbol)
        {
          if (OrderMagicNumber()==iMN) 
            {
              if(OrderType()==iType) retval++;
            }       
        } // sOrderSymbol
     } // for loop

return(retval);
}
 
prweza: get this warning; return value of 'OrderSelect' should be checked

So check your return codes for errors, report them. Don't just silence the compile, it is trying to help you.
          What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

 
whroeder1:

So check your return codes for errors, report them. Don't just silence the compile, it is trying to help you.
          What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check afteHw 

How do I check the return Code?

Reason: