Formal Parameters by Reference - How do you make it work?

 

I'm new to this. Does anyone know how to call mql4 functions and pass parameters by reference. I've searched for hours now ...

I've also tried one dimensional arrays (as described for external dll calls.) How do you do this ???

Thanks in advance.

// Function returns total of open orders: Open Bought, Sold and Average price by reference ...

int OpenOrdersCount(int magicNumber, double& openBoughtLots, double& openSoldLots, double& openAveragePrice) {

// Loop and Set formal parameters in loop ...

return(li_nOrders);

}

// Call the Function ....

double ld_OpenBought = 0;

double ld_OpenSold = 0;

double ld_AveragePrice = 0;

int li_TotalOpen = OpenOrdersCount(MagicNumber, ld_OpenBought,ld_OpenSold, ld_AveragePrice );

// li_TotalOpen is > 0, but all ref parameters are zero.

 

have'nt looked at your source code.

please use the SRC button

 

I made a sample for your assistance.

//+------------------------------------------------------------------+
//|                                                     SourceEA.mq4 |
//|                                           Copyright © 2010, CSSI |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, CSSI"
#property link      "http://www.metaquotes.net"


#import "SourceLib.ex4"
    int OpenOrdersCount(int magicNumber, double& openBoughtLots, double& openSoldLots, double& openAveragePrice);

double _OpenBoughtLots = 0;
double _OpenSoldLots = 0;
double _OpenAveragePrice = 0;
int MagicNumber = 100729;
bool _Ordered = false;
int _StopAfter = 20;

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
    int li_OpenOrders = 0;
    
    if(!_Ordered)
    {
        // Just Sell 10 ...
        _Ordered = (OrderSend(Symbol(), OP_SELL, 10, Bid, 3, 0,0, "GO SHORT 10", MagicNumber) > 0);       
    }
    
    if (_StopAfter > 0 )
    {
        /******************* ARRAY DOES NOT WORK (won't compile) *****************/
        //double lda_OpenBought[1] = {0};
        //double lda_OpenSold[1] = {0};
        //double lda_AveragePrice[1] = {0};    
        // Check For Open Position(s)
        //li_OpenOrders = OpenOrdersCount(MagicNumber, lda_OpenBought, lda_OpenSold, lda_AveragePrice);
        //_OpenBoughtLots = lda_OpenBought[0];
        //_OpenSoldLots = lda_OpenSold[0];
        //_OpenAveragePrice = lda_AveragePrice[0];


        /******************* VARIABLE DOES NOT WORK (no reference, asterisk invalid) *****************/
        _OpenBoughtLots = 1;
        _OpenSoldLots = 2; 
        // Check For Open Position(s)
        li_OpenOrders = OpenOrdersCount(MagicNumber, _OpenBoughtLots , _OpenSoldLots, _OpenAveragePrice);

        // With 10 sold it prints 1 and 2 below ...

        Print("..... Returned Open Bought " + DoubleToStr(_OpenBoughtLots, 2) + ", Open Sold " + DoubleToStr(_OpenSoldLots, 2) + ".");
        _StopAfter--;
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Journal:

2010.07.29 15:44:29 SourceEA EURUSD,M5: ..... Returned Open Bought 1.00, Open Sold 2.00.

2010.07.29 15:45:25 SourceLib EURUSD,M5: Updated Open Bought 0.00, Open Sold 10.00...

2010.07.29 15:44:56 SourceLib EURUSD,M5: Passed Open Bought 1.00, Open Sold 2.00...

2010.07.29 15:40:18 SourceEA EURUSD,M1: open #130693816 sell 10.00 EURUSD at 1.30753 ok

Files:
sourcelib.mq4  4 kb
 
CZAZ:

I'm new to this. Does anyone know how to call mql4 functions and pass parameters by reference. I've searched for hours now ...

I've also tried one dimensional arrays (as described for external dll calls.) How do you do this ???

Thanks in advance.

// Function returns total of open orders: Open Bought, Sold and Average price by reference ...

int OpenOrdersCount(int magicNumber, double& openBoughtLots, double& openSoldLots, double& openAveragePrice) {

// Loop and Set formal parameters in loop ...

return(li_nOrders);

}

// Call the Function ....

double ld_OpenBought = 0;

double ld_OpenSold = 0;

double ld_AveragePrice = 0;

int li_TotalOpen = OpenOrdersCount(MagicNumber, ld_OpenBought,ld_OpenSold, ld_AveragePrice );

// li_TotalOpen is > 0, but all ref parameters are zero.

use the ampersand & syntax. in this way the address only is passed in so function works on caller's memory..

ref: https://docs.mql4.com/basis/variables/formal

same for []'s too. eg,

double dBigVal;
int iaData[100];
string saMessages[20];

vExample(dBigVal,iaData,saMessages);

void vExample (double& dBigVal, int& iaData[], string& saMessages[])

{

...

dBigVal = 0xefffffff;

...

}

.

see also ref: https://docs.mql4.com/basis/preprosessor/import

Reason: