static behavioral of non static struct in class !!!

 

Hello friends

Does anyone know why when I create several different objects with different timeFrames the CFirstPullBack class, if the condition of one of the objects is met with the Strategy() function, the SetPrice() function is called for the rest of the objects and sets the _OHLC structure for all of them!! !

If you can, please help me, because I did everything I knew and didn't get any results.

//+------------------------------------------------------------------+
//|          Abstract Class For Design Strategy                      |
//+------------------------------------------------------------------+
class CStrategy : public CCondition
  {
private:

public:

   virtual double              Factory()                             { return 0;                }
   virtual datetime            GetTime()                             { return 0;                }
   virtual int                 GetMajick()                           { return 0;                }

  };
//+------------------------------------------------------------------+
//|                          CFirstPullBack                          |
//+------------------------------------------------------------------+
class CFirstPullBack : public CStrategy
  {


private:

   COHLC             _OHLC;
   double            _price;
   datetime          _time;
   int               _period;
   int               _majicknumber;
   bool              Strategy(void);

public:

                     CFirstPullBack(int, int);
   void              SetPrice();
   double            GetPrice(void)                                  { return _price;          }
   //--- Abs func
   double            Factory(void);
   datetime          GetTime(void)                                   { return _time;           }
   int               GetMajick(void)                                 { return _majicknumber;   }
  };
//+------------------------------------------------------------------+
//| Cunstractor                                                      |
//+------------------------------------------------------------------+
CFirstPullBack::CFirstPullBack(int period, int majicknumber):_period(period),
   _majicknumber(majicknumber)
  {
   CCondition(period);
  }
//+------------------------------------------------------------------+
//| Avoid duplicate pricing                                          |
//+------------------------------------------------------------------+
void CFirstPullBack::SetPrice()
  {


   _OHLC.Set(_period);
//--- if NEW price then set
   if(_OHLC.time!=_time)
     {
      _time  = _OHLC.time;
      _price = _OHLC.open;
     }
  }
//+------------------------------------------------------------------+
//|  Definition of strategy Conditions                               |
//+------------------------------------------------------------------+
bool CFirstPullBack::Strategy(void)
  {
//--- Definition
   if(IsThreeSoldier(0.2))
      return true;
//---
   return false;
  }
//+------------------------------------------------------------------+
//|  check strategy Condition & call for setting price               |
//+------------------------------------------------------------------+
double CFirstPullBack::Factory(void)
  {
//--- strategy check
   if(Strategy())
      SetPrice();
//---
   return GetPrice();

thank you

 
moslem alavi: Does anyone know why when I create several different objects with different timeFrames the CFirstPullBack class, if the condition of one of the objects is met with the Strategy() function, the SetPrice() function is called for the rest of the objects and sets the _OHLC structure for all of them!! !
  1. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

    Always post all relevant code (using Code button) or attach the source file.

    All you posted was your class definition.

  2. What do you mean different timeFrames? A chart only has one. All objects are on the one chart.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem


 
moslem alavi:

Hello friends

Does anyone know why when I create several different objects with different timeFrames the CFirstPullBack class, if the condition of one of the objects is met with the Strategy() function, the SetPrice() function is called for the rest of the objects and sets the _OHLC structure for all of them!! !

If you can, please help me, because I did everything I knew and didn't get any results.

thank you

void              SetPrice();

  1. You can add this function to the base class.
  2. The derived class objects you can add in an array. The array can be defined as static member in the base class or can be in global scope.
  3. When condition is met, iterate over the objects and call SetPrice for all of them.
Documentation on MQL5: Language Basics / Object-Oriented Programming / Static Members of a Class
Documentation on MQL5: Language Basics / Object-Oriented Programming / Static Members of a Class
  • www.mql5.com
Static Members of a Class - Object-Oriented Programming - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

CCondition is defined with different times and the IsThreeSoldier method comments the correct value with different times.

The same method is used in CFirstPullBack::Strategy(void).

As you can see, in the CFirstPullBack constructor, a time framrs is set to the inherited CCondition(period).

//+------------------------------------------------------------------+
//|                                                     AlgoTest.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include  <Order.mqh>
#include  <Strategy.mqh>
#include  <Condition.mqh>
#include <MyLib.mqh>

//+------------------------------------------------------------------+
//|                     global object section                        |
//+------------------------------------------------------------------+
CCondition M1(PERIOD_M1);
CCondition M5(PERIOD_M5);
CCondition M15(PERIOD_M15);
CCondition M30(PERIOD_M30);
CCondition H1(PERIOD_H1);
CCondition H4(PERIOD_H4);
CCondition D1(PERIOD_D1);
CCondition W1(PERIOD_W1);
CCondition MN1(PERIOD_MN1);
//+------------------------------------------------------------------+
//|                      strategy section                            |
//+------------------------------------------------------------------+
CFirstPullBack     FirstPullBack_M1(PERIOD_M1, 111111);
CFirstPullBack     FirstPullBack_M5(PERIOD_M5, 111555);
CFirstPullBack     FirstPullBack_M15(PERIOD_M15, 151515);
//+------------------------------------------------------------------+
//|                managment & setting order section                 |
//+------------------------------------------------------------------+
COrder      MNG_FirstPullBack_M1(PERIOD_M1, FirstPullBack_M1);
COrder      MNG_FirstPullBack_M5(PERIOD_M1, FirstPullBack_M5);
COrder      MNG_FirstPullBack_M15(PERIOD_M1, FirstPullBack_M15);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   Comment("FirstPullBack_M1 ", MNG_FirstPullBack_M1.Call(FirstPullBack_M1), "\n",	//It just calls the CFirstPullBack::Factory(void) method
           "FirstPullBack_M5 ", MNG_FirstPullBack_M5.Call(FirstPullBack_M5), "\n",	//It just calls the CFirstPullBack::Factory(void) method
           "FirstPullBack_M15 ", MNG_FirstPullBack_M15.Call(FirstPullBack_M15), "\n", "\n",
           "FirstPullBack_M1 ", M1.IsThreeSoldier(0.2), "\n",	//CCondition  For test
           "FirstPullBack_M5 ", M5.IsThreeSoldier(0.2), "\n",	//CCondition	For Test
           "FirstPullBack_M15 ", M15.IsThreeSoldier(0.2));	//CCondition	


  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }

//+------------------------------------------------------------------+

William Roeder #:
  1. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

    Always post all relevant code (using Code button) or attach the source file.

    All you posted was your class definition.

  2. What do you mean different timeFrames? A chart only has one. All objects are on the one chart.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem


 
Samuel Manoel De Souza #:

  1. You can add this function to the base class.
  2. The derived class objects you can add in an array. The array can be defined as static member in the base class or can be in global scope.
  3. When condition is met, iterate over the objects and call SetPrice for all of them.
Do you mean something similar to the observer design pattern?
 
moslem alavi #:
Do you mean something similar to the observer design pattern?

yes.

 
Samuel Manoel De Souza #:

yes.

Interestingly, the problem is exactly something similar to the unwanted execution of this mode!
In this way, when the Strategy(void) method is TRUE for one, SetPrice() is called for all and as a result, _OHLC is updated for all !!!
Maybe I explained my problem wrong/I expect each object to work independently, but _OHLC shared .
 

Check the documentation for static members https://www.mql5.com/en/docs/basis/oop/staticmembers

Or use _OHLC as global variable

Or pass the _OHLC as pointer to the objects where you want to use it.

Documentation on MQL5: Language Basics / Object-Oriented Programming / Static Members of a Class
Documentation on MQL5: Language Basics / Object-Oriented Programming / Static Members of a Class
  • www.mql5.com
Static Members of a Class - Object-Oriented Programming - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
moslem alavi:

Hello friends

Does anyone know why when I create several different objects with different timeFrames the CFirstPullBack class, if the condition of one of the objects is met with the Strategy() function, the SetPrice() function is called for the rest of the objects and sets the _OHLC structure for all of them!! !

If you can, please help me, because I did everything I knew and didn't get any results.

thank you

Can you share all the code that is necessary for testing. Maybe your IsThreeSoldier method is broken and returns true all the time.
 

Struct OHLC

CLS BASE Calculation --> Basic candlestick patterns

CLS CONDITION --> IsThreeSoldier()

CLS STRATEGY --> Price production platform with specific conditions

//+------------------------------------------------------------------+
//|                                                     Strategy.mqh |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <Condition.mqh>

//+------------------------------------------------------------------+
//|          Abstract Class For Design Strategy                      |
//+------------------------------------------------------------------+
class CStrategy : public CCondition
  {
private:

public:

   virtual double              Factory()                             { return 0;                }
   virtual datetime            GetTime()                             { return 0;                }
   virtual int                 GetMajick()                           { return 0;                }

  };
//+------------------------------------------------------------------+
//|                          CFirstPullBack                          |
//+------------------------------------------------------------------+
class CFirstPullBack : public CStrategy
  {


private:

   COHLC             _OHLC;
   double            _price;
   datetime          _time;
   int               _period;
   int               _majicknumber;
   bool              Strategy(void);

public:

                     CFirstPullBack(int, int);
   void              SetPrice();
   double            GetPrice(void)                                  { return _price;          }
   //--- Abs func
   double            Factory(void);
   datetime          GetTime(void)                                   { return _time;           }
   int               GetMajick(void)                                 { return _majicknumber;   }
  };
//+------------------------------------------------------------------+
//| Cunstractor                                                      |
//+------------------------------------------------------------------+
CFirstPullBack::CFirstPullBack(int period, int majicknumber):_period(period),
   _majicknumber(majicknumber)
  {
   CCondition(period);
  }
//+------------------------------------------------------------------+
//| Avoid duplicate pricing                                          |
//+------------------------------------------------------------------+
void CFirstPullBack::SetPrice()
  {


   _OHLC.Set(_period);
//--- if NEW price then set
   if(_OHLC.time!=_time)
     {
      _time  = _OHLC.time;
      _price = _OHLC.open;
     }
  }
//+------------------------------------------------------------------+
//|  Definition of strategy Conditions                               |
//+------------------------------------------------------------------+
bool CFirstPullBack::Strategy(void)
  {
//--- Definition
   if(IsThreeSoldier(0.2))
      return true;
//---
   return false;
  }
//+------------------------------------------------------------------+
//|  check strategy Condition & call for setting price               |
//+------------------------------------------------------------------+
double CFirstPullBack::Factory(void)
  {
//--- strategy check
   if(Strategy())
      SetPrice();
//---
   return GetPrice();
  }
//+------------------------------------------------------------------+
//|                                                     BaseCalc.mqh |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include "OHLC.mqh"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CBaseCalc
  {
private:

   int               _period;

public:
                     CBaseCalc(int period):_period(period) {}
                     CBaseCalc(){}
   double            BodyPrc(int);
   bool              IsFullbody(int, double);
   int               Direction(int);

//+------------------------------------------------------------------+
//|   Return Body Percentage Of Range in Decimal Number              |
//+------------------------------------------------------------------+
double CBaseCalc::BodyPrc(int shift=NULL)
  {
//--- if is Base return 0
   if(Direction()==0)
      return 0;
//---
   COHLC  OHLC;
//---
   if(shift!=NULL)
      OHLC.Set(_period, shift);
   else
      OHLC.Set(_period);
   return OHLC.body/OHLC.range;
  }
//+------------------------------------------------------------------+
//|  Return True if Body PRC More Than Given minbodyprc = %60        |
//+------------------------------------------------------------------+
bool CBaseCalc::IsFullbody(int shift=1, double minbodyprc=0.6)
  {
   if(BodyPrc(shift)>=minbodyprc)
      return true;
   else
      return false;
  }

//+------------------------------------------------------------------+
//|  Return Direction By Close Point                                 |
//+------------------------------------------------------------------+
int CBaseCalc::Direction(int shift=NULL)
  {
   COHLC  OHLC;
//---
   if(shift!=NULL)
      OHLC.Set(_period, shift);
   else
      OHLC.Set(_period);
//---
   if(OHLC.open<OHLC.close)
      return 1;
   if(OHLC.open>OHLC.close)
      return -1;
//---
   return 0;
  }
//+------------------------------------------------------------------+
//|                                                    Condition.mqh |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include  <BaseCalc.mqh>


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CCondition : public CBaseCalc
  {
private:

   int               _period;

public:
                     CCondition(int period):CBaseCalc(period),
                     _period(period) {}
                     CCondition() {};
   bool              IsThreeSoldier(double, int);

  };
//+------------------------------------------------------------------+
//| Three consecutive FullBody Bars in Same Direction                |
//+------------------------------------------------------------------+
bool CCondition::IsThreeSoldier(double minbd=0.5, int shift=1)
  {
   int direct=(Direction(shift)+Direction(shift+1)+Direction(shift+2));
//--- Check diredtion consecutive bars
   if(direct==3||direct==-3)
     {
      //---Checked IsFullBody
      if(IsFullbody(shift, minbd)&&IsFullbody(shift+1, minbd)&&IsFullbody(shift+2, minbd))
         return true;
     }
//---
   return false;
  }
//+------------------------------------------------------------------+
//|                                                         OHLC.mqh |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


struct COHLC
  {
private:
   void              _Init(int , int );
public:
   int               timeframe;
   int               shift;
   double            open;
   double            high;
   double            low;
   double            close;
   datetime          time;
   double            range;
   double            body;

   void              Set(int , int Shift=1);
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void COHLC::Set(int period, int Shift=1)
  {
   _Init(period, Shift);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void COHLC::_Init(int period, int Shift)
  {
   timeframe = period;
   shift     = Shift;
   open      = iOpen(NULL, period, shift);
   high      = iHigh(NULL, period, shift);
   low       = iLow(NULL, period, shift);
   close     = iClose(NULL, period, shift);
   time      = iTime(NULL, period ,shift);
   range     = high - low;
   body      = MathAbs(open - close);
  }
//+------------------------------------------------------------------+
Laszlo Tormasi #:
Can you share all the code that is necessary for testing. Maybe your IsThreeSoldier method is broken and returns true all the time.
 
moslem alavi #:

Struct OHLC

CLS BASE Calculation --> Basic candlestick patterns

CLS CONDITION --> IsThreeSoldier()

CLS STRATEGY --> Price production platform with specific conditions

Why did you (re)declare _period in each subclass? This way the baseclass variable is not initialized with the correct timeframe. If you create FirstPullBack instances with different timeframes the IsThreeSoldier is still checked on each with _period=0, so all of them will be true at the same time. That's why they called SetPrice simultaneously. 

Reason: