from warnings import filterwarnings


def calcRSI(df):
    """
    Calculates RSI indicator
    Read about RSI: https://www.investopedia.com/terms/r/rsi.asp

    Args:
        df : pandas.DataFrame()
            dataframe of historical ticker data
    Returns:
        pandas.DataFrame()
            dataframe of calculated RSI indicators + original data
    """

    filterwarnings("ignore")

    df["price_change"] = df["adjclose"].pct_change()
    df["Upmove"] = df["price_change"].apply(lambda x: x if x > 0 else 0)
    df["Downmove"] = df["price_change"].apply(lambda x: abs(x) if x < 0 else 0)
    df["avg_Up"] = df["Upmove"].ewm(span=19).mean()
    df["avg_Down"] = df["Downmove"].ewm(span=19).mean()
    df = df.dropna()
    df["RS"] = df["avg_Up"] / df["avg_Down"]
    df["RSI"] = df["RS"].apply(lambda x: 100 - (100 / (x + 1)))
    return df