Stochastic Oscillator
The is a momentum indicator that compares a security’s closing price to its price range over a specific time period. This indicator is shown as two lines on a chart:
- %K, which is the main line representing the level of the Stochastic Oscillator.
- %D, which is a moving average of %K and typically displayed as a dotted line.
Market Signals
The Stochastic Oscillator provides signals for buying and selling:
- Buy Signal: When either %K or %D falls below a certain threshold (e.g., 20) and then climbs back above it.
- Sell Signal: When the Oscillator rises above a higher threshold (e.g., 80) and then drops below it.
These levels indicate overbought or oversold conditions and suggest potential price reversals.
Calculation
The calculation of the Stochastic Oscillator involves several steps:
RawK = 100 * (Close - Lowest(Low, RawPeriod)) / (Highest(High, RawPeriod) - Lowest(Low, RawPeriod))
%K = SMA(RawK, KPeriod)
%D = SMA(%K, DPeriod)
RawKcalculates the raw Stochastic value.%Ksmooths this value using a Simple Moving Average (SMA) overKPeriod.%Dthen takes an SMA of %K overDPeriodto provide a signal line.
import { Bar , InstrumentProcessorBase } from '@botmain/common';
import { StochasticOscillator } from '@botmain/indicators';
export class InstrumentProcessor extends InstrumentProcessorBase {
stochastic = new StochasticOscillator (28, 4, 4);
...
onBarClose(bar: Bar ) {
//Add bar to calculate new stochastic value
this.stochastic.addBar(bar);
...
//Get indicator values
const percentK = this.stochastic.percentK;
const percentD = this.stochastic.percentD;
// Get previous values
if(this.stochastic.historyCount >= 2) {
const percentK = this.stochastic.values[1].percentK;
const percentD = this.stochastic.values[1].percentD;
}
}
}