Download MetaTrader 5

share the method of creating class, member

To add comments, please log in or register
LEE Chun Wing
LEE Chun Wing  

that is a revision of me to how to creating class :

class CTrade
   MqlTradeRequest   m_request;         // request data
   MqlTradeResult    m_result;          // result data
   ulong             m_magic;           // expert magic number
   ulong             m_deviation;       // deviation default

  public:               CTrade(); // what is the aim of this line??

   bool              PositionClose(const string symbol,ulong deviation=ULONG_MAX);

bool CTrade::PositionClose(const string symbol,ulong deviation)   {...} // declare PositionClose() is a memeber of class CTrade , and define the job of PositionClose();

CTrade trader;   //declarfe trader as a object of class CTrade 

int check_Exit()

  position_bar_name = define_Pos_bar_name();
  if (position_bar_name >= 2)  trader.PositionClose(symbol,deviation); // declare PositionClose() is under object trader, and trader is under class CTrade  

I turn out if you want to clear the sequence of program, parameter still needed in OOP program 

What is the aim/job at line 9 ?


Create Your Own Trading Robot in 6 Steps!
  • 2012.03.22
  • MetaQuotes Software Corp.
If you don't know how trade classes are constructed, and are scared of the words "Object Oriented Programming", then this article is for you. In fact, you do not need to know the details to write your own module of trading signals. Just follow some simple rules. All the rest will be done by the MQL5 Wizard, and you will get a ready-to-use trading robot!
Rashid Umarov
Rashid Umarov  

This is constructor, see Structures and Classes

  public:               CTrade(); // what is the aim of this line??

Constructors and Destructors

A constructor is a special function, which is called automatically when an object of structure or class type is created, and is typically used to initialize class members. Further we will discuss only classes, all the discussed can be applied to structures also, unless otherwise specified. The name of the constructor must match with the name of the class. The constructor has no return type (you can specify the type of void). In MQL5 constructors do not have any input parameters. Thus, each class can have only one constructor.

Class members: strings, dynamic arrays and objects that require initialization will be initialized anyway regardless of the constructor presence or absence.

A destructor is a special function that is called automatically when a class object is destroyed. The name of the destructor is written as a class name with a tilde (~). Strings, dynamic arrays and objects, requiring deinitialization, will be de-initialized anyway, regardless of the destructor presence or absence. If there is a destructor, these actions will be performed after calling the destructor.

Destructors are always virtual, regardless of whether they are declared with the virtual keyword or not.

You should read article The Order of Object Creation and Destruction in MQL5
To add comments, please log in or register