sample EA code (not tested, but it's the basic idea):
extern int TrailingStop = 10; extern int StopLoss = 20; #include <MoneyMgmt.mqh> int start( ) { Print( "TrailingStop:" + TrailingStop ); Print( "StopLoss:" + StopLoss ); Print( "RiskPercent:" + RiskPercent ); }
MoneyMgmt.mqh (in "include" dir):
#import "MoneyMgmt.mq4" extern int RiskPercent = 1; int MoneyManagement( );
MoneyMgmt.mq4 (in "libraries" dir):
#include <MoneyMgmt.mqh> int MoneyManagement( ) { return ( 1 ); }
Using the above code, you'll see 3 variables on the Input page when you add it to a chart: TrailingStop, StopLoss, and RiskPercent. If you modify all 3 values on the Inputs page and run the EA, TrailingStop and StopLoss will retain the values you entered, but RiskPercent will ALWAYS equal 1 (one).
This isn't the case if RiskPercent is defined in the main EA module, only if it's defined in the include file.
You hopefully can see the usefulness of having extern'ed vars in include files: it allows you to not only modularize your code but also the input vars. All you need to do is add one "#include" statement to your main EA and you're all set!
I hope you can get this working. Thanks for your time!
Alan
All external variables changes are saved
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
For example, I'd like to move all my Trailing Stop code to libraries/TrailingStop.mq4 and include/TrailingStop.mqh, and declare all relevant input vars in the .mqh file. That way, the using EA only needs to include the header file, and all the inputs will show up on the Inputs tab.
I noticed that if I externalize variables in a .mqh file (like "extern int TrailingStop") and include that header file in my EA, the externalized variable will indeed show up on the Inputs page when the EA is attached to a chart.
Wonderful!
However, these variables don't retain their values. Only externalized variables declared in the ACTUAL EA retain their values.
Can someone inform me how to make this work, or confirm that this is a bug?
Thank you!
Alan
MT 4 build 193