Automated Trading Course - Part 1: Programming

Discussion in 'Automated Trading' started by jcl, Sep 8, 2012.

  1. jcl


    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.
  2. jcl


    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 Price;
    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:

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

    Now let's take a look at some bad examples:

    var #ItoldYou;
    var 1_for_all;
    var 12345;

    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:

    int MyTradePositions;

    and then we use it later in our code this way:

    mytradepositions = 5; // or
    Mytradepositions = 5; // or

    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:

    var x32;
    var a125;
    var h_34_5;
    var _z34187;

    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.
  3. This is a great idea for a tutorial...

    So far everything is clear!

    Once you get to "loops" and "arrays" I will definitely have some questions to ask. I like most don't have any formal computer training so I am sure my input will be useful.

    To save everyone the trouble of Googling the link here it is Zorro Automated Trading Platforms and Trading Bots
  4. Nice thread. Well explained and provided with good examples.
    Look forward to your posts and a successful tutorial.
  5. Humpy


    Keep going and damn the flak

  6. JCL - very cool thread! I have a programmer do all of my systems work but I would like to learn the programming side. I run ats in NT7 and I am also setting up a system in MC.

    Looking forward to following the thread. :)
  7. 77 % of trader lost money and overall losses were greater than profits made.

    How do you explain that?

    Give them 12 months and they will all lose what they made ,in the championship period market conditions suited the codes , but not for long ...luck .

    others blew their accounts ,or would have lose more than the winners.
  8. That is good. We need the losers to feed the winners, that is how the game works. The winners can't win without the losers on the same field.
  9. You missed the point winners eventually lose it all .statistics.:)
  10. Mr_You


    I'll say it...

    Please do not hijack this thread. Please keep on topic. K? Thx!
    #10     Sep 8, 2012