Files
QuanTAlib/lib/oscillators/stochf/Stochf.md
T
Miha Kralj 951842acca Add validation tests for various volume and momentum indicators
- Introduced Massi validation tests to ensure mathematical properties hold for the Mass Index indicator.
- Added Va validation tests for Volume Accumulation, checking for finite outputs and correct accumulation behavior.
- Implemented Vf validation tests for Volume Force, verifying outputs for rising and falling prices, and ensuring batch and streaming results match.
- Created Vo validation tests for Volume Oscillator, confirming behavior with constant, increasing, and decreasing volumes.
- Developed Vroc validation tests for Volume Rate of Change, validating outputs for constant volume and changes in volume.
- Updated project file to include new momentum indicators (MACD and RSI) in the compilation.
2026-02-12 19:43:09 -08:00

5.8 KiB
Raw Blame History

Stochastic Fast Oscillator (STOCHF)

Overview

The Stochastic Fast Oscillator is the unsmoothed variant of the classic Stochastic Oscillator. It measures the position of the closing price relative to the high-low range over a lookback period, producing a raw (fast) %K line and its SMA-smoothed %D signal line.

Unlike the standard Stochastic (STOCH), which may apply additional SMA smoothing to %K, StochF outputs the raw %K directly — making it more responsive to price changes but also noisier.

The indicator produces two lines:

  • %K (Fast %K): Raw position within the range, scaled 0100
  • %D (Signal line): Simple Moving Average of %K

Origin and Sources

George C. Lane introduced the Stochastic Oscillator in the late 1950s. The "Fast" variant is the original unsmoothed form, while the "Slow" variant applies additional SMA smoothing to reduce noise. Most modern platforms offer both versions; TA-Lib specifically separates them as STOCH (slow) and STOCHF (fast).

Key references:

  • Lane, George C. "Lane's Stochastics." Technical Analysis of Stocks & Commodities, 1984
  • Murphy, John J. Technical Analysis of the Financial Markets, 1999
  • Appel, Gerald & Hitschler, Fred. Stock Market Trading Systems, 1980

Mathematical Formula

Core Calculation

%K = 100 × (Close  Lowest Low) / (Highest High  Lowest Low)

Where:
  Lowest Low  = min(Low[i])  for i ∈ [0, kLength-1]
  Highest High = max(High[i]) for i ∈ [0, kLength-1]

%D = SMA(%K, dPeriod)

Edge Case

When Highest High = Lowest Low (zero range), %K = 0.

Signal Line

%D is computed as a Simple Moving Average of %K values using a circular buffer with a running sum for O(1) per-bar computation.

Architecture

Streaming Path

The streaming implementation uses monotonic deques for O(1) amortized highest-high and lowest-low tracking:

  • MonotonicDeque (max): Maintains decreasing order of high values; front always holds the current maximum
  • MonotonicDeque (min): Maintains increasing order of low values; front always holds the current minimum
  • Circular buffer + running sum for SMA(%K → %D)

Bar correction (isNew=false) triggers deque rebuild from the circular buffer, ensuring correct state without allocation.

State Management

State record struct:
  DSum        — running sum of %K values in the SMA window
  DHead       — circular buffer head index for %D SMA
  PrevDVal    — previous buffer value at DHead (for rollback)
  LastValidHigh/Low/Close — NaN/Infinity protection

The standard _s / _ps pattern enables bar correction:

  • isNew=true: _ps = _s, advance index/count
  • isNew=false: _s = _ps, recalculate from previous state

Batch Path

Static Batch() methods use Highest.Batch() and Lowest.Batch() for vectorized min/max computation, with ArrayPool for buffers exceeding 256 elements and stackalloc for smaller inputs.

Parameters

Parameter Type Default Range Description
kLength int 5 ≥ 1 Lookback period for highest high / lowest low
dPeriod int 3 ≥ 1 SMA smoothing period for %D signal line

Performance Profile

Metric Value
Time complexity (streaming) O(1) amortized per bar
Time complexity (batch) O(n)
Space complexity O(kLength + dPeriod)
Warmup period kLength bars
Output range 0100 (both %K and %D)

Interpretation

Overbought / Oversold

Zone %K Level Interpretation
Overbought > 80 Price near top of range — potential reversal
Neutral 2080 Normal trading range
Oversold < 20 Price near bottom of range — potential reversal

Signal Patterns

  • %K/%D Crossover: Bullish when %K crosses above %D; bearish when %K crosses below %D
  • Divergence: Price makes new highs/lows while StochF doesn't — potential reversal
  • Failure Swings: %K reaches overbought/oversold then reverses before re-reaching the extreme
  • Hook: Short-term reversal pattern when %K or %D hooks at extremes

StochF vs STOCH (Slow Stochastic)

This implementation is the Fast Stochastic where:

  • %K is the raw (unsmoothed) oscillator
  • %D is the SMA of %K

The "Slow Stochastic" (STOCH) additionally smooths %K with an SMA before computing %D. StochF is more responsive but generates more false signals in choppy markets.

Validation

Library Match Notes
Skender ✔️ Via GetStoch(kLength, dPeriod, smoothPeriods=1) — smoothPeriods=1 produces Fast %K
TALib ✔️ Via TALib.Functions.StochF(high, low, close, ...) — dedicated Fast Stochastic function

Common Pitfalls

  1. Zero range: When all bars in the window have identical H/L, range = 0 and %K = 0 (not 50 or NaN)
  2. Fast vs Slow confusion: Many platforms default to "Slow Stochastic"; StochF outputs the raw unsmoothed %K
  3. Overbought ≠ sell signal: In strong trends, %K can stay above 80 for extended periods
  4. Short lookback noise: kLength < 3 creates excessive whipsaws in volatile markets
  5. SMA warmup for %D: The first dPeriod bars use the PineScript convention of filling the SMA buffer with the first %K value, not NaN
  6. Default period difference: StochF defaults to kLength=5 (shorter than STOCH's kLength=14) for faster response

References

  • Lane, G. C. (1984). "Lane's Stochastics." Technical Analysis of Stocks & Commodities
  • Murphy, J. J. (1999). Technical Analysis of the Financial Markets. New York Institute of Finance
  • Achelis, S. B. (2000). Technical Analysis from A to Z. McGraw-Hill
  • TradingView Stochastic
  • StockCharts Stochastic Oscillator