
- www.mql5.com
vCurrTFD01 = (datetime)SeriesInfoInteger(InpSymbol,PERIOD_H1,SERIES_LASTBAR_DATE); ⋮ vCurrTFH01 = (datetime)SeriesInfoInteger(InpSymbol,PERIOD_H1,SERIES_LASTBAR_DATE);
Same value assigned.
Hi Carl
Thanks for reply.
If Parametric Constructor is defined in the class to be loaded, we can use direct method instead of new method.
However, as per your suggestion I have tried loading class by new method, the results are still same and time frame error continues.
Right now they are global variables defined outside the class.
Hey mate,
I am not a native English speaker,
So if my answer is not clear, please tell me and I'll try to rephrase it in a more clear way.
Your problem is not shown in the code fragments you've posted (which is also a big hint for the problem).
Amir Yacoby explained it correctly-
In your init function of your "CDivergenceMQL5" class,
you set the mTimeFrame variable,
HOWEVER-
mTimeFrame is not a variable that is part of the "CDivergenceMQL5" class,
It is a global variable which is declared in the "IncludeClassesMQL5.mqh" file.
So essentially all of your "CDivergenceMQL5" instances are using the same variable to hold the timeframe,
So in your EA's "OnInit()" function, each of the below calls, overwrite the value stored in the "mTimeFrame" variable-
cDVRG_M15.OnInit(); // mTimeFrame gets the initial value of PERIOD_M15 cDVRG_M05.OnInit(); // mTimeFrame now gets overwritten with the value of PERIOD_M5 cDVRG_M01.OnInit(); // mTimeFrame gets overwritten again, now with the value of PERIOD_M1
Set a variable to save the timeframe as Amir Yacoby had suggested, and your code should work as expected.
Cheers.
You should define all the instance variables like mTimeFrame inside the class in private part when possible, protected if not possible and public otherwise.
Right now they are global variables defined outside the class.
Thanks Amir for your reply.
I realized that mTimeFrame was declared in the "IncludeClassesMQL5.mqh" file and not within CDivergenceMQL5 class. With this change I have also declared mSymbol and mDigits locally. It has helped at least to separate the instances time frame.
class CDivergence { public: CDivergence() { } CDivergence(string pSymbol,ENUM_TIMEFRAMES pTimeFrame,int pMFIPeriod,int pMFIOnBBPeriod,double pMFIOnBBDeviation, int pStochKPeriod,int pStochDPeriod,int pStochSlowing); ~CDivergence(); int OnInit(); void OnTick(); private: int mDigits; // NO OF PRICE DECIMAL DIGITS (_Digits) OF THE ASSETS string mSymbol; ENUM_TIMEFRAMES mTimeFrame; CiMFI cMFI; CiStoch cStoch; CFractals cFractals;
However, it seems IsNewBar() method is failing to catch the time frame and returns value almost on each tick.
A little extra help is highly appreciated.
Please review Rev1 tester results in the attached file to reply to AMI289.
Hey mate,
I am not a native English speaker,
So if my answer is not clear, please tell me and I'll try to rephrase it in a more clear way.
Your problem is not shown in the code fragments you've posted (which is also a big hint for the problem).
Amir Yacoby explained it correctly-
In your init function of your "CDivergenceMQL5" class,
you set the mTimeFrame variable,
HOWEVER-
mTimeFrame is not a variable that is part of the "CDivergenceMQL5" class,
It is a global variable which is declared in the "IncludeClassesMQL5.mqh" file.
So essentially all of your "CDivergenceMQL5" instances are using the same variable to hold the timeframe,
So in your EA's "OnInit()" function, each of the below calls, overwrite the value stored in the "mTimeFrame" variable-
Set a variable to save the timeframe as Amir Yacoby had suggested, and your code should work as expected.
Cheers.
Hi AMI
Thanks a lot for your reply and much more detailed explanation to the solution. Your english is well and enough for me to understand.
I have implemented your suggestion (please see reply to Amir above), however there seems still problem with NewBar detection, as it is printing on almost ever tick.
Attached is the revised Tester Results for your review.
" It is a global variable which is declared in the "IncludeClassesMQL5.mqh" file." ... your pointer to this way of declaring variable will go long way to improve my codes and I realized that short cuts are not always good.
Thanks Amir for your reply.
I realized that mTimeFrame was declared in the "IncludeClassesMQL5.mqh" file and not within CDivergenceMQL5 class. With this change I have also declared mSymbol and mDigits locally. It has helped at least to separate the instances time frame.
However, it seems IsNewBar() method is failing to catch the time frame and returns value almost on each tick.
A little extra help is highly appreciated.
Please review Rev1 tester results in the attached file to reply to AMI289.
Hey Anil,
static variables declared in a class are same as global variables, meaning there is only one instance of them thru all instances and it is the same.
You only need one of them, declared in the class private part
datetime vPrevTF;
Initialize with WRONG_VALUE in the constructor.
And the method IsNewBar() // not tested
datetime vCurrTF = (datetime)SeriesInfoInteger(mSymbol,mTimeFrame,SERIES_LASTBAR_DATE); if(vPrevTF != vCurrTF) { vPrevTF = vCurrTF; return true; } else return false;
Hey Anil,
static variables declared in a class are same as global variables, meaning there is only one instance of them thru all instances and it is the same.
You only need one of them, declared in the class private part
Initialize with WRONG_VALUE in the constructor.
And the method IsNewBar() // not tested
Hi Amir
This is amazing, you really have excellent knowledge of OOP. These causes of problem as well as their solutions were beyond my imaginations. Hats off to you man.
It did worked perfectly well ... THANKS A LOT
2022.11.06 15:11:18.309 2022.01.03 01:00:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][CLASSIC] [11][5] isMFI[false] isStoch[true] 2022.11.06 15:11:18.309 2022.01.03 01:00:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M5][LONG][CLASSIC] [6][1] isMFI[true] isStoch[false] 2022.11.06 15:11:18.309 2022.01.03 01:00:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M15][LONG][CLASSIC] [9][5] isMFI[false] isStoch[true] 2022.11.06 15:11:18.352 2022.01.03 01:01:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][CLASSIC] [12][5] isMFI[false] isStoch[true] 2022.11.06 15:11:18.434 2022.01.03 01:02:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][CLASSIC] [24][4] isMFI[false] isStoch[true] 2022.11.06 15:11:18.537 2022.01.03 01:03:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][CLASSIC] [25][5] isMFI[false] isStoch[true] 2022.11.06 15:11:18.580 2022.01.03 01:04:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][CLASSIC] [31][5] isMFI[true] isStoch[true] 2022.11.06 15:11:18.660 2022.01.03 01:05:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [27][4] isMFI[true] isStoch[true] 2022.11.06 15:11:18.660 2022.01.03 01:05:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M5][LONG][CLASSIC] [7][2] isMFI[true] isStoch[false] 2022.11.06 15:11:18.751 2022.01.03 01:06:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [28][5] isMFI[true] isStoch[true] 2022.11.06 15:11:18.808 2022.01.03 01:07:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [29][4] isMFI[true] isStoch[true] 2022.11.06 15:11:18.868 2022.01.03 01:08:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [30][5] isMFI[true] isStoch[true] 2022.11.06 15:11:18.937 2022.01.03 01:09:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [31][5] isMFI[false] isStoch[true] 2022.11.06 15:11:18.984 2022.01.03 01:10:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [32][5] isMFI[false] isStoch[true] 2022.11.06 15:11:18.984 2022.01.03 01:10:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M5][LONG][CLASSIC] [8][3] isMFI[true] isStoch[false] 2022.11.06 15:11:19.080 2022.01.03 01:11:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [33][5] isMFI[false] isStoch[true] 2022.11.06 15:11:19.123 2022.01.03 01:12:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][CLASSIC] [2][5] isMFI[true] isStoch[false] 2022.11.06 15:11:19.209 2022.01.03 01:13:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [35][1] isMFI[true] isStoch[true] 2022.11.06 15:11:19.304 2022.01.03 01:14:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [36][1] isMFI[true] isStoch[true] 2022.11.06 15:11:19.349 2022.01.03 01:15:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [37][1] isMFI[true] isStoch[true] 2022.11.06 15:11:19.349 2022.01.03 01:15:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M5][LONG][CLASSIC] [9][4] isMFI[true] isStoch[false] 2022.11.06 15:11:19.349 2022.01.03 01:15:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M15][LONG][CLASSIC] [2][5] isMFI[true] isStoch[true] 2022.11.06 15:11:19.409 2022.01.03 01:16:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [38][2] isMFI[true] isStoch[true] 2022.11.06 15:11:19.469 2022.01.03 01:17:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [39][3] isMFI[true] isStoch[true] 2022.11.06 15:11:19.552 2022.01.03 01:18:00 CDivergence::IsDVRG_Bullish: DVRG[PERIOD_M1][LONG][HIDDEN] [40][4] isMFI[true] isStoch[true]

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Dear Members
Wish all of you happy weekend.
I have created CDivergence CLASS to calculate divergence and trying to use multiple instances of it to get divergence on different time frames. The challenge I am facing is that it return only last time frame defined in the instance declaration. Below is the tester report.
Seems CLASS did instantiate THREE time frames in Constructor, but...
2022.11.04 12:13:41.784 2022.01.01 00:00:00 CDivergence::CDivergence: mTimeFrame[PERIOD_M15] pTimeFrame[PERIOD_M15]
2022.11.04 12:13:41.784 2022.01.01 00:00:00 CDivergence::CDivergence: mTimeFrame[PERIOD_M5] pTimeFrame[PERIOD_M5]
2022.11.04 12:13:41.784 2022.01.01 00:00:00 CDivergence::CDivergence: mTimeFrame[PERIOD_M1] pTimeFrame[PERIOD_M1]
but it took only the last time frame when running OnInit(), I have tried to shift code into Constructor() but results are same.
2022.11.04 12:13:41.784 2022.01.01 00:00:00 CDivergence::OnInit: mTimeFrame[PERIOD_M1]
2022.11.04 12:13:41.897 2022.01.01 00:00:00 CDivergence::OnInit: mTimeFrame[PERIOD_M1]
2022.11.04 12:13:41.897 2022.01.01 00:00:00 CDivergence::OnInit: mTimeFrame[PERIOD_M1]
I have attached the scale down version of code (other wise there are too many files and codes which are not relevant.
Will need one more favor, in scale down version when I compile CDivergence.mqh it does compile without any error. However when I compile the TestDivergence EXPERT, it gives the following error, which I have failed to figure out. Please support me on this and then try to run the EA.
'CStochMQL5' - unexpected token, probably type is missing? DivergenceMQL5.mqh 31 3
'cStoch' - semicolon expected DivergenceMQL5.mqh 31 18
'CFractalsMQL5' - unexpected token, probably type is missing? DivergenceMQL5.mqh 32 3
'cFractal' - semicolon expected DivergenceMQL5.mqh 32 20
Look forward to solution to the issue at the earliest.
Regards
Error CDivergence.txt for much more detailed Tester Results as message characters limit is exceeding.