https://github.com/bukosabino/ta
Tip revision: ab347c8ad21724c14e1388da53770bbd058de3be authored by Darío López Padial on 11 March 2020, 18:48:21 UTC
Update setup.py
Update setup.py
Tip revision: ab347c8
others.py
"""
.. module:: others
:synopsis: Others Indicators.
.. moduleauthor:: Dario Lopez Padial (Bukosabino)
"""
import numpy as np
import pandas as pd
from ta.utils import IndicatorMixin
class DailyReturnIndicator(IndicatorMixin):
"""Daily Return (DR)
Args:
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
"""
def __init__(self, close: pd.Series, fillna: bool = False):
self._close = close
self._fillna = fillna
self._run()
def _run(self):
self._dr = (self._close / self._close.shift(1, fill_value=self._close.mean())) - 1
self._dr *= 100
def daily_return(self) -> pd.Series:
"""Daily Return (DR)
Returns:
pandas.Series: New feature generated.
"""
dr = self._check_fillna(self._dr, value=0)
return pd.Series(dr, name='d_ret')
class DailyLogReturnIndicator(IndicatorMixin):
"""Daily Log Return (DLR)
https://stackoverflow.com/questions/31287552/logarithmic-returns-in-pandas-dataframe
Args:
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
"""
def __init__(self, close: pd.Series, fillna: bool = False):
self._close = close
self._fillna = fillna
self._run()
def _run(self):
self._dr = np.log(self._close).diff()
self._dr *= 100
def daily_log_return(self) -> pd.Series:
"""Daily Log Return (DLR)
Returns:
pandas.Series: New feature generated.
"""
dr = self._check_fillna(self._dr, value=0)
return pd.Series(dr, name='d_logret')
class CumulativeReturnIndicator(IndicatorMixin):
"""Cumulative Return (CR)
Args:
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
"""
def __init__(self, close: pd.Series, fillna: bool = False):
self._close = close
self._fillna = fillna
self._run()
def _run(self):
self._cr = (self._close / self._close.iloc[0]) - 1
self._cr *= 100
def cumulative_return(self) -> pd.Series:
"""Cumulative Return (CR)
Returns:
pandas.Series: New feature generated.
"""
cr = self._check_fillna(self._cr, value=-1)
return pd.Series(cr, name='cum_ret')
def daily_return(close, fillna=False):
"""Daily Return (DR)
Args:
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
return DailyReturnIndicator(close=close, fillna=fillna).daily_return()
def daily_log_return(close, fillna=False):
"""Daily Log Return (DLR)
https://stackoverflow.com/questions/31287552/logarithmic-returns-in-pandas-dataframe
Args:
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
return DailyLogReturnIndicator(close=close, fillna=fillna).daily_log_return()
def cumulative_return(close, fillna=False):
"""Cumulative Return (CR)
Args:
close(pandas.Series): dataset 'Close' column.
fillna(bool): if True, fill nan values.
Returns:
pandas.Series: New feature generated.
"""
return CumulativeReturnIndicator(close=close, fillna=fillna).cumulative_return()