def trima(df, price, trima, n): """ The Triangular Moving Average (TRIMA) is similar to other moving averages in that it shows the average (or mean) price over a specified number of data points (usually a number of price bars). However, the triangular moving average differs in that it is double smoothed—which also means averaged twice. Parameters: df (pd.DataFrame): DataFrame which contain the asset price. price (string): the column name of the price of the asset. trima (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 triangular moving average of the asset calculated. """ first_period_sma = None second_period_sma = None if n % 2 == 0: first_period_sma = int((n / 2) + 1) second_period_sma = int(n / 2) else: first_period_sma = int((n + 1) / 2) second_period_sma = int((n + 1) / 2) df = sma(df, price, trima + "_sma", first_period_sma) df = sma(df[first_period_sma - 1:], trima + "_sma", trima, second_period_sma) df = df.dropna().reset_index(drop=True) df.drop([trima + "_sma"], axis=1, inplace=True) return df
def smacompare3(df, price, sma1, sma2, sma3, n1, n2, n3): df = sma(df, price, sma1, n1) df = sma(df, price, sma2, n2) df = sma(df, price, sma3, n3) df[sma1 + '/' + sma2] = df[sma1] / df[sma2] df[sma2 + '/' + sma3] = df[sma2] / df[sma3] df[sma1 + '/' + sma3] = df[sma1] / df[sma3] return df
def cci(df, high, low, close, cci, n, c=0.015): """ The CCI is designed to detect beginning and ending market trends. The range of 100 to -100 is the normal trading range. CCI values outside of this range indicate overbought or oversold conditions. You can also look for price divergence in the CCI. If the price is making new highs, and the CCI is not, then a price correction is likely. 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. cci (string): the column name for the cci values. n (int): the total number of periods. c (float): scaling factor to provide more readable numbers, usually 0.015. Returns: df (pd.DataFrame): Dataframe with commodity channel index calculated. """ df[cci + "_tp"] = (df[high] + df[low] + df[close]) / 3.0 df = sma(df, cci + "_tp", cci + "_atp", n) mdev = ( df[cci + "_tp"] .rolling(n) .apply(lambda x: np.fabs(x - x.mean()).mean(), raw=True) ) df[cci] = (df[cci + "_tp"] - df[cci + "_atp"]) / (c * mdev) df.drop([cci + "_tp", cci + "_atp"], axis=1, inplace=True) df = df.dropna().reset_index(drop=True) return df
def test_simple_moving_average(self): self.wdf = ( sma(self.wdf, "open", "sma", 10).dropna().reset_index(drop=True) ) self.assertEqual(len(self.wdf["sma"]), 1092) self.assertAlmostEqual(self.wdf["sma"][0], 124.8190, places=4) self.assertAlmostEqual(self.wdf["sma"][1], 119.9380, places=4) self.assertAlmostEqual(self.wdf["sma"][2], 115.5950, places=4) self.assertAlmostEqual(self.wdf["sma"][1089], 104.5530, places=4) self.assertAlmostEqual(self.wdf["sma"][1090], 104.3520, places=4) self.assertAlmostEqual(self.wdf["sma"][1091], 104.1600, places=4)
def ultosc( df, high, low, close, ultosc, time_period_1=7, time_period_2=14, time_period_3=28, ): """ The Ultimate Oscillator (ULTOSC) by Larry Williams is a momentum oscillator that incorporates three different time periods to improve the overbought and oversold signals. 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. ultosc (string): the column name for the ultimate oscillator values. time_period_1 (int): The first time period for the indicator. By default, 7. time_period_2 (int): The second time period for the indicator. By default, 14. time_period_3 (int): The third time period for the indicator. By default, 28. Returns: df (pd.DataFrame): Dataframe with ultimate oscillator of the asset calculated. """ df[ultosc + "previous_close"] = df[close].shift(1) df = trange(df, high, low, close, ultosc + "_true_range") df = df.dropna().reset_index(drop=True) df[ultosc + "_true_low"] = df[[low, ultosc + "previous_close"]].min(axis=1) df[ultosc + "_close-tl"] = df[close] - df[ultosc + "_true_low"] df = sma(df, ultosc + "_close-tl", ultosc + "_a1", time_period_1) df = sma(df, ultosc + "_true_range", ultosc + "_b1", time_period_1) df = sma(df, ultosc + "_close-tl", ultosc + "_a2", time_period_2) df = sma(df, ultosc + "_true_range", ultosc + "_b2", time_period_2) df = sma(df, ultosc + "_close-tl", ultosc + "_a3", time_period_3) df = sma(df, ultosc + "_true_range", ultosc + "_b3", time_period_3) a1_b1 = df[ultosc + "_a1"] / df[ultosc + "_b1"] a2_b2 = df[ultosc + "_a2"] / df[ultosc + "_b2"] a3_b3 = df[ultosc + "_a3"] / df[ultosc + "_b3"] df[ultosc] = 100 * ((4 * a1_b1) + (2 * a2_b2) + a3_b3) / 7.0 df.drop( [ ultosc + "_true_range", ultosc + "previous_close", ultosc + "_true_low", ultosc + "_close-tl", ultosc + "_a1", ultosc + "_b1", ultosc + "_a2", ultosc + "_b2", ultosc + "_a3", ultosc + "_b3", ], axis=1, inplace=True, ) df = df.dropna().reset_index(drop=True) return df
def smacompare(df, price, sma1, sma2, n1, n2): df = sma(df, price, sma1, n1) df = sma(df, price, sma2, n2) df[sma1 + '/' + sma2] = df[sma1] / df[sma2] 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