Supported Indicators
Super Trend
Introduction
Super trend indicator. Formula can be found here via the excel file: https://tradingtuitions.com/supertrend-indicator-excel-sheet-with-realtime-buy-sell-signals/
To view the implementation of this indicator, see the LEAN GitHub repository.
Using STR Indicator
To create an automatic indicators for SuperTrend
, call the STR
helper method from the QCAlgorithm
class. The STR
method creates a SuperTrend
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 Initialize
method.
public class SuperTrendAlgorithm : QCAlgorithm { private Symbol _symbol; private SuperTrend _str; public override void Initialize() { _symbol = AddEquity("SPY", Resolution.Daily).Symbol; _str = STR(_symbol, 20, 2, MovingAverageType.Wilders); } public override void OnData(Slice data) { if (_str.IsReady) { // The current value of _str is represented by itself (_str) // or _str.Current.Value Plot("SuperTrend", "str", _str); // Plot all properties of str Plot("SuperTrend", "basicupperband", _str.BasicUpperBand); Plot("SuperTrend", "basiclowerband", _str.BasicLowerBand); Plot("SuperTrend", "currenttrailingupperband", _str.CurrentTrailingUpperBand); Plot("SuperTrend", "currenttrailinglowerband", _str.CurrentTrailingLowerBand); } } }
class SuperTrendAlgorithm(QCAlgorithm): def Initialize(self) -> None: self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol self.str = self.STR(self.symbol, 20, 2, MovingAverageType.Wilders) def OnData(self, slice: Slice) -> None: if self.str.IsReady: # The current value of self.str is represented by self.str.Current.Value self.Plot("SuperTrend", "str", self.str.Current.Value) # Plot all attributes of self.str self.Plot("SuperTrend", "basicupperband", self.str.BasicUpperBand) self.Plot("SuperTrend", "basiclowerband", self.str.BasicLowerBand) self.Plot("SuperTrend", "currenttrailingupperband", self.str.CurrentTrailingUpperBand) self.Plot("SuperTrend", "currenttrailinglowerband", self.str.CurrentTrailingLowerBand)
The following reference table describes the STR
method:
STR()1/1
SuperTrend QuantConnect.Algorithm.QCAlgorithm.STR (Symbol
symbol,Int32
period,Decimal
multiplier,*MovingAverageType
movingAverageType,*Nullable<Resolution>
resolution,*Func<IBaseData, IBaseDataBar>
selector )
Creates a new SuperTrend 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:
You can manually create a SuperTrend
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 Update
method with a TradeBar
, or QuoteBar
. The indicator will only be ready after you prime it with enough data.
public class SuperTrendAlgorithm : QCAlgorithm { private Symbol _symbol; private SuperTrend _str; public override void Initialize() { _symbol = AddEquity("SPY", Resolution.Daily).Symbol; _str = new SuperTrend(20, 2, MovingAverageType.Wilders); } public override void OnData(Slice data) { if (data.Bars.TryGeValue(_symbol, out var bar)) { _str.Update(bar); } if (_str.IsReady) { // The current value of _str is represented by itself (_str) // or _str.Current.Value Plot("SuperTrend", "str", _str); // Plot all properties of str Plot("SuperTrend", "basicupperband", _str.BasicUpperBand); Plot("SuperTrend", "basiclowerband", _str.BasicLowerBand); Plot("SuperTrend", "currenttrailingupperband", _str.CurrentTrailingUpperBand); Plot("SuperTrend", "currenttrailinglowerband", _str.CurrentTrailingLowerBand); } } }
class SuperTrendAlgorithm(QCAlgorithm): def Initialize(self) -> None: self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol self.str = SuperTrend(20, 2, MovingAverageType.Wilders) def OnData(self, slice: Slice) -> None: bar = slice.Bars.get(self.symbol) if bar: self.str.Update(bar) if self.str.IsReady: # The current value of self.str is represented by self.str.Current.Value self.Plot("SuperTrend", "str", self.str.Current.Value) # Plot all attributes of self.str self.Plot("SuperTrend", "basicupperband", self.str.BasicUpperBand) self.Plot("SuperTrend", "basiclowerband", self.str.BasicLowerBand) self.Plot("SuperTrend", "currenttrailingupperband", self.str.CurrentTrailingUpperBand) self.Plot("SuperTrend", "currenttrailinglowerband", self.str.CurrentTrailingLowerBand)
To register a manual indicator for automatic updates with the security data, call the RegisterIndicator
method.
public class SuperTrendAlgorithm : QCAlgorithm { private Symbol _symbol; private SuperTrend _str; public override void Initialize() { _symbol = AddEquity("SPY", Resolution.Daily).Symbol; _str = new SuperTrend(20, 2, MovingAverageType.Wilders); RegisterIndicator(_symbol, _str, Resolution.Daily); } public override void OnData(Slice data) { if (_str.IsReady) { // The current value of _str is represented by itself (_str) // or _str.Current.Value Plot("SuperTrend", "str", _str); } } }
class SuperTrendAlgorithm(QCAlgorithm): def Initialize(self) -> None: self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol self.str = SuperTrend(20, 2, MovingAverageType.Wilders) self.RegisterIndicator(self.symbol, self.str, Resolution.Daily) def OnData(self, slice: Slice) -> None: if self.str.IsReady: # The current value of self.str is represented by self.str.Current.Value self.Plot("SuperTrend", "str", self.str.Current.Value)
The following reference table describes the SuperTrend
constructor:
SuperTrend()1/2
SuperTrend QuantConnect.Indicators.SuperTrend (string
name,int
period,decimal
multiplier,*MovingAverageType
movingAverageType )
Creates a new SuperTrend indicator using the specified name, period, multiplier and moving average type.
SuperTrend()2/2
SuperTrend QuantConnect.Indicators.SuperTrend (int
period,decimal
multiplier,*MovingAverageType
movingAverageType )
Creates a new SuperTrend indicator using the specified period, multiplier and moving average type.
Visualization
The following image shows plot values of selected properties of SuperTrend
using the plotly library.
