Discussion of article "Writing an Expert Advisor using the MQL5 Object-Oriented Programming approach"

 

New article Writing an Expert Advisor using the MQL5 Object-Oriented Programming approach is published:

This article focuses on the object oriented approach to doing what we did in the article "Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners" - creating a simple expert advisor. Most people think this is difficult, but I want to assure you that by the time you finish reading through this article, you will be able to write your own Expert Advisor which is object oriented based.

Author: Samuel

 

In general, it's not bad, but it's a bit lame in following the OOP slogan used in the name. For example, why stoploss, take and price are placed outside the class? STP and TKP should be members, we need the setSLTP method; latest_price and levels should be considered inside openBuy/openSell. They themselves are written very irrationally - it is desirable to leave only one if with MarginOK call, and inside it add the first line of the check if(Chk_Margin == 0) return(true);

And one more little thing. Why is Chk_Margin declared as int-, though it is used only as a bool? It would be better to describe it by a minimal sufficient type, otherwise there are questions when reading the source code, what will happen if Chk_Margin == 2, 3, etc.?

 
With the specified pair on the specified period with default input parameters, you can't get such a beautiful balance chart. Did the author have other quotes? Could you attach the input parameters with which such a chart was obtained?
 
capr:
With the specified pair on the specified period with default input parameters, you can't get such a beautiful balance chart. Did the author have other quotes? Could you attach the input parameters, with which such a chart was obtained?

How different should be the quotes between you and the author for the results to be very different))))))) I got almost the same, and the final result is even a little better:

 

the design is puzzling

   if(Buy_Condition_1 && Buy_Condition_2 && Buy_Condition_3 && Buy_Condition_4)
     {
       return(true);
     }
     else
     {
       return(false);
     }
 
ortv:

the design is puzzling

What exactly do you dislike it so much? Of course, I haven't seen it in the context of the code....
 
Interesting:
What exactly do you dislike it so much? Of course, I haven't seen it in the context of the code.....

isn't that how you usually write it in such cases?

return(Buy_Condition_1 && Buy_Condition_2 && Buy_Condition_3 && Buy_Condition_4);
 
ortv:

Isn't that how they usually write it in such cases?

You can do it this way, if the code is reproduced exactly and there is nothing else in the processing...
 

I don't understand the following part of the code:

// Copy the closing price of the previous bar (bar 1) to the corresponding Expert Advisor variable
   Cexpert.setCloseprice(mrate[1].close);  // bar 1 closing price
//--- Check if there is a buy position
   if (Cexpert.checkBuy()==true)
   {
      if (Buy_opened) 
         {
            Alert("We already have a position to buy!!!"); 
            return;    // Do not add to the long position
         }
      double aprice = NormalizeDouble(latest_price.ask,_Digits);
      double stl    = NormalizeDouble(latest_price.ask - STP*_Point,_Digits);
      double tkp    = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits);
      int    mdev   = 100;
      // place the order
      Cexpert.openBuy(ORDER_TYPE_BUY,Lot,aprice,stl,tkp,mdev);
   }
If we are going to open a buy position, we should focus on the latest_price.ask price, but when setting a Stop Loss and Take Profit for such a position - on the latest_price.bid price. Is this correct? Why do Stop Loss and Take Profit are set based on the ask price in the code text? Is it a misprint or a peculiarity of a particular strategy (the code has a similar construction for opening a Sell position )?
 
Yedelkin:

I don't understand the following part of the code:

If we are going to open a buy position, we should focus on the latest_price.ask price, but when setting a Stop Loss and Take Profit for such a position - on the latest_price.bid price. Is this correct? Then why do Stop Loss and Take Profit are set based on the ask price in the code text? Is it a misprint or a peculiarity of a particular strategy (the code has a similar construction for opening a Sell position)?

Here is the logic (probably, I usually use it too):

1. The opening price for Buy is considered to be Ask (and it is correct);

Let's say we open at 1.27 on EURUSD.

2. When opening a position we get the difference equal to the spread, for Buy it is Ask-Bid (potential loss if we close at the current price);

Let's assume that spred is equal to 5 pips. Thus Bid at opening will be at 1.2695 (right?).

3. Longs are closed at the Bid price (and this is quite logical).

So when Bid goes to 1.27 we get a BU on the position (right?).

4. But we also need to get a profit (100 pips in the standard quote).

That is TP should be higher than Open by 100 pips. In our case it is 1.28 (right?).

At the same time TP will be triggered if Bid (not Ask) reaches this 1.28....

5. SL also needs to be set (let's say it is 50 pips). So in our example it will be located at 1.2650 (50 pips below Open).

Under what conditions will SL work?

Logically, it should work when the price passes these very 50 pips against us (i.e. Ask in the closing coins on SL should logically be at these very 1.2650).

Where should Bid be in this case? And at this time it will be located at 1.2645 (because Spred by the condition we have 5 pips).

Remembering that Longs are fixed.

6. Now let's look at what is actually happening (let's take the market opening of a long in MQL4 as an example).

//Взято из справки по OrderSend
 ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,Bid-25*Point,Ask+25*Point);

What we see here

The opening price is considered as Ask, SL is considered as Bid, and TP is considered as Ask.

In our case we will get this pattern.

OrderSend(Symbol(),OP_BUY,1,Open = Ask,3,SL = Bid-50*Point,TP Ask+100*Point);

Let's substitute our data here:

OrderSend(Symbol(),OP_BUY,1,1.2700,3,1.2695-50*Point,TP 1.2700+100*Point);

What we get in the end - Open = 1.2700 SL= 1.2645 TP 1.28

PS

Let's compare with the original data:

Order - Open = 1.2700 SL= 1.2645 TP 1.28

Model - Open = 1.2700 SL= 1.2645 TP 1.28

 
Interesting:

There's this logic here (most likely, I tend to use it too):

1. The opening price for Buy is considered to be Ask (and it is correct);

Let's say we open at 1.27 on EURUSD

2. When opening a position we get the difference equal to the spread, for Buy it is Ask-Bid (potential loss if we close at the current price);

Let's assume that spred is equal to 5 pips. Thus Bid at opening will be at 1.2695 (right?).

5. SL should also be set (let's say it is 50 pips). So in our example it will be located at 1.2650 (50 pips below Open)

Under what conditions will SL be triggered?

Logically, it should work when the price passes these very 50 pips against us (i.e. Ask in closing coins on SL should logically be at these very 1.2650).

Where should Bid be in this case? And at this time it will be located at 1.2645 (because Spred by the condition we have 5 pips).

Remembering that Longs are fixed.

If Bid at opening will be at 1.2695, we already have 5 pips of loss automatically. If at the same time SL is 50 pips according to the developer's idea, then we have 45 more pips to go in the unfavourable direction before it triggers. I.e. when the stoploss is triggered, Bid should be not at 1.2645, but at 1.2650; and Ask, respectively, at 1.2655.