Supported Indicators

Chande Kroll Stop

Introduction

This indicator computes the short stop and lower stop values of the Chande Kroll Stop Indicator. It is used to determine the optimal placement of a stop-loss order.

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

Using CKS Indicator

To create an automatic indicator for ChandeKrollStop, call the CKScks helper method from the QCAlgorithm class. The CKScks method creates a ChandeKrollStop 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 ChandeKrollStopAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private ChandeKrollStop _cks;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _cks = CKS(_symbol, 10, 1, 9);
    }

    public override void OnData(Slice data)
    {

        if (_cks.IsReady)
        {
            // The current value of _cks is represented by itself (_cks)
            // or _cks.Current.Value
            Plot("ChandeKrollStop", "cks", _cks);
            // Plot all properties of abands
            Plot("ChandeKrollStop", "shortstop", _cks.ShortStop);
            Plot("ChandeKrollStop", "longstop", _cks.LongStop);
        }
    }
}
class ChandeKrollStopAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._cks = self.cks(self._symbol, 10, 1, 9)

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

        if self._cks.is_ready:
            # The current value of self._cks is represented by self._cks.current.value
            self.plot("ChandeKrollStop", "cks", self._cks.current.value)
            # Plot all attributes of self._cks
            self.plot("ChandeKrollStop", "short_stop", self._cks.short_stop.current.value)
            self.plot("ChandeKrollStop", "long_stop", self._cks.long_stop.current.value)

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

You can manually create a ChandeKrollStop 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 ChandeKrollStopAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private ChandeKrollStop _chandekrollstop;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _chandekrollstop = new ChandeKrollStop(10, 1, 9);
    }

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

        if (_chandekrollstop.IsReady)
        {
            // The current value of _chandekrollstop is represented by itself (_chandekrollstop)
            // or _chandekrollstop.Current.Value
            Plot("ChandeKrollStop", "chandekrollstop", _chandekrollstop);
            // Plot all properties of abands
            Plot("ChandeKrollStop", "shortstop", _chandekrollstop.ShortStop);
            Plot("ChandeKrollStop", "longstop", _chandekrollstop.LongStop);
        }
    }
}
class ChandeKrollStopAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._chandekrollstop = ChandeKrollStop(10, 1, 9)

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

        if self._chandekrollstop.is_ready:
            # The current value of self._chandekrollstop is represented by self._chandekrollstop.current.value
            self.plot("ChandeKrollStop", "chandekrollstop", self._chandekrollstop.current.value)
            # Plot all attributes of self._chandekrollstop
            self.plot("ChandeKrollStop", "short_stop", self._chandekrollstop.short_stop.current.value)
            self.plot("ChandeKrollStop", "long_stop", self._chandekrollstop.long_stop.current.value)

For more information about this indicator, see its referencereference.

Visualization

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

ChandeKrollStop line plot.

Indicator History

To get the historical data of the ChandeKrollStop 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 ChandeKrollStopAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private ChandeKrollStop _cks;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _cks = CKS(_symbol, 10, 1, 9);

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

        // Access all attributes of indicatorHistory
        var shortStop = indicatorHistory.Select(x => ((dynamic)x).ShortStop).ToList();
        var longStop = indicatorHistory.Select(x => ((dynamic)x).LongStop).ToList();
    }
}
class ChandeKrollStopAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self._symbol = self.add_equity("SPY", Resolution.DAILY).symbol
        self._cks = self.cks(self._symbol, 10, 1, 9)

        indicator_history = self.indicator_history(self._cks, self._symbol, 100, Resolution.MINUTE)
        timedelta_indicator_history = self.indicator_history(self._cks, self._symbol, timedelta(days=10), Resolution.MINUTE)
        time_period_indicator_history = self.indicator_history(self._cks, 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
        short_stop = indicator_history_df["shortstop"]
        long_stop = indicator_history_df["longstop"]

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: