forked from twopirllc/pandas-ta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pr/246' into development
- Loading branch information
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
import numpy as np | ||
from pandas import DataFrame, Series | ||
from pandas_ta.utils import get_offset, verify_series | ||
|
||
def true_sequence_count(s): | ||
index = s.where(s == False).last_valid_index() | ||
|
||
if index is None: | ||
return s.count() | ||
else: | ||
s = s[s.index > index] | ||
return s.count() | ||
|
||
def calc_td(close, direction, show_all): | ||
td_bool = close.diff(4) > 0 if direction=='up' else close.diff(4) < 0 | ||
td_num = np.where(td_bool, td_bool.rolling(13, min_periods=0).apply(true_sequence_count), 0) | ||
td_num = Series(td_num) | ||
|
||
if show_all: | ||
td_num = td_num.mask(td_num == 0) | ||
else: | ||
td_num = td_num.mask(~td_num.between(6,9)) | ||
|
||
return td_num | ||
|
||
def td(close, offset=None, show_all=True, **kwargs): | ||
up = calc_td(close, 'up', show_all) | ||
down = calc_td(close, 'down', show_all) | ||
df = DataFrame({'TD_up': up, 'TD_down': down}) | ||
|
||
# Offset | ||
if offset and offset != 0: | ||
df = df.shift(offset) | ||
|
||
if "fillna" in kwargs: | ||
df.fillna(kwargs["fillna"], inplace=True) | ||
|
||
# Name & Category | ||
df.name = "TD" | ||
df.category = "momentum" | ||
|
||
return df | ||
|
||
td.__doc__ = \ | ||
"""TD Sequential (TD) | ||
TD Sequential indicator. | ||
Sources: | ||
https://tradetrekker.wordpress.com/tdsequential/ | ||
Calculation: | ||
compare current close price with 4 days ago price, up to 13 days. | ||
for the consecutive ascending or descending price sequence, display 6th to 9th day value. | ||
Args: | ||
close (pd.Series): Series of 'close's | ||
offset (int): How many periods to offset the result. Default: 0 | ||
show_all (bool): default True, show 1 - 13. If set to false, only show 6 - 9 | ||
Kwargs: | ||
fillna (value, optional): pd.DataFrame.fillna(value) | ||
Returns: | ||
pd.DataFrame: New feature generated. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters