DLL-Problem: Using the results of a function in another one

 

Hi! It's me again.

I recently opened a thread concerning the problems I had with my version of GetRatesItemValue. But now there is some new strange trouble. (I open this thread because no one wrote a comment in the old one.)

At the moment I'm trying to code functions in the dll which have access to the results of Kursdatenzeiger (GetRatesItemValue).

When I am doing this, the same problem occurs like I had before with GetRatesItemValue when forgetting the #pragma pack which defines the data types of the elements of the array. That means the function returns no useful results. Probably because it does not consider the different types in the array.

In a handbook I've read that the "const"-declaration of the parameters can cause serious problem while parameter passing. So I just removed it but that's not the solution.

The direct access with e.g. "kursdaten[baranzahl-1-shift].close" also does not function.


    int __stdcall Funktionen::E_Open(KursInfo* kursdaten, int baranzahl)
    {
        if (MovingAvg(13,2,kursdaten, baranzahl)<MovingAvg(55,2,kursdaten, baranzahl) && MovingAvg(13,1,kursdaten, baranzahl)>MovingAvg(55,1,kursdaten, baranzahl)){
            return(1);
        }
        if ( MovingAvg(13,2,kursdaten, baranzahl)>MovingAvg(55,2,kursdaten, baranzahl) && MovingAvg(13,1,kursdaten, baranzahl)<MovingAvg(55,1,kursdaten, baranzahl)) {
            return(-1);
        }

        return(2);

    }
        double __stdcall Funktionen::MovingAvg(int period, int shift, KursInfo* kursdaten, int baranzahl)
        {
                double summe=0;
                for (int i=period+shift-1; i<=shift; i--){
                        double close=Kursdatenzeiger(kursdaten, baranzahl,4,1);
                        summe+=close;
                }
                double ergebnis= summe/period;
                return (ergebnis);
        }
        double  __stdcall Funktionen::Kursdatenzeiger(const KursInfo* kursdaten, const int baranzahl, const int auswahl, const int shift)
          {
        //----
           if(kursdaten==NULL)
                 {
                  printf("FEHLER: Leerer Array!\n");
                  return(1);
                 }
        //----
           if(baranzahl<=0)
                 {
                  printf("Fehler: Falsche Baranzahl! (%d)\n", baranzahl);
                  return(2);
                 }
        //----
           if(shift<0 || shift>=baranzahl)
                 {
                  printf("Fehler: Falscher Shift (%d)\n", shift);
                  return(3);
                 }
        //----
           if(auswahl<0 || auswahl>5)
                 {
                  printf("Fehler: Falscher Kursartindex (%d)\n", auswahl);
                  return(4);
                 }
        //----
           int Zeiger=baranzahl-1-shift;
           switch(auswahl)
                 {
                  case 0: return double(kursdaten[Zeiger].ctm);
                  case 1: return kursdaten[Zeiger].open;
                  case 2: return kursdaten[Zeiger].low;
                  case 3: return kursdaten[Zeiger].high;
                  case 4: return kursdaten[Zeiger].close;
                  case 5: return kursdaten[Zeiger].vol;
                 }
        //----
           return(5);
  }     

Thanks for your help!

 
vostro1500:

Hi! It's me again. [...]

You need to be specific about where the problem is. You imply - but don't say for certain - that the Kursdatenzeiger() function works correctly, but that MovingAvg() and E_Open() do not work correctly.

If so, the problem is likely to be this line in MovingAvg():

for (int i=period+shift-1; i<=shift; i--)

It looks as though the condition should be i>=shift, not i<=shift. The loop will only execute at all if period <= 1.

 

First of all thanks for the correction of the mistake in the for-loop.

But thats not the reason of the problem.

You asked whether Kursdatenzeiger works. Well that simply depends. So if I call Kursdatenzeiger in the MQL4 and prints its results it works fine. BUT if I call it in MovingAvg and return simply the data Kursdatenzeiger gives into MovingAvg anp print this in MT4, MetaTrader just prints crap (e.g. 1842896).

So it seems as if the problem is -again- connected to the definition of the data types in the array.

Actually I cannot really imagine how Kursdatenzeiger can work when it is called in the MQL4-Code but not when it is called from another function in the same dll.

 
vostro1500:

But thats not the reason of the problem. [...]

I can't get the code to produce nonsense values. Everything seems to work correctly. For example, the return value from MovingAvg(period, shift, kurse, Bars) is the same as MT4's own iMA(NULL, 0, period, 0, MODE_SMA, PRICE_CLOSE, shift)

[ ...But only if I correct the line in MovingAvg() which is passing a fixed shift parameter to Kursdatenzeiger(). In other words, changing "double close=Kursdatenzeiger(kursdaten, baranzahl,4,1);" to "double close=Kursdatenzeiger(kursdaten, baranzahl, i, 4);". Otherwise, what I get is, of course, Open[4]. ]

 
vostro1500:

[...] MetaTrader just prints crap (e.g. 1842896).

...The only way I can get that kind of output is if, in the MQ4 code, I incorrectly define MovingAvg() as returning an int rather than a double. Your import of MovingAvg() definitely looks the following, yes?

double MovingAvg(int, int, double rates[], int);
 

Yep you're right. After you've said that the mistake is not in the dll, I found it in the mqh file...

Thanks for your help!

(When the next problem occurs I'm going to check my code rather one time more than one less...)

Reason: