Some people have expressed interest in a course about programming automated strategies, so I'll post the first part of the course here over the next days. The first part is about learning programming in C, and covers variables, functions, branches, and loops. The second part will be about trading strategies, and cover trend trading, counter trend trading, optimizing, walk forward analysis, portfolio strategies, and money management. It uses some new trading algorithms such as frequency filters.
The course will go over about 2 weeks. The goal is to develop a portfolio strategy with a solid profit. There might be a third part about using artificial intelligence, such a perceptrons and decision trees, but it's not finished yet and comes at a later time.
The course is based on my half finished C trading tutorial. I hope from posting it here that I get some feedback about what is easy to understand and what not, so that I can improve the tutorial.
The language C is used for the course because it's simple and clear, and can relatively easily be converted to C#, EasyLanguage, or most other scripts used by trading platforms. For testing and trading the examples here, we'll use the free Zorro platform. I can not post a download link here, but you can download it from the web when you google for "Zorro Trading Automaton". I hope no one will be offended when I mention this software here, but we'll need some free platform for doing the examples.
Let's start working. At first a little theory. The purpose of a script (or program) is telling the computer what to do under which circumstances. A script consists of variables and functions - in this first lesson let me explain variables. A variable is a place in your computer's memory (just like a container) that is used to store numbers, text, or other information. Because you don't want to have to remember where in the computer which variable is stored, any variable has a unique name that's used in the script. A few example script lines that define variables:
var PercentPerMonth = 1.5; // the monthly interest
int Days = 7;
string Wealth = "I am rich!";
bool Winning = true;
These are a few short lines of code but we can use them to learn many new things:
► Every variable must be defined (programmers also say 'declared') before being used. We have different variable types: var for variables with decimals, like prices or rates; int for variables that have no decimals, such as for counting something; string for text; and bool for a sort of 'toggle switch' that is either true or false. There are even more basic variable types, but in trading scripts you'll normally encounter only these four. If you write this line in your script:
Days = 3;
and you haven't defined the variable named Days before, you'll get an error message. The only exception are variables that Zorro already knows because they are pre-defined in the compiler.
► Any variable can receive an initial value at start, but we aren't forced to do that when the initial value does not matter. Example:
int num_bars = 7;
var limit = 3.5;
► We can add our own comments to the code. Every time it encounters two slashes //, Zorro will ignore the words that follow it, up to the end of the line. This way we can add useful information to our code:
int bar_width; // the width of one bar in minutes
or we can temporarily disable a line in the script by placing two slashes in front of it. This is called "commenting out" a line, and while programming it is used so frequently that the script editor has two extra buttons for commenting out and commenting in.
► Every definition or any command in C needs to end with a semicolon. Many beginners forget to add ";" at the end of their lines of code, and this also leads to an error message - not in the line with the missing semicolon, but in the following line!
► Every variable name must start with either a letter or with an underscore _. Here are some valid variable names:
Now let's take a look at some bad examples:
I'll let you discover what is wrong in the definitions above.
► Variable names are case sensitive. This means that if we define an int this way:
and then we use it later in our code this way:
mytradepositions = 5; // or
Mytradepositions = 5; // or
MYTRADEPOSITIONS = 5;
Zorro will not accept it.
► You can define several variables in one line. This saves lines and keeps your code short. Example:
var momentum, strength, score;
int width = 7, height = 14, depth = 20;
► Finally, the variables should have significant names. While it is possible to define a pile of variables that look like this:
it isn't a good idea to do it this way. You will have problems trying to remember what these variables do if you look at your script a few weeks later. If you want to show your script to other people, they will have a hard time trying to figure what you wanted to do with your code.
Tomorrow we'll write a little example script that does something with variables.
Please don't hesitate to ask here if something is unclear or bad explained.