Strategy script language basics

At minimum, a strategy contains tow sections. Buy section, and Sell section.
Each starts with "if" condition to define the Buy conditions. When we execute the strategy (within the Chart, Strategy optimization or Today modules), these Buy conditions are tested and if all criteria met, Buy signal is issued. Same for the Sell signal.

Example of Buy section: the following will issue a Buy signal whenever the Symbol Close value of today is greater than 10.1:
if ( Close() > 10.1 ) {
Buy();
}

Notes:

  • "if" must be lower case.
  • Buy (and Sell operations) must start with capital letter and as any operator/function follows ().
  • Block of code is located between BEGIN and ENG statements. "{" represents BEGIN and "}" represents END. You may have few commands with the code block
  • All commands within the block code, will execute if the "if" conditions met. In the example, if Close of last day was greater than 10.1.
  • Every statement (which is not ending with { or } ) must end with ";"

    Example of Sell section: the following will issue a Sell signal whenever the Symbol Close value of today is smaller than 1.0:
    elsif ( Close() < 1.0 ) {
    Sell();
    }

    In order for this to work, one more important function is required: MP()
    MP() function stands for "market position". We want to issue a Buy signal, if prior to the Buy we had no market position in the symbol (we did not issue a prior Buy and we do not hold any position yet). MP() function, return "0" if we have no position in the stock, "1" if we have a long position and "-1" if we have a short position. At the terms, we will not issue a Sell signal unless we have a long market position, therefore our sample strategy script will look like this:


    ############################################
    # Our first Sample Strategy #
    ############################################

    if ( MP() == 0 and
    Close() > 10.1 ) {
    Buy();
    }

    elsif ( MP() == 1 and
    Close() < 1.0 ) {
    Sell();
    }

    Notes:

  • The "==" sign is used for comparing (different than "=" that is used to assign value).
  • The sign "#" is used to have a comment.
  • Comment can strat at the beggining of a new line or from the middle of a line.
  • Use "elsif" and not "if" for the Sell rules. Read more on avoiding intra-day signals
  • Important:
    You must write "Close" and not "close" even within a comment, otherwise, the strategy will be saved but will fail to compile and will not be ready for use. Same for Open, Hight, Low, Volue, etc.

    We can now use technical indicator within the Buy or Sell condition, making our strategy more robust, for example, the following strategy is using SMA - simple moving average indicator for both Buy and Sell conditions:


    ############################################
    # Our second Sample Strategy #
    ############################################

    if ( MP() == 0 and
    Close() > 10.1 and
    Close < SMA(14) ) {
    Buy();
    }

    elsif ( MP() == 1 and
    Close() SMA(20) ) {
    Sell();
    }

    The above strategy will issue a Buy signal for every day in which we have no market position, the Close is greater than 10.1 and the Close is also smaller than the simple moving average of the last 14 days. The Sell signal will be issued if we have a market position, and the Close is greater than the simple moving average of the last 20 days.
    For a detailed list of all currently support technical indicators and functions click here.

    That's it! you are ready to write your own strategies now, start with simple ones, make sure that compilation is successful (once you save it, you will get a green message) and later continue with more advanced options and functions.

    Defining variables:
    As in any computers language, you may define variables. You may do mathematical operations on variables and other manipulations, depending on their type. All variable names start with "4" sign and their declaration (and optionally initialization) with "my", for example:

    # declaration only:
    my $counter;

    #declaration and assigning a "9" value.
    my $last_value = 9 ;

    Basic operations on variables:

    $counter = $counter + 112;
    $last_value = $last_value + $counter ;

    You can use variables within "if" and any script line:

    if ( MP() == 0 and
    Close() > $last_value ) {
    $last_value = Close();
    Buy();
    }

    Sample Strategies you may copy and paste into yours:
    3 Days Down
    MINMAX

    Additional readings:
    Writing strategy
    Our Functions and Technical Indicators

    X
    Enter your Strategy Seeker username.
    Enter the password that accompanies your username.
    5 + 2 =
    Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
    Secure Login

    This login is SSL protected

    Loading