def adosc(df, high, low, close, volume, adosc, fast_period, slow_period): """ Marc Chaikin uses the Chaikin Oscillator to monitor the flow of money in and out of the market - comparing money flow to price action helps to identify tops and bottoms in short and intermediate cycles. Parameters: df (pd.DataFrame): DataFrame which contain the asset information. high (string): the column name for the period highest price of the asset. low (string): the column name for the period lowest price of the asset. close (string): the column name for the closing price of the asset. volume (string): the column name for the volume of the asset. adosc (string): the column name for the adosc values. fast_period (int): the time period of the fast exponential moving average. slow_period (int): the time period of the slow exponential moving average. Returns: df (pd.DataFrame): Dataframe with adosc of the asset calculated. """ df = ad(df, high, low, close, volume, adosc + "_ad") df = ema(df, adosc + "_ad", adosc + "_ad_fast", fast_period) df = ema(df, adosc + "_ad", adosc + "_ad_slow", slow_period) df[adosc] = df[adosc + "_ad_fast"] - df[adosc + "_ad_slow"] df = df.dropna().reset_index(drop=True) df.drop( [adosc + "_ad", adosc + "_ad_fast", adosc + "_ad_slow"], axis=1, inplace=True, ) return df
def tema(df, price, tema, n): """ Triple Exponential Moving Average (TEMA) was designed to smooth price fluctuations and filter out volatility, thereby making it easier to dentify trends without the lag associated with moving averages. The TEMA equation is a composite of a single exponential moving average, a double exponential moving average, and a triple exponential moving average. TEMA = 3 * EMA(p, n) - 3 * EMA(EMA(p, n), n) + EMA(EMA(EMA(p, n), n), n) Parameters: df (pd.DataFrame): DataFrame which contain the asset price. price (string): the column name of the price of the asset. tema (string): the column name for the n-day double exponential moving average results. n (int): the total number of periods. Returns: df (pd.DataFrame): Dataframe with n-day double exponential moving average of the asset calculated. """ df = ema(df, price, tema + "_ema", n) df = ema(df[n - 1:], tema + "_ema", tema + "_ema_2", n) df = ema(df[n - 1:], tema + "_ema_2", tema + "_ema_3", n) df[tema] = (3 * df[tema + "_ema"] - 3 * df[tema + "_ema_2"] + df[tema + "_ema_3"]) df = df.dropna().reset_index(drop=True) df.drop([tema + "_ema", tema + "_ema_2", tema + "_ema_3"], axis=1, inplace=True) return df
def dema(df, price, dema, n): """ Double Exponential Moving Average (DEMA) attempts to offer a smoothed average with less lag than a straight exponential moving average. The DEMA equation doubles the EMA, but then cancels out the lag by subtracting the square of the EMA. DEMA = 2 * EMA(p, n) - EMA(EMA(p, n), n) Parameters: df (pd.DataFrame): DataFrame which contain the asset price. price (string): the column name of the price of the asset. dema (string): the column name for the n-day double exponential moving average results. n (int): the total number of periods. Returns: df (pd.DataFrame): Dataframe with n-day double exponential moving average of the asset calculated. """ df = ema(df, price, dema + "_ema", n) df = ema(df[n - 1:], dema + "_ema", dema + "_ema_2", n) df[dema] = 2 * df[dema + "_ema"] - df[dema + "_ema_2"] df = df.dropna().reset_index(drop=True) df.drop([dema + "_ema", dema + "_ema_2"], axis=1, inplace=True) return df
def gd(df, price, gd, n): df = ema(df, price, gd + "_ema", n) df = ema(df[n - 1:], gd + "_ema", gd + "_ema_2", n) df[gd] = (1 + v_factor) * df[gd + "_ema"] - v_factor * df[gd + "_ema_2"] df = df.dropna().reset_index(drop=True) df.drop([gd + "_ema", gd + "_ema_2"], axis=1, inplace=True) return df
def test_exponential_moving_average(self): self.wdf = ( ema(self.wdf, "open", "ema", 10).dropna().reset_index(drop=True) ) self.assertEqual(len(self.wdf["ema"]), 1092) self.assertAlmostEqual(self.wdf["ema"][0], 124.8190, places=4) self.assertAlmostEqual(self.wdf["ema"][1], 117.1137, places=4) self.assertAlmostEqual(self.wdf["ema"][2], 110.5821, places=4) self.assertAlmostEqual(self.wdf["ema"][1089], 104.5003, places=4) self.assertAlmostEqual(self.wdf["ema"][1090], 104.8202, places=4) self.assertAlmostEqual(self.wdf["ema"][1091], 104.4656, places=4)
def trix(df, price, trix, n): """ TRIX is a momentum oscillator that displays the percent rate of change of a triple exponentially smoothed moving average. Parameters: df (pd.DataFrame): DataFrame which contain the asset price. price (string): the column name of the price of the asset. trix (string): the column name for the rate of change of a triple exponential moving average results. n (int): the total number of periods. Returns: df (pd.DataFrame): Dataframe with the rate of change of a triple exponential moving average of the asset calculated. """ df = ema(df, price, trix + "_ema", n) df = ema(df[n - 1:], trix + "_ema", trix + "_ema_2", n) df = ema(df[n - 1:], trix + "_ema_2", trix + "_ema_3", n) df = roc(df, trix + "_ema_3", trix, 1) return df
def macd(df, price, macd, fast_period, slow_period, signal_period): """ The Moving Average Convergence Divergence (MACD) is the difference between two Exponential Moving Averages. The Signal line is an Exponential Moving Average of the MACD. The MACD signals trend changes and indicates the start of new trend direction. High values indicate overbought conditions, low values indicate oversold conditions. Divergence with the price indicates an end to the current trend, especially if the MACD is at extreme high or low values. When the MACD line crosses above the signal line a buy signal is generated. When the MACD crosses below the signal line a sell signal is generated. To confirm the signal, the MACD should be above zero for a buy, and below zero for a sell. Parameters: df (pd.DataFrame): DataFrame which contain the asset information. price (string): the column name for the series type of the asset. macd (string): the column name for the macd results. close (string): the column name for the closing price of the asset. fast_period (int): the time period of the fast exponential moving average. slow_period (int): the time period of the slow exponential moving average. signal_period (int): the time period of the macd signal. Returns: df (pd.DataFrame): Dataframe with macd of the asset calculated. """ df = ema(df, price, macd + "_fast_ema", fast_period) df = ema(df, price, macd + "_slow_ema", slow_period) df[macd] = df[macd + "_fast_ema"] - df[macd + "_slow_ema"] df = ema(df[slow_period - 1:], macd, macd + "_signal", signal_period) df[macd + "_hist"] = df[macd] - df[macd + "_signal"] df.drop([macd + "_fast_ema", macd + "_slow_ema"], axis=1, inplace=True) df = df.dropna().reset_index(drop=True) return df
def indicatorDF(candlefile, timeframe): pd.set_option('display.max_columns', None) warnings.simplefilter("ignore") df = pd.read_csv(candlefile) df = df.rename( columns={ 'date': 'Date', 'open': 'Open', 'high': 'High', 'low': 'Low', 'close': 'Close', 'volume': 'Volume' }) df['Timestamp'] = pd.to_datetime(df['Date'] * 1000000) #df = df[df['Date']>1572566400*1000] df = sma(df, 'Close', 'ma', iParams['SMAWINDOW']['val']) df = dema(df, 'Close', 'dema', iParams['DEMAWINDOW']['val']) df = ema(df, 'Close', 'ema', iParams['EMAWINDOW']['val']) #df = macd(df, 'Close', 'macd', iParams['MACDFAST']['val'],iParams['MACDSLOW']['val'],iParams['MACDSIGNAL']['val']) indicator_psar = ta.trend.PSARIndicator(high=df['High'], low=df['Low'], close=df['Close'], step=iParams['PSARAF']['val'], max_step=iParams['PSARMAX']['val']) df['psar'] = indicator_psar.psar() df = SuperTrend(df=df, period=iParams['STPERIOD']['val'], multiplier=iParams['STMULTIPLIER']['val']) df = ATR(df=df, period=iParams['ATRWINDOW']['val']) df = stoch(df=df, high='High', low='Low', close='Close', fast_k_n=iParams['STOCHFAST']['val'], slow_k_n=iParams['STOCHSLOWK']['val'], slow_d_n=iParams['STOCHSLOWD']['val']) df.rename(columns={'slow_%k': 'slow_k', 'slow_%d': 'slow_d'}, inplace=True) indicator_rsi = ta.momentum.RSIIndicator(close=df['Close'], n=iParams['RSIWINDOW']['val']) df['rsi'] = indicator_rsi.rsi() indicator_uo = ta.momentum.UltimateOscillator( high=df['High'], low=df['Low'], close=df['Close'], s=iParams['UOS']['val'], m=iParams['UOM']['val'], len=iParams['UOWINDOW']['val']) df['uo'] = indicator_uo.uo() indicator_macd = ta.trend.MACD(close=df['Close'], n_fast=iParams['MACDFAST']['val'], n_slow=iParams['MACDSLOW']['val'], n_sign=iParams['MACDSIG']['val']) df['macd'] = indicator_macd.macd() df['macd_diff'] = indicator_macd.macd_diff() df['macd_signal'] = indicator_macd.macd_signal() df = mom(df, 'Close', 'mom', iParams['MOMWINDOW']['val']) indicator_adx = ta.trend.ADXIndicator(high=df['High'], low=df['Low'], close=df['Close'], n=iParams['ADXWINDOW']['val']) df['adx'] = indicator_adx.adx() df['DI-'] = indicator_adx.adx_neg() df['DI+'] = indicator_adx.adx_pos() df.rename(columns={ 'Close': 'close', 'High': 'high', 'Low': 'low' }, inplace=True) df = pd.concat([ df, TA.WTO(ohlc=df, channel_lenght=iParams['WTO_CHANNEL_LENGTH']['val'], average_lenght=iParams['WTO_AVERAGE_LENGTH']['val']) ], axis=1) df.rename(columns={ 'close': 'Close', 'high': 'High', 'low': 'Low' }, inplace=True) df = ppo(df, 'Close', 'ppo', fast_period=iParams['PPOFAST']['val'], slow_period=iParams['PPOSLOW']['val'], ma_type=0) indicator_cci = ta.trend.CCIIndicator(high=df['High'], low=df['Low'], close=df['Close'], n=iParams['CCIWINDOW']['val'], c=iParams['CCIcoeff']['val']) df['cci'] = indicator_cci.cci() df['Date'] = pd.to_datetime(df['Date'] / 1000, unit='s') #kill off the microseconds timestamp_index = pd.DatetimeIndex(df['Date'].values) df = df.set_index(timestamp_index) export_csv = df.to_csv(r'indicators-' + candlefile, index=None, header=True) return df