Questions on OOP (Object Oriented Programming)

 

Hi all!

I think it's time to create this thread, because there is no good OOP training anywhere, but we need and want to learn.

To litter the branch "questions from newbies" with more complicated and not understandable to newbies code examples, probably do not need.

Having read the two articles "The Basics of Object Oriented Programming andOOP in MQL5 by Example: Error and Warning Code Handling", I have written a sample code that I would like to explain in details.

I have written a sample code and I want to study it in details. I also suggest you to publish your own codes and discuss them together.

I am interested in the following:

  • Is this code correct?
  • How to improve the code?
  • How to speed up the code?
My code for opening orders:

#property strict
input int Slip=30;
input int Magic=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
class vr_trade
  {
private:
   int               openorders(string sy,int typ,double lot,double price);
   string            tip(int typ);
public:
   int               Buy(string sy,double lot);
   int               Sel(string sy,double lot);
   int               BuyLimit(string sy,double lot, double price);
   int               SelLimit(string sy,double lot, double price);
   int               BuyStop(string sy,double lot, double price);
   int               SelStop(string sy,double lot, double price);
                     vr_trade(){}
                    ~vr_trade(){}
  };
MqlTick st;
vr_trade trade;
//+------------------------------------------------------------------+
void OnTick()
  {
trade.Buy("EURUSD",0.01); // Пример открытия позиции возвращающей тиккет ордера.
  }
//+------------------------------------------------------------------+  
int vr_trade :: Buy(string sy,double lot)
{
return openorders(sy,0,lot);
}
//+------------------------------------------------------------------+  
int vr_trade :: Sel(string sy,double lot)
{
return openorders(sy,1,lot);
}
//+------------------------------------------------------------------+  
int vr_trade :: BuyLimit(string sy,double lot, double price)
{
return openorders(sy,2,lot,price);
}
//+------------------------------------------------------------------+  
int vr_trade :: SelLimit(string sy,double lot, double price)
{
return openorders(sy,3,lot,price);
}
//+------------------------------------------------------------------+  
int vr_trade :: BuyStop(string sy,double lot, double price)
{
return openorders(sy,4,lot,price);
}
//+------------------------------------------------------------------+  
int vr_trade :: SelStop(string sy,double lot, double price)
{
return openorders(sy,5,lot,price);
}
//+------------------------------------------------------------------+
int vr_trade :: openorders(string sy="",int typ=0,double lot=0,double price=0)
  {
   int tik=-2;
   double di=NormalizeDouble(500*_Point,_Digits);
   if(sy==""){sy=_Symbol;Print("Установлен символ текущего графика ",sy);}
   if(lot<MarketInfo(sy,MODE_MINLOT)){lot=MarketInfo(sy,MODE_MINLOT); Print("Советник скорректировал лот ",lot);}
   if(!SymbolInfoTick(sy,st))Print("Не удалось прогрузить цены для символа ",sy);
   if(price==0)
     {
      if(typ==0)price=st.ask;
      if(typ==1)price=st.bid;
      if(typ==2)price=st.ask-di;
      if(typ==3)price=st.bid+di;
      if(typ==4)price=st.ask+di;
      if(typ==5)price=st.bid-di;
     }
   if(IsTradeAllowed()==true)
     {
      RefreshRates();
      tik=OrderSend(sy,typ,lot,price,Slip,0,0,"",Magic,0,clrRed);
      if(tik>0)Print("Успешно открыт ордер Ticket ",tik," Typ ",tip(typ)," Symbol ",sy," Lot ",lot," Price ",price);
      else Print("Ошибка открытия ордера N",GetLastError());
     }
   else
      Print("Торговый поток занят");
   return tik;
  }
//+------------------------------------------------------------------+
string vr_trade :: tip(int typ)
  {
   string txt="";
   switch(typ)
     {
      case 0: txt="BUY";        break;
      case 1: txt="SELL";       break;
      case 2: txt="BUY LIMIT";  break;
      case 3: txt="SELL LIMIT"; break;
      case 4: txt="BUY STOP";   break;
      case 5: txt="SELL STOP";  break;
      default : txt="Ошибка типа ордера";
     }
   return txt;
  }
//+------------------------------------------------------------------+
 
So OOP is just a function to cram into a class? Productive, it's going to be fun on the forum soon.
 
VOLDEMAR:


  • Is the code correct ?
  • How to improve the code ?
  • How to speed up the code?


What do you mean by right or wrong? If it works correctly, it's right. If it doesn't work, it's wrong.

Improve. Improve when something doesn't satisfy a need. What needs it doesn't meet, then you should improve it in this direction.

Why speed up? Orders don't open that often, even rarely compared to the total number of ticks in the tester.

But that was philosophy.

Practice. Why do we need this class if there is a great standard one? It's true, it is for MT5 and I do not know if it is available for MT4. Don't you have one?

There is one disadvantage in your code - absence of Stop Loss and Take Profit.

 
TheXpert:
So OOP is just a function to cram into a class? Productive, it's going to be fun on the forum soon.

If you are so clever, then write the right way, the topic was not created for sarcasm ....
 
VOLDEMAR:

If you are so smart then write the right way, the topic is not for sarcasm ....


It depends on what you want, what you need, and what your programming style is.

You can make a class as a set of functions with parameters, like in your case.

You can make methods to set parameters, and directly open order by calling method without parameters.

It would be better when it is more simple - in this case, it will be more applicable in different cases. Trying to do something super-perfectly universal is the dream of an idiot, a waste of time and mental effort.

More. Until you have had to solve the same problem 20 times, you'd better not try to create a universal approach.

 
Integer:


What does it mean right or wrong? If it works correctly, it's right. If it doesn't work, it's wrong.

Improve. You should improve when something doesn't meet a need. What needs are not met, in that direction to improve.

Why speed up? Orders don't open that often, even rarely compared to the total number of ticks in the tester.

But that was philosophy.

Practice. Why do we need this class if there is a great standard one? It's true, it is for MT5 and I do not know if it is available for MT4. Don't you have one?

There is one disadvantage in your code - absence of Stop Loss and Take Profit.


Stop losses and takeaways for orders are set separately, as we may not know what type of account we will be working on ....
 
Integer:

You can make methods to set parameters and directly open an order by calling a method without parameters.

Can you demonstrate this with my example ?

 

I don't feel like coding. Anyway... In private section declare variables for Stop Loss, Take Profit and lot values. These variables are used in order opening methods, of course, lot is as it is and price levels are calculated using these variables. But we should set values for these variables. This means we need methods like: SetTakeProfit(int Value), SetStopLoss(int Value), SetLots(double Value).

In most EAs, methods SetTakeProfit(int Value), SetStopLoss(int Value), SetLots(double Value) will only need to be called once in the inite. Calling methods to open orders without parameters greatly speeds up execution time.

 
VOLDEMAR:
If you're so smart, write it down correctly, the topic was not created for sarcasm ....
Don't drag OOP in places where it is not necessary. The mere presence of objects does not make the code faster, more efficient, etc.
 

As always, I wanted to learn, but there are bound to be those who have nothing more to say except to be clever...

I wrote a simple example to take it apart, I don't know how to write more competently with OOP ... It is just an example, if you know how to write a similar code correctly and OOP then please write, so that I and others could learn ...

 
VOLDEMAR:

As always, I wanted to learn, but there are bound to be those who have nothing more to say except to be clever...

I wrote a simple example to take it apart, I don't know how to write more competently with OOP ... It's just an example, if you know how to write such a code correctly and OOP then please write, so that I and others could learn ...


Don't mind me.

* * *

You should strive to ensure that the main and most frequently used methods are called without passing any parameters. This would increase performance. But it will reduce usability.

Reason: