Download MetaTrader 5
To add comments, please log in or register
Interested in MQL5 features? Find them in documentation!
franzk
70
franzk 2015.07.24 14:22 

Hi,

I have a problem passing the current MqlRates to a C++ DLL. Here is what I have done so far:

MQL

#import "MQL.dll"
double ihigh(MqlRates&[],int);
#import

MqlRates rates[];
bool called = false;

void OnTick()
{
   if(called == false)
   {
   ArrayCopyRates(rates,NULL,0);
   called = true;
   }
Comment(DoubleToStr(ihigh(rates,1)),5);
}

C++

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

_DLLAPI double __stdcall ihigh(RateInfo* rate, int i)
{
        return rate[i].high;
}


I would expect the DLL to have the current MqlRates for calculation. The values returned by the dll are totally different of the actual mqlrates however. Any hints on what I am doing wrong?

Best Regards

whroeder1
13656
whroeder1 2015.07.24 15:11  
  1.  if(called == false)
       {
       ArrayCopyRates(rates,NULL,0);
       called = true;
       }
    ACR you could do in OnInit and drop the rest.
  2. if(called == false)
    You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence. "if called" is incomplete. if isFirstTick or if needACR are not.
  3. In MT4 rates[0] is the current value (a series array,) in the DLL rates[0] is the oldest.
franzk
70
franzk 2015.07.24 17:23  
WHRoeder:
  1. ACR you could do in OnInit and drop the rest.
  2. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence. "if called" is incomplete. if isFirstTick or if needACR are not.
  3. In MT4 rates[0] is the current value (a series array,) in the DLL rates[0] is the oldest.


thank you for your input. Strangely the DLL only returns correct values for rates[0] (oldest MqlRates). All other entries (rates[1,x]) return some "random" numbers which are obviously not random but just don't make sense to me.

Here is the updated code

MQL

int OnInit()
{
ArrayCopyRates(rates,NULL,0);

   int i = 0;
      while(i <= 5)
      {
      Print(StringConcatenate(
      "mql: ",iHigh(NULL,0,ArraySize(rates)-i-1),
      " | c++: ",ihigh(rates,ArraySize(rates)+i,ArraySize(rates))
      ));
      i++;
      }

return(INIT_SUCCEEDED);
}

C++

_DLLAPI double __stdcall ihigh(RateInfo* rate, int pos, int size)
{
        return rate[(size - pos)].high;
}

Output:

mql: 0.73693 | c++: -3.967731946867355e-270
mql: 0.73693 | c++: 0
mql: 0.7370600000000001 | c++: 5.896060841714434e-316
mql: 0.73714 | c++: 0
mql: 0.73716 | c++: 0
mql: 0.73717 | c++: 0.73717
/
To add comments, please log in or register