Skip to content

Commit 8093e35

Browse files
committed
Normalized Average True Range
1 parent f64c233 commit 8093e35

File tree

8 files changed

+102
-18
lines changed

8 files changed

+102
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Last but not least, `talipp` is a community project and therefore open to any su
3030

3131
### What's new in the recent versions
3232

33+
- Normalized Average True Range indicator
3334
- Rogers-Satchell volatility indicator
3435
- auto-sampling of input values
35-
- [v2.0.0 scope](https://github.com/nardew/talipp/issues/111)
3636

37-
For the full history of changes see [CHANGELOG](https://github.com/nardew/talipp/releases).
37+
For the full history of changes see [Release Notes](https://github.com/nardew/talipp/releases).
3838

3939
---
4040

@@ -45,7 +45,7 @@ For the full history of changes see [CHANGELOG](https://github.com/nardew/talipp
4545
- Accumulation/Distribution (ADL)
4646
- Aroon
4747
- Average Directional Index (ADX)
48-
- Average True Range (ATR)
48+
- Average True Range (ATR), Normalized Average True Range (NATR)
4949
- Awesome Oscillator (AO)
5050
- Balance of Power (BOP)
5151
- Bollinger Bands (BB)

docs/indicator-catalogue.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Indicator catalogue
22

3-
| Name | Full name | Description |
4-
|-------------------------------------------------------------------------|---------------------------------------|----------------------------------------------------------------------------------------------------------------------|
3+
| Name | Full name | Description |
4+
|-------------------------------------------------------------------------|---------------------------------------|----------------------------------------------------------------------------------------------------------------|
55
| [ADL][talipp.indicators.AccuDist.AccuDist] | Accumulation Distribution Line | [](https://school.stockcharts.com/doku.php?id=technical_indicators:accumulation_distribution_line) |
66
| [ADX][talipp.indicators.ADX.ADX] | Average Directional Index | [](https://school.stockcharts.com/doku.php?id=technical_indicators:average_directional_index_adx) |
7-
| [ALMA][talipp.indicators.ALMA.ALMA] | Arnaud Legoux Moving Average | |
7+
| [ALMA][talipp.indicators.ALMA.ALMA] | Arnaud Legoux Moving Average | |
88
| [AO][talipp.indicators.AO.AO] | Awesome Oscillator | [](https://www.babypips.com/forexpedia/awesome-oscillator) |
99
| [Aroon][talipp.indicators.Aroon.Aroon] | Aroon Indicator | [](https://www.investopedia.com/terms/a/aroon.asp) |
1010
| [ATR][talipp.indicators.ATR.ATR] | Average True Range | [](https://school.stockcharts.com/doku.php?id=technical_indicators:average_true_range_atr) |
@@ -22,7 +22,7 @@
2222
| [EMV][talipp.indicators.EMV.EMV] | Ease of Movement | [](https://school.stockcharts.com/doku.php?id=technical_indicators:ease_of_movement_emv) |
2323
| [ForceIndex][talipp.indicators.ForceIndex.ForceIndex] | Force Index | [](https://school.stockcharts.com/doku.php?id=technical_indicators:force_index) |
2424
| [HMA][talipp.indicators.HMA.HMA] | Hull Moving Average | [](https://school.stockcharts.com/doku.php?id=technical_indicators:hull_moving_average) |
25-
| [IBS][talipp.indicators.IBS.IBS] | Internal Bar Strength | [](https://www.coingecko.com/learn/internal-bar-strength-ibs) |
25+
| [IBS][talipp.indicators.IBS.IBS] | Internal Bar Strength | [](https://www.coingecko.com/learn/internal-bar-strength-ibs) |
2626
| [IchimokuCloud][talipp.indicators.Ichimoku.Ichimoku] | Ichimoku Cloud | [](https://school.stockcharts.com/doku.php?id=technical_indicators:ichimoku_cloud) |
2727
| [KAMA][talipp.indicators.KAMA.KAMA] | Kauffman's Adaptive Moving Average | [](https://school.stockcharts.com/doku.php?id=technical_indicators:kaufman_s_adaptive_moving_average) |
2828
| [KeltnerChannels][talipp.indicators.KeltnerChannels.KeltnerChannels] | Keltner Channels | [](https://school.stockcharts.com/doku.php?id=technical_indicators:keltner_channels) |
@@ -32,18 +32,19 @@
3232
| [MassIndex][talipp.indicators.MassIndex.MassIndex] | Mass Index | [](https://school.stockcharts.com/doku.php?id=technical_indicators:mass_index) |
3333
| [McGinleyDynamic][talipp.indicators.McGinleyDynamic.McGinleyDynamic] | McGinley Dynamic | [](https://www.investopedia.com/terms/m/mcginley-dynamic.asp) |
3434
| [MeanDev][talipp.indicators.MeanDev.MeanDev] | Mean Deviation | [](https://school.stockcharts.com/doku.php?id=technical_indicators:accumulation_distribution_line) |
35+
| [NATR][talipp.indicators.NATR.NATR] | Normalized Average True Range | [](https://www.macroption.com/normalized-atr/) |
3536
| [OBV][talipp.indicators.OBV.OBV] | On-balacen Volume | [](https://school.stockcharts.com/doku.php?id=technical_indicators:on_balance_volume_obv) |
3637
| [ParabolicSAR][talipp.indicators.ParabolicSAR.ParabolicSAR] | Parabolic Stop and Reverse | [](https://school.stockcharts.com/doku.php?id=technical_indicators:parabolic_sar) |
37-
| [PivotsHL][talipp.indicators.PivotsHL.PivotsHL][^1][^2] | Pivot High Low Points | |
38+
| [PivotsHL][talipp.indicators.PivotsHL.PivotsHL][^1][^2] | Pivot High Low Points | |
3839
| [ROC][talipp.indicators.ROC.ROC] | Rate of Change | [](https://school.stockcharts.com/doku.php?id=technical_indicators:rate_of_change_roc_and_momentum) |
39-
| [RogersSatchell][talipp.indicators.RogersSatchell.RogersSatchell] | Rogers-Satchell volatility indicator | [](https://portfolioslab.com/tools/rogers-satchell) |
40+
| [RogersSatchell][talipp.indicators.RogersSatchell.RogersSatchell] | Rogers-Satchell volatility indicator | [](https://portfolioslab.com/tools/rogers-satchell) |
4041
| [RSI][talipp.indicators.RSI.RSI] | Relative Strength Index | [](https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi) |
41-
| [SFX][talipp.indicators.SFX.SFX] | | |
42+
| [SFX][talipp.indicators.SFX.SFX] | | |
4243
| [SMA][talipp.indicators.SMA.SMA] | Simple Moving Average | [](https://www.investopedia.com/terms/s/sma.asp) |
43-
| [SMMA][talipp.indicators.SMMA.SMMA] | Smoothed Simple Moving Average | |
44-
| [SOBV][talipp.indicators.SOBV.SOBV] | Smoothed On-Balance Volume | |
44+
| [SMMA][talipp.indicators.SMMA.SMMA] | Smoothed Simple Moving Average | |
45+
| [SOBV][talipp.indicators.SOBV.SOBV] | Smoothed On-Balance Volume | |
4546
| [STC][talipp.indicators.STC.STC] | Schaff Trend Cycle | [](https://howtotrade.com/indicators/schaff-trend-cycle/) |
46-
| [StdDev][talipp.indicators.StdDev.StdDev] | Standard Deviation | |
47+
| [StdDev][talipp.indicators.StdDev.StdDev] | Standard Deviation | |
4748
| [Stoch][talipp.indicators.Stoch.Stoch] | Stochastic Oscillator | [](https://school.stockcharts.com/doku.php?id=technical_indicators:stochastic_oscillator_fast_slow_and_full) |
4849
| [StochRSI][talipp.indicators.StochRSI.StochRSI] | Stochastic Relative Strength Index | [](https://school.stockcharts.com/doku.php?id=technical_indicators:stochrsi) |
4950
| [SuperTrend][talipp.indicators.SuperTrend.SuperTrend] | Super Trend | [](https://www.investopedia.com/supertrend-indicator-7976167) |
@@ -57,7 +58,7 @@
5758
| [VWAP][talipp.indicators.VWAP.VWAP] | Volume-weighted Average Price | [](https://school.stockcharts.com/doku.php?id=technical_indicators:vwap_intraday) |
5859
| [VWMA][talipp.indicators.VWMA.VWMA] | Volume-weighted Moving Average | [](https://www.tradingsetupsreview.com/volume-weighted-moving-average-vwma/) |
5960
| [WMA][talipp.indicators.WMA.WMA] | Weighted Moving Average | [](https://fxopen.com/blog/en/what-is-a-weighted-moving-average-and-how-do-you-calculate-it/) |
60-
| [ZigZag][talipp.indicators.ZigZag.ZigZag][^1] | ZigZag | [](https://school.stockcharts.com/doku.php?id=technical_indicators:zigzag) |
61+
| [ZigZag][talipp.indicators.ZigZag.ZigZag][^1] | ZigZag | [](https://school.stockcharts.com/doku.php?id=technical_indicators:zigzag) |
6162
| [ZLEMA][talipp.indicators.ZLEMA.ZLEMA] | Zero-lag Exponential Moving Average | [](https://en.wikipedia.org/wiki/Zero_lag_exponential_moving_average) |
6263

6364
Request a new indicator via [GitHub Issues](https://github.com/nardew/talipp/issues/new).

examples/binance_online.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from talipp.indicators import AccuDist, ADX, ALMA, AO, Aroon, ATR, BB, BOP, CCI, ChaikinOsc, ChandeKrollStop, CHOP, \
1010
CoppockCurve, DEMA, DonchianChannels, DPO, EMA, EMV, ForceIndex, HMA, IBS, Ichimoku, \
11-
KAMA, KeltnerChannels, KST, KVO, MACD, MassIndex, McGinleyDynamic, MeanDev, OBV, ROC, RogersSatchell, RSI, \
11+
KAMA, KeltnerChannels, KST, KVO, MACD, MassIndex, McGinleyDynamic, MeanDev, NATR, OBV, ROC, RogersSatchell, RSI, \
1212
ParabolicSAR, \
1313
SFX, SMA, SMMA, SOBV, STC, StdDev, \
1414
Stoch, StochRSI, SuperTrend, T3, TEMA, TRIX, TSI, TTM, UO, VTX, VWAP, VWMA, WMA, ZigZag, ZLEMA
@@ -61,6 +61,7 @@ async def run():
6161
print(f'MassIndex: {MassIndex(9, 9, 10, ohlcv)[-1]}')
6262
print(f'McGinleyDynamic: {McGinleyDynamic(14, close)[-1]}')
6363
print(f'MeanDev: {MeanDev(10, close)[-1]}')
64+
print(f'NATR: {NATR(14, ohlcv)[-5:]}')
6465
print(f'OBV: {OBV(ohlcv)[-1]}')
6566
print(f'ROC: {ROC(9, close)[-1]}')
6667
print(f'RogersSatchell: {RogersSatchell(9, ohlcv)[-5:]}')

examples/indicators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from talipp.indicators import AccuDist, ADX, ALMA, AO, Aroon, ATR, BB, BOP, CCI, ChaikinOsc, ChandeKrollStop, CHOP, \
44
CoppockCurve, DEMA, DonchianChannels, DPO, EMA, EMV, ForceIndex, HMA, IBS, Ichimoku, KAMA, KeltnerChannels, KST, KVO, \
5-
MACD, MassIndex, MeanDev, OBV, ROC, RogersSatchell, RSI, ParabolicSAR, SFX, SMA, SMMA, SOBV, STC, StdDev, Stoch, StochRSI, \
5+
MACD, MassIndex, MeanDev, NATR, OBV, ROC, RogersSatchell, RSI, ParabolicSAR, SFX, SMA, SMMA, SOBV, STC, StdDev, Stoch, StochRSI, \
66
SuperTrend, T3, TEMA, TRIX, TSI, TTM, UO, VTX, VWAP, VWMA, WMA, ZigZag, ZLEMA
77
from talipp.ohlcv import OHLCVFactory
88

@@ -47,9 +47,10 @@
4747
print(f'MACD: {MACD(12, 26, 9, close)[-1]}')
4848
print(f'MassIndex: {MassIndex(9, 9, 10, ohlcv)[-1]}')
4949
print(f'MeanDev: {MeanDev(10, close)[-1]}')
50+
print(f'NATR: {NATR(14, ohlcv)[-1]}')
5051
print(f'OBV: {OBV(ohlcv)[-1]}')
5152
print(f'ROC: {ROC(9, close)[-1]}')
52-
#print(f'RogersSatchell: {RogersSatchell(9, ohlcv)[-1]}')
53+
print(f'RogersSatchell: {RogersSatchell(9, ohlcv)[-1]}')
5354
print(f'RSI: {RSI(14, close)[-1]}')
5455
print(f"SAR: {ParabolicSAR(0.02, 0.02, 0.2, ohlcv)[-20:]}")
5556
print(f'SFX: {SFX(12, 12, 3, ohlcv)[-1]}')

talipp/indicators/NATR.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List, Any
2+
3+
from talipp.indicator_util import has_valid_values
4+
from talipp.indicators import ATR
5+
from talipp.indicators.Indicator import Indicator, InputModifierType
6+
from talipp.input import SamplingPeriodType
7+
from talipp.ohlcv import OHLCV
8+
9+
10+
class NATR(Indicator):
11+
"""Normalized Average True Range
12+
13+
Input type: [OHLCV][talipp.ohlcv.OHLCV]
14+
15+
Output type: `float`
16+
17+
Args:
18+
period: Period.
19+
input_values: List of input values.
20+
input_indicator: Input indicator.
21+
input_modifier: Input modifier.
22+
input_sampling: Input sampling type.
23+
"""
24+
25+
def __init__(self, period: int,
26+
input_values: List[OHLCV] = None,
27+
input_indicator: Indicator = None,
28+
input_modifier: InputModifierType = None,
29+
input_sampling: SamplingPeriodType = None):
30+
super(NATR, self).__init__(input_modifier=input_modifier,
31+
input_sampling=input_sampling)
32+
33+
self.period = period
34+
self.atr = ATR(period)
35+
36+
self.add_sub_indicator(self.atr)
37+
38+
self.initialize(input_values, input_indicator)
39+
40+
def _calculate_new_value(self) -> Any:
41+
if not has_valid_values(self.atr, 1):
42+
return None
43+
44+
if self.input_values[-1].close == 0:
45+
return None
46+
47+
return 100.0 * self.atr[-1] / self.input_values[-1].close

talipp/indicators/RogersSatchell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ def _calculate_new_value(self) -> Any:
4242
for ohlcv in self.input_values[-self.period:]:
4343
s += log(float(ohlcv.high) / ohlcv.close) * log(float(ohlcv.high) / ohlcv.open) + log(float(ohlcv.low) / ohlcv.close) * log(float(ohlcv.low) / ohlcv.open)
4444

45-
return sqrt(s / self.period)
45+
return sqrt(s / self.period) if s > 0 else None

talipp/indicators/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .MassIndex import MassIndex as MassIndex
3030
from .McGinleyDynamic import McGinleyDynamic as McGinleyDynamic
3131
from .MeanDev import MeanDev as MeanDev
32+
from .NATR import NATR as NATR
3233
from .OBV import OBV as OBV
3334
from .PivotsHL import PivotsHL as PivotsHL
3435
from .ROC import ROC as ROC
@@ -89,6 +90,7 @@
8990
"MassIndex",
9091
"McGinleyDynamic",
9192
"MeanDev",
93+
"NATR",
9294
"OBV",
9395
"ParabolicSAR",
9496
"PivotsHL",

test/test_NATR.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
3+
from talipp.indicators import NATR
4+
5+
from TalippTest import TalippTest
6+
7+
8+
class TestATR(TalippTest):
9+
def setUp(self) -> None:
10+
self.input_values = list(TalippTest.OHLCV_TMPL)
11+
12+
def test_init(self):
13+
ind = NATR(5, self.input_values)
14+
15+
print(ind)
16+
17+
self.assertAlmostEqual(ind[-3], 6.387410, places = 5)
18+
self.assertAlmostEqual(ind[-2], 6.501871, places = 5)
19+
self.assertAlmostEqual(ind[-1], 6.861131, places = 5)
20+
21+
def test_update(self):
22+
self.assertIndicatorUpdate(NATR(5, self.input_values))
23+
24+
def test_delete(self):
25+
self.assertIndicatorDelete(NATR(5, self.input_values))
26+
27+
def test_purge_oldest(self):
28+
self.assertIndicatorPurgeOldest(NATR(5, self.input_values))
29+
30+
31+
if __name__ == '__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)