# Pattern Recognition Functions

This section lists functions used to identify simple patterns such as gaps and crossovers. Also refer to the candle patterns section for candle specific functions.

## Cross

Usage

`CROSS(EXPR1,EXPR2)`
expr1
First data source for testing crosses.
expr1
Second data source for testing crosses.

Description

If expr1 crosses above expr2 on the current bar then the cross function returns true. It returns false on every other bar. To determine if a cross happened in the other direction, reverse the two parameters.

Example

cross(C, ma(C,14)) returns true when close breaks above the 14 day moving average.

cross(ma(C,14), C) returns true when close pushes below the 14 day moving average.

## DownTrend

Usage

`DownTrend`

Description

The downtrend function returns 1 if the security is considered to be in a downtrend. Otherwise it returns 0.

For this function, down trends and up trends are determined by considering the highs and lows of each bar. A bar is called a higher bar if both its high and low price are both higher than the previous high and previous low respectively. Similarly, a bar is called a lower bar if its high price and low price are both lower than the previous high and low prices respectively.

When a lower bar is encountered, a down trend is established. The DownTrend function will return 1 on this bar, and on every bar until a higher bar is encountered. That is, the chart flips between being in an uptrend (started on a higher bar) and being in a downtrend (started on a lower bar).

The DownTrend function will generally return 1 at the same time that the UpTrend function returns zero, except for the first few bars of the chart where both may return zero until an initial trend is established.

Note that this is the same method for detecting trends as is used in the “Higher, Lower trend” colouring option for the price chart.

Example #1

{Return the low price when in an uptrend, and the high price when in a downtrend}
{This is identical to the uptrend example script, except how it behaves in the first few bars of the chart}
[target=Price] if (downtrend, High, Low);

Example #2

{Draw a marker on the first day of a downtrend}
[target=Price] [linestyle=marker; marker=type2]
downtrend and not hist(downtrend,1)

## GapDown

Usage

`GAPDOWN()`

Description

If a gap down occurs, then gapdown returns true. Otherwise it returns false. A gap down occurs if the previous bar’s low is greater than this bar’s high, thus leaving a gap in the chart.

Note: Technically a 1 or 0 is returned, however this is interpreted as true or false respectively by any functions, such as if that take a boolean expression.

## GapUp

Usage

`GAPUP()`

Description

If a gap up occurs, then gapup returns true.  Otherwise it returns false. A gap up occurs if the previous bar’s high is lower than this bar’s low, thus leaving a gap in the chart.

Note: Technically a 1 or 0 is returned, however this is interpreted as true or false respectively by any functions, such as if that take a boolean expression.

## Inside

Usage

`INSIDE()`

Description

If the current bar is an ‘inside’ bar, then inside returns true. Otherwise it returns false. A bar is inside if its high is less than the previous bar’s high, and its low is greater than the previous bar’s low, thus the current bar is ‘inside’ the previous bar.

Note: Technically a 1 or 0 is returned, however this is interpreted as true or false respectively by any functions, such as if that take a boolean expression.

The bar must strictly be an inside bar for this function to return true. That is, if either the high is equal to the previous high, or the low is equal to the previous low then false is returned. This is in contrast to the colouring scheme used by BullCharts, which may colour these as inside bars because it needs to pick one of the four colours from higher, lower, inside, or outside.

## Outside

Usage

`OUTSIDE()`

Description

If the current bar is an ‘outside’ bar then outside returns true Otherwise it returns false. A bar is outside if its high is greater than the previous bar’s high, and its low is less than the previous bar’s low.

Note: Technically a 1 or 0 is returned, however this is interpreted as true or false respectively by any functions, such as if that take a boolean expression.

The bar must strictly be an inside bar for this function to return true. That is, if either the high is equal to the previous high, or the low is equal to the previous low then false is returned. This is in contrast to the colouring scheme used by BullCharts, which may colour these as inside bars because it needs to pick one of the four colours from higher, lower, inside, or outside.

## Peak

Usage

`PEAK(N,EXPR,CHANGE)`
n
The number of peaks ago to find.
expr
The data to be analyzed.
change
The minimum percentage change passed into the ZigZag function.

Description

Uses the ZigZag indicator to determine the height (price value) of the nth last peak in the expr data. The ZigZag indicator looks for percentages changes of at least change.

Example

peak(1,close,5) returns the height of the last peak, using a 5% change in detecting peaks.

## Peak Bars

Usage

`PEAKBARS(N,EXPR,CHANGE)`
n
The number of peaks ago to find.
expr
The data to be analyzed.
change
The minimum percentage change passed into the ZigZag function.

Description

Uses the ZigZag indicator to determine the number of bars ago that the nth last peak occurred in the expr data. The ZigZag indicator looks for percentages changes of at least change.

Example

peakbars(1,close,5)

## Trough

Usage

`TROUGH(N,EXPR,CHANGE)`
n
The number of troughs ago to find.
expr
The data to be analysed.
change
The minimum percentage change passed into the ZigZag function.

Description

Uses the ZigZag indicator to determine the price value of the nth last trough in the expr data. The ZigZag indicator looks for percentages changes of at least change.

Example

trough(1,close,5) returns the price value of the last trough, using a 5% change in detecting peaks and troughs.

## Trough Bars

Usage

`TROUGHBARS(N,EXPR,CHANGE)`
n
The number of troughs ago to find.
expr
The data to be analysed.
change
The minimum percentage change passed into the ZigZag function.

Description

Uses the ZigZag indicator to determine the number of bars ago that the nth last trough occurred in the expr data. The ZigZag indicator looks for percentages changes of at least change.

Example

troughbars(1,close,5)

## UpTrend

Usage

`UpTrend`

Description

The uptrend function returns 1 if the security is considered to be in an uptrend. Otherwise it returns 0.

For this function, up trends and down trends are determined by considering the highs and lows of each bar. A bar is called a higher bar if both its high and low price are both higher than the previous high and previous low respectively. Similarly, a bar is called a lower bar if its high price and low price are both lower than the previous high and low prices respectively.

When a higher bar is encountered, an up trend is established. The UpTrend function will return 1 on this bar, and on every bar until a lower bar is encountered. That is, the chart flips between being in an uptrend (started on a higher bar) and being in a downtrend (started on a lower bar).

The UpTrend function will generally return 1 at the same time that the DownTrend function returns zero, except for the first few bars of the chart where both may return zero until an initial trend is established.

Note that this is the same method for detecting trends as is used in the “Higher, Lower trend” colouring option for the price chart.

Example #1

{Return the low price when in an uptrend, and the high price when in a downtrend}
{This is identical to the downtrend example script, except how it behaves in the first few bars of the chart}
[target=Price] if (uptrend, Low, High);

Example #2

{Draw a marker on the first day of an uptrend}
[target=Price] [linestyle=marker; marker=type1]
uptrend and not hist(uptrend,1)