OOP Object within Object

 

I've got question regarding OOP method.
say i have pseudo code below to open a Buy Order.
I've create 2 separate object Trade and Count.

How to actually combine these (object within object) in cTrade class?
I keep the object class separated for various reasons, and didn't plan to actually combine them (for the sake of organization).

Is there something like render() can be use?
any help would be appreciated.

*edit correct the code

class cTrade {
   private:
      void OpenOrder(int type, int magic, double lot, string comment);
   public:      
      void OpenBuy(int magic, double lot, string comment, int SL_Point=0, int TP_Point=0);
};

void cTrade::OpenBuy(int magic, double lot, string comment, int stopPoint=0, int takePoint=0){
   if(Count.Position(magic)==0) OpenOrder(OP_BUY,magic,lot,comment);
}

class cCount {
   public:
      int Position(int magic);
};

cTrade Trade;
cCount Count;

void OnTick(){
   double lot = 0.20;
   int magic = 1234;
   string tradeComment = "";
   
   int Signal=getSignal();
   if(Signal==OP_BUY) Trade.OpenBuy(magic,lot,tradeComment);
}
 
Ernst Van Der Merwe:

Classes and structs are read from top to bottom, unlike functions.

Thank you. will try this first.

 

class cCount {
   public:
      int Position(int magic) { return(0); }
};

class cTrade {
   private:
      cCount   m_count;
      void OpenOrder(int type, int magic, double lot, string comment) { return; }
   public:      
      void OpenBuy(int magic, double lot, string comment, int SL_Point=0, int TP_Point=0); 
};

void cTrade::OpenBuy(int magic, double lot, string comment, int stopPoint=0, int takePoint=0){
   if(m_count.Position(magic)==0) OpenOrder(OP_BUY,magic,lot,comment);
}

cTrade Trade;

 

Just a few minor tips... It was a bit difficult to make sense of your code since you're using the variable naming conventions opposite of what you'd normally see. Typically you'd want to use CapWords for class definitions and mixedWords or snake_case for methods. Note: MQL stdlib conventions are to use CapWords for both classes and methods but differentiate classes by appending the name with a capital C. To further enhance your code's readability you can prefix private methods with a leading underscore. e.g. this._private_method()

That being said, your example would be better suited to using a static class method since you are using it like a normal function nested within a namespace. 


class CCount {
public: 
   static int position(int magic) { return(0); }
};

class CTrade {
public: 
   bool open_buy(int magic, double lot, string comment, int sl_point=0, int tp_point=0){
      if(CCount::position(magic) == 0) 
         return this._open_order(...);
      return false;
   }
};
 
nicholi shen:

Just a few minor tips... It was a bit difficult to make sense of your code since you're using the variable naming conventions opposite of what you'd normally see. Typically you'd want to use CapWords for class definitions and mixedWords or snake_case for methods. Note: MQL stdlib conventions are to use CapWords for both classes and methods but differentiate classes by appending the name with a capital C. To further enhance your code's readability you can prefix private methods with a leading underscore. e.g. this._private_method()

That being said, your example would be better suited to using a static class method since you are using it like a normal function nested within a namespace. 


Thank you @nicholi shen & @Ernst Van Der Merwe for your reply.

Now, further to my question regarding Object within Object .
Consider below file in #include file and I call it on expert (this is for MT4 btw).

  1. Is there a better way (or correct way) to pass by reference variables from expert to Include file Object, then later use it Object within Object?
  2. the Reference & Count Object/Class with be use again both in .Mqh and .mql4 file. (static can be share within class only?)

// Trade.mqh file

class CReference {
    public:
      	static int _slippage;         // fixed value thoughout program (changeable at the start of program)
      	static int _maxRisk;	         // fixed value thoughout program (changeable at the start of program)
      	static int _spread;           // variable every tick.
      	static int _marginStatus;  // variable every tick.
      	void SetFixVariable(int pSlippage, int pMaxRisk);
	void SetFloatingVariable(int pSpread, int pMarginStatus);
};

static void CReference::SetFixVariable(int pSlippage, int pMaxRisk){
   _slippage = pSlippage;
   _maxRisk = pMaxRisk;
}

static void CReference::SetFloatingVariable(int pSpread, int pMarginStatus) {
   _spread = pSpread;
   _marginStatus = pMarginStatus;
}

int CReference::_slippage = 10;
int CReference::_spread = 10;
int CReference::_maxRisk = 10;
int CReference::_marginStatus = 0;


class CCount {
    public: 
        static int position(int magic) { return(0); }
};

int CCount::Position(int magic){
   int count = 0;
   for(int order = OrdersTotal()-1; order >= 0; order--){
      bool select = OrderSelect(order,SELECT_BY_POS);
      if(select && OrderMagicNumber() == magic && OrderSymbol()==_Symbol && OrderType()<2) count++;
   }
   return count;
}

class CTrade {
    private:
        static _openOrder (...);
    public: 
        bool OpenBuy(int magic, double lot, string comment, int sl_point=0, int tp_point=0){
            if(CCount::position(magic) == 0 && CReference::_spread < 15 && CReference::_marginStatus > 500) 
            return this._openOrder(...);
        return false;
    }
};


This call from expert.

// Expert file

#define MAXRISK 15
#define MAXSLIPPAGE 10
#include <Trade.mqh>

CCount Count;
CTrade Trade;
CReference Reference;

double lot = 0.20;
int magic = 1234;
string tradeComment = "";

int OnInit() {
    Reference.SetFixVariable(MAXSLIPPAGE,MAXRISK);
    return(INIT_SUCCEEDED);
}

void OnTick() {
    int Spread = getSpread();
    double MarginStatus = getMarginStatus();
    Reference.SetFloatingVariable(Spread,MarginStatus);

    int Signal=getSignal();
    if(Signal==OP_BUY) Trade.OpenBuy(magic,lot,tradeComment);

    if(Count.OpenPosition(magic)>0) {
        Alert("Testing Buy order");
        Sleep(10000);
    }
}
 
Reason: