Download MetaTrader 5

Problem sending rates to a DLL

To add comments, please log in or register
Subscribe to signals right in MetaTrader. It is easy!
Guillermo 2016.01.28 10:26 


I'm writing this since I'm having problems sending the rates to an external DLL.

I've tried the two different methods using ArrayCopyRates

- The first thing I've tried, and it has worked, is working with rates defined as "double rates[][6];". The problem with this method is that it gets really slow on backtesting after some iterations. I guess this is because copying real data into memory becomes slower the more data that needs to be copied (more bars as time goes on). Declaring it as an array of doubles gets the correct results in the DLL, but the program takes such a long time to run, even without doing nothing apart from copying the data to the array and not sending it to the DLL. Is it normal? My plan is to send all bars info to the DLL after a bar has been completed. Plus, with this method I don't get to send the Spread and Real Volume data, do I?

 - So the alternative I've tried is working with an array of type MqlRates: "MqlRates rates[];". Declaring it like that, makes the copy instant, as expected, the problem is that the output from the DLL (I'm writing to a file some elements to debug) shows the following for an entry of the array:

Time: 386302690341581928
Open: 7.79369e-283
High: 1.51315e-306
Low: 1.78019e-306
Close: 1.78017e-307
Tick: 14609821304522539008
Spread: 1073038753
Real: 3866437557884894471

 As you can see, these values are not what you would expect. Doing some research, I found that the sizeof MqlRates from Mql4 is 60 bytes, since it is making no memory padding (that's my guess, but I think its correct). Then, in the DLL, I declared the following structure for storing the data passed:

struct RateInfo
INT64             time;              // open date and time
double            open;              // Open price (absolute value)
double            high;              // Low price
double            low;               // High price
double            close;             // Close price
UINT64            tick_volume; // tick volume
INT32             spread;            // spread
UINT64            real;              // trade volume

 The size of this structure, due to memory padding, is 64 bytes, so I guess that's the reason because the values are not correctly shown, since its reading from an invalid memory position. 

I'm compiling my DLL with Microsoft Visual Studio Commynity 2015, maybe that has something to do, I'm not sure.

 Could someone give me some help please?

Thank you! 

Ex Ovo Omnia
Ex Ovo Omnia 2016.01.28 12:55  
Structures in MQL4 are always packed. Do you use the pragma directive in the DLL?
Guillermo 2016.01.28 13:15  
Structures in MQL4 are always packed. Do you use the pragma directive in the DLL?
You are my hero. It solved the problem!

#pragma pack(4)

Thank you very much! :D
To add comments, please log in or register