Supported Indicators
Arms Index
Introduction
The Arms Index, also called the Short-Term Trading Index (TRIN) is a technical analysis indicator that compares the number of advancing and declining stocks (AD Ratio) to advancing and declining volume (AD volume).
To view the implementation of this indicator, see the LEAN GitHub repository.
Using TRIN Indicator
To create an automatic indicator for ArmsIndex, call the TRINtrin helper method from the QCAlgorithm class. The TRINtrin method creates a ArmsIndex 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 ArmsIndexAlgorithm : QCAlgorithm
{
private Symbol _symbol,_reference;
private ArmsIndex _trin;
public override void Initialize()
{
_symbol = AddEquity("QQQ", Resolution.Daily).Symbol;
_reference = AddEquity("SPY", Resolution.Daily).Symbol;
_trin = TRIN([_symbol, _reference]);
}
public override void OnData(Slice data)
{
if (_trin.IsReady)
{
// The current value of _trin is represented by itself (_trin)
// or _trin.Current.Value
Plot("ArmsIndex", "trin", _trin);
// Plot all properties of abands
Plot("ArmsIndex", "adratio", _trin.ADRatio);
Plot("ArmsIndex", "advratio", _trin.ADVRatio);
}
}
} class ArmsIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("QQQ", Resolution.DAILY).symbol
self._reference = self.add_equity("SPY", Resolution.DAILY).symbol
self._trin = self.trin([self._symbol, self._reference])
def on_data(self, slice: Slice) -> None:
if self._trin.is_ready:
# The current value of self._trin is represented by self._trin.current.value
self.plot("ArmsIndex", "trin", self._trin.current.value)
# Plot all attributes of self._trin
self.plot("ArmsIndex", "ad_ratio", self._trin.ad_ratio.current.value)
self.plot("ArmsIndex", "adv_ratio", self._trin.adv_ratio.current.value)To create an automatic indicator for ArmsIndex using universe constituents, call the TRINtrin helper method from the QCAlgorithm class. The TRINtrin method creates a ArmsIndex object, hooks it up for automatic updates, and returns it so you can used it in your algorithm. In this case, you should call the helper method in the OnSecuritiesChangedon_securities_changed method.
public class ArmsIndexAlgorithm : QCAlgorithm
{
private Universe _universe;
private ArmsIndex _trin;
public override void Initialize()
{
UniverseSettings.Resolution = Resolution.Daily;
UniverseSettings.Schedule.On(DateRules.MonthStart());
_universe = AddUniverse(Universe.ETF("SPY"));
}
public override void OnSecuritiesChanged(SecurityChanges changes)
{
_trin = TRIN(_universe.Selected);
}
public override void OnData(Slice data)
{
if (_trin.IsReady)
{
// The current value of _trin is represented by itself (_trin)
// or _trin.Current.Value
Plot("ArmsIndex", "trin", _trin);
// Plot all properties of abands
Plot("ArmsIndex", "adratio", _trin.ADRatio);
Plot("ArmsIndex", "advratio", _trin.ADVRatio);
}
}
} class ArmsIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.universe_settings.resolution = Resolution.DAILY
self.universe_settings.schedule.on(self.date_rules.month_start())
self._universe = self.add_universe(self.universe.etf('SPY'))
def on_securities_changed(self, changes: SecurityChanges) -> None:
self._trin = self.trin(list(self._universe.selected))
def on_data(self, slice: Slice) -> None:
if self._trin.is_ready:
# The current value of self._trin is represented by self._trin.current.value
self.plot("ArmsIndex", "trin", self._trin.current.value)
# Plot all attributes of self._trin
self.plot("ArmsIndex", "ad_ratio", self._trin.ad_ratio.current.value)
self.plot("ArmsIndex", "adv_ratio", self._trin.adv_ratio.current.value)For more information about this method, see the QCAlgorithm classQCAlgorithm class.
You can manually create a ArmsIndex 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 ArmsIndexAlgorithm : QCAlgorithm
{
private Symbol _symbol,_reference;
private ArmsIndex _armsindex;
public override void Initialize()
{
_symbol = AddEquity("QQQ", Resolution.Daily).Symbol;
_reference = AddEquity("SPY", Resolution.Daily).Symbol;
_armsindex = new ArmsIndex();
_armsindex.Add(_symbol);
_armsindex.Add(_reference);
}
public override void OnData(Slice data)
{
if (data.Bars.TryGetValue(_symbol, out var bar))
_armsindex.Update(bar);
if (data.Bars.TryGetValue(_reference, out bar))
_armsindex.Update(bar);
if (_armsindex.IsReady)
{
// The current value of _armsindex is represented by itself (_armsindex)
// or _armsindex.Current.Value
Plot("ArmsIndex", "armsindex", _armsindex);
// Plot all properties of abands
Plot("ArmsIndex", "adratio", _armsindex.ADRatio);
Plot("ArmsIndex", "advratio", _armsindex.ADVRatio);
}
}
} class ArmsIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("QQQ", Resolution.DAILY).symbol
self._reference = self.add_equity("SPY", Resolution.DAILY).symbol
self._armsindex = ArmsIndex()
self._armsindex.add(self._symbol);
self._armsindex.add(self._reference);
def on_data(self, slice: Slice) -> None:
bar = slice.bars.get(self._symbol)
if bar:
self._armsindex.update(bar)
bar = slice.bars.get(self._reference)
if bar:
self._armsindex.update(bar)
if self._armsindex.is_ready:
# The current value of self._armsindex is represented by self._armsindex.current.value
self.plot("ArmsIndex", "armsindex", self._armsindex.current.value)
# Plot all attributes of self._armsindex
self.plot("ArmsIndex", "ad_ratio", self._armsindex.ad_ratio.current.value)
self.plot("ArmsIndex", "adv_ratio", self._armsindex.adv_ratio.current.value)For more information about this indicator, see its referencereference.
Indicator History
To get the historical data of the ArmsIndex 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 ArmsIndexAlgorithm : QCAlgorithm
{
private Symbol _symbol,_reference;
private ArmsIndex _trin;
public override void Initialize()
{
_symbol = AddEquity("QQQ", Resolution.Daily).Symbol;
_reference = AddEquity("SPY", Resolution.Daily).Symbol;
_trin = TRIN([_symbol, _reference]);
var indicatorHistory = IndicatorHistory(_trin, new[] { _symbol, _reference }, 100, Resolution.Minute);
var timeSpanIndicatorHistory = IndicatorHistory(_trin, new[] { _symbol, _reference }, TimeSpan.FromDays(10), Resolution.Minute);
var timePeriodIndicatorHistory = IndicatorHistory(_trin, new[] { _symbol, _reference }, new DateTime(2024, 7, 1), new DateTime(2024, 7, 5), Resolution.Minute);
// Access all attributes of indicatorHistory
var aDRatio = indicatorHistory.Select(x => ((dynamic)x).ADRatio).ToList();
var aDVRatio = indicatorHistory.Select(x => ((dynamic)x).ADVRatio).ToList();
}
} class ArmsIndexAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self._symbol = self.add_equity("QQQ", Resolution.DAILY).symbol
self._reference = self.add_equity("SPY", Resolution.DAILY).symbol
self._trin = self.trin([self._symbol, self._reference])
indicator_history = self.indicator_history(self._trin, [ self._symbol, self._reference ], 100, Resolution.MINUTE)
timedelta_indicator_history = self.indicator_history(self._trin, [ self._symbol, self._reference ], timedelta(days=10), Resolution.MINUTE)
time_period_indicator_history = self.indicator_history(self._trin, [ self._symbol, self._reference ], datetime(2024, 7, 1), datetime(2024, 7, 5), Resolution.MINUTE)
# Access all attributes of indicator_history
indicator_history_df = indicator_history.data_frame
ad_ratio = indicator_history_df["adratio"]
adv_ratio = indicator_history_df["advratio"]