Defining and Using Variables in Backtesting

This topic contains 0 replies, has 1 voice, and was last updated by Seer Seer 7 years, 1 month ago.

Viewing 1 post (of 1 total)
  • Author
  • #621

    Scalar Variables

    A scalar variable stores exactly one thing, such as a number, a piece of text, or a column. Items that can be stored in a scalar variable are called scalar values.

    The name of a scale variable starts with the character $ followed by at least one letter, which is then followed by one or more digits or character.

    The following are examples of legal scalar variable names:



    The following are examples of incorrect scalar variable names:


    $               #There must be at least one letter in the name
    $78high         #The second letter must be a letter
    $high!          #The charater ! is not allowed
    $close.price    #The charater . is not allowed

    Varable names are also case sensitive, all the following are different:




    Declaring a variable

    Before you use a variable you must tell Seer that you are going to use it. This is typical done by the keyword my.

    For example:


    my $text;

    This tells Seer that you’ll be using a variable called $text. You can also assign a value to the variable when you declare it:


    my $text='a message';

    You can also declare more than one variable at the same time:


    my ($start,$end,$step);

    This statement declares, three variables, $start, $end and $step. Each variable is separated by a comma and enclosed by brackets.

    You can also set the initial value of multiple variables:


    my ($start,$end,$step)=(20,100,5);

    Here we are setting $start to 20, $end to 100 and $step to 5.


    If you forget to declare a variable you will be presented with an error message. For example, if you have the following in the account begin event:



    You’ll be given this error message:


    Error in account begin event : Global symbol "$var" needs to be explicitly declared at line 1

    To fix this error, add my:


    my $var=98;

    Once a variable is declared in this fashion, you can use it any way you wish.


    Assigning a value to a Scalar Variable

    To assign a value to a scalar you use the assignment operator =

    for example:


    $var = 87;

    This assigns the value 87 to the variable $var.

    You can use the assignment statement to assign other things to a variable:


    $close = Close;

    This assigns the column Close to the variable $close.


    $text = 'a message';

    This assigns the string “a message” to the scalar variable $text


    Variable Scope

    The life of a variable and its contents are determined by the scope. The scope of a variable is typically the block it’s contained within, such as event, indicator, chart or function.

    For example, if the following variable was defined and used within the bar event:


    my $count=1;

    The variable $count would always contain 1 when the bar event is fired. In other words, the variable $count has no memory of it’s previous value. In many cases this is what you want, but in other cases you will want the scope, and the life of the variable to last beyond the event.

    To do this, you have to define the variable using the variables object. This object will allow you to create a variable that not only lasts beyond the scope of the bar, but also across event calls and in the case of an Account variable, across systems too.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.