Supported Indicators

Schaff Trend Cycle

Introduction

This indicator creates the Schaff Trend Cycle

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

Using STC Indicator

To create an automatic indicators for SchaffTrendCycle, call the STC helper method from the QCAlgorithm class. The STC method creates a SchaffTrendCycle 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 SchaffTrendCycleAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private SchaffTrendCycle _stc;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _stc = STC(_symbol, 5, 10, 20, MovingAverageType.Exponential);
    }

    public override void OnData(Slice data)
    {
        if (_stc.IsReady)
        {
            // The current value of _stc is represented by itself (_stc)
            // or _stc.Current.Value
            Plot("SchaffTrendCycle", "stc", _stc);
            
        }
    }
}
class SchaffTrendCycleAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.stc = self.STC(self.symbol, 5, 10, 20, MovingAverageType.Exponential)

    def OnData(self, slice: Slice) -> None:
        if self.stc.IsReady:
            # The current value of self.stc is represented by self.stc.Current.Value
            self.Plot("SchaffTrendCycle", "stc", self.stc.Current.Value)
            

The following reference table describes the STC method:

STC()1/1

            SchaffTrendCycle QuantConnect.Algorithm.QCAlgorithm.STC (
    Symbol                           symbol,
    Int32                            cyclePeriod,
    Int32                            fastPeriod,
    Int32                            slowPeriod,
    *MovingAverageType               movingAverageType,
    *Nullable<Resolution>      resolution,
    *Func<IBaseData, Decimal>  selector
   )
        

Creates a new Schaff Trend Cycle indicator.

If you don't provide a resolution, it defaults 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.

For more information about plotting indicators, see Plotting Indicators.

The following table describes the MovingAverageType enumeration members:

To avoid parameter ambiguity, use the resolution argument to set the Resolution.

public class SchaffTrendCycleAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private SchaffTrendCycle _stc;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Hour).Symbol;
        _stc = STC(_symbol, 5, 10, 20, MovingAverageType.Exponential, resolution: Resolution.Daily);
    }
}
class SchaffTrendCycleAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Hour).Symbol
        self.stc = self.STC(self.symbol, 5, 10, 20, MovingAverageType.Exponential, resolution=Resolution.Daily)

You can manually create a SchaffTrendCycle 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 with time/number pair or an IndicatorDataPoint. The indicator will only be ready after you prime it with enough data.

public class SchaffTrendCycleAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private SchaffTrendCycle _stc;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _stc = new SchaffTrendCycle(5, 10, 20, MovingAverageType.Exponential);
    }

    public override void OnData(Slice data)
    {
        if (data.Bars.TryGetValue(_symbol, out var bar))
        {      
            _stc.Update(bar.EndTime, bar.Close);
        }
   
        if (_stc.IsReady)
        {
            // The current value of _stc is represented by itself (_stc)
            // or _stc.Current.Value
            Plot("SchaffTrendCycle", "stc", _stc);
            
        }
    }
}
class SchaffTrendCycleAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.stc = SchaffTrendCycle(5, 10, 20, MovingAverageType.Exponential)

    def OnData(self, slice: Slice) -> None:
        bar = slice.Bars.get(self.symbol)
        if bar:
            self.stc.Update(bar.EndTime, bar.Close)
        if self.stc.IsReady:
            # The current value of self.stc is represented by self.stc.Current.Value
            self.Plot("SchaffTrendCycle", "stc", self.stc.Current.Value)
            

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

public class SchaffTrendCycleAlgorithm : QCAlgorithm
{
    private Symbol _symbol;
    private SchaffTrendCycle _stc;

    public override void Initialize()
    {
        _symbol = AddEquity("SPY", Resolution.Daily).Symbol;
        _stc = new SchaffTrendCycle(5, 10, 20, MovingAverageType.Exponential);
        RegisterIndicator(_symbol, _stc, Resolution.Daily);
    }

    public override void OnData(Slice data)
    {
        if (_stc.IsReady)
        {
            // The current value of _stc is represented by itself (_stc)
            // or _stc.Current.Value
            Plot("SchaffTrendCycle", "stc", _stc);
            
        }
    }
}
class SchaffTrendCycleAlgorithm(QCAlgorithm):
    def Initialize(self) -> None:
        self._symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
        self.stc = SchaffTrendCycle(5, 10, 20, MovingAverageType.Exponential)
        self.RegisterIndicator(self.symbol, self.stc, Resolution.Daily)

    def OnData(self, slice: Slice) -> None:
        if self.stc.IsReady:
            # The current value of self.stc is represented by self.stc.Current.Value
            self.Plot("SchaffTrendCycle", "stc", self.stc.Current.Value)
            

The following reference table describes the SchaffTrendCycle constructor:

SchaffTrendCycle()1/2

            SchaffTrendCycle QuantConnect.Indicators.SchaffTrendCycle (
    *int                fastPeriod,
    *int                slowPeriod,
    *int                cyclePeriod,
    *MovingAverageType  type
   )
        

https://www.tradingpedia.com/forex-trading-indicators/schaff-trend-cycle.

SchaffTrendCycle()2/2

            SchaffTrendCycle QuantConnect.Indicators.SchaffTrendCycle (
    string             name,
    int                fastPeriod,
    int                slowPeriod,
    int                cyclePeriod,
    MovingAverageType  type
   )
        

Creates a new schaff trend cycle with the specified parameters.

Visualization

The following image shows plot values of selected properties of SchaffTrendCycle using the plotly library.

SchaffTrendCycle line plot.

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: