Investment Studio > Expressions > Functions > Indicator > STO
float array[*][4] sto(float array[*][4] dhlc, integer raw_days, integer fast_days, integer slow_days)
Returns a four-column array containing dates and corresponding Stochastic Oscillator (STO) values. Each row is structured as follows:
| Column # | Content |
| 1 | The quote date. |
| 2 | The raw K (or %K) value. |
| 3 | The fast D (or %D) value. |
| 4 | The slow D (smoothed %D) value. |
Given n input rows in dhlc, n - (raw_days + fast_days + slow_days - 3) rows are returned.
dhlc is a four-column array containing daily price quotes. Each row is structured as follows:
| Column # | Content |
| 1 | The quote date. |
| 2 | The day's high (highest traded price). |
| 3 | The day's low (lowest traded price). |
| 4 | 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.
raw_days > 0 is the time frame over which the recent price range (lowest low, highest high) is determined. Raw K is simply the position of the latest close relative to this range, expressed as a percentage. Popular raw_days values are 10 to 15 trading days.
fast_days > 0 is the time frame of the simple MA used to compute fast D values from raw K values. Popular values are 3 to 5.
slow_days > 0 is the time frame of the simple MA used to compute slow D values from fast D values. Popular values are 3 to 5.
Interpretation
STO is a price trend indicator. It's based on the observation that prices tend to end near recent highs in an uptrend, near recent lows in a downtrend. The raw K (or %K) value is given by the expression
| today's close - lowest recent low | |
| %K = 100 | ¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾¾ |
| highest recent high - lowest recent low |
This is simply the position of the day's close relative to the recent price range, expressed as a percentage. %K > 50 means that the day's close was in the upper half of this range, implying an uptrend; %K = 100 means that the day closed at (and presumably set) the highest recent high. %K < 50 means that the the day's close was in the lower half of this range, implying a downtrend; %K = 0 means that the day closed at (and presumably set) the lowest recent low.
The exact meaning of "recent" is determined by the raw_days argument; a value of 10 would mean that the latest 10 trading days (including the current one) are considered.
The fast D line (%D) is obtained by smoothing %K with a simple moving average (see MA) of length fast_days, and is what most technicians mean when they refer to "stochastics". As usual when moving averages are involved, %D is a tradeoff between timeliness and stability (fewer false signals).
The slow D line is obtained by smoothing the fast D line with a MA of length slow_days.
If you don't need the D lines or prefer a different smoothing than MA (e.g. EMA) consider using WRO instead.
There are several ways to use stochastics:
| Overbought/oversold:
Values over 75 or 80 (depending on whom you ask) for any
of %K, fast D or slow D indicate an overbought condition;
values below 25 or 20 indicate an oversold condition. As
usual, this does not by itself imply that a major trend
reversal is imminent, just that conditions are right for
one to occur. Any of the three lines turning up from a bottom in oversold territory is often described as a buy signal ("stochastic hook"), as is a crossing above the 25 or 20 level. Any of the three lines turning down from a top in overbought territory is often described as a sell signal, as is a crossing below the 75 or 80 level. |
|
| Crossovers:
The fast D line crossing above the slow D line is
considered a buy signal, especially in oversold territory.
Conversely, the fast D line crossing below the slow D
line is considered a sell signal, especially in
overbought territory. %K / %D crossings are not adequate as standalone buy/sell signals, as they would result in excessively frequent whipsaws due to the high volatility of %K, but they can serve as building blocks along with other signals. |
|
| Divergences:
According to the Stochastic Oscillator's inventor, George
C. Lane, the most important kind of signal is %D making a
series of lower highs while price is making a series of
higher highs (bearish), or %D making a series of higher
lows while price is making a series of lower lows (bullish).
When such divergences appear, the prescription is to enter the market on the first "right hand" %K / %D crossing: if %D is rising from a higher bottom, traders will go long when %K crosses above %D; if %D is falling from a lower top, traders will go short when %K crosses below %D. According to Lane, divergences are most reliable when %D is between 10 and 15 (for buy signals) and when %D is between 85 and 90 (for sell signals). |
For a real life example, consider Dell (NASD:DELL) from January 1 to December 31, 2001:

The top chart shows the price in standard candlestick form. The bottom chart shows %K (in red), %D (in green) and the slow D line (in blue).
Neither crossings of overbought / oversold levels nor fast / slow D line crossovers would have worked well throughout this chart: what would have done well in the spring's trading range would have done poorly in the fall's dramatic decline and recovery, and vice versa.
On the other hand, a case could be made that the three falling D tops in July and August (when price was slowly trending higher) spelled serious trouble for September, while the rising double bottom made by the Ds in September and October (when price was falling dramatically) indicated that a recovery was near.
Compare the chart above with the corresponding WRO chart.
Example
Assuming standard US date format settings,
=sto({{"1/1/1990", 100, 90, 98}, {"1/2/1990", 97, 84, 86}, {"1/3/1990", 95, 89, 91}, {"1/4/1990", 97, 92, 96}}, 2, 2, 2)
returns {{32877, 87.5, 70.6730769230769, 51.9230769230769}. 32877 is the date code for 1/4/1990; 87.5 is the 2 day %K on that date; 70.67... is the 2 day %D on that date; 51.92... is the 2 day slow D on that date.