Automated Trading Course - Part 1: Programming

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

  1. jcl

    jcl

    A little more about if statements. Let's assume that you want to do something only when two different conditions are fulfilled. Try the following program:

    Code:
    function main()
    {
      var loss = slider(2,25,0,200,"Loss",0);
      var profit = slider(3,50,0,200,"Profit",0);
      if((profit > 50) and (loss == 0))
        printf("Enough!");
      else
        printf("Not enough!");
    }
    Now two sliders are involved. How do you need to set them for the "Enough!" condition? We leave this as a little puzzle to the reader... but we've learned that we can combine two conditions using the "and" keyword. There is also an "or" keyword when one or the other condition needs be true. (C/C#C++ programmers might wonder why they have to use and and or instead of the familiar operators && and ||. Don't worry, all C operators work as before, we're just using easier-to-memorize operators in lite-C for beginner's sake.)

    Now, I'm giving you three important tips for avoiding coding mistakes. Here's the first one. Have you noted the use of parentheses around (profit > 50) and (loss == 0)? We know from school mathematics that in a mathematical equation, the expressions in the parentheses are solved first. (1+2)*3 is not the same as 1+(2*3) - this is true in mathematics and also in a programming language. Always use parentheses to make sure that the program calculates in the same order that we want... and make sure that you have as many opening as closing parentheses! A missing parentheses at the end of a line is one of the most frequent reasons of compiler error messages. The computer will usually complain about an error in the following line because it's there looking for the missing parenthesis.

    What's with that "loss == 0" in the first new line of code? Is the double equals sign a typing error? No, it isn't. Whenever you compare two expressions (loss and 0 in the example above) you have to use "==" instead of "=", because a line of code that looks like this:

    if(loss = 0)
    {
    // do some stuff
    }


    will set loss to zero instead of comparing loss with zero! This is one of the most frequent mistakes; even an experienced programmer might set a variable to a certain value by mistake, instead of comparing it with that value. Using one instead of two equality signs for comparing two expressions is a very frequent mistake. Don't forget this!

    if (a == 3) // correct
    {
    // do_some_stuff
    }

    if (a = 3) // wrong!
    {
    // do_some_stuff
    }


    You can avoid this mistake if you make it a habit to put the constant value on the left side of a comparison and the variable on the right side. The loss comparison statement would look this way:

    if(0 == loss) // correct
    ...



    If you then accidentally put a single '=' instead of '==', Zorro will report an error because it knows that 0 can't be set to a different value. That was the second tip.

    The third tip is that you should use the '==' comparison with care. When you calculate a var variable in a complicated mathematical expression, the result is usually inaccurate. Instead of 2, it might be 2.00001 or 1.99997. This is due to the limited precision of variables in a computer. When you then compare it with 2, the comparison always comes out false - so better use 'greater' or 'smaller' comparisons (< or >) when working with var variables. This problem does not affect int variables.


    We've talked a lot about expressions so far, 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!

    Here's a practical example of a while loop. Run this in Zorro:

    Code:
    function main()
    {
      var n = 0; 
      while(n < 10) { 
        n = n + 1;
        printf("%.f ",n);
      }
    }

    This little program adds 1 to the variable n, prints the variable to the message window, and repeats this until n is 10.

    [​IMG]

    Loops are very useful when something has to be done repeatedly. For instance, when we want to execute the same trade algorithm several times, each time with a different asset. This way we can create a strategy that trades with a portfolio of assets.


    We are now at the end of the first part of the course. In the next part we'll begin with developing real trading strategies; you've already learned all programming stuff that you'll need for those lessons. But there's far more to programming - we haven't touched yet concepts such as macros, pointers, arrays, or the Windows API. lite-C also supports some elements from C++, the 'big brother' of C - such as classes or function overloading. If you want, you can continue on your own with online C tutorials. You can also visit the Gamestudio community that uses Zorro's lite-C language for programming small or large computer games.

    If you want to look at some 'real' C program, run the script Mandelbrot. It has nothing to do with trading and you will probably not understand yet much of the code inside. It is a normal Windows graphics program. Don't worry, trading strategies won't be this complicated - it just shows that programming has a lot more depth and can be a lot more fun even without earning money with it. ;)

    [​IMG]
     
    #31     Sep 13, 2012
  2. jcl

    jcl

    #32     Sep 16, 2012
  3. Humpy

    Humpy

    Sorry to be a bit dense but on the first page I got this far and pressed test only to get the error message.
     
    #33     Sep 17, 2012
  4. jcl

    jcl

    You must save the script into the Strategy folder under a name like "myfirstscript3.c" (don't forget the ".c"), and then select "myfirstscript3" in the Script scrollbox.
     
    #34     Sep 17, 2012
  5. Humpy

    Humpy

    Thanks I will try that.
     
    #35     Sep 17, 2012