Function call structure to return different variables as required

 

(Hello), how to structure a function call to return two different variables?

Example:

// Function
int OrdersCount(){
  for(...){
    OrderSelect(...){
      OP_BUY(...){Long++;}
      OP_SELL(...){Short++;}
      }
    }
  return(Long||Short);
  }

// Call
if(OrdersCount(Long or Short)<1){OrderSend(Long or Short);}
if(OrdersCount(Long or Short)>0){OrderClose(opposite);}
 
David Diez: how to structure a function call to return two different variables?
  1. You don't. You can only return one thing.
  2. You can pass multiple arguments by reference and update them.
    fcn(int& a, int& b){ a=1; b=2; }
    :
    int x,y; fcn(x,y); // x=1, y=2

  3. Or rethink your code.
    bool isOpen(int op){ return OrdersCount(op) > 0; }
    int OrdersCount(int op){
      int n=0;
      for(...){
        OrderSelect(...)
      // MN
      && OrderType() == op
      ) ++n;
      return(n);
    }
    // Call
    if (isOpen(OP_BUY) ){ CloseAll(OP_SELL); }

  4. 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 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
 
David Diez:

(Hello), how to structure a function call to return two different variables?

Example:

Hi,

So you can do as this (You can't return more than one value from any functions):

int CountOrder(ENUM_ORDER_TYPE type)
  {
   int cnt=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
      if(OrderSymbol()==_Symbol && OrderType()==type && OrderMagicNumber()==MagicNumber)
        {
         cnt++;
        }
     }
   return(cnt);
  }

Then call the function by the type of each order, like:

// Call
if(CountOrder(OP_BUY)+CountOrder(OP_SELL)<1){OrderSend(Long or Short);}
if(CountOrder(OP_BUY)+CountOrder(OP_SELL)>0){OrderClose(opposite);}

Or you can reference two inputs of the function to be had two returns, like :

void CountOrder(int &countLong,int &countShort)
  {
   int l=0,s=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
      if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber)
        {
         if(OrderType()==OP_BUY)l++;
         if(OrderType()==OP_SELL)s++;
        }
     }

   countLong=l;countShort=s;

  }

Then get those reference values by external variables, like:

  // Call
  int Long,Short;
  CountOrder(Long,Short);
  
if(Long<1 || Short<1){OrderSend(Long or Short);}
if(Long>0 || Short>0){OrderClose(opposite);}

Regards.

 
Mehrdad Jeddi:
int CountOrder(ENUM_ORDER_TYPE type)

No such enumeration documented in MT4 (except in EnumToString example.)

Values are not compatible with history types.

EnumToString - Conversion Functions - MQL4 Reference
EnumToString - Conversion Functions - MQL4 Reference
  • docs.mql4.com
//| Script program start function                                    |
 
William Roeder:

No such enumeration documented in MT4 (except in EnumToString example.)

Yes,But we can use it without any compilation error and runtime error,

So this can be changed to this :

int CountOrder(int type)
 
David Diez:

(Hello), how to structure a function call to return two different variables?

Example:

You can easily accomplish this using structs. 

struct PositionCount { int longs, shorts; };

PositionCount countPositions() {
   PositionCount x = {0};
   for (int i=OrdersTotal()-1; i>=0; --i) 
      if (OrderSelect(i, SELECT_BY_POS)) 
         if (OrderType() == OP_BUY)
            x.longs++;
         else if (OrderType() == OP_SELL)
            x.shorts++;
   return x;  
}

void OnStart() {
   PositionCount pos = countPositions();
   printf("longs=%d, shorts=%d", pos.longs, pos.shorts);
}
 
Mehrdad Jeddi:

Hi,

So you can do as this (You can't return more than one value from any functions):

Then call the function by the type of each order, like:

Or you can reference two inputs of the function to be had two returns, like :

Then get those reference values by external variables, like:

Regards.

Think the first example is the more seemless to which I was looking for.
 
David Diez :

(Hola), ¿cómo estructurar una llamada de función para devolver dos variables diferentes?

Ejemplo:

Hola David Diez: veo que te va el tema de programación, tengo un ea que encontré en la red mq4, en BT me da un 99,90 de acierto,habría que configurar lo, porque solo trabaja en sell, y agregarle algunas cosas para que sea más completo, te interesa?, 

 
votija.63:

Hello David Diez: I see that you have the theme of programming, I have an ea that I found on the network mq4, BT gives me a 99.90 of success, I would have to configure it, because it only works in sell, and add some things to that is more complete, are you interested?

This is an English language forum.

Please only post in English.

Use the site's translation tool if necessary.

Reason: