akshare-one 0.2.2__py3-none-any.whl → 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- akshare_one/__init__.py +31 -31
- akshare_one/financial.py +46 -46
- akshare_one/indicators.py +395 -0
- akshare_one/insider.py +33 -33
- akshare_one/modules/cache.py +9 -9
- akshare_one/modules/eastmoney/client.py +88 -88
- akshare_one/modules/eastmoney/utils.py +104 -104
- akshare_one/modules/financial/base.py +22 -22
- akshare_one/modules/financial/factory.py +44 -44
- akshare_one/modules/financial/sina.py +273 -273
- akshare_one/modules/historical/base.py +47 -39
- akshare_one/modules/historical/eastmoney.py +241 -241
- akshare_one/modules/historical/eastmoney_direct.py +79 -79
- akshare_one/modules/historical/factory.py +48 -48
- akshare_one/modules/historical/sina.py +218 -218
- akshare_one/modules/indicators/__init__.py +0 -0
- akshare_one/modules/indicators/base.py +158 -0
- akshare_one/modules/indicators/factory.py +33 -0
- akshare_one/modules/indicators/simple.py +230 -0
- akshare_one/modules/indicators/talib.py +263 -0
- akshare_one/modules/insider/base.py +28 -28
- akshare_one/modules/insider/factory.py +44 -44
- akshare_one/modules/insider/xueqiu.py +115 -115
- akshare_one/modules/news/base.py +22 -22
- akshare_one/modules/news/eastmoney.py +47 -47
- akshare_one/modules/news/factory.py +44 -44
- akshare_one/modules/realtime/base.py +27 -27
- akshare_one/modules/realtime/eastmoney.py +57 -57
- akshare_one/modules/realtime/eastmoney_direct.py +37 -37
- akshare_one/modules/realtime/factory.py +48 -48
- akshare_one/modules/realtime/xueqiu.py +60 -60
- akshare_one/modules/utils.py +10 -10
- akshare_one/news.py +27 -27
- akshare_one/stock.py +78 -78
- {akshare_one-0.2.2.dist-info → akshare_one-0.3.0.dist-info}/METADATA +70 -66
- akshare_one-0.3.0.dist-info/RECORD +39 -0
- {akshare_one-0.2.2.dist-info → akshare_one-0.3.0.dist-info}/licenses/LICENSE +21 -21
- akshare_one-0.2.2.dist-info/RECORD +0 -33
- {akshare_one-0.2.2.dist-info → akshare_one-0.3.0.dist-info}/WHEEL +0 -0
- {akshare_one-0.2.2.dist-info → akshare_one-0.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,158 @@
|
|
1
|
+
from abc import ABC, abstractmethod
|
2
|
+
import pandas as pd
|
3
|
+
|
4
|
+
|
5
|
+
class BaseIndicatorCalculator(ABC):
|
6
|
+
"""Base class for indicator calculators"""
|
7
|
+
|
8
|
+
@abstractmethod
|
9
|
+
def calculate_sma(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
10
|
+
pass
|
11
|
+
|
12
|
+
@abstractmethod
|
13
|
+
def calculate_ema(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
14
|
+
pass
|
15
|
+
|
16
|
+
@abstractmethod
|
17
|
+
def calculate_rsi(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
18
|
+
pass
|
19
|
+
|
20
|
+
@abstractmethod
|
21
|
+
def calculate_macd(
|
22
|
+
self, df: pd.DataFrame, fast: int, slow: int, signal: int
|
23
|
+
) -> pd.DataFrame:
|
24
|
+
pass
|
25
|
+
|
26
|
+
@abstractmethod
|
27
|
+
def calculate_bollinger_bands(
|
28
|
+
self, df: pd.DataFrame, window: int, std: int
|
29
|
+
) -> pd.DataFrame:
|
30
|
+
pass
|
31
|
+
|
32
|
+
@abstractmethod
|
33
|
+
def calculate_stoch(
|
34
|
+
self, df: pd.DataFrame, window: int, smooth_d: int, smooth_k: int
|
35
|
+
) -> pd.DataFrame:
|
36
|
+
pass
|
37
|
+
|
38
|
+
@abstractmethod
|
39
|
+
def calculate_atr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
40
|
+
pass
|
41
|
+
|
42
|
+
@abstractmethod
|
43
|
+
def calculate_cci(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
44
|
+
pass
|
45
|
+
|
46
|
+
@abstractmethod
|
47
|
+
def calculate_adx(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
48
|
+
pass
|
49
|
+
|
50
|
+
@abstractmethod
|
51
|
+
def calculate_willr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
52
|
+
pass
|
53
|
+
|
54
|
+
@abstractmethod
|
55
|
+
def calculate_ad(self, df: pd.DataFrame) -> pd.DataFrame:
|
56
|
+
pass
|
57
|
+
|
58
|
+
@abstractmethod
|
59
|
+
def calculate_adosc(
|
60
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int
|
61
|
+
) -> pd.DataFrame:
|
62
|
+
pass
|
63
|
+
|
64
|
+
@abstractmethod
|
65
|
+
def calculate_obv(self, df: pd.DataFrame) -> pd.DataFrame:
|
66
|
+
pass
|
67
|
+
|
68
|
+
@abstractmethod
|
69
|
+
def calculate_mom(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
70
|
+
pass
|
71
|
+
|
72
|
+
@abstractmethod
|
73
|
+
def calculate_sar(
|
74
|
+
self, df: pd.DataFrame, acceleration: float, maximum: float
|
75
|
+
) -> pd.DataFrame:
|
76
|
+
pass
|
77
|
+
|
78
|
+
@abstractmethod
|
79
|
+
def calculate_tsf(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
80
|
+
pass
|
81
|
+
|
82
|
+
@abstractmethod
|
83
|
+
def calculate_apo(
|
84
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int, ma_type: int
|
85
|
+
) -> pd.DataFrame:
|
86
|
+
pass
|
87
|
+
|
88
|
+
@abstractmethod
|
89
|
+
def calculate_aroon(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
90
|
+
pass
|
91
|
+
|
92
|
+
@abstractmethod
|
93
|
+
def calculate_aroonosc(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
94
|
+
pass
|
95
|
+
|
96
|
+
@abstractmethod
|
97
|
+
def calculate_bop(self, df: pd.DataFrame) -> pd.DataFrame:
|
98
|
+
pass
|
99
|
+
|
100
|
+
@abstractmethod
|
101
|
+
def calculate_cmo(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
102
|
+
pass
|
103
|
+
|
104
|
+
@abstractmethod
|
105
|
+
def calculate_dx(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
106
|
+
pass
|
107
|
+
|
108
|
+
@abstractmethod
|
109
|
+
def calculate_mfi(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
110
|
+
pass
|
111
|
+
|
112
|
+
@abstractmethod
|
113
|
+
def calculate_minus_di(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
114
|
+
pass
|
115
|
+
|
116
|
+
@abstractmethod
|
117
|
+
def calculate_minus_dm(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
118
|
+
pass
|
119
|
+
|
120
|
+
@abstractmethod
|
121
|
+
def calculate_plus_di(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
122
|
+
pass
|
123
|
+
|
124
|
+
@abstractmethod
|
125
|
+
def calculate_plus_dm(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
126
|
+
pass
|
127
|
+
|
128
|
+
@abstractmethod
|
129
|
+
def calculate_ppo(
|
130
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int, ma_type: int
|
131
|
+
) -> pd.DataFrame:
|
132
|
+
pass
|
133
|
+
|
134
|
+
@abstractmethod
|
135
|
+
def calculate_roc(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
136
|
+
pass
|
137
|
+
|
138
|
+
@abstractmethod
|
139
|
+
def calculate_rocp(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
140
|
+
pass
|
141
|
+
|
142
|
+
@abstractmethod
|
143
|
+
def calculate_rocr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
144
|
+
pass
|
145
|
+
|
146
|
+
@abstractmethod
|
147
|
+
def calculate_rocr100(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
148
|
+
pass
|
149
|
+
|
150
|
+
@abstractmethod
|
151
|
+
def calculate_trix(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
152
|
+
pass
|
153
|
+
|
154
|
+
@abstractmethod
|
155
|
+
def calculate_ultosc(
|
156
|
+
self, df: pd.DataFrame, window1: int, window2: int, window3: int
|
157
|
+
) -> pd.DataFrame:
|
158
|
+
pass
|
@@ -0,0 +1,33 @@
|
|
1
|
+
from .base import BaseIndicatorCalculator
|
2
|
+
from .simple import SimpleIndicatorCalculator
|
3
|
+
|
4
|
+
_calculators = {
|
5
|
+
"simple": SimpleIndicatorCalculator,
|
6
|
+
}
|
7
|
+
TALIB_AVAILABLE = False
|
8
|
+
try:
|
9
|
+
from .talib import TalibIndicatorCalculator
|
10
|
+
|
11
|
+
_calculators["talib"] = TalibIndicatorCalculator
|
12
|
+
TALIB_AVAILABLE = True
|
13
|
+
except ImportError:
|
14
|
+
# talib is optional
|
15
|
+
pass
|
16
|
+
|
17
|
+
|
18
|
+
class IndicatorFactory:
|
19
|
+
"""Factory for indicator calculators"""
|
20
|
+
|
21
|
+
@classmethod
|
22
|
+
def get_calculator(cls, calculator_type: str = "talib") -> BaseIndicatorCalculator:
|
23
|
+
"""Get indicator calculator instance
|
24
|
+
|
25
|
+
If talib is not installed, it will fall back to the simple implementation.
|
26
|
+
"""
|
27
|
+
if calculator_type == "talib" and not TALIB_AVAILABLE:
|
28
|
+
calculator_type = "simple"
|
29
|
+
|
30
|
+
calculator_class = _calculators.get(calculator_type)
|
31
|
+
if not calculator_class:
|
32
|
+
raise ValueError(f"Unsupported calculator type: {calculator_type}")
|
33
|
+
return calculator_class()
|
@@ -0,0 +1,230 @@
|
|
1
|
+
import pandas as pd
|
2
|
+
from .base import BaseIndicatorCalculator
|
3
|
+
|
4
|
+
|
5
|
+
class SimpleIndicatorCalculator(BaseIndicatorCalculator):
|
6
|
+
"""Basic pandas-based indicator implementations"""
|
7
|
+
|
8
|
+
def calculate_sma(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
9
|
+
return (
|
10
|
+
df["close"]
|
11
|
+
.rolling(window=window, min_periods=window)
|
12
|
+
.mean()
|
13
|
+
.to_frame("sma")
|
14
|
+
)
|
15
|
+
|
16
|
+
def calculate_ema(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
17
|
+
return (
|
18
|
+
df["close"]
|
19
|
+
.ewm(span=window, adjust=False, min_periods=window)
|
20
|
+
.mean()
|
21
|
+
.to_frame("ema")
|
22
|
+
)
|
23
|
+
|
24
|
+
def calculate_rsi(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
25
|
+
delta = df["close"].diff()
|
26
|
+
gain = delta.clip(lower=0)
|
27
|
+
loss = -delta.clip(upper=0)
|
28
|
+
|
29
|
+
avg_gain = gain.ewm(alpha=1 / window, min_periods=window, adjust=False).mean()
|
30
|
+
avg_loss = loss.ewm(alpha=1 / window, min_periods=window, adjust=False).mean()
|
31
|
+
|
32
|
+
rs = avg_gain / avg_loss
|
33
|
+
rsi = 100 - (100 / (1 + rs))
|
34
|
+
|
35
|
+
return rsi.clip(0, 100).to_frame("rsi")
|
36
|
+
|
37
|
+
def calculate_macd(
|
38
|
+
self, df: pd.DataFrame, fast: int, slow: int, signal: int
|
39
|
+
) -> pd.DataFrame:
|
40
|
+
close = df["close"]
|
41
|
+
ema_fast = close.ewm(span=fast, adjust=False, min_periods=fast).mean()
|
42
|
+
ema_slow = close.ewm(span=slow, adjust=False, min_periods=slow).mean()
|
43
|
+
|
44
|
+
macd_line = ema_fast - ema_slow
|
45
|
+
signal_line = macd_line.ewm(
|
46
|
+
span=signal, adjust=False, min_periods=signal
|
47
|
+
).mean()
|
48
|
+
|
49
|
+
return pd.DataFrame(
|
50
|
+
{
|
51
|
+
"macd": macd_line,
|
52
|
+
"signal": signal_line,
|
53
|
+
"histogram": macd_line - signal_line,
|
54
|
+
}
|
55
|
+
)
|
56
|
+
|
57
|
+
def calculate_bollinger_bands(
|
58
|
+
self, df: pd.DataFrame, window: int, std: int
|
59
|
+
) -> pd.DataFrame:
|
60
|
+
close = df["close"]
|
61
|
+
sma = close.rolling(window=window, min_periods=window).mean()
|
62
|
+
rolling_std = close.rolling(window=window, min_periods=window).std()
|
63
|
+
upper_band = sma + (rolling_std * std)
|
64
|
+
lower_band = sma - (rolling_std * std)
|
65
|
+
return pd.DataFrame(
|
66
|
+
{"upper_band": upper_band, "middle_band": sma, "lower_band": lower_band}
|
67
|
+
)
|
68
|
+
|
69
|
+
def calculate_stoch(
|
70
|
+
self, df: pd.DataFrame, window: int, smooth_d: int, smooth_k: int
|
71
|
+
) -> pd.DataFrame:
|
72
|
+
high = df["high"]
|
73
|
+
low = df["low"]
|
74
|
+
close = df["close"]
|
75
|
+
|
76
|
+
lowest_low = low.rolling(window=window).min()
|
77
|
+
highest_high = high.rolling(window=window).max()
|
78
|
+
|
79
|
+
k = 100 * (close - lowest_low) / (highest_high - lowest_low)
|
80
|
+
slow_k = k.rolling(window=smooth_k).mean()
|
81
|
+
slow_d = slow_k.rolling(window=smooth_d).mean()
|
82
|
+
|
83
|
+
return pd.DataFrame({"slow_k": slow_k, "slow_d": slow_d})
|
84
|
+
|
85
|
+
def calculate_atr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
86
|
+
high = df["high"]
|
87
|
+
low = df["low"]
|
88
|
+
close = df["close"]
|
89
|
+
|
90
|
+
tr1 = high - low
|
91
|
+
tr2 = abs(high - close.shift())
|
92
|
+
tr3 = abs(low - close.shift())
|
93
|
+
tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
|
94
|
+
|
95
|
+
atr = tr.ewm(alpha=1 / window, adjust=False, min_periods=window).mean()
|
96
|
+
return atr.to_frame("atr")
|
97
|
+
|
98
|
+
def calculate_cci(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
99
|
+
high = df["high"]
|
100
|
+
low = df["low"]
|
101
|
+
close = df["close"]
|
102
|
+
|
103
|
+
tp = (high + low + close) / 3
|
104
|
+
tp_sma = tp.rolling(window=window, min_periods=window).mean()
|
105
|
+
mean_dev = tp.rolling(window=window, min_periods=window).apply(
|
106
|
+
lambda x: (x - x.mean()).abs().mean()
|
107
|
+
)
|
108
|
+
|
109
|
+
cci = (tp - tp_sma) / (0.015 * mean_dev)
|
110
|
+
return cci.to_frame("cci")
|
111
|
+
|
112
|
+
def calculate_adx(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
113
|
+
high = df["high"]
|
114
|
+
low = df["low"]
|
115
|
+
close = df["close"]
|
116
|
+
|
117
|
+
# Calculate +DM, -DM and TR
|
118
|
+
move_up = high.diff()
|
119
|
+
move_down = low.diff().apply(abs)
|
120
|
+
|
121
|
+
plus_dm = pd.Series((move_up > move_down) & (move_up > 0)).astype(int) * move_up
|
122
|
+
minus_dm = (
|
123
|
+
pd.Series((move_down > move_up) & (move_down > 0)).astype(int) * move_down
|
124
|
+
)
|
125
|
+
|
126
|
+
tr1 = high - low
|
127
|
+
tr2 = abs(high - close.shift())
|
128
|
+
tr3 = abs(low - close.shift())
|
129
|
+
tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
|
130
|
+
|
131
|
+
# Smooth +DM, -DM and TR
|
132
|
+
atr = tr.ewm(alpha=1 / window, adjust=False, min_periods=window).mean()
|
133
|
+
plus_di = 100 * (
|
134
|
+
plus_dm.ewm(alpha=1 / window, adjust=False, min_periods=window).mean() / atr
|
135
|
+
)
|
136
|
+
minus_di = 100 * (
|
137
|
+
minus_dm.ewm(alpha=1 / window, adjust=False, min_periods=window).mean()
|
138
|
+
/ atr
|
139
|
+
)
|
140
|
+
|
141
|
+
# Calculate ADX
|
142
|
+
dx = 100 * (abs(plus_di - minus_di) / (plus_di + minus_di))
|
143
|
+
adx = dx.ewm(alpha=1 / window, adjust=False, min_periods=window).mean()
|
144
|
+
|
145
|
+
return adx.to_frame("adx")
|
146
|
+
|
147
|
+
def calculate_willr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
148
|
+
raise NotImplementedError("WILLR not implemented in simple calculator")
|
149
|
+
|
150
|
+
def calculate_ad(self, df: pd.DataFrame) -> pd.DataFrame:
|
151
|
+
raise NotImplementedError("AD not implemented in simple calculator")
|
152
|
+
|
153
|
+
def calculate_adosc(
|
154
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int
|
155
|
+
) -> pd.DataFrame:
|
156
|
+
raise NotImplementedError("ADOSC not implemented in simple calculator")
|
157
|
+
|
158
|
+
def calculate_obv(self, df: pd.DataFrame) -> pd.DataFrame:
|
159
|
+
raise NotImplementedError("OBV not implemented in simple calculator")
|
160
|
+
|
161
|
+
def calculate_mom(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
162
|
+
raise NotImplementedError("MOM not implemented in simple calculator")
|
163
|
+
|
164
|
+
def calculate_sar(
|
165
|
+
self, df: pd.DataFrame, acceleration: float, maximum: float
|
166
|
+
) -> pd.DataFrame:
|
167
|
+
raise NotImplementedError("SAR not implemented in simple calculator")
|
168
|
+
|
169
|
+
def calculate_tsf(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
170
|
+
raise NotImplementedError("TSF not implemented in simple calculator")
|
171
|
+
|
172
|
+
def calculate_apo(
|
173
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int, ma_type: int
|
174
|
+
) -> pd.DataFrame:
|
175
|
+
raise NotImplementedError("APO not implemented in simple calculator")
|
176
|
+
|
177
|
+
def calculate_aroon(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
178
|
+
raise NotImplementedError("AROON not implemented in simple calculator")
|
179
|
+
|
180
|
+
def calculate_aroonosc(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
181
|
+
raise NotImplementedError("AROONOSC not implemented in simple calculator")
|
182
|
+
|
183
|
+
def calculate_bop(self, df: pd.DataFrame) -> pd.DataFrame:
|
184
|
+
raise NotImplementedError("BOP not implemented in simple calculator")
|
185
|
+
|
186
|
+
def calculate_cmo(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
187
|
+
raise NotImplementedError("CMO not implemented in simple calculator")
|
188
|
+
|
189
|
+
def calculate_dx(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
190
|
+
raise NotImplementedError("DX not implemented in simple calculator")
|
191
|
+
|
192
|
+
def calculate_mfi(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
193
|
+
raise NotImplementedError("MFI not implemented in simple calculator")
|
194
|
+
|
195
|
+
def calculate_minus_di(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
196
|
+
raise NotImplementedError("MINUS_DI not implemented in simple calculator")
|
197
|
+
|
198
|
+
def calculate_minus_dm(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
199
|
+
raise NotImplementedError("MINUS_DM not implemented in simple calculator")
|
200
|
+
|
201
|
+
def calculate_plus_di(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
202
|
+
raise NotImplementedError("PLUS_DI not implemented in simple calculator")
|
203
|
+
|
204
|
+
def calculate_plus_dm(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
205
|
+
raise NotImplementedError("PLUS_DM not implemented in simple calculator")
|
206
|
+
|
207
|
+
def calculate_ppo(
|
208
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int, ma_type: int
|
209
|
+
) -> pd.DataFrame:
|
210
|
+
raise NotImplementedError("PPO not implemented in simple calculator")
|
211
|
+
|
212
|
+
def calculate_roc(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
213
|
+
raise NotImplementedError("ROC not implemented in simple calculator")
|
214
|
+
|
215
|
+
def calculate_rocp(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
216
|
+
raise NotImplementedError("ROCP not implemented in simple calculator")
|
217
|
+
|
218
|
+
def calculate_rocr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
219
|
+
raise NotImplementedError("ROCR not implemented in simple calculator")
|
220
|
+
|
221
|
+
def calculate_rocr100(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
222
|
+
raise NotImplementedError("ROCR100 not implemented in simple calculator")
|
223
|
+
|
224
|
+
def calculate_trix(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
225
|
+
raise NotImplementedError("TRIX not implemented in simple calculator")
|
226
|
+
|
227
|
+
def calculate_ultosc(
|
228
|
+
self, df: pd.DataFrame, window1: int, window2: int, window3: int
|
229
|
+
) -> pd.DataFrame:
|
230
|
+
raise NotImplementedError("ULTOSC not implemented in simple calculator")
|
@@ -0,0 +1,263 @@
|
|
1
|
+
import talib
|
2
|
+
import pandas as pd
|
3
|
+
from .base import BaseIndicatorCalculator
|
4
|
+
|
5
|
+
|
6
|
+
class TalibIndicatorCalculator(BaseIndicatorCalculator):
|
7
|
+
"""TA-Lib based indicator implementations"""
|
8
|
+
|
9
|
+
def calculate_sma(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
10
|
+
close = df["close"].values
|
11
|
+
sma = talib.SMA(close, timeperiod=window)
|
12
|
+
return pd.DataFrame({"sma": sma}, index=df.index)
|
13
|
+
|
14
|
+
def calculate_ema(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
15
|
+
close = df["close"].values
|
16
|
+
ema = talib.EMA(close, timeperiod=window)
|
17
|
+
return pd.DataFrame({"ema": ema}, index=df.index)
|
18
|
+
|
19
|
+
def calculate_rsi(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
20
|
+
close = df["close"].values
|
21
|
+
rsi = talib.RSI(close, timeperiod=window)
|
22
|
+
return pd.DataFrame({"rsi": rsi}, index=df.index)
|
23
|
+
|
24
|
+
def calculate_macd(
|
25
|
+
self, df: pd.DataFrame, fast: int, slow: int, signal: int
|
26
|
+
) -> pd.DataFrame:
|
27
|
+
close = df["close"].values
|
28
|
+
macd, signal_line, histogram = talib.MACD(
|
29
|
+
close, fastperiod=fast, slowperiod=slow, signalperiod=signal
|
30
|
+
)
|
31
|
+
return pd.DataFrame(
|
32
|
+
{"macd": macd, "signal": signal_line, "histogram": histogram},
|
33
|
+
index=df.index,
|
34
|
+
)
|
35
|
+
|
36
|
+
def calculate_bollinger_bands(
|
37
|
+
self, df: pd.DataFrame, window: int, std: int
|
38
|
+
) -> pd.DataFrame:
|
39
|
+
close = df["close"].values
|
40
|
+
upper, middle, lower = talib.BBANDS(
|
41
|
+
close, timeperiod=window, nbdevup=std, nbdevdn=std, matype=talib.MA_Type.SMA
|
42
|
+
)
|
43
|
+
return pd.DataFrame(
|
44
|
+
{"upper_band": upper, "middle_band": middle, "lower_band": lower},
|
45
|
+
index=df.index,
|
46
|
+
)
|
47
|
+
|
48
|
+
def calculate_stoch(
|
49
|
+
self, df: pd.DataFrame, window: int, smooth_d: int, smooth_k: int
|
50
|
+
) -> pd.DataFrame:
|
51
|
+
high = df["high"].values
|
52
|
+
low = df["low"].values
|
53
|
+
close = df["close"].values
|
54
|
+
slow_k, slow_d = talib.STOCH(
|
55
|
+
high,
|
56
|
+
low,
|
57
|
+
close,
|
58
|
+
fastk_period=window,
|
59
|
+
slowk_period=smooth_k,
|
60
|
+
slowk_matype=talib.MA_Type.SMA,
|
61
|
+
slowd_period=smooth_d,
|
62
|
+
slowd_matype=talib.MA_Type.SMA,
|
63
|
+
)
|
64
|
+
return pd.DataFrame({"slow_k": slow_k, "slow_d": slow_d}, index=df.index)
|
65
|
+
|
66
|
+
def calculate_atr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
67
|
+
high = df["high"].values
|
68
|
+
low = df["low"].values
|
69
|
+
close = df["close"].values
|
70
|
+
atr = talib.ATR(high, low, close, timeperiod=window)
|
71
|
+
return pd.DataFrame({"atr": atr}, index=df.index)
|
72
|
+
|
73
|
+
def calculate_cci(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
74
|
+
high = df["high"].values
|
75
|
+
low = df["low"].values
|
76
|
+
close = df["close"].values
|
77
|
+
cci = talib.CCI(high, low, close, timeperiod=window)
|
78
|
+
return pd.DataFrame({"cci": cci}, index=df.index)
|
79
|
+
|
80
|
+
def calculate_adx(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
81
|
+
high = df["high"].values
|
82
|
+
low = df["low"].values
|
83
|
+
close = df["close"].values
|
84
|
+
adx = talib.ADX(high, low, close, timeperiod=window)
|
85
|
+
return pd.DataFrame({"adx": adx}, index=df.index)
|
86
|
+
|
87
|
+
def calculate_willr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
88
|
+
high = df["high"].values
|
89
|
+
low = df["low"].values
|
90
|
+
close = df["close"].values
|
91
|
+
willr = talib.WILLR(high, low, close, timeperiod=window)
|
92
|
+
return pd.DataFrame({"willr": willr}, index=df.index)
|
93
|
+
|
94
|
+
def calculate_ad(self, df: pd.DataFrame) -> pd.DataFrame:
|
95
|
+
high = df["high"].values
|
96
|
+
low = df["low"].values
|
97
|
+
close = df["close"].values
|
98
|
+
volume = df["volume"].values.astype(float)
|
99
|
+
ad = talib.AD(high, low, close, volume)
|
100
|
+
return pd.DataFrame({"ad": ad}, index=df.index)
|
101
|
+
|
102
|
+
def calculate_adosc(
|
103
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int
|
104
|
+
) -> pd.DataFrame:
|
105
|
+
high = df["high"].values
|
106
|
+
low = df["low"].values
|
107
|
+
close = df["close"].values
|
108
|
+
volume = df["volume"].values.astype(float)
|
109
|
+
adosc = talib.ADOSC(
|
110
|
+
high, low, close, volume, fastperiod=fast_period, slowperiod=slow_period
|
111
|
+
)
|
112
|
+
return pd.DataFrame({"adosc": adosc}, index=df.index)
|
113
|
+
|
114
|
+
def calculate_obv(self, df: pd.DataFrame) -> pd.DataFrame:
|
115
|
+
close = df["close"].values
|
116
|
+
volume = df["volume"].values.astype(float)
|
117
|
+
obv = talib.OBV(close, volume)
|
118
|
+
return pd.DataFrame({"obv": obv}, index=df.index)
|
119
|
+
|
120
|
+
def calculate_mom(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
121
|
+
close = df["close"].values
|
122
|
+
mom = talib.MOM(close, timeperiod=window)
|
123
|
+
return pd.DataFrame({"mom": mom}, index=df.index)
|
124
|
+
|
125
|
+
def calculate_sar(
|
126
|
+
self, df: pd.DataFrame, acceleration: float, maximum: float
|
127
|
+
) -> pd.DataFrame:
|
128
|
+
high = df["high"].values
|
129
|
+
low = df["low"].values
|
130
|
+
sar = talib.SAR(high, low, acceleration=acceleration, maximum=maximum)
|
131
|
+
return pd.DataFrame({"sar": sar}, index=df.index)
|
132
|
+
|
133
|
+
def calculate_tsf(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
134
|
+
close = df["close"].values
|
135
|
+
tsf = talib.TSF(close, timeperiod=window)
|
136
|
+
return pd.DataFrame({"tsf": tsf}, index=df.index)
|
137
|
+
|
138
|
+
def calculate_apo(
|
139
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int, ma_type
|
140
|
+
) -> pd.DataFrame:
|
141
|
+
close = df["close"].values
|
142
|
+
apo = talib.APO(
|
143
|
+
close, fastperiod=fast_period, slowperiod=slow_period, matype=ma_type
|
144
|
+
)
|
145
|
+
return pd.DataFrame({"apo": apo}, index=df.index)
|
146
|
+
|
147
|
+
def calculate_aroon(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
148
|
+
high = df["high"].values
|
149
|
+
low = df["low"].values
|
150
|
+
aroon_down, aroon_up = talib.AROON(high, low, timeperiod=window)
|
151
|
+
return pd.DataFrame(
|
152
|
+
{"aroon_down": aroon_down, "aroon_up": aroon_up}, index=df.index
|
153
|
+
)
|
154
|
+
|
155
|
+
def calculate_aroonosc(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
156
|
+
high = df["high"].values
|
157
|
+
low = df["low"].values
|
158
|
+
aroonosc = talib.AROONOSC(high, low, timeperiod=window)
|
159
|
+
return pd.DataFrame({"aroonosc": aroonosc}, index=df.index)
|
160
|
+
|
161
|
+
def calculate_bop(self, df: pd.DataFrame) -> pd.DataFrame:
|
162
|
+
open_ = df["open"].values
|
163
|
+
high = df["high"].values
|
164
|
+
low = df["low"].values
|
165
|
+
close = df["close"].values
|
166
|
+
bop = talib.BOP(open_, high, low, close)
|
167
|
+
return pd.DataFrame({"bop": bop}, index=df.index)
|
168
|
+
|
169
|
+
def calculate_cmo(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
170
|
+
close = df["close"].values
|
171
|
+
cmo = talib.CMO(close, timeperiod=window)
|
172
|
+
return pd.DataFrame({"cmo": cmo}, index=df.index)
|
173
|
+
|
174
|
+
def calculate_dx(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
175
|
+
high = df["high"].values
|
176
|
+
low = df["low"].values
|
177
|
+
close = df["close"].values
|
178
|
+
dx = talib.DX(high, low, close, timeperiod=window)
|
179
|
+
return pd.DataFrame({"dx": dx}, index=df.index)
|
180
|
+
|
181
|
+
def calculate_mfi(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
182
|
+
high = df["high"].values
|
183
|
+
low = df["low"].values
|
184
|
+
close = df["close"].values
|
185
|
+
volume = df["volume"].values.astype(float)
|
186
|
+
mfi = talib.MFI(high, low, close, volume, timeperiod=window)
|
187
|
+
return pd.DataFrame({"mfi": mfi}, index=df.index)
|
188
|
+
|
189
|
+
def calculate_minus_di(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
190
|
+
high = df["high"].values
|
191
|
+
low = df["low"].values
|
192
|
+
close = df["close"].values
|
193
|
+
minus_di = talib.MINUS_DI(high, low, close, timeperiod=window)
|
194
|
+
return pd.DataFrame({"minus_di": minus_di}, index=df.index)
|
195
|
+
|
196
|
+
def calculate_minus_dm(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
197
|
+
high = df["high"].values
|
198
|
+
low = df["low"].values
|
199
|
+
minus_dm = talib.MINUS_DM(high, low, timeperiod=window)
|
200
|
+
return pd.DataFrame({"minus_dm": minus_dm}, index=df.index)
|
201
|
+
|
202
|
+
def calculate_plus_di(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
203
|
+
high = df["high"].values
|
204
|
+
low = df["low"].values
|
205
|
+
close = df["close"].values
|
206
|
+
plus_di = talib.PLUS_DI(high, low, close, timeperiod=window)
|
207
|
+
return pd.DataFrame({"plus_di": plus_di}, index=df.index)
|
208
|
+
|
209
|
+
def calculate_plus_dm(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
210
|
+
high = df["high"].values
|
211
|
+
low = df["low"].values
|
212
|
+
plus_dm = talib.PLUS_DM(high, low, timeperiod=window)
|
213
|
+
return pd.DataFrame({"plus_dm": plus_dm}, index=df.index)
|
214
|
+
|
215
|
+
def calculate_ppo(
|
216
|
+
self, df: pd.DataFrame, fast_period: int, slow_period: int, ma_type
|
217
|
+
) -> pd.DataFrame:
|
218
|
+
close = df["close"].values
|
219
|
+
ppo = talib.PPO(
|
220
|
+
close, fastperiod=fast_period, slowperiod=slow_period, matype=ma_type
|
221
|
+
)
|
222
|
+
return pd.DataFrame({"ppo": ppo}, index=df.index)
|
223
|
+
|
224
|
+
def calculate_roc(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
225
|
+
close = df["close"].values
|
226
|
+
roc = talib.ROC(close, timeperiod=window)
|
227
|
+
return pd.DataFrame({"roc": roc}, index=df.index)
|
228
|
+
|
229
|
+
def calculate_rocp(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
230
|
+
close = df["close"].values
|
231
|
+
rocp = talib.ROCP(close, timeperiod=window)
|
232
|
+
return pd.DataFrame({"rocp": rocp}, index=df.index)
|
233
|
+
|
234
|
+
def calculate_rocr(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
235
|
+
close = df["close"].values
|
236
|
+
rocr = talib.ROCR(close, timeperiod=window)
|
237
|
+
return pd.DataFrame({"rocr": rocr}, index=df.index)
|
238
|
+
|
239
|
+
def calculate_rocr100(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
240
|
+
close = df["close"].values
|
241
|
+
rocr100 = talib.ROCR100(close, timeperiod=window)
|
242
|
+
return pd.DataFrame({"rocr100": rocr100}, index=df.index)
|
243
|
+
|
244
|
+
def calculate_trix(self, df: pd.DataFrame, window: int) -> pd.DataFrame:
|
245
|
+
close = df["close"].values
|
246
|
+
trix = talib.TRIX(close, timeperiod=window)
|
247
|
+
return pd.DataFrame({"trix": trix}, index=df.index)
|
248
|
+
|
249
|
+
def calculate_ultosc(
|
250
|
+
self, df: pd.DataFrame, window1: int, window2: int, window3: int
|
251
|
+
) -> pd.DataFrame:
|
252
|
+
high = df["high"].values
|
253
|
+
low = df["low"].values
|
254
|
+
close = df["close"].values
|
255
|
+
ultosc = talib.ULTOSC(
|
256
|
+
high,
|
257
|
+
low,
|
258
|
+
close,
|
259
|
+
timeperiod1=window1,
|
260
|
+
timeperiod2=window2,
|
261
|
+
timeperiod3=window3,
|
262
|
+
)
|
263
|
+
return pd.DataFrame({"ultosc": ultosc}, index=df.index)
|