In MQL4, properties are additional details about the program that you are developing, they are not mandatory and the code can be run without any property. Often though, properties are very useful. In this guide, you will see some of the most popular #property items and how to use them in MQL4.
If you read the guide on How to Create a Basic Script to Test Your Code, you may have noticed that in the first few lines of code, there are some properties defined, the code is:
//+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Demo-1.mq4 | //| EarnForex.com | //| https://www.earnforex.com/ | //+------------------------------------------------------------------+ #property copyright "EarnForex.com" #property link "https://www.earnforex.com/" #property version "1.00" #property strict //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- Alert("Hello World!"); } //+------------------------------------------------------------------+
Properties are used mostly in indicators and expert advisors to define some settings and provide additional details regarding the software. You can see some examples of properties in a demo expert advisor and see the results on screen:
// Copyright is the property for the author or company, to specify who wrote the code. #property copyright "EarnForex.com" // Link is used to add a clickable URL to the program's About tab. #property link "https://www.earnforex.com/" // Version to specify the version of the program. #property version "1.00" // Description is simply a textual description. // It can be multiline using multiple #property description statements. #property description "This is a demo expert advisor." #property description "This is just to show some property examples." // Strict is necessary to maintain better compatibility of MQL4 code with MQL5. #property strict int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void OnTick() { }
If you try to add this expert advisor to a chart, you will see the following About screen:
Remember that properties set in include files are totally ignored by the MQL4 compiler. Properties are taken into consideration only when they are placed in .mq4 files. If you don't know what are include files, you can read our guide on MQL4 Program Types.
Properties can be important, especially in indicators, as you can configure colors, labels, and other characteristics. We will not go into details for the moment but you will see more of the properties when exploring the code of MetaTrader indicators.
This was an introduction to the #property
directive, which you can use to add extra information to your MQL4 programs. Try it yourself and see the results!