MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

Operators and Expressions in MQL4

In MQL4, operators are the building blocks used to create expressions. These expressions allow you to perform operations like arithmetic, comparison, and logic on your data.

Arithmetic Operators

These operators perform basic mathematical operations:

Example:

double PriceDifference =(OrderStoploss() - OrderOpenPrice());

This example creates for us a "double", that will tell us the difference between where the stoploss is placed and where a sell trade was opened.

Comparison Operators

These operators compare two values and return a boolean (true or false) result. Here are some very basic examples to try to explain this better:

Example 1:

if(Close[1]>Close[2]) { /* Your trading logic here */ }

This conditional statement evaluates if the closing value of a candle one period prior is greater than the closing value of a candle two periods prior. If this condition is met, the subsequent logic encapsulated within the curly braces `{ }` is executed.

Example 2:

if(Open[2]==Close[3]) { /* Enter trade or some other action */ }

This statement checks if the open of the candle 2 periods ago is equal to the close of 3 candles ago. If the condition is satisfied, the code within the `{ }` is triggered.

At the heart of algorithmic trading lies the conditional statement. As you craft your Expert Advisor (EA), experimenting with elementary conditions will bolster your proficiency.

Example 3:

For enhanced readability, you may opt to enclose individual conditions within parentheses, as illustrated below:

if( (Close[1]>Close[2]) && (Open[2]==Close[3])){ /* Your trading logic here */ }

Example 4:

Here is an example from our test EA, using MyFirstCondition that was initially set to false in the global variables section... Now it would be set to true, if this condition were met, until it is reset back to false by some other condtion.

if(Close[1]>Close[2]) { MyFirstCondition=true; }

Logical Operators

These operators combine boolean expressions and return a boolean result. This just means that they are ways in code of saying AND, OR and NOT:


Example:


if((1 + 2 = 3) && (2 + 3 = 6) || (1 + 1 = 3) || (7 - 7 != 3)){ //Your code to enter trade here}

This above conditions result would still be true simply because the 3rd condition is true. 7 - 7 is NOT equal to 3... Even though our first 2 condtions will never be true in this example.


Assignment Operators

These operators assign values to variables:

Although the above may be confusing, it is simply a method of changing a value as circumstances evolve.

1. We begin with the integer that we named "a" and we assined it a value of 5. int a = 5;

2. Then we used += to alter its value. The + is telling the EA to use addition and the = is telling the EA that we are to now assign the resulting number as the new value for the EA. a += 3 = 8 because 5 + 3 = 8

3. Now using the same principles we are going to subtract 2 from the new value of "a" and asign the result as the new value. 8 - 2 = 6, so a now equals 6.

4. Now, we are have told the EA to multiply our new value of "a" by 2 and to assin that as its new value. 6 * 2 = 12 .

5. Finally we are telling the EA to divide our newest value of "a" by 3. 12 / 3 = 4, therefore the final value of "a" has become 4.

This example is not a very real world example but is meant to display the capabilites of mql4 coding.

Lets now try out some of our new skills in our test EA.

Look for new comments:




#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; 


extern bool TradingEnabled = true; 




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


double CalculateSpread() 
 
 { return Ask - Bid; }
 
 // As you can see, we are already using subtraction here.
 
 
 
bool MyFirstCondition=false; 


////////////////////////////////////
// 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(!TradingEnabled){ 

// We have added a condition here that TradingEnabled must be set to "false" in order for us to continue to the next conditions... We use the "!" the say this...


if (MyFirstCondition  && ((Close[1] + Open[1]) > (Close[10] + Open[10])) || ( High[1] > Low[10] ) ) 

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

// We have now exanded this condition to include an ||(OR) statement...

// Our new condition will now be triggered only if MyFirstCondition is true && Close[1] + Open[1] is greater than the sum of Close[1] + Open[10]... OR if High[1] is greater than Low[10]....

// Now only one of these 2 conditions need to be true in order for the EA to continue on the the Print function.
  

if (!MyFirstCondition) // This condition has stayed the same...

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

}  // This is the new bracket that closes everything that is meant to be within our new condition of "if(!TradingEnabled)"... 



 return;
 

  }
  

Conclusion

Operators in MQL4 are essential tools that allow you to manipulate and evaluate data. Understanding them thoroughly can significantly enhance the effectiveness and efficiency of your algorithms.

NEXT UP: Loops and Conditional Statements.