MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

Data types, variables, and constants in MQL4

One of the fundamental concepts in programming is understanding how to store and manipulate data. In MQL4, as in many languages, we use data types, variables, and constants to achieve this.

Data Types

Data types define what kind of information a variable can hold. MQL4 offers various basic data types:

Variables

Variables are named containers that store data values. The value inside a variable can change, hence the name "variable". Here's how to declare and initialize a variable:

int numberOfTrades = 10; 

// Declares an integer variable named numberOfTrades and initializes it with the value 10

// An int cannot contain any decimals
    

Variables should be given meaningful names that describe the data they hold. This makes your code easier to read and understand.

Constants

Constants are similar to variables, but their values cannot change once they are set. They are useful for values that should remain consistent throughout a program. In MQL4, constants are declared using the input modifier for externally adjustable variables or the const keyword for true constants:

input double fixedLotSize = 0.1; 
// This is double(Decimal value) that can be changed in the Expert Advisor properties, but once set it cannot be changed when the EA is in use.


const double PI = 3.14159; 
// This is a true constant and cannot be changed by the EA and will produce an error if attempted.

double PI = 3.14159; 
// This would have the same effect, but can be changed later by the EA if needed, therefore it is NOT considered a constant


    

Lets check for some of these in our test EA

Here is our EA from the previous page... WITHOUT MOST OF THE COMMENTS. We will only add new comments as we add new parts.

As you can see we already have a constant double in use for "LotSize", a constant bool in use for setting TradingEnabled to true or false, a normal bool for our MyFirstCondition and a string being used in both our Print functions.




#property copyright "Copyright 2023, www.mql4trader.com."
#property link      "https://www.mql4trader.com"
#property version   "1.00"
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>


///////////////////////////////////
// External items go below here... These are items that can be altered by the user via expert properties.
///////////////////////////////////


input double LotSize = 0.1; 

// This is our constant double //


extern bool TradingEnabled = true; 

// This is our 1st bool and it is a constant because once the user sets it in the EA properties, it will remain either true or false. //


/////////////////////////////////
/// Global variables go here ////
/////////////////////////////////


double CalculateSpread() 
 
 { return Ask - Bid; }
 
 // This is our non-constant double. This value will change every time the EA uses it for a calculation.
 
 
 
bool MyFirstCondition=false; 

// This is our second bool and is not a constant because it can be changed during live operations of the EA.


////////////////////////////////////
// Expert initialization function //                
// Initialization code goes here //
///////////////////////////////////

int OnInit()
  
  { return(INIT_SUCCEEDED); }
  
  
/////////////////////////////////
// Deinitialization code here //
////////////////////////////////


void OnDeinit(const int reason)

  {
  


  }
  

///////////////////////////////
// OnTick Function goes here //
//////////////////////////////


void OnTick()

  {
  
// Trading logic here/// 


if (MyFirstCondition) 

{ Print("The Spread is: ",CalculateSpread()); }

// A string is being used where it says "The Spread is: "

  

if (!MyFirstCondition) 

{ Print("Sorry, but MyFirstCondition is still false"); }

// A string is being used where it says "Sorry, but MyFirstCondition is still false"


 return;
 

  }
  

Conclusion

Understanding data types, variables, and constants is a foundational concept in MQL4 programming. By mastering these elements, you'll be better equipped to store and manipulate data effectively in your trading algorithms.

NEXT UP:Operators and Expressions in MQL4.