Suggestion: ADX different values for neg and pos
Brankhos opened this issue · 2 comments
Brankhos commented
Can you add new _window for neg and pos? Default will fetch from default adx but if you typed a number(neg, pos or both) it will fetch it. And can you do this for other functions as well? Its hard to add it for me
Brankhos commented
wrong//
class ADXIndicator2(IndicatorMixin2):
def __init__(
self,
high: pd.Series,
low: pd.Series,
close: pd.Series,
window: int = 14,
fillna: bool = False,
window_p: int = 14,
window_n: int = 14,
):
self._high = high
self._low = low
self._close = close
self._window = window
self._fillna = fillna
self._window_p = window_p
self._window_n = window_n
self._run()
def _run(self):
if self._window == 0:
raise ValueError("window may not be 0")
close_shift = self._close.shift(1)
pdm = _get_min_max(self._high, close_shift, "max")
pdn = _get_min_max(self._low, close_shift, "min")
diff_directional_movement = pdm - pdn
self._trs_initial = np.zeros(self._window - 1)
self._trs = np.zeros(len(self._close) - (self._window - 1))
self._trs[0] = diff_directional_movement.dropna()[0 : self._window].sum()
diff_directional_movement = diff_directional_movement.reset_index(drop=True)
for i in range(1, len(self._trs) - 1):
self._trs[i] = (
self._trs[i - 1]
- (self._trs[i - 1] / float(self._window))
+ diff_directional_movement[self._window + i]
)
diff_up = self._high - self._high.shift(1)
diff_down = self._low.shift(1) - self._low
pos = abs(((diff_up > diff_down) & (diff_up > 0)) * diff_up)
neg = abs(((diff_down > diff_up) & (diff_down > 0)) * diff_down)
self._dip = np.zeros(len(self._close) - (self._window_p - 1))
self._dip[0] = pos.dropna()[0 : self._window_p].sum()
pos = pos.reset_index(drop=True)
for i in range(1, len(self._dip) - 1):
self._dip[i] = (
self._dip[i - 1]
- (self._dip[i - 1] / float(self._window_p))
+ pos[self._window_p + i]
)
self._din = np.zeros(len(self._close) - (self._window_n - 1))
self._din[0] = neg.dropna()[0 : self._window_n].sum()
neg = neg.reset_index(drop=True)
for i in range(1, len(self._din) - 1):
self._din[i] = (
self._din[i - 1]
- (self._din[i - 1] / float(self._window_n))
+ neg[self._window_n + i]
)
def adx(self) -> pd.Series:
"""Average Directional Index (ADX)
Returns:
pandas.Series: New feature generated.tr
"""
dip = np.zeros(len(self._trs))
for i in range(len(self._trs)):
dip[i] = 100 * (self._dip[i] / self._trs[i])
din = np.zeros(len(self._trs))
for i in range(len(self._trs)):
din[i] = 100 * (self._din[i] / self._trs[i])
directional_index = 100 * np.abs((dip - din) / (dip + din))
adx_series = np.zeros(len(self._trs))
adx_series[self._window] = directional_index[0 : self._window].mean()
for i in range(self._window + 1, len(adx_series)):
adx_series[i] = (
(adx_series[i - 1] * (self._window - 1)) + directional_index[i - 1]
) / float(self._window)
adx_series = np.concatenate((self._trs_initial, adx_series), axis=0)
adx_series = pd.Series(data=adx_series, index=self._close.index)
adx_series = self._check_fillna(adx_series, value=20)
return pd.Series(adx_series, name="adx")
def adx_pos(self) -> pd.Series:
"""Plus Directional Indicator (+DI)
Returns:
pandas.Series: New feature generated.
"""
dip = np.zeros(len(self._close))
for i in range(1, len(self._trs) - 1):
dip[i + self._window_p] = 100 * (self._dip[i] / self._trs[i])
adx_pos_series = self._check_fillna(
pd.Series(dip, index=self._close.index), value=20
)
return pd.Series(adx_pos_series, name="adx_pos")
def adx_neg(self) -> pd.Series:
"""Minus Directional Indicator (-DI)
Returns:
pandas.Series: New feature generated.
"""
din = np.zeros(len(self._close))
for i in range(1, len(self._trs) - 1):
din[i + self._window_n] = 100 * (self._din[i] / self._trs[i])
adx_neg_series = self._check_fillna(
pd.Series(din, index=self._close.index), value=20
)
return pd.Series(adx_neg_series, name="adx_neg")
sasan-saqaeeyan commented
hello, i got different ADX value using ta library from meta trader5 adx indicator ?
help me to solve my problem?