Skip to content

Commit

Permalink
Added some styles, debuged to get the same result from pandas-ta and …
Browse files Browse the repository at this point in the history
…TALIB versions.
  • Loading branch information
aligheshlaghi97 committed Feb 2, 2024
1 parent 63384d1 commit 1e7fd0e
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions pandas_ta/trend/ht_trendline.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
# -*- coding: utf-8 -*-
from numpy import nan, zeros_like, arctan
from numpy import nan, zeros_like, arctan, zeros
from numba import njit
from pandas import DataFrame, Series
from pandas_ta._typing import DictLike, Int, IntFloat
from pandas_ta.utils import v_offset, v_pos_default, v_series, zero
from pandas_ta.maps import Imports
from pandas_ta.utils import v_offset, v_series, v_talib


@njit
def np_ht_trendline(x):
# Variables used for the Hilbert Transformation
a, b = 0.0962, 0.5769
rad_to_deg = 45 / arctan(1)
period, smooth_period = 0.0, 0.0

m = x.size
smooth_price = zeros_like(m)
de_trender = zeros_like(m)
q1 = zeros_like(m)
i1 = zeros_like(m)
i2 = zeros_like(m)
q2 = zeros_like(m)
_re = zeros_like(m)
_im = zeros_like(m)
i_trend = zeros_like(m)

period, _prev_i2, _prev_q2, _re, _im, smooth_period = 0, 0, 0, 0, 0, 0
smooth_price = zeros(m)
de_trender = zeros(m)
q1 = zeros(m)
i1 = zeros(m)
i2 = zeros(m)
q2 = zeros(m)
_re = zeros(m)
_im = zeros(m)
i_trend = zeros(m)
trend_line = zeros(m)

for i in range(x.size):
if i > 50:
smooth_price[i] = (4 * x[i] + 3 * x[i - 1] + 2 * x[i - 2] + x[i - 3]) / 10
else:
if i < 50:
smooth_price[i] = 0
else:
smooth_price[i] = (4 * x[i] + 3 * x[i - 1] + 2 * x[i - 2] + x[i - 3]) / 10

for i in range(x.size):
adjusted_prev_period = 0.075 * period + 0.54

de_trender = (a * smooth_price[i] + b * smooth_price[i - 2] -
b * smooth_price[i - 4] - a * smooth_price[i - 6]) * adjusted_prev_period
de_trender[i] = (a * smooth_price[i] + b * smooth_price[i - 2] -
b * smooth_price[i - 4] - a * smooth_price[i - 6]) * adjusted_prev_period

q1[i] = (a * de_trender[i] + b * de_trender[i-2] -
b * de_trender[i-4] - a * de_trender[i-6]) * adjusted_prev_period
i1[i] = de_trender[i-3]
ji = (a * i1[i] + b * i1[i-2] - b * i1[i-4] - a * i1[i-6]) * adjusted_prev_period
jq = (a * q1[i] + b * q1[i-2] - b * q1[i-4] - a * q1[i-6]) * adjusted_prev_period
q1[i] = (a * de_trender[i] + b * de_trender[i - 2] -
b * de_trender[i - 4] - a * de_trender[i - 6]) * adjusted_prev_period
i1[i] = de_trender[i - 3]
ji = (a * i1[i] + b * i1[i - 2] - b * i1[i - 4] - a * i1[i - 6]) * adjusted_prev_period
jq = (a * q1[i] + b * q1[i - 2] - b * q1[i - 4] - a * q1[i - 6]) * adjusted_prev_period

i2[i] = i1[i] - jq
q2[i] = q1[i] + ji

i2 = 0.2 * i2[i] + 0.8 * i2[i-1]
q2 = 0.2 * q2[i] + 0.8 * q2[i-1]
i2[i] = 0.2 * i2[i] + 0.8 * i2[i - 1]
q2[i] = 0.2 * q2[i] + 0.8 * q2[i - 1]

_re[i] = i2[i] * i2[i-1] + q2[i] * q2[i-1]
_im[i] = i2[i] * q2[i-1] - q2[i] * i2[i-1]
_re[i] = i2[i] * i2[i - 1] + q2[i] * q2[i - 1]
_im[i] = i2[i] * q2[i - 1] - q2[i] * i2[i - 1]

_re[i] = 0.2 * _re[i] + 0.8 * _re[i-1]
_im[i] = 0.2 * _im[i] + 0.8 * _im[i-1]
_re[i] = 0.2 * _re[i] + 0.8 * _re[i - 1]
_im[i] = 0.2 * _im[i] + 0.8 * _im[i - 1]

new_period = 0
if _re[i] and _im[i]:
new_period = 360 / arctan(_re[i]/_im[i])
if _re[i] != 0 and _im[i] != 0:
new_period = 360 / (arctan(_im[i]/_re[i]) * rad_to_deg)
if new_period > 1.5 * period:
new_period = 1.5 * period
if new_period < 0.67 * period:
Expand All @@ -71,13 +72,17 @@ def np_ht_trendline(x):
dc_period = int(smooth_period + 0.5)
temp_real = 0
for k in range(dc_period):
temp_real += x[i-k]
temp_real += x[i - k]

if dc_period > 0:
temp_real /= dc_period

i_trend[i] = temp_real
trend_line = (4 * i_trend[i] + 3 * i_trend[i-1] + 2 * i_trend[i-2] + i_trend[i-3]) / 10.0

if i < 12:
trend_line[i] = x[i]
else:
trend_line[i] = (4 * i_trend[i] + 3 * i_trend[i - 1] + 2 * i_trend[i - 2] + i_trend[i - 3]) / 10.0

return trend_line

Expand All @@ -90,6 +95,7 @@ def ht_trendline(
Sources:
https://c.mql5.com/forextsd/forum/59/023inst.pdf
https://github.com/TA-Lib/ta-lib/blob/main/src/ta_func/ta_HT_TRENDLINE.c
Args:
close (pd.Series): Series of 'close's.
Expand Down

0 comments on commit 1e7fd0e

Please sign in to comment.