System functions perform some basic operations or calculations that are frequently needed.
Some functions (CurrentBar, LastBar, TimePeriod) return information about the underlying time series (security) and are called without brackets, since they do not require any input.
CurrentBar 
Returns the index number of the current bar. 
LastBar 
Returns the index number of the last available bar. 
TimePeriod 
Returns the current time compression of the time series in minutes. 
IsValid(CheckValue) 
Returns True if the input value passed to it is valid. 
Invalidate(VariableName) 
Invalidates the value of a variable. 
Sum(SumValues, Range) 
Returns the sum of the given values for the specified period. 
Sum_2(SumValues, Range) 
Returns the sum of the squared values for the specified period. 
Sum_DownDiff(SumValues, Range) 
Returns the sum of the downwards price differences between a bar and its previous bar for the specified period. 
Sum_UpDiff(SumValues, Range) 
Returns the sum of the upwards price differences between a bar and its previous bar for the specified period. 
Sum_xy(Source1, Source2, Range) 
Returns the scalar product between two securities for the specified period. 
SumAbsDistance(SumValues, NumValue, Range)

Returns the sum of the absolute distances between a bar and a certain numeric value for the specified period. 
Average(Values, Range) 
Returns the average value of the given values for the specified period 
StdDev(Source, Range) 
Returns the standard deviation for the given values over the specified period. 
StdDev_biased(Source, Range) 
Returns the biased standard deviation for the given values over the specified period. 
LinearRegression(Source, Range, Target) 
Returns the linear regression for the given values over the specified period. 
Covariance(Source1, Source2, Range) 
Returns the covariance between two securities over the specified period. 
Covariance_biased(Source1, Source2, Range) 
Returns the biased covariance between two securities over the specified period. 
RSquared(Source1, Source2, Range) 
Returns R squared for the given securities over the specified period. 
Correlation(Source1, Source2, Range) 
Returns the correlation between two securities over the specified period. 
Beta(Source1, Source2, Range) 
Returns the beta for the given securities over the specified period. 
Highest(Source, Range) 
Returns the highest value of a given set of values which is defined by a specific period. 
HighestIndex(Source, Range) 
Returns the index number of the bar that has the highest value of a given set of values, which is defined by a specific period. 
Lowest(Source, Range) 
Returns the lowest value of a given set of values which is defined by a specific period. 
LowestIndex(Source, Range) 
Returns the index number of the bar that has the lowest value of a given set of values, which is defined by a specific period. 
CountHigher2(Source1, Source2, Range) 
Returns the number of bars where the value of the first security is higher than the value of the second security inside the specified period. 
CountLower2(Source1, Source2, Range) 
Returns the number of bars where the value of the first security is lower than the value of the second security inside the specified period. 
CountTrue(BoolValues, Range) 
Returns the number of true values inside the specified period. 
CountFalse(BoolValues, Range) 
Returns the number of false values inside the specified period. 
This function returns the index number of the current bar. For the first bar, CurrentBar returns the value 1.
CurrentBar
Parameters 

None 
Returns 

Numeric 
This function is often used to control the execution of calculations with If statements. For example, some calculations might not be reasonable before a certain bar number is reached.
If
CurrentBar = 4 Or CurrentBar =
5 Then
cycle = (price  2 * price[1] + price[2]) / 4
Else
...
This function returns the index number of the last bar that is available on the chart (in the time series).
LastBar
Parameters 

None 
Returns 

Numeric 
You can use this function for example to determine whether the last bar has been reached already in a calculation:
If
LastBar =
CurrentBar Then
...
This function returns the current time compression of the time series in minutes. You can use it to find out which data compression is currently used in the chart that the study is applied to.
TimePeriod
Parameters 

None 
Returns 

Numeric 
This function returns True if the input value passed to it is valid, and False if the input value is not valid.
IsValid(CheckValue)
Parameters 
CheckValue 
Numeric 
Specifies the value that should be checked for validity. 
Returns 

Boolean 

This function is most commonly used to check if certain needed values are available before carrying out any calculations with them:
If
IsValid(Source.High) And IsValid(Source.Low) Then
resultDiff = Source.High  Source.Low;
This function invalidates the value of a numeric variable. Subsequent checks with IsValid will return False for this variable.
Invalidate(VariableName)
Parameters 
VariableName 
Name of the variable that should invalidated. The variable must be of the type Numeric. 
Returns 

Nothing 
This function sums up the given values (prices of a security) for a specific period (range).
Sum(SumValues, Range)
Parameters 
SumValues 
Numeric Field 
Specifies the values that should be summed up, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be summed up (period, range). 
Returns 

Numeric 

The function Sum is used in many studies, for example to calculate the middle band of the Bollinger Bands:
middleBand = Sum(source, period) / period;
If for example the parameter period is set to 21 and source is set to the Close prices of a security, the Sum function will sum up the Close prices of the last 21 bars.
This function squares the given values (prices of a security) and then sums them up for a specific period (range).
Sum_2(SumValues, Range)
Parameters 
SumValues 
Numeric Field 
Specifies the values that should be squared and then summed up, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be summed up (period, range). 
Returns 

Numeric 

This function sums up the downwards price differences between a bar and its previous bar for a specific period (range). When you use for example the Close prices of a security as an input, this function will only sum up the differences for those bars where the Close price of the previous bar was higher than the Close price of the current bar ("down days").
Sum_DownDiff(SumValues, Range)
Parameters 
SumValues 
Numeric Field 
Specifies the values that should be summed up, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be summed up (period, range). The function will only use those values out of this range where the price has moved down from the previous bar. 
Returns 

Numeric 

This function is for example used in the Chande Momentum Oscillator, that calculates the difference between the sum of all recent gains and the sum of all recent losses and then divides the result by the sum of all price movement over the period.
SumUp = Sum_UpDiff(Source, period);
SumDown = Sum_DownDiff(Source, period);
rslt = 100 *
(SumUp  SumDown) / (SumUp + SumDown);
This function sums up the upwards price differences between a bar and its previous bar for a specific period (range). When you use for example the Close prices of a security as an input, this function will only sum up the differences for those bars where the Close price of the previous bar was lower than the Close price of the current bar ("up days").
Sum_UpDiff(SumValues, Range)
Parameters 
SumValues 
Numeric Field 
Specifies the values that should be summed up, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be summed up (period, range). The function will only use those values out of this range where the price has moved up from the previous bar. 
Returns 

Numeric 

This function calculates the scalar product between two securities for a specific period (range). That means that for each bar, it multiplies the values of the two securities, and then sums up the results for the whole range.
Sum_xy(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be multiplied and then summed up (period, range). 
Returns 

Numeric 

This function could for example be used in the calculations for the Beta Factor, that shows the development of a stock within the general market growth direction, represented by its index:
If
IsValid(Source1) Then
rend = #roc(Source1, 2);
If IsValid(Source2) Then
mktRend = #roc(Source2, 2);
...
help_1 = Sum_xy(rend, mktRend, period);
This function sums up the absolute distance between the given values (prices of a security) and a certain numeric value for a specific period (range).
SumAbsDistance(SumValues, NumValue, Range)
Parameters 
SumValues 
Numeric Field 
Specifies the values whose absolute distance to a certain value should be summed up, usually the prices of a security. 

NumValue 
Numeric 
Specifies a certain numeric value to measure the absolute distance to. 

Range 
Numeric 
Defines the number of values that should be summed up (period, range). 
Returns 

Numeric 

This function is for example used in the calculations for the Commodity Channel Index. X is defined here as the "typical price" of a security, avgX is the average of the typical price over the last period bars. avgMD calculates the mean deviation for the CCI using the function SumAbsDistance.
avgMD = SumAbsDistance(X, avgX, period) / period;
This function returns the average value of all given values (for example prices of a security) for a specific period (range).
Average(Values, Range)
Parameters 
Values 
Numeric Field 
Specifies the values for which the average should be calculated, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used for the average (period, range). 
Returns 

Numeric 

This function is for example used to calculate the Moving Average Simple:
Result = Average(Source, period) * (1 + shift / 100);
This function calculates the standard deviation for the given values (prices of a security) over a specific period (range).
StdDev(Source, Range)
Parameters 
Source 
Numeric Field 
Specifies the values for which the standard deviation should be calculated, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

In statistics, the standard deviation is a means of measuring the variability of a data set. It is for example used for calculating the Bollinger Bands. While the middle band ("Mid") is a standard moving average, the outer bands ("Boll") are calculated based on the standard deviation over the same period of time:
middleBand = Sum(Source, period) / period;
dev = StdDev(Source, period);
DrawArea("Boll", middleBand + deviation1
*
dev, middleBand – deviation2
* dev);
DrawLine("Mid", middleBand);
If the standard deviation is low, the data points of the examined period tend to be close to the mean value, which means that the outer bands are close to the middle band. If the standard deviation is high, the data points tend to be further away from the mean value (average), which means that the outer bands are at a greater distance to the middle band.
This function calculates the biased standard deviation for the given values (prices of a security) over a specific period (range). The biased standard deviation should be used if you already know the target value around which fluctuations occur.
StdDev_biased(Source, Range)
Parameters 
Source 
Numeric Field 
Specifies the values for which the biased standard deviation should be calculated, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function calculates the linear regression for the given values over a specific period (range).
LinearRegression(Source, Range, Target)
Parameters 
Source 
Numeric Field 
Specifies the values for which the linear regression should be calculated. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 

Target 
Numeric 
Specifies the (future) target bar for the linear regression line. For example, use 1 as an input to address the value of the next bar to the right of the resulting line and so on. 
Returns 

Numeric 

Linear regression is a mathematical tool to represent a set of input data (here: the last period bars of the Source parameter) as a line by using the least squares approach. This means that the squared distances from the linear regression line to all points of the input data are minimal. With the additional parameter Target, you can choose if you want to get the value for the current bar or for a future bar of the resulting line. It is for example used in the functions for the candlestick signals Hammer or Hanging Man:
lin_reg_value = LinearRegression(Source.Close, period, 0);
This function calculates the covariance between two securities over a specific period (range).
Covariance(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

Covariance is a statistical tool to measure how much two securities change together – positive covariance means that the securities move in tandem, negative covariance means that the two securities move inverted to each other (inverse correlation). A value near zero means that there is no covariance, that means that the two given values are probably not dependent of each other.
This function calculates the biased covariance between two securities over a specific period (range). The biased covariance should be used if you already know the target value around which fluctuations occur.
Covariance_biased(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function calculates R squared for two given securities over a specific period (range).
RSquared(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

R squared is a statistical tool, also called coefficient of determination. It represents the percentage of a fund or security's movement that can be explained by movements in a benchmark index. The values returned range from 0 to 100, with 100 meaning that all movements of the first source parameter can be fully explained by the movements of the second source parameter.
This function calculates the correlation between two securities over a specific period (range).
Correlation(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

Correlation is a statistical tool to measure the tendency of two securities to move into the same direction. It returns a value between +1 and 1, with +1 meaning that the two securities tend to move together and 1 meaning that the two securities tend to move in opposite directions. A correlation value near zero indicates that there is probably no connection between the movements of the two securities.
This function calculates the beta for the given securities / indices over a specific period (range).
Beta(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

The beta describes the relation between a stock (first source parameter) and the financial market (second source parameter). A positive beta means that the movements of the stock follow the market movements, a negative beta shows an inverse correlation and a beta close to zero means that there is no connection between the movements of the two source parameters.
This function returns the highest value of a given set of values (prices of a security), which is defined by a specific period (range).
Highest(Source, Range)
Parameters 
Source 
Numeric Field 
Specifies the values for which the highest value should be determined, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function is for example used in the study OHLC of Last x Periods to display the highest value of the specified range (for example, the highest High price of the last 21 bars):
DrawLine("Highest", Highest(Source, period));
This function returns the index number of the bar that has the highest value of a given set of values, which is defined by a specific period (range). The returned index number is relative to the chosen range – for example, when you use a range of 10, the current bar is bar 100, and the bar with the highest value is bar 95, the index number returned by HighestIndex will be 5 (the fifth bar to the left of the current bar).
HighestIndex(Source, Range)
Parameters 
Source 
Numeric Field 
Specifies the values for which the index number of the highest value should be determined, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function can be used to find the bar that has the highest value of a given set of bars. The Aroon indicator for example looks for the bar with the highest value in a given time period. To find out when the highest value in this period was reached, the HighestIndex function is used:
aroon_up = (period – HighestIndex(Source.High, period)) / period * 100;
This function returns the lowest value of a given set of values (prices of a security), which is defined by a specific period (range).
Lowest(Source, Range)
Parameters 
Source 
Numeric Field 
Specifies the values for which the lowest value should be determined, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function is for example used in the study Donchian Channel to find out the lowest low of the specified range (for example, the lowest Low price of the last 21 bars, offset by one bar into the past):
lowestL = Lowest(Source.Low[1], periodLow));
This function returns the index number of the bar that has the lowest value of a given set of values, which is defined by a specific period (range). The returned index number is relative to the chosen range – for example, when you use a range of 10, the current bar is bar 100, and the bar with the lowest value is bar 95, the index number returned by HighestIndex will be 5 (the fifth bar to the left of the current bar).
LowestIndex(Source, Range)
Parameters 
Source 
Numeric Field 
Specifies the values for which the index number of the lowest value should be determined, usually the prices of a security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function returns the number of bars where the value of the first security is higher than the value of the second security inside the specified period.
CountHigher2(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function is for example used in the signal Count Criteria. It is used to compare the Close values of a security with the High values of the same security from the day before (rising values). It returns the number of times that today's Close was higher than yesterday's High inside the specified period (for example during the last 21 days).
CountHigher2(Source.Close, Source.High[1], Period)
This function returns the number of bars where the value of the first security is lower than the value of the second security inside the specified period.
CountLower2(Source1, Source2, Range)
Parameters 
Source1 
Numeric Field 
Specifies the values of the first security. 

Source2 
Numeric Field 
Specifies the values of the second security. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

This function returns the number of true values inside the specified period.
CountTrue(BoolValues,Range)
Parameters 
BoolValues 
Boolean 
Specifies the Boolean values that should be counted. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 

You can for example use this function in signals where you want to make sure that a certain condition is true for several bars before you continue with other calculations. In the example below, you get some Boolean output from a function for a number of bars and then check with CountTrue if this output is true for three bars in a row before drawing a marker on the last bar.
bool_output = funcBoolCalc(Source, Period);
If CountTrue(bool_output, 3) =
3 Then
DrawMarker("MarkThisBar", Source.High, False, "My
Signal");
This function returns the number of false values inside the specified period.
CountFalse(BoolValues,Range)
Parameters 
BoolValues 
Boolean 
Specifies the Boolean values that should be counted. 

Range 
Numeric 
Defines the number of values that should be used (period, range). 
Returns 

Numeric 
