#property library void afunc() { static int starttik = 0; if (starttik == 0) { Print("afunc(): library reinitialized"); starttik = GetTickCount(); } else { Print("afunc(): running for "+(GetTickCount()-starttik)+" milliseconds"); } }
and indicator
#property indicator_chart_window extern bool debug = TRUE; #import "afunc.ex4" void afunc(); #import int init () { Print("Indicator initialized"); return(0); } int start() { afunc(); return(0); }for better view.
void afunc() { static int starttik = 0; if (starttik == 0) { Print("afunc(): library reinitialized"); starttik = GetTickCount(); } else { Print("afunc(): running for "+(GetTickCount()-starttik)+" milliseconds"); } }
save as include test.mqh in experts\include\
and modify the indicator so:
#property indicator_chart_window extern bool debug = TRUE; #include <test.mqh> int init () { Print("Indicator initialized"); return(0); } int start() { afunc(); return(0); }
Hi Rosh,
Thanks for the reply.
At present all of my code is in the form of MQH files and I have quite a lot of code. I wanted to modularize my code into libraries because it means that I wouldnt have to recompile every indicator/expert/script after I make a change to my library code. If the code is in the form of include files (as it is at present) then any change to an include file means that I have to recompile every indicator/expert/script that makes use of that include file.
I have encountered other issues with respect to MQL4 libraries that make me suspect that the MetaTrader implementation of libraries is a little flaky.
Regards,
Laurence.
when changing input values, timeframes etc., global variables do not reinitialize.
If you want to reninitalize global vars do it explicitly in the init() function.
in order to reinitialize library vars create function in library, for example initvars() and import it and call in init() func of indicator.
This way libraries act as sort of quasi-objects allowing certain degree of encapsulation of properties and actions.
when changing input values, timeframes etc., global variables do not reinitialize.
If you want to reninitalize global vars do it explicitly in the init() function.
in order to reinitialize library vars create function in library, for example initvars() and import it and call in init() func of indicator.
This way libraries act as sort of quasi-objects allowing certain degree of encapsulation of properties and actions.
Thank you for the clarification irusoh1, I shall do as you suggest.
Regards,
Laurence.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
I have written a custom indicator which imports 3 libraries that I have also written.
If I attach the indicator to a chart it works fine. If I recompile the indicator it deinitializes the indicator on the chart and the imported libraries used by the indicator, as expected.
However, if I change the imput properties of the indicator the indicator reinitializes but the imported libraries do not.
Because the imported libraries do not reinitialize the static variables within libraries retain their current state. This undertandably causes the indicator to go awry since I would expect if the indicator is reinitialized for any reason then any imported libraries (and their static variables) would also be reinitialized.
To reiterate, if I do a recompile of the indicator the indicator and its imported libraries all reinitialize fine.
If I change the input properties of the indicator the indicator reinitializes but the imported libraries do not.
Here is an example: a very simply library called afunc.eq4...cut and paste the library and compile a library called afunc.ex4 with it.
#property library
void afunc() {
static int starttik = 0;
if (starttik == 0) {
Print("afunc(): library reinitialized");
starttik = GetTickCount();
}
else {
Print("afunc(): running for "+(GetTickCount()-starttik)+" milliseconds");
}
}
A very simple indicator (that displays nothing but calls the afunc library above). ..cut and paste and compile a custom indicator called afuncind.ex4 also.
#property indicator_chart_window
extern bool debug = TRUE;
#import "afunc.ex4"
void afunc();
#import
int init () {
Print("Indicator initialized");
return(0);
}
int start() {
afunc();
return(0);
}
If you attach afuncind.ex4 to any chart the experts tab will display the output of the indicator. You will see that the afunc() function is called and it keeps a count of how long has elapsed since it was last called via a static variable called 'starttik'. If you then select from the menu bar "Charts...Indicators" and modify the input parameters of afuncind you will see that although the indicator itself reinitializes the afunc library does not. As a result the 'starttik' variable in the afunc library is not reset and the counter continues unabated.
Hope someone can shed some light on this.
Best wishes,
Laurence.