Indicator Reference

Stochastic

Introduction

This indicator computes the Slow Stochastics %K and %D. The Fast Stochastics %K is is computed by (Current Close Price - Lowest Price of given Period) / (Highest Price of given Period - Lowest Price of given Period) multiplied by 100. Once the Fast Stochastics %K is calculated the Slow Stochastic %K is calculated by the average/smoothed price of of the Fast %K with the given period. The Slow Stochastics %D is then derived from the Slow Stochastics %K with the given period.

Create Manual Indicators

You can manually create a Stochastic indicator, so it doesn’t automatically update. Manual indicators let you update their values with any data you choose. The following reference table describes the Stochastic constructor.

Stochastic()1/2

            Stochastic QuantConnect.Indicators.Stochastic (
     string  name,
     int     dPeriod
   )
        

Creates a new Stochastics Indicator from the specified periods.

Stochastic()2/2

            Stochastic QuantConnect.Indicators.Stochastic (
     int  dPeriod
   )
        

Creates a new Stochastic indicator from the specified inputs.

Update Manual Indicators

You can update the indicator automatically or manually.

Automatic Update

To register a manual indicator for automatic updates with the security data, call the RegisterIndicator method.


private Stochastic _sto;

// In Initialize()
_sto = new Stochastic(name, period, kPeriod, dPeriod);
_sto.Updated += IndicatorUpdateMethod;

RegisterIndicator(symbol, _sto, Resolution.Daily);

// In IndicatorUpdateMethod()
if (_sto.IsReady)
{
    var indicatorValue = _sto.Current.Value;
}
    
# In Initialize()
self.sto = Stochastic(name, period, kPeriod, dPeriod)
self.sto.Updated += self.IndicatorUpdateMethod

self.RegisterIndicator(symbol, self.sto, Resolution.Daily)

# In IndicatorUpdateMethod()
if self.sto.IsReady:
    indicator_value = self.sto.Current.Value

To customize the data that automatically updates the indicator, see Custom Indicator Periods and Custom Indicator Values.

Manual Update

Updating your indicator manually enables you to control when the indicator is updated and what data you use to update it. To manually update the indicator, call the Update method with a TradeBar, QuoteBar, or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

    
private Stochastic _sto;
private Symbol symbol;

// In Initialize()
_sto = new Stochastic(period, kPeriod, dPeriod);
symbol = AddEquity("SPY").Symbol;

// In OnData()
if (data.QuoteBars.ContainsKey(_symbol))
{
    _sto.Update(data.QuoteBars[symbol]);
}
if (_sto.IsReady)
{
    var indicatorValue = _sto.Current.Value;
}
    
# In Initialize()
self.sto = Stochastic(period, kPeriod, dPeriod)
self.symbol = self.AddEquity("SPY").Symbol

# In OnData()
if data.QuoteBars.ContainsKey(self.symbol):
    self.sto.Update(data.QuoteBars[self.symbol])
if self.sto.IsReady:
    indicator_value = self.sto.Current.Value

Create Automatic Indicators

The STO method creates an Stochastic indicator, sets up a consolidator to update the indicator, and then returns the indicator so you can use it in your algorithm.

The following reference table describes the STO method:

STO()1/2

            Stochastic QuantConnect.Algorithm.QCAlgorithm.STO (
    Symbol                       symbol,
    Int32                        period,
    Int32                        kPeriod,
    Int32                        dPeriod,
    *Nullable<Resolution>  resolution
   )
        

Creates a new Stochastic indicator.

STO()2/2

            Stochastic QuantConnect.Algorithm.QCAlgorithm.STO (
    Symbol                       symbol,
    Int32                        period,
    *Nullable<Resolution>  resolution
   )
        

Overload short hand to create a new Stochastic indicator; defaulting to the 3 period for dStoch


If you don't provide a resolution, it defauls to the security resolution. If you provide a resolution, it must be greater than or equal to the resolution of the security. For instance, if you subscribe to hourly data for a security, you should update its indicator with data that spans 1 hour or longer.

For more information about the selector argument, see Alternative Price Fields.

Get Indicator Values

To get the value of the indicator, use its Current.Value attribute.

private Stochastic _sto;

// In Initialize()
var symbol = AddEquity("SPY").Symbol;
_sto = STO(symbol, period);

// In OnData()
if (_sto.IsReady)
{
    var fastStoch = _sto.FastStoch.Current.Value;
    var stochK = _sto.StochK.Current.Value;
    var stochD = _sto.StochD.Current.Value;
    var current = _sto.Current.Value;
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.sto = self.STO(symbol, period)

# In OnData()
if self.sto.IsReady:
    fast_stoch = self.sto.FastStoch.Current.Value
    stoch_k = self.sto.StochK.Current.Value
    stoch_d = self.sto.StochD.Current.Value
    current = self.sto.Current.Value

Visualization

To plot indicator values, in the OnData event handler, call the Plot method.

private Stochastic _sto;

// In Initialize()
var symbol = AddEquity("SPY").Symbol;
_sto = STO(symbol, period);

// In OnData()
if (_sto.IsReady)
{
    Plot("My Indicators", "faststoch", _sto.FastStoch);
    Plot("My Indicators", "stochk", _sto.StochK);
    Plot("My Indicators", "stochd", _sto.StochD);
    Plot("My Indicators", "stochastic", _sto.Current);
}
# In Initialize()
symbol = self.AddEquity("SPY").Symbol
self.sto = self.STO(symbol, period)

# In OnData()
if self.sto.IsReady:
    self.Plot("My Indicators", "faststoch", self.sto.FastStoch)
    self.Plot("My Indicators", "stochk", self.sto.StochK)
    self.Plot("My Indicators", "stochd", self.sto.StochD)
    self.Plot("My Indicators", "stochastic", self.sto.Current)

For more information about plotting indicators, see Plotting Indicators.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: