Investment Studio > Expressions > Functions > Indicator > CSP
float array[*][2] csp(float array[*][5] dohlc, integer lookback_days = 5, float none_below_average_fraction = 0.1, float long_above_average_fraction = 0.7, float no_trend_below_slope = 0.001, float no_trend_above_slope = -0.001)
Returns a two-column array containing dates (first column) and corresponding candlestick pattern codes (second column).
dohlc is a five-column array containing daily price quotes. Each row is structured as follows:
| Column # | Content |
| 1 | The quote date. |
| 2 | The day's open (first traded price). |
| 3 | The day's high (highest traded price). |
| 4 | The day's low (lowest traded price). |
| 5 | The day's close (last traded price). |
The array is assumed to be time-sorted, with earlier dates preceding later dates.
Automatic type conversion allows the use of date strings as arguments instead of explicit date values.
lookback_days is the length of the time frame (in trading days) used to determine (1) the trend direction preceding a pattern and (2) the average candlestick body length (days in the pattern also contribute to this average). Note that the determination of a trend direction requires at least two lookback days. Setting this argument to 0 or 1 will result in prices always being considered trendless.
none_below_average_fraction is a cutoff length, expressed as a fraction of the average candlestick body length: candlestick bodies and shadows shorter than this cutoff are considered vanishing (i.e. of length = 0). If omitted, none_below_average_fraction defaults to 0.1.
long_above_average_fraction is a threshold length, expressed as a fraction of the average candlestick body length: candlestick bodies and shadows are considered long if in excess of this threshold, short if less than half its value. If omitted, long_above_average_fraction defaults to 0.7.
no_trend_below_slope is a low slope cutoff for price trends, expressed as relative change per trading day: if this value exceeds the slope of the linear interpolation of closing prices in the lookback time frame, prices are considered trendless. If omitted, no_trend_below_slope defaults to 0.001, i.e. 0.1% per trading day.
no_trend_above_slope is a high slope cutoff for price trends, expressed as relative change per trading day: if the linear interpolation of closing prices in the lookback time frame has a slope exceeding this cutoff value, prices are considered trendless. If omitted, no_trend_above_slope defaults to -0.001, i.e. -0.1% per trading day.
Given an effective annual rate of change and the number of trading days in a year,
daily rate = (1 + annual rate)^(1 / days) - 1
For instance, a 5% annual rate of change over 250 days is equivalent to the daily rate (1 + 5%)^(1 / 250) - 1 » 0.0195%.
Interpretation
CSP searches the quotes for occurrences of classic candlestick patterns, returning the start date (first day) and code of each detected pattern.
Longer patterns take precedence over shorter patterns: only one pattern code can be reported for each date, and it's always the code of the longest pattern starting on that date. Among patterns of the same duration, patterns with larger absolute code values take precedence over patterns with lower absolute code values.
Bullish patterns have positive codes, bearish patterns have negative codes.
The following patterns are recognized:
| Code | Description |
| 1000 | Rising
4 Six-day downtrend reversal pattern. Same as Rising 3 (see below) but with four intermediate down days instead of three. |
| 990
|
Rising
3 Five-day downtrend reversal pattern: long, white body followed by three short, black, falling bodies (midpoint of each one < midpoint of previous one) and finally by a long white body closing above the first day's close. If volume data is available, the down days should be accompanied by declining volume, while the final day's rally should be on heavy volume. |
| 980
|
Upside
Gap (Tasuki) Three-day uptrend continuation pattern: gap up (first day's high < second day's low) to white body, followed by a black candlestick which does not close the gap (third day's low > first day's high). The color of the first day's candlestick body is not important. |
| 970
|
Morning
Star (Downtrend Exhaustion Gap) Three-day downtrend reversal pattern: long black body followed by gap down (first day's low > second day's high) to short body followed by long white body. The last day should open below the close of the first day and close above the midpoint of the first candlestick. The color of the second day's candlestick body is not important. |
| 800
|
Bullish
Engulfing (Tsutsumi) Two-day downtrend reversal pattern: shadowless black body on first day engulfed by shadowless white body on second day. |
| 790
|
Bullish
Harami Two-day downtrend reversal pattern: long body on first day engulfing body of opposite color on second day. If first day is up, its close > second day's open, its open < second day's close. If first day is down, its open > second day's close, its close < second day's open. |
| 780
|
Breakout
Gap Up Two-day breakout pattern: the second day's low is higher than the previous day's high. Differs from a continuation gap by occurring in the absence of a trend. The color of the candlestick bodies is not important. |
| 770
|
Continuation
Gap Up Two-day uptrend continuation pattern: the second day's low is higher than the previous day's high. The color of the candlestick bodies is not important. |
| 760
|
Piercing
Line (Kirikomi) Two-day key reversal pattern: strong downday (long black body) followed by upday (white body) opening below previous low (gap down) and closing in upper half of previous body. No trend required. |
| 600
|
Hammer
(Takuri) One-day downtrend reversal pattern, characterized by vanishing upper shadow, lower shadow length >= 2 * body length. The color of the body (i.e. whether open > close or not) is not important. Gets its English name from the metaphore that price is "hammering" out a bottom. |
| -600
|
Hanging
Man (Kubitsuri) One-day uptrend reversal pattern, characterized by vanishing upper shadow, lower shadow length >= 2 * body length. The color of the body (i.e. whether open > close or not) is not important. Considered particularly bearish after a prolonged upmove and/or gap. |
| -760
|
Dark
Cloud Cover (Kabuse) Two-day uptrend reversal pattern: strong upday (long white body) followed by downday (black body) opening above previous high (gap up) and closing in lower half of previous body. |
| -770
|
Continuation
Gap Down Two-day downtrend continuation pattern: the second day's high is lower than the previous day's low. The color of the candlestick bodies is not important. |
| -780
|
Breakout
Gap Down Two-day breakout pattern: the second day's high is lower than the previous day's low. Differs from a continuation gap by occurring in the absence of a trend. The color of the candlestick bodies is not important. |
| -790
|
Bearish
Harami Two-day uptrend reversal pattern: long body on first day engulfing body of opposite color on second day. If first day is up, its close > second day's open, its open < second day's close. If first day is down, its open > second day's close, its close < second day's open. |
| -800
|
Bearish
Engulfing (Tsutsumi) Two-day uptrend reversal pattern: shadowless white body on first day engulfed by shadowless black body on second day. |
| -950
|
Doji
Star Two-day uptrend reversal pattern: long white body on first day followed by gap up (first day's high < second day's low) to body-less ("doji") second day. Considered particularly bearish after a prolonged rally. |
| -960
|
Shooting
Star Two-day uptrend reversal pattern: long white body on first day followed by gap up (first day's high < second day's low) to a short black body hanging at the end of a long upper shadow. Considered indicative of a short term top. |
| -970
|
Evening
Star (Uptrend Exhaustion Gap) Three-day uptrend reversal pattern: long white body followed by gap up (first day's high < second day's low) to short body followed by long black body. The last day should open above the close of the first day and close below the midpoint of the first candlestick. The color of the second day's candlestick body is not important. |
| -980
|
Upside
Gap Two Crows (Narabi Kuro) Three-day uptrend reversal pattern: long white body followed by gap up (first day's high < second day's low) to black body, in turn followed by a third black body which may or may not close the gap. |
| -990
|
Falling
3 Five-day uptrend reversal pattern: long, black body followed by three short, white, rising bodies (midpoint of each one > midpoint of previous one) and finally by a long black body closing below the first day's close. If volume data is available, the up days should be accompanied by declining volume, while the final day's fall should be on heavy volume. |
| -1000 | Falling
4 Six-day uptrend reversal pattern. Same as Falling 3 (see above) but with four intermediate up days instead of three. |
For a real life example, consider Borland (NASD:BORL) from July 1 to November 30, 2001:

The red (barish) and green (bullish) numbers are CSP codes for lookback_days = 20 and default values for the other arguments.
Example
Assuming standard US date format settings,
=csp({{"10/3/1990", 100, 110, 90, 105}, {"10/4/1990", 100, 110, 90, 105}, {"10/5/1990", 100, 100, 60, 60}, {"10/8/1990", 50, 90, 50, 90}, {"10/9/1990", 100, 110, 90, 105}, {"10/10/1990", 120, 130, 111, 125}}, 2)
returns {{33151, 760}, {33155, 770}}. 33151 is the date code for October 5, 1990; 760 means that an occurrence of pattern number 760 (Piercing Line) starts that day. 33155 is the date code for October 9, 1990; 770 means that it's the first day in a Continuation Gap Up pattern.
The chart in the previuos section uses the definitions
_NDAYS = 20
_QUOTES = asset_quotes(SELF, "OHLC", FROM_DATE - 2 * _NDAYS, TO_DATE)
_CSP = csp(array(_QUOTES), _NDAYS)
_CSP_TODAY = vlookup(X, array(_CSP), 2, FALSE)
and the text source
{_CSP_TODAY, asset_close(SELF, X), 15 * sign(_CSP_TODAY), 2, if(_CSP_TODAY > 0, 32768, 255)}