def average_directional_index(df, lookback=14): """ ADX technical indicator :param df: Chart data, as pandas dataframe :param lookback: Optional. Look-back period. Default 14 :return: A pandas series """ high_values = df['high'] low_values = df['low'] up_move = (high_values - high_values.shift(1).fillna(0)) dn_move = (low_values.shift(1).fillna(0) - low_values) up_index = (up_move > dn_move) & (up_move > 0) dn_index = (dn_move > up_move) & (dn_move > 0) plus_dm, minus_dm = high_values*0, low_values*0 plus_dm[up_index] = up_move[up_index] minus_dm[dn_index] = dn_index[dn_index] this_atr = atr(df, lookback) plus_di = 100*(ema((plus_dm/this_atr), lookback)) minus_di = 100*(ema((minus_dm/this_atr), lookback)) adx = 100 * (ema(((plus_di - minus_di)/(plus_di + minus_di)).abs(), lookback)) return adx
def moving_average_convergence_divergence(df, column='close', short=12, long=26, medium=9, return_as='delta'): """ MACD technical indicator. Is returned either as - The delta between main- and signal-line - The main-line - The signal-line :param df: Chart data, as pandas dataframe :param column: Optional. Column upon which indicator is to be computed. Default 'close' :param short: Optional. Short look-back period. Default 12 :param long: Optional. Long look-back period. Default 26 :param medium: Optional. Medium look-back period. Default 9 :param return_as: Optional. How to return the indicator. Can be 'delta', 'main' or 'signal'. Default 'delta' :return: A pandas series """ values = df[column] # The basic EMAs short_ema = ema(values, short) long_ema = ema(values, long) # The lines calculated from the EMAs main_line = short_ema - long_ema signal_line = ema(main_line, medium) if return_as == 'delta': return signal_line - main_line elif return_as == 'main': return main_line else: return signal_line
def triple_exponential_moving_average(df, column='close', lookback_trix=14, lookback_signal=9, return_as='delta'): """ TRIX technical indicator. Is returned either as - The delta between main- and signal-line - The main-line - The signal-line :param df: Chart data, as pandas dataframe :param column: Optional. Column upon which indicator is to be computed. Default 'close' :param lookback_trix: Optional. The lookback-period for the main-line. Default 14 :param lookback_signal: Optional. The lookback-period for the signal-line. Default9 :param return_as: Optional. How to return the indicator. Can be 'delta', 'main' or 'signal'. Default 'delta' :return: A pandas series """ values = df[column] single_ema = ema(values, lookback_trix) double_ema = ema(single_ema, lookback_trix) triple_ema = ema(double_ema, lookback_trix) triple_ema_prev = triple_ema.shift(1).fillna(triple_ema.iloc[0]) trix = 100 * (triple_ema - triple_ema_prev)/(triple_ema_prev) signal = ema(trix, lookback_signal) if return_as == 'delta': return signal - trix elif return_as == 'main': return trix else: return signal
def relative_strength_index(df, column='close', lookback=14): """ RSI technical indicator :param df: Chart data, as pandas dataframe :param column: Optional. Column upon which indicator is to be computed. Default 'close' :param lookback: Optional. Look-back period. Default 14 :return: A pandas series """ values = df[column] deltas = values.diff() gain_pos, loss_pos = deltas > 0, deltas < 0 gains, losses = deltas * 0, deltas * 0 gains[gain_pos], losses[loss_pos] = deltas[gain_pos], deltas[loss_pos] * -1 avg_gain, avg_loss = ema(gains, lookback=lookback), ema(losses, lookback=lookback) return (100 - (100 / (1 + avg_gain / avg_loss))).fillna(0)
def mass_index(df, short=9, long=25): """ AO technical indicator :param df: Chart data, as pandas dataframe :param short: Optional. Short look-back period. Default 9 :param long: Optional. Long look-back period. Default 25 :return: A pandas series """ high_values = df['high'] low_values = df['low'] high_low_delta = high_values - low_values ema_single = ema(high_low_delta, short) ema_double = ema(ema_single, short) ema_div = ema_single/ema_double midx = ema_div.rolling(window=long, min_periods=0).sum().fillna(0) return midx
def force_index(df, lookback=14): """ FIDX technical indicator :param df: Chart data, as pandas dataframe :param lookback: Optional. Look-back period. Default 14 :return: A pandas series """ close_values = df['close'] volumes = df['volume'] prev_close_values = close_values.shift(1).fillna(0) base = (close_values - prev_close_values) * volumes fidx = ema(base, lookback) return fidx
def accumulation_distribution_line(df, lookback=20): """ ADL technical indicator :param df: Chart data, as pandas dataframe :param lookback: Optional. Look-back period. Default 20 :return: A pandas series """ high_values = df['high'] low_values = df['low'] close_values = df['close'] volumes = df['volume'] mf_multiplier = ((close_values - low_values) - (high_values - close_values)) / (high_values - low_values) mf_volume = mf_multiplier * volumes adl = ema(mf_volume, lookback) return adl
def keltner_channel(df, column='close', lookback=14, return_as='total_delta'): """ KLTCH technical indicator. Can be returned as - Delta between upper and lower band - Delta between upper and middle or middle and lower band - Either of the three bands :param df: Chart data, as pandas dataframe :param column: Optional. Column upon which indicator is to be computed. Default 'close' :param lookback: Optional. Look-back period. Default 14 :param return_as: Optional. How to return the indicator. Can be 'total_delta', 'upper_delta', 'lower_delta', 'upper_band', 'middle_band' and 'lower_band'. Default 'total_delta' :return: A pandas series. """ values = df[column] mband = ema(values, lookback) double_atr = average_true_range(df, lookback)*2 uband = mband + double_atr lband = mband - double_atr if return_as == 'total_delta': return uband - lband elif return_as == 'upper_delta': return uband - mband elif return_as == 'lower_delta': return mband - lband elif return_as == 'upper_band': return uband elif return_as == 'lower_band': return lband elif return_as == 'middle_band': return mband else: raise ValueError("return_as: expected to be either 'total_delta', 'upper_delta' or 'lower_delta', but was " + str(return_as) + " instead!")
def average_true_range(df, lookback=14): """ ATR technical indicator :param df: Chart data, as pandas dataframe :param lookback: Optional. Look-back period. Default 14 :return: A pandas series """ high_values = df['high'] low_values = df['low'] close_values = df['close'] a = high_values - low_values b = (high_values - close_values.shift(1).fillna(0)).abs() c = (low_values - close_values.shift(1).fillna(0)).abs() true_ranges = pd.concat([a,b,c], axis=1).max(axis=1) atr = ema(true_ranges, lookback) return atr