In this lesson we are going to take a practical look at “if statements”.
By learning these, you will be up and running in no time coding your own entries and exits.
Now, have a look at the code snippet below:
What you just read was an if statement that gives the instruction to buy on the open of the next bar, if the close is greater than the open.
In other words, the code snippet contained a condition, and an instruction.
The condition was Close> open
The instruction was Buy next bar open
An If statement checks if the condition is true or false, and executes the instruction if the condition is true.
Here is another example
Translation: Buy if the close is greater than yesterday’s close.
The best way to learn if-statements is to start using them yourself!
In the box below you have a template that comes very handy when you’re just starting out. Just by replacing condition and instruction, you will be up and running quickly, coding different entries and exits!
If condition then instruction ;
Below follow some different conditions that you could use to replace condition and instructions you could use to replace instruction.
Right now we’re only going to compare the high, low, open and close of the bar to keep it simple. In the coming lessons you’ll be introduced to indicators and functions.
Try to build one if-statement for the entry and one for the exit. That way you will get a trading strategy that actually enters and exits a trade.
Remember that you can chain several conditions together. You just separate them with and, like below:
Now you just have to practice combining conditions and instructions yourself until you get the hang of it! In the exercise section below you will find a couple of exercises.
We recommend that you apply the strategy to a chart, and open up the performance report window.
Then you will see the performance report change as you edit the strategy, which makes it a lot more fun!