How to return an array from a function in MQL4?
Asked Answered
B

1

7

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!

Breastpin answered 16/4, 2018 at 5:53 Comment(0)
K
14

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;
}
Kelsiekelso answered 16/4, 2018 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.