Skip to content
Open RaamTrade

StochasticClose — Stochastic Close

Where the current close sits inside the last N closes, on a 0–100 scale. A close-only stochastic: it uses the highest and lowest close, not the highest high and lowest low.

StochasticClose is the identifier a strategy writes; Stochastic Close is the label the chart’s indicator picker shows. Single value — .value is the only accessor.

Signature

StochasticClose(period, on=<source>)
Argument Type Required Notes
period int Param or literal Yes Lookback window. Default 14, bounds 5–100.
on= symbol, OHLCV field, or indicator No Source. Defaults to the trade symbol’s close.

Formula

Over the last N values of the source:

$$ \text{StochasticClose} = \frac{\text{close}_t - \min}{\max(\max - \min,\ 0.0001)} \times 100 $$

0 means the current close is the lowest of the window, 100 means it is the highest, 50 means it sits mid-range. The denominator is floored at a small epsilon so a dead-flat window cannot divide by zero.

Warmup

period bars. Before the window fills, the indicator returns 50 — the neutral midpoint, not 0. A strategy never observes it (execute() is suppressed until every indicator is ready), but it is what the chart plots on the leading bars.

Reading the value

stoch.value # 0..100
stoch.value_at(1)
stoch < 20 # oversold
stoch > 80 # overbought
stoch.crossed_above(20) # the bar it leaves oversold

Example

class StochasticDip(Strategy):
"""Buy the exit from oversold, sell back into the top of the range."""
period = Param(14, min=5, max=100)
trade = Symbol()
sizing = FixedCapital()
def execute(self):
stoch = StochasticClose(self.period, on=self.trade)
self.stop_loss(3.0)
if stoch.crossed_above(20) and self.trade.is_flat:
self.buy(self.trade, reason=f"Leaving oversold ({stoch.value:.0f})")
if stoch.value > 80 and self.trade.is_long:
self.sell(self.trade, self.trade.position, reason="Top of the range")

Notes

  • A perfectly flat window reads 0, not 50. When every close in the window is identical, the numerator is zero and the epsilon-floored denominator keeps the division finite — so the indicator reports “at the bottom of the range” for a market that has no range at all. On thinly-traded or halted instruments, filter those bars out rather than trusting the read.
  • Close-only by design. The classic %K uses the window’s highest high and lowest low; this one uses closes on both ends, so it is smoother and never reacts to a wick.
  • Bounded 0–100, which makes it directly comparable across instruments — unlike MADeviation or PriceChannelWidth.
  • There is no built-in %D smoothing line. Chain one yourself: EMA(3, on=stoch).
  • WilliamsClose is the same measurement on a −100–0 scale.