Formula Quick Reference

Market Data | Date Functions | Reference Functions | Logical Functions | Math Functions | Statistic Functions | Indicator Functions | Drawing Functions | Drawing Descriptors | Miscellaneous Functions | Formula Forum


The Formula Syntax

General description
  • The formula system provided by tecstock.com is a powerful, yet flexible and easy to use script system.
  • The formula system can be used to design user's own indicators, specify trading rules, draw signals and user defined patterns in charts.
  • The user only needs to specify how data should be calculated. The formula system will do the rest and draw the data in charts.
  • The formula system calculates data based on the time series. Except ZIG() and BACKSET(), all the functions in the formula system use data in the past periods to calculate the result for the current period, i.e., no future data is used. This ensures that once a trading signal, for example, is generated, it will never disappear.
 

Formula name, full name, group and description

  • Each formula has to have a name. A formula name starts with a letter, followed by letters, numbers, or underscores.
  • Formula names are unique for each user. Creating a formula with the same name as a system formula will hide that system formula.
  • Formula's full name is a more descriptive name for the formula. Formula's full name is not mandatory and can be blank.
  • Formulas are cataloged in groups. A group name follows the same rule as formula names do. If the group name is blank, then the formula does not belong to any group.
  • Each formula can have a description which describes the premises, usages of the formula or is just whatever the user wants to say about the formula. Formula's description is not mandatory.
  • All the names used in formulas are case insensitive.

MACD, MACD_1, MY_MACD_2 are all valid formula names.

my_fml and My_Fml are referring to the same formula.

Parameters
  • A formula can have as many parameters as it needs. For a practical use of formulas, however, four parameters are sufficient in most cases.
  • Each parameter has a name. A parameter name follows the same rule as formula names do.
  • For each parameter, a default value and its minimum and maximum values have to be specified.
  • When referring a formula without specifying parameters, default parameter values will be used.

The system formula MACD has three defined parameters:

SHORT 12, 5, 40
LONG 26, 20, 100
M 9, 2, 60

Calling MACD(1, 1, 1) will cause the system generating an error message indicating that the parameters are invalid.

Formula expressions and internal/external variables

 

  • A formula consists of a set of statements.
  • Each statement has to be terminated by a semicolon.
  • A statement is an expression or a drawing function plus drawing descriptors.
  • An expression can have a name, also refered to as a variable which holds the result of the expression so that other expressions can refer to it.
  • Expressions without any name are called anonymous expressions. The result of an anonymous expression is held in a system generated variable without name.
  • A user defined variable name follows the same rule as formula names do.
  • Variable names are unique within a formula.
  • There are two kinds of variables in formulas: internal and external variables.
  • Internal variables are used to hold temporary results, while external variables hold the results which will be drawn in charts by the system. External variables can be referred to by other formulas.
  • Internal/external variables can be defined in the following way:

    var_1 := expression; # var_1 is an internal variable
    var_2 : expression; # var_2 is an external variable


  • Unless drawing functions are used, a formula has to have at least one external variable.
  • A Formula can have unlimited number of internal and external variables. Since external variables hold results which will be drawn in the chart, the user should avoid using too many external variables in formulas or the chart will get messy.

v1 := ema(close, 12); # internal variable v1
v2 := ema(close, 26); # internal variable v2
# the following expression uses results
# held in v1 and v2 and assign
#
the result to an external variable diff.
# the system will draw diff in the chart.
diff : v1 - v2;

Arithmetical and logical operators
  • An expression consists of operands and arithmetical and/or logical operators in a natural way. Arithmetical operators supported are

    + - * /


  • Logical operators supported are

    = != <>
    > <  
    >= <=  
    AND && &
    OR || |


  • For more examples, see Formula Help

# b is set to 1 for the current period
# if the close price in the current period is
# greater than the close price in the previous
# period, otherwise, b is set to 0
b := close > ref(close, 1); # up

# d is set to 1 for the current period
# if the close price in the current period is
# the same as the close price in the previous
# period, otherwise, d is 0
d := c = ref(c, 1); # in balance

# assign a constant value 60 to the internal
# variable m
m := 60;

# the internal variable v is set to the average
# value of the current close, open, high and low
# prices for the current period.
v := (close + open + high + low) / 4;

# set the external variable cond to 1 in the current
# period if the close price went up in each of the
# recent 3 periods, otherwise set cond1 to 0
cond:close>ref(close,1) and ref(close,1)>ref(close,2) and ref(close,2)>ref(close,3);

Drawing functions
  • The formula system provides a number of drawing functions to ensure flexibility and high quality of charts:

    DRAWICON()
    DRAWLINE()
    POLYLINE()
    STICKLINE()
    FILLRGN()
    PARTLINE()
    VERTLINE()
    DRAWNUMBER()
    DRAWTEXT()

  • For more details and examples, see Drawing Functions

# draw 5-periods simple moving average of the
# close price using the red color and lines with
#
the thickness 2
ma(close, 5), colorred, linethick2;

# draw close price using dashed line with the
# color ffee00
my_close_price : close, colorffee00, linestyle1;

Drawing descriptors
  • The formula system provides a number of drawing descriptors which tell the system how to draw the data. Some descriptors are

    COLOR
    ALIGN
    LINETHICK
    LINEstyle
    TRANSPARENCY

  • For more details and examples, see Drawing Descriptors

# draw 5-periods simple moving average of the
# close price using the red color and lines with
#
the thickness 2
ma(close, 5), colorred, linethick2;

# draw close price using dashed line with the
# color ffee00
my_close_price : close, colorffee00, linestyle1;

Formula reference
  • Formula expressions can refer to other formula's results to avoid redefinning formulas.
  • The general syntax for formula reference is the following:

    "FormulaName"(parm_1, .., parm_n)

    or

    "FormulaName.ExternalVar"(parm_1, .., parm_n)

    The first expression refers to the last external variable in the formula FormulaName (so FormulaName has to have at least one external variable in this case), while the second expression refers to the external variable ExternalVar in the formula FormulaName. In both cases, if parameter list is missing, the default parameter values of FormulaName will be used.
  • If a system formula ABC exists and a user defines his/her own formula using the same name ABC, then the system formula ABC will be hidden by the user's formula. I.e., the following code will refer to user's formula ABC instead of system's one.

    v1 := "ABC.var1";

    If in the user defined formula ABC, the external variable var1 does not exist, a syntax error message will be generated by the system.

# The system formula KDJ has three external
# variables K, D, J. The following formula
# reuses the formula KDJ to generate
# buy and sell signals

k := "KDJ.K"(10, 3, 3); # call exsting formula with parameter 10, 3, 3
d := "KDJ.D"(10, 3, 3) ;
j := "KDJ.J"(10, 3, 3);

cond1 := d < 20 && cross(k, d);
cond2 := d > 80 && cross(d, k);
buy: cond1;
sell: cond2;

Stock data reference
  • Formula expressions can also refer to specific stock data. This is some time useful when the user wants to create a formula which checks the market status using, for example, Nasdaq's data.
  • The general syntax for stock data reference is the following:

    "StockName$FormualName"(parm_1, .., parm_n)

    or

    "StockName$FormulaName.ExternalVar"(parm_1, .., parm_n)

    The first expression refers to StockName's data held by the last external variable in the formula FormulaName (so FormulaName has to have at least one external variable in this case), while the second expression refers to StockName's data held by the external variable ExternalVar in the formula FormulaName. In both cases, if parameter list is missing, the default parameter values of FormulaName will be used.

# The system formula KDJ has three external
# variables K, D, J. The following formula uses
# the Nasdaq data calculated by
# formula KDJ to generate buy and sell signals

k := "^IXIC$KDJ.K"; # call with the default parameters
d := "^IXIC$KDJ.D";
j := "^IXIC$KDJ.J";

cond1 := d < 20 && cross(k, d);
cond2 := d > 80 && cross(d, k);
buy: cond1;
sell: cond2;

Comments
  • In formulas, any text between the pound sign '#' and the line end is considered as a comment and will be ignored.

# This is a comment

a : MA(close, 30); # this is also a comment

 

Function Index
abs | align | avedev | backset | barscount | barslast | barssince | between | capital | ceiling | circledot | close | color | colorstick | cost | count | cross | crossdot | date | datediff | day | dma | drawicon | drawline | drawnumber | drawtext | ema | every | exist | fillrgn | filter | finance | floor | hhv | hhvbars | high | if | intpart | iscamarket | isdown | isequal | islastbar | isup | isusmarket | last | linestick | linestyle | linethick | llv | llvbars | ln | log | longcross | low | ma | max | min | mod | month | not | open | partline | peak | peakbars | peaktrough | peaktroughbars | pointdot | polyline | pow | range | ref | relate | reverse | sar | sarturn | sgn | sigperform | slope | sma | std | stick | stickline | sum | sumbars | transparency | trough | troughbars | var | vertline | vol | volstick | weekday | winner | year | zig