Custom Function Parameters

Forex Trader
114545
Forex Trader  
Hi,

I think this was working prior to the last update, but I really need to understand what is possible when declaring functions.

 bool MyFunc(double& dRtn, double& dPerc)
{
    //do something and set dRtn, dPrec variables
    dRtn = 10;
    dPerc = 20;
    return (true)
}


So I expect that when this is declared by defining the variable type with & is means the address of the variable enabling to the ability to set its values ie.

double d1, d2;
If (MyFunc(d1,d2)
{
    Print(d1, d2);
}



so the values for d1 would be 10 and d2 would be 20. But when I do this both d1 & d2 are equal to 0

I know it works for passing arrays as I am currently doing this.

Is this a bug or is it just not possible?

Pedro

Forex Trader
114545
Forex Trader  
please expose exact source! following program works properly
int start()
  {
   double d1, d2;
   if (MyFunc(d1,d2))
      Print(d1, d2);
  }
bool MyFunc(double& dRtn, double& dPerc)
  {
   dRtn = 10;
   dPerc = 20;
   return (true);
  }
Forex Trader
114545
Forex Trader  
int start()
  {
   double d1=1, d2=2, b[6];
   
   b[0] = 10000;
   b[1] = 9000;
   b[2] = 8500;
   b[3] = 11000;
   b[4] = 8000;
   b[5] = 12000;
   
   MaxDrawDown(b, d1, d2);
   Print("TestDD: ", d1 , " - ", d2);
}
//+------------------------------------------------------------------+
double MaxDrawDown(double dVal[], double& dRtn, double& dPerc)
{
   int nSize = ArraySize(dVal);
   int i;
   double diff, test;
   dRtn = 0;
   
   for (i=0; i < (nSize-1); i++)
   {
      test = dVal[i+1] - dVal[i];
      if (test <= 0) diff = diff + test;
      else if (dRtn > diff) 
      {
         dRtn = diff;
         dPerc = Div(MathAbs(diff),dVal[i]+MathAbs(diff))*100;
         diff = 0;  
      }
      else
      {
         diff = 0;  
      }
      Print("Diff: ", diff);
   }
   dRtn = MathAbs(dRtn);
   Print("MaxDrawDown: ", dRtn, " : ", dPerc);
   return (dRtn);
  
}



Ok the above code works as intended.

What I have done is created a library file and put the MaxDrawdown function in it. Then create a library include file which imports the library.ex4 file and exposes the function definition.

Then in the script I include the library include file.

Doing it this way the function does not return the correct values

Your help on this is greatly appreciated.

Pedro

Forex Trader
114545
Forex Trader  
works properly
16:09:06 Expert test: loaded successfully
16:09:06 Expert 'test' (,) : Diff: -1000
16:09:06 Expert 'test' (,) : Diff: -1500
16:09:06 Expert 'test' (,) : Diff: 0
16:09:06 Expert 'test' (,) : Diff: -3000
16:09:06 Expert 'test' (,) : Diff: 0
16:09:06 Expert 'test' (,) : MaxDrawDown: 3000 : 27.2727
16:09:06 Expert 'test' (,) : TestDD: 3000 - 27.2727
16:09:06 Expert 'test' (,) : removed


what the build do You use?

Forex Trader
114545
Forex Trader  
Hi Slawa,

Can I confirm that you created a library with the MaxDrawDown function in it. Created a Include file importing the library and having the definition of MaxDrawDown. Then creating a script including the library include file and calling MaxDrawDown from their. When I do this it fails. The function works correctly but the variables passed into the function when returned are as their original values.

I am using Build 171

Thanks
Forex Trader
114545
Forex Trader  
references are not allowed in the library functions. for parameter referencing use arrays. for instance
double MaxDrawDown(double dVal[], double& TwoItemsArray[])
{
   double dRtn, dPerc;
...
   TwoItemsArray[0]=dRtn;
   TwoItemsArray[1]=dPerc;
   return (dRtn);
}
Forex Trader
114545
Forex Trader  
Thanks,

Is there any plans to make this available in library functions?

Pedro
Forex Trader
114545
Forex Trader  
yes. we are redesigning our compiler now. it will be ready in 2-3 month time
Forex Trader
114545
Forex Trader  
Thanks Slawa,

Great news. Keep up the good work.

Pedro