Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array from a function in MQL4?

I would like to return an array from my function, how can I do that?
Look!

int GetOrdresVente(){
    int ordrevente;
    int Tabordresvente[];
    for(int j = OrdersTotal() - 1; j >= 0 ; j--){
        if(OrderSelect( j, SELECT_BY_POS ) == true){
            if(OrderSymbol() == Symbol()){
                if(OrderType() == OP_SELL ){
                    ordrevente = OrderTicket();
                    ArrayResize( Tabordresvente, ArraySize( Tabordresvente ) + 1);
                    Tabordresvente[ArrayResize( Tabordresvente, ArraySize( Tabordresvente ) - 1 )] = ordrevente;
                }
            }
        }
    }
    return Tabordresvente;
}

Thanks for replies!

like image 696
packy Avatar asked Jul 19 '26 01:07

packy


1 Answers

Not possible. Create an array, pass it into the function, run inside the function as arrays are always passed by reference and never by value.

void OnTick(){
   int array[];
   GetOrdresVente(array);
}

void GetOrdresVente(int &array[]){
   //use counter and size of the array or CArrayInt* and do not resize every time
   int counter=0,size=OrdersTotal();
   ArrayResize(array,size);
   for(int i=OrdersTotal()-1;i>=0;i--){
       if(OrderSelect(i,SELECT_BY_POS){
          if(OrderType()==OP_SELL){
             array[counter++]=OrderTicket();
          }
       }
   }
   ArrayResize(array,counter);
   return;
}
like image 191
Daniel Kniaz Avatar answered Jul 21 '26 22:07

Daniel Kniaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!