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.
These operators perform basic mathematical operations:
int result = 5 + 3; // result is 8
int result = 5 - 3; // result is 2
double result = 4.5 * 2; // result is 9.0
double result = 9.0 / 2; // result is 4.5
int result = 11 % 3; // result is 2
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.
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:
== (Equal to)
Heres how to use it:
(5 == 5); // isEqual is true because 5 does equal 5
!= (Not equal to)
Heres how to use it:
(5 != 4); // isNotEqual is true because 5 does not equal 4
< (Less than)
Heres how to use it:
(3 < 5); // isLess is true because 3 is less than 5
> (Greater than)
Heres how to use it:
(6 > 5); // isGreater is true because 6 is greater than 5
<= (Less than or equal to)
Heres how to use it:
(4 <= 4); // isLE is true because 4 is less than OR equal to 4.
>= (Greater than or equal to)
Heres how to use it:
(7 >= 6); // isGE is true because 7 is greater than 6
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; }
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:
(1 > 2 && 4 > 3); // result is false because both conditions are not true... Only one is.
(1 > 2 || 2 > 1); // result is true because this statement is asking if 1 is greater than 2 OR if 2 is greater than 1. As long as one of these statements is true, the condition is fulfilled.
(5 + 4 != 1); // result is true because 5 + 4 is NOT equal to 1
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.
These operators assign values to variables:
int a = 5;
a += 3; // a is now 8
a -= 2; // a is now 6
a *= 2; // a is now 12
a /= 3; // a is now 4
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.
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; }
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.