We've talked a lot about expressions, but what is an expression? An (arithmetic) expression is a sequence of variables, numbers, operators, etc that are linked together. You can have simple expressions that look like the one below:
if (indicator > 50) // simple expression inside the parenthesis
{
// do something
}
Or you can have arbitrary complicated expressions that are a combination of sub-expressions:
if((factor1*indicator1 > 50) and (indicator2 < 35) and (volatility < 10)) // more complicated expression
{
// do something
}
You can combine as many expressions as you want, using parentheses, just like in the example above. The same rules from your math classes apply here, too. Sadly, you have to know some basic math if you want to be a good trader.
----------------------------------
Now that you know everything about if statements, let me just talk a little about a statement that seems quite similar to if, but introduces a new concept in the programming language: Loops.
While I have less than a million, I must continue trading.
That was just an example of a "while loop". In code it would look like this:
while (my_money < 1000000)
trade(my_money);
The while statement has the same syntax as the if statement. There is some expression - in this case the condition that the variable my_money is less than a million - and a command that is executed when the expression is true. However, in the case of an if statement, the program whould then go on with executing the next command afterwards. In the case of a while statement, the program "jumps back" to the while condition and tests it again. If it's still true, the command is executed again. And the while condition is then tested again. And so on. This process is repeated until the while expression eventually comes out false, or until eternity, whichever happens first.
A "loop" is called so because the program runs in a circle. Here's the basic form of the while loop:
while (some condition is true)
do_something(); // execute this command repeatedly until the condition becomes false
or
while (some condition is true)
{
// execute all commands inside the curly brackets repeatedly until the condition becomes false
}
Note that whatever the commands do, they must somehow affect the while condition, because when the condition never changes, we have an "infinite loop" that never ends!
We're now almost at the end of the introduction in writing EAs, scripts, and program. One last programming exercise and then we'll start with developing real trade systems. Please ask when you run into any problems or if something was unclear.
if (indicator > 50) // simple expression inside the parenthesis
{
// do something
}
Or you can have arbitrary complicated expressions that are a combination of sub-expressions:
if((factor1*indicator1 > 50) and (indicator2 < 35) and (volatility < 10)) // more complicated expression
{
// do something
}
You can combine as many expressions as you want, using parentheses, just like in the example above. The same rules from your math classes apply here, too. Sadly, you have to know some basic math if you want to be a good trader.
----------------------------------
Now that you know everything about if statements, let me just talk a little about a statement that seems quite similar to if, but introduces a new concept in the programming language: Loops.
While I have less than a million, I must continue trading.
That was just an example of a "while loop". In code it would look like this:
while (my_money < 1000000)
trade(my_money);
The while statement has the same syntax as the if statement. There is some expression - in this case the condition that the variable my_money is less than a million - and a command that is executed when the expression is true. However, in the case of an if statement, the program whould then go on with executing the next command afterwards. In the case of a while statement, the program "jumps back" to the while condition and tests it again. If it's still true, the command is executed again. And the while condition is then tested again. And so on. This process is repeated until the while expression eventually comes out false, or until eternity, whichever happens first.
A "loop" is called so because the program runs in a circle. Here's the basic form of the while loop:
while (some condition is true)
do_something(); // execute this command repeatedly until the condition becomes false
or
while (some condition is true)
{
// execute all commands inside the curly brackets repeatedly until the condition becomes false
}
Note that whatever the commands do, they must somehow affect the while condition, because when the condition never changes, we have an "infinite loop" that never ends!
We're now almost at the end of the introduction in writing EAs, scripts, and program. One last programming exercise and then we'll start with developing real trade systems. Please ask when you run into any problems or if something was unclear.