MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal

Automated Trading and Strategy Testing Forum

XML parser Library
XML parser
Author: yu-sha
A BATTERY OUT IN ENVELOPESA BATTERY OUT IN ENVELOPES Try product
A BATTERY OUT IN ENVELOPES
Author: tol64
Screenshot
NK_JPK5U, D1
Real
MQL5 Wizard: How to Create a Risk and Money Management Module MQL5 Wizard: How to Create a Risk and Money Management... Subscribe to signal
Pogulyaev GOLD
24.60%, 8 533.60 USD

// [bug] return issue with structs!

To add comments, please log in or register

dck
68
dck 2012.07.23 18:44

Hello,

this bug took me some days to catch.
The same code executed from Onstart() works, but from a method or function does NOT.

Here is the Script code:

struct STest{
   double      a, b;
   
   void        STest(): a(0), b(0)                    {}
   void        STest(double A, double B): a(A), b(B)  {}
   void        STest(STest &s): a(s.a), b(s.b)        {}
   
   STest       SelfMod(double c)                      { a+=c; b+=c*.5; return this; }
   void        Log(string src="") const               { Print( "STest:"+ StringFormat("{%G, %G, %s}", NormalizeDouble(a,2), NormalizeDouble(b,2), src)); }
};

class CTest{
   public:
   double a, b;
   
   STest  TestMethod();
};

STest CTest::TestMethod()
{
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from CTest::TestMethod");
   result2.Log("called from CTest::TestMethod");
   
   //method chaining test:
   result.SelfMod(2).Log("called from CTest::TestMethod");
   
   return result;
};

void TestFunction()
{
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from TestFunction");
   result2.Log("called from TestFunction");
   
   //method chaining test:
   result.SelfMod(2).Log("called from TestFunction");
} 

void OnStart()
{
   // from class:
   CTest* c = new CTest();
   STest s = c.TestMethod();
   
   // from TestFunction:
   TestFunction();
   
   // from OnStart:
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from OnStart");
   result2.Log("called from OnStart");
   
   //method chaining test:
   result.SelfMod(2).Log("called from OnStart");
   
   delete(c);
};

The above code Prints:

STest:{5.1, 4.2, called from OnStart}
STest:{3.1, 3.2, called from OnStart}
STest:{3.1, 3.2, called from OnStart}
STest:{0, 0, called from TestFunction}
STest:{0, 0, called from TestFunction}
STest:{3.1, 3.2, called from TestFunction}
STest:{0, 0, called from CTest::TestMethod }
STest:{0, 0, called from CTest::TestMethod }
STest:{3.1, 3.2, called from CTest::TestMethod }

The lines with the red values (0 ,0) are wrong!
In the above code I'm forced to write a standard and copy constructor for the struct, to be able to compile the script, even thought I'm not using them. 

Hope that helps to improve mql5 and that you can fix it.
Daniel

 

 

Attached files
dck
68
dck 2012.07.23 21:08

Just added one more test.
The code placed within a struct method isn't working too:

static STest STest::TestMethod()
{
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from STest::TestMethod");
   result2.Log("called from STest::TestMethod");
   
   //method chaining test:
   result.SelfMod(2).Log("called from STest::TestMethod");
   
   return result;
};

the attached file contains the combined tests.

 

Attached files
Ilyas
704
mql5 2012.07.24 10:41
Thank you for your message. Fixed.
Please try
the new
compiler version.
Attached files
mql5.zip (1062.42 KB)
mql564.zip (1365.26 KB)
dck
68
dck 2012.07.24 16:41

Wow, that was fast, a big thank you!

IT IS WORKING NOW :-) 

/

To add comments, please log in or register