- Articles on the development of trading applications
- Moving from MQL4
- MQL5 Wizard: Development of trading robots for MetaTrader 5
// Use 0 for period and an empty string for symbol CDataSeries(int period_ = 0, string symbol_ = "") { /* ...your code... */ }
Static pointer array only has static storage; it does not control the lifetime of the objects. To prevent the object from being destroyed prematurely, do not call delete on it (or provide an explicit lifetime management strategy).
Try this Options....
Change the default arguments from NULL to valid defaults (e.g., 0 and "" ), and ensure that the static member function (or friend functions) has access to the private constructor.
Static pointer array only has static storage; it does not control the lifetime of the objects. To prevent the object from being destroyed prematurely, do not call delete on it (or provide an explicit lifetime management strategy).
Try this Options....
Thank you my friend for your time, changing the constructor worked correctly. But for the array problem, how can I implement explicit lifetime? Could you give an example? Thanks again.
moslem alavi:
MqlRates rate[]; Array<double> *Open(void); Array<double> *High(void); Array<double> *Low(void); Array<double> *Close(void); //--- extracted esries Array<double> *open; Array<double> *high; Array<double> *low; Array<double> *close;
I'm curious why you need such a cumbersome thing, instead of copying synchronized data into several separate arrays with one function call.
https://www.mql5.com/en/docs/series/copyseries
The CopySeries function allows obtaining only the necessary timeseries into different specified arrays during one call, while all of timeseries data will be synchronized. This means that all values in the resulting arrays at a certain index N will belong to the same bar on the specified Symbol/Timeframe pair. Therefore, there is no need for the programmer to ensure the synchronization of all received timeseries by the bar opening time.
Thus, if you need to get the values of the time, close and real_volume timeseries for the last 100 bars of the current Symbol/Timeframe, you should use the following call:
datetime time[]; double close[]; long volume[]; CopySeries(NULL,0,0,100,COPY_RATES_TIME|COPY_RATES_CLOSE|COPY_RATES_VOLUME_REAL,time,close,volume);
I'm curious why you need such a cumbersome thing, instead of copying synchronized data into several separate arrays with one function call.
You are absolutely right. It is very cumbersome and redundant. But unfortunately the CopySeries function does not exist in mql4. Of course, as you mentioned, there are similar built-in functions that I will use in the main code. Thank you for your attention. In any case, the main problem was in storing the instances in the static Array , which (probably was not initialized correctly and was solved by rewriting the instance function). I will leave the code sample below. Thank you
CDataSeries* CDataSeries::instance(int period_ = 0, string symbol_ = "") { for(int i = 0; i < 8; i++) { if(period_ == CPriceFactory::period[i]) { if(!CDataSeries::rates[i]) CDataSeries::rates[i] = new CDataSeries(period_, symbol_); return(CDataSeries::rates[i]); } } return(instance(period_, symbol_)); // Changed to recursive to ensure initialization }
moslem alavi #:
if(!CDataSeries::rates[i]) CDataSeries::rates[i] = new CDataSeries(period_, symbol_);
in mql4
Implicit call to CheckPointer() using the NLOT operator does not work fully in MQL4. Therefore, in MQL4 I personally prefer to call CheckPointer explicitly
#property strict class A { public: int get() { return(x); } private: int x; public: A::A(int a_x) : x(a_x) {} }; void OnStart() { A* a = new A(MathRand() % 100); if(!a) Print("#1: Pointer is ", EnumToString(CheckPointer(a))); else Print("#1: x = ", a.get()); delete a; if(!a) Print("#2: Pointer is ", EnumToString(CheckPointer(a))); else Print("#2: x = ", a.get()); }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use