Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding plot, Heikin Ashi Candlestick, Smoothed Simple Moving Average, Williams indicators. #202

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples_to_use/ta_plots_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from ta.plots import StreamlitPlot, PlotlyPlot
from ta.momentum import StochasticOscillator
from ta.volatility import BollingerBands
import yfinance as yf

def getData():
data = yf.download(
tickers='TSLA',
period="1y",
group_by='ticker',
auto_adjust=True,
prepost=False,
threads=True,
)
return data

df = getData()
df = df.reset_index()

indicator = StochasticOscillator(high=df['High'], low=df['Low'], close=df['Close'], window=14, smooth_window=3)
df['Stoch'] = indicator.stoch()
df['Stoch_signal'] = indicator.stoch_signal()
indicator_bb = BollingerBands(close=df['Close'], window=20, window_dev=2)
df["bbh"] = indicator_bb.bollinger_hband()
df["bbl"] = indicator_bb.bollinger_lband()


def streamlitPlot_demo(df):
st_plot = StreamlitPlot(df, 'Date', 'Close', 'Open', 'High', 'Low', rows=2, row_heights=[0.7, 0.3])
st_plot.addLine(df['bbh'], 'bb_high', row=1)
st_plot.addLine(df['bbl'], 'bb_low', row=1)
st_plot.addLine(df['Stoch'], 'Stoch', row=2)
st_plot.addLine(df['Stoch_signal'], 'Stoch_signal', row=2)
st_plot.addHorizontalLine(20, 'oversold', row=2, showlegend=False, color='blue')
st_plot.addHorizontalLine(80, 'overbought', row=2, showlegend=False, color='blue')
st_plot.addHorizontalArea(range=(0, 20), row=2, color='green')
st_plot.addHorizontalArea(range=(80, 100), row=2, color='red')
st_plot.show()

def plotlyPlot_demo(df):
plotly_plot = PlotlyPlot(df, 'Date', 'Close', 'Open', 'High', 'Low', rows=2, row_heights=[0.7, 0.3])
plotly_plot.addLine(df['Stoch'], 'Stoch', row=2)
plotly_plot.addLine(df['Stoch_signal'], 'Stoch_signal', row=2)
plotly_plot.addHorizontalLine(20, 'oversold', row=2, showlegend=False, color='blue')
plotly_plot.addHorizontalLine(80, 'overbought', row=2, showlegend=False, color='blue')
plotly_plot.addHorizontalArea(range=(0, 20), row=2, color='green')
plotly_plot.addHorizontalArea(range=(80, 100), row=2, color='red')
plotly_plot.show()


streamlitPlot_demo(df)
# plotlyPlot_demo(df)
153 changes: 153 additions & 0 deletions ta/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,79 @@ def cumulative_return(self) -> pd.Series:
return pd.Series(cum_ret, name="cum_ret")


class HeikinAshiCandlestick(IndicatorMixin):
"""HeikinAshiCandlestick

https://www.investopedia.com/trading/heikin-ashi-better-candlestick/

Args:
open(pandas.Series): dataset 'Open' column.
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
"""

def __init__(self,
open: pd.Series,
high: pd.Series,
low: pd.Series,
close: pd.Series,
fillna: bool = False):
self._open = open
self._high = high
self._low = low
self._close = close
self._fillna = fillna
self._run()

def _run(self):
self._ha_close = (self._open + self._high + self._low + self._close) / 4
self._ha_open = pd.DataFrame(np.nan, index=self._open.index, columns=['Open'])
self._ha_open.iloc[0] = self._open.iloc[0]
for i in range(1, len(self._open)):
self._ha_open.iloc[i] = (self._ha_open.iloc[i - 1] + self._ha_close.iloc[i - 1]) / 2
self._ha_open = self._ha_open['Open']
self._ha_high = pd.concat([self._ha_open, self._ha_close, self._high], axis=1).max(axis=1)
self._ha_low = pd.concat([self._ha_open, self._ha_close, self._low], axis=1).min(axis=1)

def heikin_ashi_candlestick_open(self) -> pd.Series:
"""Heikin-Ashi Candlestick Open

Returns:
pandas.Series: New feature generated.
"""
ha_open = self._check_fillna(self._ha_open, value=0)
return pd.Series(ha_open, name='heikin_ashi_candlestick_open')

def heikin_ashi_candlestick_high(self) -> pd.Series:
"""Heikin-Ashi Candlestick High

Returns:
pandas.Series: New feature generated.
"""
ha_high = self._check_fillna(self._ha_high, value=0)
return pd.Series(ha_high, name='heikin_ashi_candlestick_high')

def heikin_ashi_candlestick_low(self) -> pd.Series:
"""Heikin-Ashi Candlestick Low

Returns:
pandas.Series: New feature generated.
"""
ha_low = self._check_fillna(self._ha_low, value=0)
return pd.Series(ha_low, name='heikin_ashi_candlestick_low')

def heikin_ashi_candlestick_close(self) -> pd.Series:
"""Heikin-Ashi Candlestick Close

Returns:
pandas.Series: New feature generated.
"""
ha_close = self._check_fillna(self._ha_close, value=0)
return pd.Series(ha_close, name='heikin_ashi_candlestick_close')


def daily_return(close, fillna=False):
"""Daily Return (DR)

Expand Down Expand Up @@ -135,3 +208,83 @@ def cumulative_return(close, fillna=False):
pandas.Series: New feature generated.
"""
return CumulativeReturnIndicator(close=close, fillna=fillna).cumulative_return()


def heikin_ashi_candlestick_open(open, high, low, close, fillna=False):
"""Heikin-Ashi Candlestick Open

Args:
open(pandas.Series): dataset 'Open' column.
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.

Returns:
pandas.Series: New feature generated.
"""
return HeikinAshiCandlestick(open=open,
high=high,
low=low,
close=close,
fillna=fillna).heikin_ashi_candlestick_open()


def heikin_ashi_candlestick_high(open, high, low, close, fillna=False):
"""Heikin-Ashi Candlestick High

Args:
open(pandas.Series): dataset 'Open' column.
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.

Returns:
pandas.Series: New feature generated.
"""
return HeikinAshiCandlestick(open=open,
high=high,
low=low,
close=close,
fillna=fillna).heikin_ashi_candlestick_high()


def heikin_ashi_candlestick_low(open, high, low, close, fillna=False):
"""Heikin-Ashi Candlestick Low

Args:
open(pandas.Series): dataset 'Open' column.
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.

Returns:
pandas.Series: New feature generated.
"""
return HeikinAshiCandlestick(open=open,
high=high,
low=low,
close=close,
fillna=fillna).heikin_ashi_candlestick_low()


def heikin_ashi_candlestick_close(open, high, low, close, fillna=False):
"""Heikin-Ashi Candlestick Close

Args:
open(pandas.Series): dataset 'Open' column.
high(pandas.Series): dataset 'High' column.
low(pandas.Series): dataset 'Low' column.
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.

Returns:
pandas.Series: New feature generated.
"""
return HeikinAshiCandlestick(open=open,
high=high,
low=low,
close=close,
fillna=fillna).heikin_ashi_candlestick_close()
Loading