Previous: Script Introduction

Strategy Coding Tutorial

Welcome to the coding tutorial! Walk through 7 workshops for becoming a trading system programmer - even if you've never programmed before.

The language we'll be using is named "C". It has the syntax in which most professional software today is written and which most trade platforms - such as MetaTrader 4™, Ninja Trader™, and of course Zorro - use for trading scripts. The first 3 workshops give you an introduction into formulating tasks in C code - also called 'coding'. The next 4 workshops are about developing and optimizing trading strategies. For testing the examples, you can use the free Zorro platform. If you don't have Zorro yet, just download it from zorro-project.com.

Zorro's programming language, lite-C, is a 'lite' version of the C/C++ programming language. The 'lite' means here it contains a framework that hides complex stuff - like pointers, structs, etc. - that is difficult to beginners. lite-C was originally developed for the now defunct computer game company Atari, for controlling the artificial intelligence of actors in computer games. As you can imagine, a language that controls ten thousands of actors in a virtual world, and at the same time is easy enough to be used by computer gamers for the behavior of their players, is an excellent choice for trading strategies. After finishing the tutorial, you can use your programming knowledge not only for C-based languages like lite-C, C#, C++, or Java. Most languages have a similar structure. So after finishing the first workshops, you will also be able to at least understand code in Python, Pascal, or Tradestation's EasyLanguage, even though the latter declares a variable not with var x = 7, but with Vars: x(7).

Here's our curriculum:

Although the strategies in this tutorial are profitable in the tests, they are meant for educational purposes only and are designed for simplicity, not for maximum profit. For trading with real money, better develop your own strategies.

Workshop 1: Variables

A script (or program) is a list of instructions in plain text format that tell the computer what to do under which circumstances. Any script consists of two types of objects, variables and functions. In this first workshop 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, a variable has a name that's used in the script. A few example script lines that define variables:

var Price;
var PercentPerMonth = 1.5; // the monthly interest
int Days = 7;
string Wealth = "I am rich!";
bool Winning = true;

We can see from those examples:

► A variable must be defined (programmers also say 'declared') before being used. The only exception are pre-defined variables such as Spread, Stop etc. that Zorro already knows. We have different variable types: var for a number with decimals, like prices or rates; vars for a series of many numbers; int for a number that has 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 more basic variable types in lite-C, but in trading scripts you'll normally encounter only these five.

► 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 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 _. Some valid names:

var AlohA;
var _me_too;
var go42;
var _12345;

Some bad examples:

var #ofList;
var 1_for_all;

► Variable names are case sensitive. MyTradePositions is a different variable than mytradepositions.

► You can define several variables of the same type in one line. This saves lines and keeps your code short. Example:

var momentum, strength, score;
int width = 7, height = 14, depth = 20;

► Variables should have significant names. Otherwise you will have problems trying to remember what these variables do if you look at your script a few weeks later.

The first script

Start Zorro, select Workshop1 in the Script dropdown menu, then press [Edit]. The strategy script editor opens up and shows you this script:

// Tutorial Workshop 1: Variables
////////////////////////////////////////

function main()
{
  var a = 1;
  var b = 2;
  var c;

  c = a + b;
  printf("Result = %.f",c);
}

The script starts with a comment (the lines beginning with //) that tells us what it is. Then we have a function named main - everything that happens in a program is inside a function. But we'll come to functions in the next workshop. Here we concentrate on variables, and in the next lines we can see that the 3 variables a, b, c are defined, just as described above.

Now, press [Test] on Zorro's panel, and watch what happens in its message window:

You can now edit the script in the editor, replace the a and b variable definitions with different numbers, like this:

var a = 5;
var b = 12;

Save the edited script (File / Save or [Ctrl-S]), then press [Test] again: it looks like c is the sum of a and b. This happens in the following line:

c = a + b;

This is a command (also called instruction) to the computer to add the content of the variables a and b, and store the result in the variable c. Commands are lines in the code that usually do something with variables. The last line is also a command, used for displaying the content of c in the message window:

printf("Result = %.f",c);

You have now done the first steps with lite-C: Zorro has added two numbers.

Next: Functions


Further reading: ► Variables. Expressions.