-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadx.py
387 lines (317 loc) · 12.8 KB
/
adx.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import pandas as pd
import polars as pl
from typing import Union
from pyindicators.exceptions import PyIndicatorException
from .utils import pad_zero_values_pandas, pad_zero_values_polars
def adx(
data: Union[pd.DataFrame, pl.DataFrame],
period=14,
high_column="High",
low_column="Low",
close_column="Close",
result_adx_column="adx",
result_pdi_column="+di",
result_ndi_column="-di",
) -> Union[pd.DataFrame, pl.DataFrame]:
"""
Calculate the Average Directional Index (ADX) for a given DataFrame.
Args:
data (Union[pd.DataFrame, pl.DataFrame]): Input data containing
the price series.
period (int, optional): Period for the ADX calculation (default: 14).
high_column (str, optional): Column name for the high price series.
low_column (str, optional): Column name for the low price series.
close_column (str, optional): Column name for the close price series.
result_adx_column (str, optional): Column name to store the ADX.
result_pdi_column (str, optional): Column name to store the +DI.
result_ndi_column (str, optional): Column name to store the -DI.
Returns:
Union[pd.DataFrame, pl.DataFrame]: DataFrame with ADX, +DI, and -DI.
"""
# Check if the high, low, and close columns are in the DataFrame
if high_column not in data.columns:
raise PyIndicatorException(
f"Column '{high_column}' not found in DataFrame"
)
if low_column not in data.columns:
raise PyIndicatorException(
f"Column '{low_column}' not found in DataFrame"
)
if close_column not in data.columns:
raise PyIndicatorException(
f"Column '{close_column}' not found in DataFrame"
)
if isinstance(data, pd.DataFrame):
# Pandas version of the ADX calculation
high = data[high_column]
low = data[low_column]
close = data[close_column]
# Calculate True Range (TR)
tr = pd.DataFrame({
'TR': pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
})
# Calculate Directional Movement (+DM and -DM)
plus_dm = pd.DataFrame(
{'+DM': (high.diff() > low.diff()).astype(int)
* (high.diff().clip(lower=0))}
)
minus_dm = pd.DataFrame(
{'-DM': (low.diff() > high.diff()).astype(int)
* (-low.diff().clip(upper=0))}
)
# Smooth the TR, +DM, and -DM over the period
tr_smooth = tr['TR'].rolling(window=period).mean()
plus_dm_smooth = plus_dm['+DM'].rolling(window=period).mean()
minus_dm_smooth = minus_dm['-DM'].rolling(window=period).mean()
# Calculate +DI and -DI
pdi = 100 * (plus_dm_smooth / tr_smooth)
ndi = 100 * (minus_dm_smooth / tr_smooth)
# Smooth the difference to get ADX
adx = pd.DataFrame({
result_adx_column: (pdi - ndi).abs().rolling(window=period).mean()
})
# Add columns to the original dataframe
data[result_adx_column] = adx
data[result_pdi_column] = pdi
data[result_ndi_column] = ndi
pad_zero_values_pandas(data, result_adx_column, period)
pad_zero_values_pandas(data, result_pdi_column, period - 1)
pad_zero_values_pandas(data, result_ndi_column, period - 1)
return data
elif isinstance(data, pl.DataFrame):
# Polars version of the ADX calculation
high = data[high_column]
low = data[low_column]
close = data[close_column]
# Calculate True Range (TR)
tr = pl.max_horizontal([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
])
# Calculate Directional Movement (+DM and -DM)
plus_dm = high.diff().clip_min(0)
minus_dm = (-low.diff()).clip_min(0).abs()
# Smooth the TR, +DM, and -DM over the period
# (use rolling sum, not mean)
tr_smooth = tr.rolling_sum(window_size=period, min_periods=1)
plus_dm_smooth = plus_dm.rolling_sum(window_size=period, min_periods=1)
minus_dm_smooth = minus_dm.rolling_sum(
window_size=period, min_periods=1
)
# Calculate +DI and -DI
pdi = 100 * (plus_dm_smooth / tr_smooth)
ndi = 100 * (minus_dm_smooth / tr_smooth)
# Calculate ADX (average of the absolute difference
# between +DI and -DI)
di_diff = (pdi - ndi).abs()
# Smooth the difference to get ADX
adx = di_diff.rolling_mean(window_size=period)
# Add columns to the original dataframe
data = data.with_columns([
adx.alias(result_adx_column),
pdi.alias(result_pdi_column),
ndi.alias(result_ndi_column)
])
# Pad the first `period` rows with zero values
data = pad_zero_values_polars(data, result_adx_column, period)
data = pad_zero_values_polars(data, result_pdi_column, period - 1)
data = pad_zero_values_polars(data, result_ndi_column, period - 1)
return data
else:
raise PyIndicatorException(
"Input data must be either a pandas or polars DataFrame."
)
def adx_v2(
data: Union[pd.DataFrame, pl.DataFrame],
period=14,
high_column="High",
low_column="Low",
close_column="Close",
result_adx_column="ADX",
result_pdi_column="+DI",
result_ndi_column="-DI",
) -> Union[pd.DataFrame, pl.DataFrame]:
"""
Calculate the Average Directional Index (ADX) using Wilder's smoothing.
Matches Tulipy's ADX calculation.
Args:
data: Input DataFrame (Pandas or Polars).
period: Period for the ADX calculation (default: 14).
high_column, low_column, close_column: Column names for price data.
result_adx_column, result_pdi_column,
result_ndi_column: Output column names.
Returns:
DataFrame with ADX, +DI, and -DI.
"""
if high_column not in data.columns \
or low_column not in data.columns \
or close_column not in data.columns:
raise PyIndicatorException(
"High, Low, or Close column not found in DataFrame."
)
if isinstance(data, pd.DataFrame):
# Pandas version
high, low, close = data[high_column], data[low_column], \
data[close_column]
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
plus_dm = high.diff().clip(lower=0)
minus_dm = -low.diff().clip(upper=0)
# Wilder’s smoothing with EMA
tr_smooth = tr.ewm(span=period, adjust=False).mean()
plus_dm_smooth = plus_dm.ewm(span=period, adjust=False).mean()
minus_dm_smooth = minus_dm.ewm(span=period, adjust=False).mean()
pdi = 100 * (plus_dm_smooth / tr_smooth)
ndi = 100 * (minus_dm_smooth / tr_smooth)
adx = (100 * (pdi - ndi).abs().ewm(span=period, adjust=False).mean())
# Add results to DataFrame
data[result_adx_column] = adx
data[result_pdi_column] = pdi
data[result_ndi_column] = ndi
# Pad with zeros
pad_zero_values_pandas(data, result_adx_column, period)
pad_zero_values_pandas(data, result_pdi_column, period - 1)
pad_zero_values_pandas(data, result_ndi_column, period - 1)
return data
elif isinstance(data, pl.DataFrame):
# Polars version
high, low, close = data[high_column], data[low_column], \
data[close_column]
tr = pl.max_horizontal([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
])
plus_dm = high.diff().clip_min(0)
minus_dm = (-low.diff()).clip_min(0).abs()
# Wilder’s smoothing (manual EMA for Polars)
def wilder_ema(series, period):
alpha = 1 / period
return series.cumsum() * alpha
tr_smooth = wilder_ema(tr, period)
plus_dm_smooth = wilder_ema(plus_dm, period)
minus_dm_smooth = wilder_ema(minus_dm, period)
pdi = 100 * (plus_dm_smooth / tr_smooth)
ndi = 100 * (minus_dm_smooth / tr_smooth)
adx = (100 * (pdi - ndi).abs()).cumsum() / period
# Add results to DataFrame
data = data.with_columns([
adx.alias(result_adx_column),
pdi.alias(result_pdi_column),
ndi.alias(result_ndi_column)
])
# Pad with zeros
data = pad_zero_values_polars(data, result_adx_column, period)
data = pad_zero_values_polars(data, result_pdi_column, period - 1)
data = pad_zero_values_polars(data, result_ndi_column, period - 1)
return data
else:
raise PyIndicatorException(
"Input data must be either a pandas or polars DataFrame."
)
def di(
data: Union[pd.DataFrame, pl.DataFrame],
period=14,
high_column="High",
low_column="Low",
close_column="Close",
result_pdi_column="+DI",
result_ndi_column="-DI",
) -> Union[pd.DataFrame, pl.DataFrame]:
"""
Calculate the +DI and -DI indicators exactly like Tulipy,
supporting both Pandas and Polars.
Args:
data (Union[pd.DataFrame, pl.DataFrame]): Input data
containing the price series.
period (int, optional): Period for the DI calculation (default: 14).
high_column (str, optional): Column name for the high price series.
low_column (str, optional): Column name for the low price series.
close_column (str, optional): Column name for the close price series.
result_pdi_column (str, optional): Column name to store the +DI.
result_ndi_column (str, optional): Column name to store the -DI.
Returns:
Union[pd.DataFrame, pl.DataFrame]: DataFrame with +DI and -DI.
"""
if isinstance(data, pd.DataFrame):
high = data[high_column]
low = data[low_column]
close = data[close_column]
# True Range
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
# Directional Movement
plus_dm = (
(high.diff() > low.shift(1) - low) & (high.diff() > 0)
) * high.diff()
minus_dm = (
(low.shift(1) - low > high.diff()) & (low.shift(1) - low > 0)
) * (low.shift(1) - low)
# Smoothed values
tr_smooth = tr.rolling(window=period).sum()
plus_dm_smooth = plus_dm.rolling(window=period).sum()
minus_dm_smooth = minus_dm.rolling(window=period).sum()
# Calculate +DI and -DI
pdi = 100 * (plus_dm_smooth / tr_smooth)
ndi = 100 * (minus_dm_smooth / tr_smooth)
# Add to DataFrame
data[result_pdi_column] = pdi
data[result_ndi_column] = ndi
# Pad initial values with zero
# (replace NaN values for first `period-1` rows)
data[result_pdi_column].iloc[:period-1] = 0
data[result_ndi_column].iloc[:period-1] = 0
return data
elif isinstance(data, pl.DataFrame):
high = data[high_column]
low = data[low_column]
close = data[close_column]
# True Range
tr = pl.max_horizontal([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
])
# Directional Movement
plus_dm = (high.diff() > low.shift(1) - low) & (high.diff() > 0)
plus_dm = plus_dm * high.diff()
minus_dm = (
low.shift(1) - low > high.diff()
) & (low.shift(1) - low > 0)
minus_dm = minus_dm * (low.shift(1) - low)
# Smoothed values
tr_smooth = tr.rolling_sum(window_size=period)
plus_dm_smooth = plus_dm.rolling_sum(window_size=period)
minus_dm_smooth = minus_dm.rolling_sum(window_size=period)
# Calculate +DI and -DI
pdi = 100 * (plus_dm_smooth / tr_smooth)
ndi = 100 * (minus_dm_smooth / tr_smooth)
# Add to DataFrame
data = data.with_columns([
pdi.alias(result_pdi_column),
ndi.alias(result_ndi_column)
])
# Pad initial values with zero
# (replace NaN values for first `period-1` rows)
data = data.with_columns([
pl.when(pl.col(result_pdi_column).is_null()).then(0)
.otherwise(pl.col(result_pdi_column)).alias(result_pdi_column),
pl.when(pl.col(result_ndi_column).is_null()).then(0)
.otherwise(pl.col(result_ndi_column)).alias(result_ndi_column)
])
return data
else:
raise ValueError(
"Input data must be either a pandas or polars DataFrame."
)