I’m hoping you’ve got your basic forex EA operations down pat since we’ll be taking things up a notch and looking at operators, which contain a bunch of simple expressions enclosed in a pair of curly braces. Here’s an overview of the different types of operators you might need:
1. If-Else
The if-else statement is probably the most commonly used compound operator by forex robots, as the ‘if’ component specifies a certain set of conditions that must be met before certain expressions are executed. The ‘else’ component, which can actually be omitted, also gives commands that will be run if the conditions are not met.
In English terms, you can use this conditional operator to say “IF it’s raining in the morning, I will bring an umbrella and a jacket. ELSE, I will bring a cap.” or “IF my weight goes beyond 120 lbs., I will never eat carbs again.”
For a forex EA, this compound operator can be used to tell the program “IF an hourly candle closes above the 200 SMA and the succeeding candle closes above the previous high, set a buy order. ELSE, don’t take any action.”
2. For
The for statement is also a useful compound operator that basically says “FOR as long as these conditions are met, execute the following commands.” It is somewhat similar to the if-else statement minus the ‘else’ component, except that the for statement is used more often when one wants a cycle to be repeated.
This usually applies to risk management rules wherein one wants to set a maximum amount of orders open or make sure that the total risk limit is not exceeded. For instance, you can tell the forex robot “FOR as long as the total number of open positions is less than five, execute another valid trade signal then add to the total number of trades.”
If you’ve probably noticed, another characteristic that distinguishes the for statement from the if-else operator, it’s that the former can also add subsequent operations in order to update the number of open orders or total risk or whatever the program is keeping track of.
3. Break
Nothin’ fancy with this one! The break operator simply tells the forex robot to take a break, grab some pizza, watch a movie, get a drink, hit the beach… Hmm, it looks like I’m the one who needs a break!
This operator can be used inside the for statement in order to tell it to stop. Otherwise, it could go on in an infinite loop and possibly cause your monitor to show that dreaded BSOD when your machine eventually gets exhausted. It can also be used to tell the forex robot to take a break at the end of a trading session.
Next week, we’ll zoom into each type of operator to look at more forex world examples and the proper syntax. See you then!