Include .ex4 library problem (using by ref variables)

 
Hello all,


Im kind of new with mql4 language, and I'm facing a weird problem (weird to me at least :) ). I have a custom script, with the following function :

void valuate_operation(double currency_spread, double lot_size, double take_profit, double stop_loss, int current_tick, int operation_control_result, 
                       int& n_trade, double& equity_value, double& current_trade_value, double& start_trade_value, bool& long_position, bool& short_position)
{
    // here comes my code
}

Its working pretty well in the script, but as this script is going to be long (and I want the function to be callable later, with other scripts, EA or anything) I'd liked to add it to a library. So i transfered it entirely, in an existing library I had (with only one function - operation_control(....), working properly with my script). So im calling it from the script with the following code :

//+------------------------------------------------------------------+
//|                                     ADX_strategy_data_export.mq4 |
//|                                                    Samuel Guedon |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Samuel Guedon"
#property link      ""
#import"ADX library.ex4" 
   int operation_control(string symbol_name, int adx_minimum_value, int back_test_length, int timeframe_name, int period_name, int current_tick, int d_minimum_delta, 
                         int adx_maximum_drop, bool allow_test_average, bool allow_test_adx_minimum);
   void valuate_operation(double currency_spread, double lot_size, double take_profit, double stop_loss, int current_tick, int operation_control_result, 
                         int& n_trade, double& equity_value, double& current_trade_value, double& start_trade_value, bool& long_position, bool& short_position);
#import
//+------------------------------------------------------------------+
//| Main function                                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
...................

After doing that, valuate_operation seems not to transfer anymore my variables to my script : once in the library, it properly calculate my equity_value or other variable (tested with a Print(equity_value) in the library), but it just doesn't transfer them to the script. I guess its a problem with using by ref variables as the code is working both in the script and the library. But as i've never worked with libraries.... :)


If you someone can help, you'd make my day :)

 

I think it would be pretty hard to help u with the code you provided

 

I know its hard (hope not impossible though) to help me without seeing the code, but as it moved from working to not working just by transferring it to a library from a script and adding the void valuate_operation(...) line in my #import, I don't see the point in copying hundreds of code lines.

valuate_operation is working properly when placed directly in the script, and in the library, it also calculates my data, but it just doesn't transfer anything to the script (i.e. long_position is 'false' when transfered to the library, when the correct parameters are met, it becomes true in the library (tested with a Print(long_position);, operate the calculation of my new position, ... but when it returns to the script, its still false, my equity hasn't moved, ...... no parameters have been transfered). I've only moved the function from the script to the library, and haven't changed anything else (should I?).

If it can help, i'm not using the same exact label for my variable (i.e. "equity_value" from my function in the library is "equity" in the start() function) but it didn't have caused trouble when everything was in the script, and it doesn't make operation_control (the other function in my library) to go wild (this function is using return(....) instead of by ref, and works well). Also, valuate_operation is not calling any other function.

So to make simple questions (but plenty of them) :

- should I use something else than #import to call my library's functions?

- Does passing by ref variable may cause trouble in a library ?

- When I use the #import and then reference the function, does the parameter names have to be like in the library or like in the script (not a problem for operation_control so far, but why not?) ?

- should i reference the library directly in the start() function instead of outside, at the header of my script? (and if so, how?)

- should the parameters have the exact same names (or none the same)?

- is there special things to do in a library to have data sent back through by ref, that is not needed in a simple function inside a script ?

 

if it can help too, here is a test I made, so here is what my library look like, maybe its just in the way i've declared my function as equity_value which is 1000 when sent remains 1000 after :


//+------------------------------------------------------------------+
//|                                                  ADX library.mq4 |
//|                                                    Samuel Guedon |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Samuel Guedon"
#property link      ""
#property library

//+------------------------------------------------------------------+
//| buy / sell / stop control                                        |
//+------------------------------------------------------------------+

int operation_control(string symbol_name, int adx_minimum_value, int back_test_length, int timeframe_name, int period_value, int current_tick, int d_minimum_delta, int adx_maximum_drop, 
         bool allow_test_average, bool allow_test_adx_minimum)
  {

       (function that work perfectly, so not shown)

  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| valuate deal, open & close deals, update equity_value            |
//+------------------------------------------------------------------+

void valuate_operation(double currency_spread, double lot_size, double take_profit, 
         double stop_loss, int current_tick, int operation_control_result, int& n_trade,
         double& equity_value, double& current_trade_value, double& start_trade_value, bool& long_position, bool& short_position)
  {
//----
   equity_value=0;
//----
  }
//+------------------------------------------------------------------+
 

the following var. r not arrays ?

int& n_trade,
double& equity_value, double& current_trade_value, double& start_trade_value, bool& long_position, bool& short_position)
 

Nope... I wish I could use arrays :) Here is how I'm calling the function in the script :

      valuate_operation(spread, lot_size, tp, sl, cur_tick, op_c, n_trade, equity_value, current_trade_value, start_trade_value, long_position, short_position);
 

Here is the kind of result I have when the function is in the script :

and here is the same run, with the function in the library


 

You can only pass arrays by ref to libraries

void valuate_operation(double currency_spread, double lot_size, double take_profit, 
         double stop_loss, int current_tick, int operation_control_result, int& n_trade[],
         double& equity_value[], double& current_trade_value[], double& start_trade_value[], bool& long_position[], bool& short_position[])
  {
//----
   equity_value[0]=0;
//----
  }

Obviously you would use only one array for your ints and one for your doubles, but you get the point.

 

ty qjol & fxcourt. Didn't know that. Indeed it's gonna shorten a bit my variable declaration in the function name :) I'll test tonight.

 

Hi Guys,

I am facing the same problem. I have concluded that its not possible to use reference variables in libraries. Only referencing for arrays can be possible for in libraries.

This is weird. I just hope this is not a mt4 bug. Can anybody comment on the same if they have faced the same issue.

 
mustafa52:

Hi Guys,

I am facing the same problem. I have concluded that its not possible to use reference variables in libraries. Only referencing for arrays can be possible for in libraries.

This is weird. I just hope this is not a mt4 bug. Can anybody comment on the same if they have faced the same issue.

Well this older post agree with you

https://www.mql5.com/en/forum/114128

Reason: