Exemple #1
0
 def setUpClass(cls):
     cls._df = pd.read_csv(cls._filename, sep=",")
     cls._params = dict(close=cls._df["Close"],
                        window_slow=25,
                        window_fast=13,
                        fillna=False)
     cls._indicator = TSIIndicator(**cls._params)
Exemple #2
0
class TestTSIIndicator(unittest.TestCase):
    """
    https://school.stockcharts.com/doku.php?id=technical_indicators:true_strength_index
    """

    _filename = 'ta/tests/data/cs-tsi.csv'

    def setUp(self):
        self._df = pd.read_csv(self._filename, sep=',')
        self._indicator = TSIIndicator(close=self._df['Close'], r=25, s=13, fillna=False)

    def tearDown(self):
        del(self._df)

    def test_kama(self):
        target = 'TSI'
        result = self._indicator.tsi()
        pd.testing.assert_series_equal(
            self._df[target].tail(), result.tail(), check_names=False, check_less_precise=True)
Exemple #3
0
 def setUpClass(cls):
     cls._df = pd.read_csv(cls._filename, sep=',')
     cls._params = dict(close=cls._df['Close'], r=25, s=13, fillna=False)
     cls._indicator = TSIIndicator(**cls._params)
Exemple #4
0
def calculate_Momentum_Indicators():

    JSON_sent = request.get_json()
    df = pd.DataFrame(JSON_sent[0])

    _, RSI, TSI, UO, STOCH, STOCH_SIGNAL, WR, AO, KAMA, ROC = JSON_sent

    indicator_RSI = RSIIndicator(close=df["close"], n=RSI['N'])
    df['rsi'] = indicator_RSI.rsi()

    if TSI['displayTSI']:
        indicator_TSI = TSIIndicator(close=df["close"],
                                     r=TSI['rTSI'],
                                     s=TSI['sTSI'])
        df['tsi'] = indicator_TSI.tsi()

    if UO['displayUO']:
        indicator_UO = uo(high=df['high'],
                          low=df['low'],
                          close=df['close'],
                          s=UO['sForUO'],
                          m=UO['mForUO'],
                          len=UO['lenForUO'],
                          ws=UO['wsForUO'],
                          wm=UO['wmForUO'],
                          wl=UO['wlForUO'])
        df['uo'] = indicator_UO

    if STOCH['displaySTOCH']:
        indicator_Stoch = stoch(high=df['high'],
                                low=df['low'],
                                close=df['close'],
                                n=STOCH['nForSTOCH'],
                                d_n=STOCH['dnForSTOCH'])
        df['stoch'] = indicator_Stoch

    if STOCH_SIGNAL['displayStochSignal']:
        indicator_StochSignal = stoch_signal(
            high=df['high'],
            low=df['low'],
            close=df['close'],
            n=STOCH_SIGNAL['nForStochSignal'],
            d_n=STOCH_SIGNAL['dnForStochSignal'])
        df['stoch_signal'] = indicator_StochSignal

    if WR['displayWR']:
        indicator_wr = wr(high=df['high'],
                          low=df['low'],
                          close=df['close'],
                          lbp=WR['lbpForWR'])
        df['wr'] = indicator_wr

    if AO['displayAO']:
        indicator_ao = ao(high=df['high'],
                          low=df['low'],
                          s=AO['sForAO'],
                          len=AO['lenForAO'])
        df['ao'] = indicator_ao

    if KAMA['displayKama']:
        indicator_kama = kama(close=df['close'],
                              n=KAMA['nForKama'],
                              pow1=KAMA['pow1ForKama'],
                              pow2=KAMA['pow2ForKama'])
        df['kama'] = indicator_kama

    if ROC['displayROC']:
        indicator_roc = roc(close=df['close'], n=ROC['nForROC'])
        df['roc'] = indicator_roc

    df.fillna(0, inplace=True)
    export_df = df.drop(columns=['open', 'high', 'low', 'close', 'volume'])
    return (json.dumps(export_df.to_dict('records')))
Exemple #5
0
def add_momentum_ta(
    df: pd.DataFrame,
    high: str,
    low: str,
    close: str,
    volume: str,
    fillna: bool = False,
    colprefix: str = "",
    vectorized: bool = False,
) -> pd.DataFrame:
    """Add trend technical analysis features to dataframe.

    Args:
        df (pandas.core.frame.DataFrame): Dataframe base.
        high (str): Name of 'high' column.
        low (str): Name of 'low' column.
        close (str): Name of 'close' column.
        volume (str): Name of 'volume' column.
        fillna(bool): if True, fill nan values.
        colprefix(str): Prefix column names inserted
        vectorized(bool): if True, use only vectorized functions indicators

    Returns:
        pandas.core.frame.DataFrame: Dataframe with new features.
    """

    # Relative Strength Index (RSI)
    df[f"{colprefix}momentum_rsi"] = RSIIndicator(close=df[close],
                                                  window=14,
                                                  fillna=fillna).rsi()

    # Stoch RSI (StochRSI)
    indicator_srsi = StochRSIIndicator(close=df[close],
                                       window=14,
                                       smooth1=3,
                                       smooth2=3,
                                       fillna=fillna)
    df[f"{colprefix}momentum_stoch_rsi"] = indicator_srsi.stochrsi()
    df[f"{colprefix}momentum_stoch_rsi_k"] = indicator_srsi.stochrsi_k()
    df[f"{colprefix}momentum_stoch_rsi_d"] = indicator_srsi.stochrsi_d()

    # TSI Indicator
    df[f"{colprefix}momentum_tsi"] = TSIIndicator(close=df[close],
                                                  window_slow=25,
                                                  window_fast=13,
                                                  fillna=fillna).tsi()

    # Ultimate Oscillator
    df[f"{colprefix}momentum_uo"] = UltimateOscillator(
        high=df[high],
        low=df[low],
        close=df[close],
        window1=7,
        window2=14,
        window3=28,
        weight1=4.0,
        weight2=2.0,
        weight3=1.0,
        fillna=fillna,
    ).ultimate_oscillator()

    # Stoch Indicator
    indicator_so = StochasticOscillator(
        high=df[high],
        low=df[low],
        close=df[close],
        window=14,
        smooth_window=3,
        fillna=fillna,
    )
    df[f"{colprefix}momentum_stoch"] = indicator_so.stoch()
    df[f"{colprefix}momentum_stoch_signal"] = indicator_so.stoch_signal()

    # Williams R Indicator
    df[f"{colprefix}momentum_wr"] = WilliamsRIndicator(
        high=df[high], low=df[low], close=df[close], lbp=14,
        fillna=fillna).williams_r()

    # Awesome Oscillator
    df[f"{colprefix}momentum_ao"] = AwesomeOscillatorIndicator(
        high=df[high], low=df[low], window1=5, window2=34,
        fillna=fillna).awesome_oscillator()

    # Rate Of Change
    df[f"{colprefix}momentum_roc"] = ROCIndicator(close=df[close],
                                                  window=12,
                                                  fillna=fillna).roc()

    # Percentage Price Oscillator
    indicator_ppo = PercentagePriceOscillator(close=df[close],
                                              window_slow=26,
                                              window_fast=12,
                                              window_sign=9,
                                              fillna=fillna)
    df[f"{colprefix}momentum_ppo"] = indicator_ppo.ppo()
    df[f"{colprefix}momentum_ppo_signal"] = indicator_ppo.ppo_signal()
    df[f"{colprefix}momentum_ppo_hist"] = indicator_ppo.ppo_hist()

    # Percentage Volume Oscillator
    indicator_pvo = PercentageVolumeOscillator(volume=df[volume],
                                               window_slow=26,
                                               window_fast=12,
                                               window_sign=9,
                                               fillna=fillna)
    df[f"{colprefix}momentum_pvo"] = indicator_pvo.pvo()
    df[f"{colprefix}momentum_pvo_signal"] = indicator_pvo.pvo_signal()
    df[f"{colprefix}momentum_pvo_hist"] = indicator_pvo.pvo_hist()

    if not vectorized:
        # KAMA
        df[f"{colprefix}momentum_kama"] = KAMAIndicator(close=df[close],
                                                        window=10,
                                                        pow1=2,
                                                        pow2=30,
                                                        fillna=fillna).kama()

    return df
Exemple #6
0
def add_momentum_ta(df: pd.DataFrame,
                    high: str,
                    low: str,
                    close: str,
                    volume: str,
                    fillna: bool = False,
                    colprefix: str = ""):
    """Add trend technical analysis features to dataframe.

    Args:
        df (pandas.core.frame.DataFrame): Dataframe base.
        high (str): Name of 'high' column.
        low (str): Name of 'low' column.
        close (str): Name of 'close' column.
        fillna(bool): if True, fill nan values.
        colprefix(str): Prefix column names inserted

    Returns:
        pandas.core.frame.DataFrame: Dataframe with new features.
    """

    # Relative Strength Index (RSI)
    df[f'{colprefix}momentum_rsi'] = RSIIndicator(close=df[close],
                                                  n=14,
                                                  fillna=fillna).rsi()

    # Money Flow Indicator
    df[f'{colprefix}momentum_mfi'] = MFIIndicator(
        high=df[high],
        low=df[low],
        close=df[close],
        volume=df[volume],
        n=14,
        fillna=fillna).money_flow_index()

    # TSI Indicator
    df[f'{colprefix}momentum_tsi'] = TSIIndicator(close=df[close],
                                                  r=25,
                                                  s=13,
                                                  fillna=fillna).tsi()

    # Ultimate Oscillator
    df[f'{colprefix}momentum_uo'] = UltimateOscillatorIndicator(
        high=df[high],
        low=df[low],
        close=df[close],
        s=7,
        m=14,
        len=28,
        ws=4.0,
        wm=2.0,
        wl=1.0,
        fillna=fillna).uo()

    # Stoch Indicator
    indicator = StochIndicator(high=df[high],
                               low=df[low],
                               close=df[close],
                               n=14,
                               d_n=3,
                               fillna=fillna)
    df[f'{colprefix}momentum_stoch'] = indicator.stoch()
    df[f'{colprefix}momentum_stoch_signal'] = indicator.stoch_signal()

    # Williams R Indicator
    df[f'{colprefix}momentum_wr'] = WilliamsRIndicator(high=df[high],
                                                       low=df[low],
                                                       close=df[close],
                                                       lbp=14,
                                                       fillna=fillna).wr()

    # Awesome Oscillator
    df[f'{colprefix}momentum_ao'] = AwesomeOscillatorIndicator(
        high=df[high], low=df[low], s=5, len=34, fillna=fillna).ao()

    # KAMA
    df[f'{colprefix}momentum_kama'] = KAMAIndicator(close=df[close],
                                                    n=10,
                                                    pow1=2,
                                                    pow2=30,
                                                    fillna=fillna).kama()

    # Rate Of Change
    df[f'{colprefix}momentum_roc'] = ROCIndicator(close=df[close],
                                                  n=12,
                                                  fillna=fillna).roc()
    return df
Exemple #7
0
 def setUp(self):
     self._df = pd.read_csv(self._filename, sep=',')
     self._indicator = TSIIndicator(close=self._df['Close'], r=25, s=13, fillna=False)