Mathematical Functions
This section lists a variety of mathematical functions including statistical functions, trigonometry, and rounding.
Absolute Value
Usage
ABS(A)
- a
- A number.
Description
Finds the absolute value of a. That is, the value of a without regard to sign.
The abs function is often used to calculate the difference between two values when it is not known which of the two values is greater.
Example
abs(4) is 4, and abs(-4) is 4.
abs(close-open) calculates the difference between Close and Open price, and always returns a positive result, regardless of whether the Close or Open is higher. (This is equal to the height of the body of a candle.)
Note that abs(close-open) is equivalent to abs(open-close) because both will return the positive difference.
See Also
Add
Usage
ADD(n1,n2...)
- n1, n2…
- Values to be added together
Description
Adds all its parameters together. This is equivalent to n1+n2. Multiple parameters can be passed in.
Example
add(3,2) returns 5.
add(3,2,5,2) returns 12
See Also
Ceiling
Usage
CEILING(N) or CEIL(N)
- n
- A number.
Description
Calculates the smallest integer which is greater than or equal to n. That is, rounds the number up to the nearest whole number. If the number is negative, it is rounded towards zero.
The functions ceiling and ceil are equivalent.
Remarks
There are many different rounding techniques. Differences between them commonly relate to how numbers at and beyond 1/2 are treated, and how negative numbers are treated. The following table show how various examples would be rounded.
3.4 | 3.5 | 3.6 | -3.4 | -3.5 | -3.6 | |
int | 3 | 3 | 3 | -3 | -3 | -3 |
round | 3 | 4 | 4 | -3 | -4 | -4 |
floor | 3 | 3 | 3 | -4 | -4 | -4 |
ceiling | 4 | 4 | 4 | -3 | -3 | -3 |
Example
ceiling(4.2) returns 5.
ceiling(-3.6) returns -3.
See Also
Correlation
Usage
CORREL(EXPR1,EXPR2,N)
- expr1
- The first data set.
- expr2
- The second data set.
- n
- Number of periods used in calculation.
Description
Calculates the statistical correlation between the two datasets returned by expr1 and expr2 over n periods.
Note that correl can be used in conjunction with loadsymbol to calculate the correlation between two different securities, or a security and an index.
Example
{ Calculate the correlation between the current security and the all ords }
symb := inputsymbol(“Security”,”XAO”);
otherClose := LoadSymbol(symb, Close);
correl(Close, otherClose);
Divide
Usage
DIV(A,B)
- a
- The numerator.
- b
- The denominator.
Description
Calculates a divided by b. This is equivalent to writing a/b. If the divisor is zero then the special undefined result is returned.
Example
div(8,2) returns 4.
See Also
Exponent
Usage
EXP(POWER)
- power
- The power that e is raised to.
Description
Raises the natural exponent ‘e’ (approximately 2.718) to the power of power. That is, it returns epower.
To raise any arbitrary number to the power of another, use the operator. For example, 3^2 is equivalent to the calculation 32 and returns 9.
Exp and log are inverse operations such that:
n=log(x) and x=exp(n) hold for the same x and n.
Example
exp(5)
See Also
Fibonacci
Usage
FIB(N)
- n
- The nth Fibonacci number is returned.
Description
Returns the nth Fibonacci number. Fibonacci numbers are a sequence of numbers were each number is equal to the sum of the previous two. The first few Fibonacci numbers are: 1,1,2,3,5,8,13,21…
The Fibonacci number series appears frequently in nature, and some believe that Fibonacci patterns can also be found in stock market data.
Example
fib(1) returns 1.
fib(2) returns 1.
fib(3) returns 2.
fib(4) returns 3.
Floor
Usage
FLOOR(N)
- n
- A number.
Description
Rounds n to a whole number by calculating the greatest integer which is smaller than or equal n.
Remarks
There are many different rounding techniques. Differences between them commonly relate to how numbers at and beyond 1/2 are treated, and how negative numbers are treated. The following table show how various examples would be rounded.
3.4 | 3.5 | 3.6 | -3.4 | -3.5 | -3.6 | |
int | 3 | 3 | 3 | -3 | -3 | -3 |
round | 3 | 4 | 4 | -3 | -4 | -4 |
floor | 3 | 3 | 3 | -4 | -4 | -4 |
ceiling | 4 | 4 | 4 | -3 | -3 | -3 |
Example
floor(4.2) returns 4.
floor(-3.6) returns -4.
See Also
Fractional Part
Usage
FRAC(N)
- n
- A number.
Description
Returns the fractional (decimal) portion of n.
Example
frac(4.87) returns 0.87
frac(-7.3) returns -0.3
See Also
Int
Usage
INT(N)
- n
- A number.
Description
Returns the integer portion of n.
Remarks
There are many different rounding techniques. Differences between them commonly relate to how numbers at and beyond 1/2 are treated, and how negative numbers are treated. The following table show how various examples would be rounded.
3.4 | 3.5 | 3.6 | -3.4 | -3.5 | -3.6 | |
int | 3 | 3 | 3 | -3 | -3 | -3 |
round | 3 | 4 | 4 | -3 | -4 | -4 |
floor | 3 | 3 | 3 | -4 | -4 | -4 |
ceiling | 4 | 4 | 4 | -3 | -3 | -3 |
Example
int(7.8) returns 7
int(-7.8) returns -7
See Also
Ceiling | Floor | Frac | Round
Linear Regression
Usage
LINEARREG(EXPR,N)
- expr
- Data to be processed.
- n
- Number of periods sampled.
Description
Calculates the Linear Regression indicator for expr over n periods.
Example
linearreg(C,21)
See Also
Linear Regression Slope
Usage
LINREGSLOPE(EXPR,N)
- n
- Number of periods sampled.
Description
Calculates the Linear Regression Slope indicator for expr over n periods.
Example
linregslope(C,21)
See Also
Logarithm
Usage
LOG(N[,BASE])
- n
- A number.
- base
- The log base that will be used, or e if omitted.
Description
Calculates the base logarithm of n. If base is not given, then the natural logarithm base, e, is used.
The log has the effect of returning the order of magnitude of a number.
Log and power are inverse operations such that:
x=power(b,n) and n=log(x,b) hold for the same x,b and n.
Log and exp are inverse operations such that:
n=log(x) and x=exp(n) hold for the same x and n.
Example
log(Volume) calculates the logarithm of Volume using base e.
log(Volume,10) calculates the logarithm of Volume using base 10.
log(10000,10) returns 4, because 104 is 10000.
See Also
Max
Usage
MAX(N1,N2...)
- n1, n2
- A list of numbers.
Description
Returns the largest value currently assumed by any of its parameters.
Max should not be confused with the Highest function, which returns the largest value a single parameter has assumed over a number of bars.
Example
max(2,5,4) is 5.
max(open,close) is the greater of the open and close price for the current bar.
highest(close,5) is the largest value of close over the last 5 days.
max(expr1, 5) This example uses the min function to put an lower limit on the value returned by expr1. If expr1 is greater than 5, then expr1 will be returned unchanged. But if it’s less 5, then 5 is returned.
min(max(expr1,0),100) The above technique can be used in this way to restrict a result to a range. In this example, expr1 is cropped to return a result between 0 and 100.
See Also
MaxLinearReg
Usage
MAXLINEARREG(EXPR,N1,N2[,AHEAD])
- expr
- Data that linear regressing is performed on.
- n1
- Minimum number of periods. At least 2.
- n2
- Maximum number of periods. At least 2.
- ahead
- The number of days that each linear regression forecasts ahead.
Description
Calculates the result of a linear regression of every length between n1 and n2inclusive, then returns the largest result.
Example
maxlinearreg(C,26,52)
maxlinearreg(C,26,52,1)
See Also
Statistical Median
Usage
MEDIAN(EXPR[,N])
- expr
- Data to be analyzed.
- n
- Number of periods to be analyzed. Or if not given the median for the entire dataset so far.
Description
Calculates the statistical median of expr over the last n periods, or over the entire dataset if n is omitted.
The statistical median is calculated by ordering all of the values being analyzed into ascending order. Then the middle value is returned. Or if an even number of points is being considered, the two values points are averaged.
Importantly, this is not the same as the Median Price.
Example
Consider a security with closing prices over the last seven trading days:
3.5, 3.3, 3.4, 3.5, 3.3, 3.4, 3.3, 3.6
To calculate median(C,7) these values are arranged in ascending order and the
3.3, 3.3, 3.3, 3.4, 3.4, 3.5, 3.5, 3.6
Therefore the result is 3.4.
See Also
Median Price | Midpoint | Mode | Moving Average
Min
Usage
MIN(N1,N2...)
- n1, n2
- A list of numbers.
Description
Selects the smallest value currently assumed by any of its parameters.
Min should not be confused with the Lowest function, which returns the smallest value a single parameter has assumed over a number of bars.
Example
min(2,5,4) is 2.
min(open,close) is the smaller of the open and close price for the current bar.
lowest(close,5) is the smaller value of close over the last 5 days.
min(expr1, 10) This example uses the min function to put an upper limit on the value returned by expr1. If expr1 is less than 10, then expr1 will be returned unchanged. But if it’s greater than 10, then 10 is returned.
min(max(expr1,0),100) The above technique can be used in this way to restrict a result to a range. In this example, expr1 is cropped to return a result between 0 and 100.
See Also
MinLinearReg
Usage
MINLINEARREG(EXPR,N1,N2[,AHEAD])
- expr
- Data that linear regressing is performed on.
- n1
- Minimum number of periods. At least 2.
- n2
- Maximum number of periods. At least 2.
- ahead
- The number of days that each linear regression forecasts ahead.
Description
Calculates the result of a linear regression of every length between n1 and n2inclusive, then returns the smallest result.
Example
minlinearreg(C,26,52)
minlinearreg(C,26,52,1)
See Also
Modulus
Usage
MOD(A,B)
- a
- The number being divided.
- b
- The number dividing.
Description
Calculates a modulo b. That is, the remainder of a divided by b.
Example 1
mod(21,8) returns 5 because 21 divided by 8 is 2 with a remainder of 5.
Example 2
if(barnumber mod 8 == 0, 1, 0) will return ‘1’ every 8th trading day because every 8 days, 8 will divide into the barnumber. Therefore mod can be used as a crude periodic timer.
See Also
Statistical Mode
Usage
MODE(EXPR[,N])
- expr
- Data to be analyzed.
- n
- Number of periods to be analyzed. Or if not given the mode for the entire dataset so far.
Description
Calculates the statistical mode of expr over the last n periods, or over the entire dataset if n is omitted.
The statistical mode is the expr value that has occurred most often. Or if two or more values have occurred equally most often, it is the last one encountered (with the most recent date).
Example
Consider a security with closing prices over the last seven trading days:
3.5, 3.3, 3.4, 3.5, 3.3, 3.4, 3.3, 3.6
In the above example, mode(C,7) is 3.3, because it occurs more often than any other value.
See Also
Multiply
Usage
MUL(N1,N2...)
- n1,n2..
- A list of numbers to be multiplied.
Description
Multiplies it’s parameters together. Equivalent to n1*n2
Example
mul(4,3) returns 12.
mul(4,2,3) returns 24.
See Also
Negate
Usage
NEG(N)
- n
- The number to be negated.
Description
Returns the negative of n. Equivalent to -(n)
Example
neg(8) returns -8.
neg(-8) returns 8.
See Also
Pi
Usage
PI
Description
Returns 3.14159265358979, the mathematical constant Pi (written with the notation p). Pi is the ratio of a circle’s circumference to its diameter.
Power
Usage
POWER(B,X) or PWR(B,X)
- b
- The base.
- x
- The exponent.
Description
Raises b to the power of x. Equivalent to b^x. The power and pwr functions are equivalent.
To raise the natural base ‘e’ to some power, use the exp function.
Power and log are inverse operations such that:
x=power(b,n) and n=log(x,b) hold for the same x,b and n.
Example
power(5,3) is equivalent to 5*5*5 and returns 125.
See Also
Round
Usage
ROUND(A[,N]) or PREC(A[,N])
- a
- A number.
- n
- Number of decimal places, zero if omitted.
Description
Rounds a to n decimal places, or to the nearest integer if n is not given.
The Prec function, short for precision, is an alias for round and gives the same result.
Remarks
There are many different rounding techniques. Differences between them commonly relate to how numbers at and beyond 1/2 are treated, and how negative numbers are treated. The following table show how various examples would be rounded.
3.4 | 3.5 | 3.6 | -3.4 | -3.5 | -3.6 | |
int | 3 | 3 | 3 | -3 | -3 | -3 |
round | 3 | 4 | 4 | -3 | -4 | -4 |
floor | 3 | 3 | 3 | -4 | -4 | -4 |
ceiling | 4 | 4 | 4 | -3 | -3 | -3 |
Example
round(4) returns 4
round(4.2) returns 4
round(4.5) returns 5
round(4.7) returns 5
round(-8.2) returns -8
round(-8.7) returns -9
round(4.56,1) returns 4.6
See Also
Sign
Usage
SIGN(A)
- a
- A number.
Description
Finds the sign a. That is, whether or not the number is negative.
If a is a positive number, then sign(a) returns 1.
If a is a negative number, then sign(a) returns -1.
If a is zero, then sign(a) returns 0.
Example
sign(4) is 1, and sign(-4) is -1.
See Also
Square Root
Usage
SQRT(N) or SQR(N)
- n
- A number.
Description
Finds the square root of n. That is, it find a number that gives n when it is squared.
Both SQRT and SQR give the same result.
Example
sqrt(16) returns 4.
Standard Deviation
Usage
STDEV(EXPR,N) or STD(EXPR,N)
- expr
- The data being evaluated.
- n
- Number of periods.
Description
Finds the standard deviation of expr over the last n periods.
The function STD is an alias for STDEV and behaves in the same way.
There are two variants of the standard deviation depending on whether the data represents a sample or a population of the data being considered. Because every data point in the specified range is used, BullScript uses the population version of the standard deviation function.
Example
var(C,14)
See Also
Standard Error
Usage
STEYX(EXPR,N)
- expr
- The data being evaluated.
- n
- Number of periods.
Description
Finds the standard error of expr over the last n periods.
Example
steyx(C,14)
See Also
Subtract
Usage
SUB(A,B)
- n1, n2…
- Values to be added together
Description
Calculates b subtracted from a. Equivalent to a-b.
Example
sub(6,2) returns 4.
See Also
Sum
Usage
SUM(EXPR[,N]) or CUM(EXPR[,N])
- expr
- Data to be summed.
- n
- Number of periods to be summed. Or if not given the cumulative sum for the dataset so far.
Description
Calculates the rolling sum of expr over the last n periods, or over the entire dataset if n is omitted.
The sum function requires n-1 days of historical data to calculate.
The cum function, short for cumulative, is an alias for the sum function and works in an identical manner.
Example
sum(CLOSE,14)/14 finds the average of the close price over the last 14 periods (and is thus equivalent to ma(C,14,simple)).
See Also
Trendline
Usage
TRENDLINE(EXPRY, EXPRX, EXTR, EXTL, DIR)
- exprY
- An expression that specifies the ‘y coordinate’ of the trendline, for example Low.
- exprX
- An expression that returns true on certain days to mark off ‘x coordinates’.
- extR
- Extend the trendline to the right. 1=true, 0=false.
- extL
- Extend the trendline to the left. 1=true, 0=false.
- dir
- The required slope of the rendline. -1=down sloping, 0=either, 1=up sloping
Description
Returns a set of values that will appear as a trendline. The last two times that exprX return true mark off the start and end of the trendline period. The values of exprY at these two locations will be used to fix the start and end points.
Several additional parameters allow extra configuration of the line – such as whether it will only yield values between the two points, or if the line will be extended. Undefined will be returned for intervals outside of the range if the line is not extended.
The dir parameter allows constraints to be placed on the line such as “only draw if the trend is sloping up (positive gradient) or down (negative gradient).
Example
z := zigzag(C,8,%);
tro := z<future(z,1) and z<hist(z,1);
trendline(L, tro, 1, 0, 1);
This example calculates a zigzag line with an 8% reversal requirement. The variable troevaluates to true whenever the zigzag has reached a trough. The trendline function as used here will draw a trendline through the last two troughs of the zig zag. It will continue to the right only, and it will only appear if the trendline is upward sloping.
Variance
Usage
VAR(EXPR,N)
- expr
- The data being evaluated.
- n
- Number of periods.
Description
Finds the statistical variance expr over the last n periods. The variance is the square of the standard deviation.
There are two variants of the variance depending on whether the data represents a sample or a population of the data being considered. Because every data point in the specified range is used, BullScript uses the population version of the variance function.
Example
stdev(C,14)
See Also
Standard Deviation | Standard Error