5 Basic Operations You Will Need in Building a Forex EA: Part II

As promised, this article will cover a few examples on how you can use the different kinds of operations in your actual forex expert advisor. Let’s dive right in!

1. Arithmetic

Basic mathematical operations can come in really handy for your forex robot. You can use the addition operation to keep track of the number of open positions, to set your entry orders X number of pips away from the previous candle’s high, or to determine exit points based on your trade entry price. Here are some examples for these:

OpenPositions = OpenPositions + 1;

EntryOrder = PreviousHigh + 0.0015;

StopLoss = EntryOrder + 0.0150;

ProfitTarget = EntryOrder + 0.0300;

Similarly, the subtraction operation can be used to set exit points, such as stop losses for long orders or profit targets for short orders. The multiplication and division operations are generally used for calculating position sizes based on a specified risk percentage. If you’ve already forgotten the formula, you gotta review our School of Pipsology lesson on proper position sizing!

2. Assignment

A few examples on the usage of the most common assignment operator or the equal sign have already been given above. Just remember that the rule of thumb in using assignment operators is to make sure that the data type on the left side of the equation is the same as the data type on the other side of the equal sign.

3. Relational

In running your forex EA, you will need to use relational operations in comparing values. You can use these in checking if a candle closes above or below an SMA or if the number of open positions is still within your limit. Here are some examples:

ClosingPrice > MovingAverageValue

ClosingPrice < MovingAverageValue

OpenPositions <= 5

These are commonly used inside a conditional if-then statement (to be covered later on!), which basically makes sure that certain conditions are met before some commands are executed.

4. Logical

Logical operations are often used in combination with relational operations to set a number of conditions that must be met before running other lines of code. For instance, you can require that the closing price must be greater than the SMA and that stochastic must be below 20 before entering a long trade by using the conjunction operation (&&) like so:

ClosingPrice > MovingAverageValue && StochasticValue > 20

If you’d only like either of this conditions met before entering a long trade, you can use the disjunction operation (||):

ClosingPrice > MovingAverageValue || StochasticValue > 20

As with relational operations, these are commonly used inside compound operators, which will be discussed soon.

5. Comma

Since robots like me tend to like to keep things concise, we sometimes prefer to list a bunch of commands in a single line. Heck, we can combine variable declaration plus a few operations in one go!

To prevent the program from getting confused, the comma is used to remind the robot to execute one command after another from left to right. For instance:

int i=0, i=i+1, i<100

Breaking this down shows that I’ve just declared the variable i as an integer, initialized it with the value of 0, then assigned its new value to its old value plus one, then made sure that it is below 100.

I’m sure you’re wondering why in the world would you ever need to do this so I’m telling y’all that things are just about to get exciting when we start talking about loops and conditional statements next week. See you then!