-
-
Notifications
You must be signed in to change notification settings - Fork 849
/
momentum.py
157 lines (113 loc) · 5 KB
/
momentum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import unittest
import pandas as pd
from ta.momentum import (KAMAIndicator, ROCIndicator, RSIIndicator,
StochasticOscillator, TSIIndicator,
UltimateOscillator, WilliamsRIndicator, roc)
from ta.tests.utils import TestIndicator
class TestRateOfChangeIndicator(TestIndicator):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:on_balance_volume_obv
"""
_filename = 'ta/tests/data/cs-roc.csv'
def test_roc(self):
target = 'ROC'
result = roc(close=self._df['Close'], n=12, fillna=False)
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def test_roc2(self):
target = 'ROC'
result = ROCIndicator(close=self._df['Close'], n=12, fillna=False).roc()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
class TestRSIIndicator(unittest.TestCase):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:relative_strength_index_rsi
Note: Using a more simple initilization (directly `ewm`; stockcharts uses `sma` + `ewm`)
"""
_filename = 'ta/tests/data/cs-rsi.csv'
def setUp(self):
self._df = pd.read_csv(self._filename, sep=',')
self._indicator = RSIIndicator(close=self._df['Close'], n=14, fillna=False)
def tearDown(self):
del(self._df)
def test_rsi(self):
target = 'RSI'
result = self._indicator.rsi()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
class TestUltimateOscillator(unittest.TestCase):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:ultimate_oscillator
"""
_filename = 'ta/tests/data/cs-ultosc.csv'
def setUp(self):
self._df = pd.read_csv(self._filename, sep=',')
self._indicator = UltimateOscillator(
high=self._df['High'], low=self._df['Low'], close=self._df['Close'],
s=7, m=14, len=28, ws=4.0, wm=2.0, wl=1.0, fillna=False)
def tearDown(self):
del(self._df)
def test_uo(self):
target = 'Ult_Osc'
result = self._indicator.uo()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
class TestStochasticOscillator(unittest.TestCase):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:stochastic_oscillator_fast_slow_and_full
"""
_filename = 'ta/tests/data/cs-soo.csv'
def setUp(self):
self._df = pd.read_csv(self._filename, sep=',')
self._indicator = StochasticOscillator(
high=self._df['High'], low=self._df['Low'], close=self._df['Close'], n=14, d_n=3, fillna=False)
def tearDown(self):
del(self._df)
def test_so(self):
target = 'SO'
result = self._indicator.stoch()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
def test_so_signal(self):
target = 'SO_SIG'
result = self._indicator.stoch_signal()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
class TestWilliamsRIndicator(unittest.TestCase):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:williams_r
"""
_filename = 'ta/tests/data/cs-percentr.csv'
def setUp(self):
self._df = pd.read_csv(self._filename, sep=',')
self._indicator = WilliamsRIndicator(
high=self._df['High'], low=self._df['Low'], close=self._df['Close'], lbp=14, fillna=False)
def tearDown(self):
del(self._df)
def test_wr(self):
target = 'Williams_%R'
result = self._indicator.wr()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
class TestKAMAIndicator(unittest.TestCase):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:kaufman_s_adaptive_moving_average
"""
_filename = 'ta/tests/data/cs-kama.csv'
def setUp(self):
self._df = pd.read_csv(self._filename, sep=',')
self._indicator = KAMAIndicator(close=self._df['Close'], n=10, pow1=2, pow2=30, fillna=False)
def tearDown(self):
del(self._df)
def test_kama(self):
target = 'KAMA'
result = self._indicator.kama()
pd.testing.assert_series_equal(self._df[target].tail(), result.tail(), check_names=False)
class TestTSIIndicator(unittest.TestCase):
"""
https://school.stockcharts.com/doku.php?id=technical_indicators:true_strength_index
"""
_filename = 'ta/tests/data/cs-tsi.csv'
def setUp(self):
self._df = pd.read_csv(self._filename, sep=',')
self._indicator = TSIIndicator(close=self._df['Close'], r=25, s=13, fillna=False)
def tearDown(self):
del(self._df)
def test_kama(self):
target = 'TSI'
result = self._indicator.tsi()
pd.testing.assert_series_equal(
self._df[target].tail(), result.tail(), check_names=False, check_less_precise=True)