The EOP for schoolchildren. - page 18

 
Alexey Viktorov:

Yes? Doesn't it create a new, independent object?

Let's check now - I'll insert lines like this:

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CTrade::CTrade(void) : m_async_mode(false),
   m_magic(0),
   m_deviation(10),
   m_type_filling(ORDER_FILLING_FOK),
   m_log_level(LOG_LEVEL_ERRORS)

  {
   SetMarginMode();
//--- initialize protected data
   ClearStructures();
//--- check programm mode
   if(MQL5InfoInteger(MQL5_TESTING))
      m_log_level=LOG_LEVEL_ALL;
   if(MQL5InfoInteger(MQL5_OPTIMIZATION))
      m_log_level=LOG_LEVEL_NO;
//---
   Print(__FUNCTION__,", magic ",IntegerToString(m_magic));
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CTrade::~CTrade(void)
  {
//---
   Print(__FUNCTION__,", magic ",IntegerToString(m_magic));
  }

as well as

//+------------------------------------------------------------------+
//| Buy operation                                                    |
//+------------------------------------------------------------------+
bool CTrade::Buy(const double volume,const string symbol=NULL,double price=0.0,const double sl=0.0,const double tp=0.0,const string comment="")
  {
//---
   Print(__FUNCTION__,", magic ",IntegerToString(m_magic));


and a test script:

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade trade;   // Объект № 1 в глобальной области программы
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   trade.SetExpertMagicNumber(123);
//---
   CTrade trade;
   trade.Buy(0.1);
  }
//+------------------------------------------------------------------+


Result:

2019.10.09 11:25:44.645 Test_ru (EURUSD,H1)     CTrade::CTrade, magic 0
2019.10.09 11:25:44.645 Test_ru (EURUSD,H1)     CTrade::CTrade, magic 0
2019.10.09 11:25:44.645 Test_ru (EURUSD,H1)     CTrade::Buy, magic 0
2019.10.09 11:25:44.645 Test_ru (EURUSD,H1)     CTrade::OrderSend: instant buy 0.10 EURUSD at 1.09862 [auto trading disabled by client]
2019.10.09 11:25:44.645 Test_ru (EURUSD,H1)     CTrade::~CTrade, magic 0
2019.10.09 11:25:44.645 Test_ru (EURUSD,H1)     CTrade::~CTrade, magic 123


One object is created, then a SECOND one (it turns out there is no recreation - a new one is created), BUY position is opened with magic "0" - i.e. the second object "trade" opened it.

Then we see that the two objects are destroyed in reverse order: first the second one (with magic "0") and then the first one (with magic "123").

 
Alexey Viktorov:

Yes? Doesn't it create a new, independent object?

Yes, it will create a new instance of theCTrade class

andeven more, CTrade destructor will be calledat exit from void OnTick() and at the next OnTick() CTrade constructor will be called again

 
Igor Makanu:

yes, there will be a new instance of theCTrade class

If the void OnTick() is exited, the CTrade destructor will be called, and the next OnTick() will call the CTrade constructor again.

Yes I realised that immediately after Artem's answer.

Forum on trading, automated trading systems and strategy testing

OOP for Schoolchildren.

Artyom Trishkin, 2019.10.08 20:23

It's exactly the same as with variables.

And the focus:

#include <Trade\Trade.mqh>
CTrade trade;   // Объект trade в глобальной области программы

int OnInit()
{
 trade.SetExpertMagicNumber(123);
}

void OnTick()
{
 CTrade *trade_ptr=GetPointer(trade);  // Указатель на объект trade
 trade_ptr.Buy(0.1);
}

 
Alexey Viktorov:

In my opinion, mql has a very narrow set of tasks that need to be solved through OOP. The language itself seems to me to be nothing more than an OOP in C++ or something. And this OOP is offered in the form of a standard library. And to this OOP it is suggested to add, otherwise I wouldn't say, another OOP. And then another step... Rightly said Warlock, though angry, but benevolent, for my tasks OOP is like a dog turntable. And what good is setting a problem and its subsequent realization by means of OOP if this problem without problems can be solved in procedural style.

Here for example to take .mqh from fxsaber`a to write codes for MT5 as well as for MT4. Maybe someone needs it, but look who. Those who do not want or absolutely cannot master mql5. Or take iCanvas from Nikolay ... I forget his last name. It seems to be a useful library, but it is not easy to understand it, and there is no documentation, even a smallest description. It's not a complaint, sorry Nikolay, it's a fact. So, when I decided to try writing a graphical label, it was easier to write it without reference to either standard library or Nikolai's library.

Actually, Alexey, you can get enough information about iCanvas from the description page in KB.https://www.mql5.com/ru/code/22164. You need to study the comments in the code carefully. The meaning of all the functions is intuitive from their names.

   double            X(double bar);        // The X coordinate by the bar number. The bar number must be of type double, otherwise, the bar will be interpreted as time.
   double            X(datetime Time);     // The X coordinate by the time.
   double            Y(double Price);      // The Y coordinate by the price.
   double            Price(int y);         // Price by the Y coordinate
   double            Bar(int x);           // bar number by coordinate X                                                                      
   datetime          TimePos(int x);       // time by coordinate X 
   double            Close(int x);     
   double            Open(int x);    
   double            High(int x);     
   double            Low(int x);     
 
   void              Comm(string text);                 // Print comment
   void              TextPosition(int x,int y);         // Setting the XY position for comment output in pixels
   void              TextPosition(double x,double y);   // Setting the XY position for outputting comments as a percentage of the width and height of the window
   void              CurentFont(string FontName="Courier New",int size=18,int LineStep=20,color clr=clrDarkOrchid,double transp=1.0);  // Set font options for comment. LineStep - step between lines. transp - transparency from 0 to 1
   void              LineD(double x1,double y1,double x2,double y2,const uint clr); // тоже самое что и Line в CCanvas, только без глюков и с double координатами
   int               TextPosX;      // Position X for comment text
   int               TextPosY;      // Position Y for comment text
   int               StepTextLine;  // line spacing for comment
   uint              TextColor;     // Font color for comment

But you might be right. I should record a video of writing some simple but useful indicator using iCanvas and explain visually some features of this class. There are still some subtleties.

Frankly speaking, I do not like how iCanvas is written right now. I want to crumple it up, throw it in the trash and rewrite it in a more correct style.

70% of the code in this library is about speeding up some of the regular functions that are needed to handle coordinates and data access.

So, if you apply this class, the graphics will be faster than if you do it "head-on" with CCanvas. That's its main feature and advantage. And, of course, it is really easier to form graphs, figures, comments.

I would also like to add, Alexey, that the purpose of many publications in CodeBase, including fxsaber and my publications is an unselfish "just to share - who needs it, will understand it". So often spending time on detailed instructions is simply a bummer.

Especially in the forum, I have posted many short and illustrative examples using iCanvas.
Here, for example:
https://www.mql5.com/ru/code/25929

https://www.mql5.com/ru/code/25113

https://www.mql5.com/ru/code/25414

https://www.mql5.com/en/code/24798

https://www.mql5.com/ru/forum/227736/page41#comment_13259627

https://www.mql5.com/ru/forum/227736/page24#comment_12836622

https://www.mql5.com/en/forum/323629#comment_13442470

https://www.mql5.com/ru/forum/321704#comment_13131995

https://www.mql5.com/ru/forum/317257/page3#comment_12341593

https://www.mql5.com/en/forum/229521/page2#comment_10258148


Easy Canvas
Easy Canvas
  • www.mql5.com
Данная библиотека и класс iCanvas упростит написание программ с применением Canvas. Вот пример простого индикатора с применением данной библиотеки и его демонстрация. Обратите внимание, что в данном примере в теле индикатора отсутствует функция обработки событий OnChartEvent. Но она также может и присутствовать. Особенности данной библиотеки...
 
Nikolai Semko:

Actually, Alexey, you can get enough information about iCanvas from the KB description page. https://www.mql5.com/ru/code/22164 You have to study the comments in the code carefully. The meaning of all functions is intuitively clear from their names.

Nikolai, don't pay any attention to my words. There are so few people like me among those who study programming, if I'm not the only one, that it's not worth paying attention to them at all.

For me to learn comments in code, you first have to learn Aglitsky)).

If I had an urgent need I could spend time and translate all comments, try to understand machine translation and make corrections. But there is no such need.

 
Alexey Viktorov:

For example, take .mqh from fxsaber`a to write codes for MT5 as well as for MT4. Maybe someone needs it, but look who... Those who do not want to or absolutely cannot master mql5.

I think I know MQL5 pretty well, but I have a need for a trading library. I am not talking about MT4-style, but about a trading library for MT5, which would be comfortable and fast to work with. And this library has no bugs during real trading.

Since such a library wasn't available (and I don't know if it exists) in the public domain, I had to develop my own one. For this purpose I had to decide, what kind of a tool in the form of a setof trading functions I was going to invent. Borrowed the MT4-set, as a pretty good one. Which, as a bonus, made it possible not to write the documentation to the library and to study it. And other goodies.

So knowledge of MQL5 is sometimes at a high level among users.

Reason: