Skip to content

Commit

Permalink
ENH ht_trendline #759 MAINT general PERF wma mfi #731 DOC TST
Browse files Browse the repository at this point in the history
  • Loading branch information
twopirllc committed Mar 13, 2024
1 parent 410224c commit 947cbde
Show file tree
Hide file tree
Showing 24 changed files with 784 additions and 739 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ data/SPY_D_TV3.csv
data/TV_5min.csv
data/tulip.csv
examples/*.csv
jnb/*
notes/*
csv/*
jnb/
notes/
csv/

*.txt
df_file
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: tests
tests:
pytest -vv -s -l tests
pytest -vvv -s -l tests

caches:
find ./pandas_ta | grep -E "(__pycache__|\.pyc|\.pyo$\)"
Expand All @@ -12,16 +12,16 @@ init:
pip install -r requirements.txt

test_metrics:
pytest -vv -s -l --cache-clear tests/test_metrics.py
pytest -vv -s -l -W ignore::DeprecationWarning --cache-clear tests/test_metrics.py

test_numba:
pytest -vv -s -l --cache-clear tests/test_numba.py
pytest -vv -s -l -W ignore::DeprecationWarning --cache-clear tests/test_numba.py

test_studies:
pytest -vv -s -l --cache-clear tests/test_studies.py
pytest -vv -s -l -W ignore::DeprecationWarning --cache-clear tests/test_studies.py

test_ta:
pytest -vv -s -l --cache-clear tests/test_indicator_*.py
pytest -vv -s -l -W ignore::DeprecationWarning --cache-clear tests/test_indicator_*.py

test_utils:
pytest -vv -s -l --cache-clear tests/test_utils.py
pytest -vv -s -l -W ignore::DeprecationWarning --cache-clear tests/test_utils.py
1,057 changes: 537 additions & 520 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pandas_ta/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
MaybeSeriesFrame = Union[T, Series, DataFrame]
AnyArray = Union[Array, Series, DataFrame]
AnyArray1d = Union[Array1d, Series]
AnyArray2d = Union[Array2d, DataFrame]
AnyArray2d = Union[Array2d, DataFrame]
51 changes: 27 additions & 24 deletions pandas_ta/overlap/mama.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,47 @@
# http://traders.com/documentation/feedbk_docs/2014/01/traderstips.html
@njit
def np_mama(x, fastlimit, slowlimit, prenan):
a1, a2 = 0.0962, 0.5769
p_w, smp_w, smp_w_c = 0.2, 0.33, 0.67 # smp_w + smp_w_c = 1

sm = zeros_like(x)
dt, smp, q1, q2 = sm.copy(), sm.copy(), sm.copy(), sm.copy()
i1, i2, jI, jQ = sm.copy(), sm.copy(), sm.copy(), sm.copy()
re, im, alpha = sm.copy(), sm.copy(), sm.copy()
period, phase, mama, fama = sm.copy(), sm.copy(), sm.copy(), sm.copy()

# Ehler's starts from 6, TV-LB starts at 3, TALib 32
n = x.size
for i in range(3, n):
w_period = .075 * period[i - 1] + .54

# Smoother and Detrend the Smoother
sm[i] = 0.4 * x[i] + 0.3 * x[i - 1] + 0.2 * x[i - 2] + 0.1 * x[i - 3]
dt[i] = w_period * (a1 * sm[i] + a2 * sm[i - 2] - a2 * sm[i - 4] - a1 * sm[i - 6])
a, b, m = 0.0962, 0.5769, x.size
p_w, smp_w, smp_w_c = 0.2, 0.33, 0.67

wma4 = zeros_like(x)
dt, smp = zeros_like(x), zeros_like(x)
i1, i2 = zeros_like(x), zeros_like(x)
ji, jq = zeros_like(x), zeros_like(x)
q1, q2 = zeros_like(x), zeros_like(x)
re, im, alpha = zeros_like(x), zeros_like(x), zeros_like(x)
period, phase = zeros_like(x), zeros_like(x)
mama, fama = zeros_like(x), zeros_like(x)

# Ehler's starts from 6, TV-LB from 3, TALib from 32
for i in range(3, m):
adj_prev_period = 0.075 * period[i - 1] + 0.54

# WMA(x,4) & Detrended WMA(x,4)
wma4[i] = 0.4 * x[i] + 0.3 * x[i - 1] + 0.2 * x[i - 2] + 0.1 * x[i - 3]
dt[i] = adj_prev_period * (a * wma4[i] + b * wma4[i - 2] - b * wma4[i - 4] - a * wma4[i - 6])

# Quadrature(Detrender) and In Phase Component
q1[i] = w_period * (a1 * dt[i] + a2 * dt[i - 2] - a2 * dt[i - 4] - a1 * dt[i - 6])
q1[i] = adj_prev_period * (a * dt[i] + b * dt[i - 2] - b * dt[i - 4] - a * dt[i - 6])
i1[i] = dt[i - 3]

# Phase advance I1 and Q1 by 90 degrees
jI[i] = w_period * (a1 * i1[i] + a2 * i1[i - 2] - a2 * i1[i - 4] - a1 * i1[i - 6])
jQ[i] = w_period * (a1 * q1[i] + a2 * q1[i - 2] - a2 * q1[i - 4] - a1 * q1[i - 6])
# Phase Q1 and I1 by 90 degrees
ji[i] = adj_prev_period * (a * i1[i] + b * i1[i - 2] - b * i1[i - 4] - a * i1[i - 6])
jq[i] = adj_prev_period * (a * q1[i] + b * q1[i - 2] - b * q1[i - 4] - a * q1[i - 6])

# Phasor Addition for 3 Bar Averaging
i2[i] = i1[i] - jQ[i]
q2[i] = q1[i] + jI[i]
i2[i] = i1[i] - jq[i]
q2[i] = q1[i] + ji[i]

# Smooth I and Q components
# Smooth I2 & Q2
i2[i] = p_w * i2[i] + (1 - p_w) * i2[i - 1]
q2[i] = p_w * q2[i] + (1 - p_w) * q2[i - 1]

# Homodyne Discriminator
re[i] = i2[i] * i2[i - 1] + q2[i] * q2[i - 1]
im[i] = i2[i] * q2[i - 1] + q2[i] * i2[i - 1]

# Smooth Re & Im
re[i] = p_w * re[i] + (1 - p_w) * re[i - 1]
im[i] = p_w * im[i] + (1 - p_w) * im[i - 1]

Expand Down
4 changes: 2 additions & 2 deletions pandas_ta/overlap/pivots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from numpy import NaN, greater, zeros_like
from numpy import greater, nan, zeros_like
from numba import njit
from pandas import DataFrame, Series, Timedelta, infer_freq, to_datetime
from pandas_ta._typing import Array, DictLike
Expand Down Expand Up @@ -197,7 +197,7 @@ def pivots(

# Create nan arrays for "demark" and "fibonacci" pivots
_nan_array = zeros_like(np_close)
_nan_array[:] = NaN
_nan_array[:] = nan
tp = s1 = s2 = s3 = s4 = r1 = r2 = r3 = r4 = _nan_array

# Calculate
Expand Down
36 changes: 24 additions & 12 deletions pandas_ta/overlap/wma.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from numpy import arange, dot
from numpy import arange, dot, float64, nan, zeros_like
from numba import njit
from pandas import Series
from pandas_ta._typing import DictLike, Int
from pandas_ta.maps import Imports
Expand All @@ -12,6 +13,25 @@
)


@njit
def np_wma(x, n, asc, prenan):
m = x.size
w = arange(1, n + 1, dtype=float64)
result = zeros_like(x, dtype=float64)

if not asc:
w = w[::-1]

for i in range(n - 1, m):
result[i] = (w * x[i - n + 1:i + 1]).sum()
result *= 2 / (n * n + n)

if prenan:
result[:n - 1] = nan

return result


def wma(
close: Series, length: Int = None,
asc: bool = None, talib: bool = None,
Expand Down Expand Up @@ -56,17 +76,9 @@ def wma(
from talib import WMA
wma = WMA(close, length)
else:
total_weight = 0.5 * length * (length + 1)
weights_ = Series(arange(1, length + 1))
weights = weights_ if asc else weights_[::-1]

def linear(w):
def _compute(x):
return dot(x, w) / total_weight
return _compute

close_ = close.rolling(length, min_periods=length)
wma = close_.apply(linear(weights), raw=True)
np_close = close.values
wma_ = np_wma(np_close, length, asc, True)
wma = Series(wma_, index=close.index)

# Offset
if offset != 0:
Expand Down
5 changes: 2 additions & 3 deletions pandas_ta/trend/alphatrend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from numpy import isnan, nan, zeros
from numpy import isnan, nan, zeros_like
from numba import njit
from pandas import DataFrame, Series
from pandas_ta._typing import Array, DictLike, Int, IntFloat
Expand All @@ -16,11 +16,10 @@
)


# Alphatrend alpha threshold calculation
@njit
def np_alpha(low_atr, high_atr, momo_threshold):
m = momo_threshold.size
alpha = zeros(m)
alpha = zeros_like(low_atr)

for i in range(1, m):
if momo_threshold[i]:
Expand Down
Loading

0 comments on commit 947cbde

Please sign in to comment.