Supported Indicators

Know Sure Thing

Introduction

This indicator creates a moving average (middle band) with an upper band and lower band fixed at k standard deviations above and below the moving average.

To view the implementation of this indicator, see the LEAN GitHub repository.

Using KST Indicator

To create an automatic indicator for KnowSureThing, call the KSTkst helper method from the QCAlgorithm class. The KSTkst method creates a KnowSureThing object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In most cases, you should call the helper method in the Initializeinitialize method.

public class KnowSureThingAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KnowSureThing _kst;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _kst = KST(_symbol, 5, 5, 10, 5, 15, 5, 25, 10, 9, MovingAverageType.Simple);
    }

    public override void OnData(Slice data)
    {

        if (_kst.IsReady)
        {
            // The current value of _kst is represented by itself (_kst)
            // or _kst.Current.Value
            Plot("KnowSureThing", "kst", _kst);
            // Plot all properties of abands
            Plot("KnowSureThing", "roc1", _kst.ROC1);
            Plot("KnowSureThing", "roc2", _kst.ROC2);
            Plot("KnowSureThing", "roc3", _kst.ROC3);
            Plot("KnowSureThing", "roc4", _kst.ROC4);
            Plot("KnowSureThing", "roc1ma", _kst.ROC1MA);
            Plot("KnowSureThing", "roc2ma", _kst.ROC2MA);
            Plot("KnowSureThing", "roc3ma", _kst.ROC3MA);
            Plot("KnowSureThing", "roc4ma", _kst.ROC4MA);
            Plot("KnowSureThing", "signalline", _kst.SignalLine);
        }
    }
}
class KnowSureThingAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._kst = self.kst(self._symbol, 5, 5, 10, 5, 15, 5, 25, 10, 9, MovingAverageType.SIMPLE)

    def on_data(self, slice: Slice) -> None:

        if self._kst.is_ready:
            # The current value of self._kst is represented by self._kst.current.value
            self.plot("KnowSureThing", "kst", self._kst.current.value)
            # Plot all attributes of self._kst
            self.plot("KnowSureThing", "roc_1", self._kst.roc_1.current.value)
            self.plot("KnowSureThing", "roc_2", self._kst.roc_2.current.value)
            self.plot("KnowSureThing", "roc_3", self._kst.roc_3.current.value)
            self.plot("KnowSureThing", "roc_4", self._kst.roc_4.current.value)
            self.plot("KnowSureThing", "roc_1_ma", self._kst.roc_1_ma.current.value)
            self.plot("KnowSureThing", "roc_2_ma", self._kst.roc_2_ma.current.value)
            self.plot("KnowSureThing", "roc_3_ma", self._kst.roc_3_ma.current.value)
            self.plot("KnowSureThing", "roc_4_ma", self._kst.roc_4_ma.current.value)
            self.plot("KnowSureThing", "signal_line", self._kst.signal_line.current.value)

For more information about this method, see the QCAlgorithm classQCAlgorithm class.

You can manually create a KnowSureThing indicator, so it doesn't automatically update. Manual indicators let you update their values with any data you choose.

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 Updateupdate method. The indicator will only be ready after you prime it with enough data.

public class KnowSureThingAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KnowSureThing _knowsurething;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _knowsurething = new KnowSureThing(5, 5, 10, 5, 15, 5, 25, 10, 9, MovingAverageType.Simple);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
            _knowsurething.Update(bar.EndTime, bar.Close);

        if (_knowsurething.IsReady)
        {
            // The current value of _knowsurething is represented by itself (_knowsurething)
            // or _knowsurething.Current.Value
            Plot("KnowSureThing", "knowsurething", _knowsurething);
            // Plot all properties of abands
            Plot("KnowSureThing", "roc1", _knowsurething.ROC1);
            Plot("KnowSureThing", "roc2", _knowsurething.ROC2);
            Plot("KnowSureThing", "roc3", _knowsurething.ROC3);
            Plot("KnowSureThing", "roc4", _knowsurething.ROC4);
            Plot("KnowSureThing", "roc1ma", _knowsurething.ROC1MA);
            Plot("KnowSureThing", "roc2ma", _knowsurething.ROC2MA);
            Plot("KnowSureThing", "roc3ma", _knowsurething.ROC3MA);
            Plot("KnowSureThing", "roc4ma", _knowsurething.ROC4MA);
            Plot("KnowSureThing", "signalline", _knowsurething.SignalLine);
        }
    }
}
class KnowSureThingAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._knowsurething = KnowSureThing(5, 5, 10, 5, 15, 5, 25, 10, 9, MovingAverageType.SIMPLE)

    def on_data(self, slice: Slice) -> None:
        bar = slice.bars.get(self._symbol)
        if bar:
            self._knowsurething.update(bar.end_time, bar.close)

        if self._knowsurething.is_ready:
            # The current value of self._knowsurething is represented by self._knowsurething.current.value
            self.plot("KnowSureThing", "knowsurething", self._knowsurething.current.value)
            # Plot all attributes of self._knowsurething
            self.plot("KnowSureThing", "roc_1", self._knowsurething.roc_1.current.value)
            self.plot("KnowSureThing", "roc_2", self._knowsurething.roc_2.current.value)
            self.plot("KnowSureThing", "roc_3", self._knowsurething.roc_3.current.value)
            self.plot("KnowSureThing", "roc_4", self._knowsurething.roc_4.current.value)
            self.plot("KnowSureThing", "roc_1_ma", self._knowsurething.roc_1_ma.current.value)
            self.plot("KnowSureThing", "roc_2_ma", self._knowsurething.roc_2_ma.current.value)
            self.plot("KnowSureThing", "roc_3_ma", self._knowsurething.roc_3_ma.current.value)
            self.plot("KnowSureThing", "roc_4_ma", self._knowsurething.roc_4_ma.current.value)
            self.plot("KnowSureThing", "signal_line", self._knowsurething.signal_line.current.value)

For more information about this indicator, see its referencereference.

Visualization

The following plot shows values for some of the KnowSureThing indicator properties:

KnowSureThing line plot.

Indicator History

To get the historical data of the KnowSureThing indicator, call the IndicatorHistoryself.indicator_history method. This method resets your indicator, makes a history request, and updates the indicator with the historical data. Just like with regular history requests, the IndicatorHistoryindicator_history method supports time periods based on a trailing number of bars, a trailing period of time, or a defined period of time. If you don't provide a resolution argument, it defaults to match the resolution of the security subscription.

public class KnowSureThingAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private KnowSureThing _kst;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _kst = KST(_symbol, 5, 5, 10, 5, 15, 5, 25, 10, 9, MovingAverageType.Simple);

        var indicatorHistory = IndicatorHistory(_kst, _symbol, 100, Resolution.Minute);
        var timeSpanIndicatorHistory = IndicatorHistory(_kst, _symbol, TimeSpan.FromDays(10), Resolution.Minute);
        var timePeriodIndicatorHistory = IndicatorHistory(_kst, _symbol, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);

        // Access all attributes of indicatorHistory
        var roc1 = indicatorHistory.Select(x => ((dynamic)x).ROC1).ToList();
        var roc2 = indicatorHistory.Select(x => ((dynamic)x).ROC2).ToList();
        var roc3 = indicatorHistory.Select(x => ((dynamic)x).ROC3).ToList();
        var roc4 = indicatorHistory.Select(x => ((dynamic)x).ROC4).ToList();
        var roc1ma = indicatorHistory.Select(x => ((dynamic)x).ROC1MA).ToList();
        var roc2ma = indicatorHistory.Select(x => ((dynamic)x).ROC2MA).ToList();
        var roc3ma = indicatorHistory.Select(x => ((dynamic)x).ROC3MA).ToList();
        var roc4ma = indicatorHistory.Select(x => ((dynamic)x).ROC4MA).ToList();
        var signalLine = indicatorHistory.Select(x => ((dynamic)x).SignalLine).ToList();
    }
}
class KnowSureThingAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._kst = self.kst(self._symbol, 5, 5, 10, 5, 15, 5, 25, 10, 9, MovingAverageType.SIMPLE)

        indicator_history = self.indicator_history(self._kst, self._symbol, 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._kst, self._symbol, timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._kst, self._symbol, datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
    
        # Access all attributes of indicator_history
        indicator_history_df = indicator_history.data_frame
        roc_1 = indicator_history_df["roc1"]
        roc_2 = indicator_history_df["roc2"]
        roc_3 = indicator_history_df["roc3"]
        roc_4 = indicator_history_df["roc4"]
        roc_1_ma = indicator_history_df["roc1ma"]
        roc_2_ma = indicator_history_df["roc2ma"]
        roc_3_ma = indicator_history_df["roc3ma"]
        roc_4_ma = indicator_history_df["roc4ma"]
        signal_line = indicator_history_df["signalline"]

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: