def COR(x_list, y_list): """ Correlation Returns: float = jhta.COR(x_list, y_list) """ x_stdev = jhta.STDEV({'x_list': x_list}, len(x_list), 'x_list')[-1] y_stdev = jhta.STDEV({'y_list': y_list}, len(y_list), 'y_list')[-1] return jhta.COV(x_list, y_list) / (x_stdev * y_stdev)
def RVIOC(df, n, price='Close'): """ Relative Volatility Index Original Calculation """ rvioc_list = [] upavg = .0 dnavg = .0 for i in range(len(df[price])): if i + 1 < n or i < 9: rvioc = float('NaN') else: start = i + 1 - n end = i + 1 y_list = df[price][start:end] stdev = jhta.STDEV({'y': y_list}, 9, 'y')[-1] if df[price][i] > df[price][i - 1]: up = stdev dn = 0 else: up = 0 dn = stdev upavg = (upavg * (n - 1) + up) / n dnavg = (dnavg * (n - 1) + dn) / n rvioc = 100 * upavg / (upavg + dnavg) rvioc_list.append(rvioc) return rvioc_list
def RVIOC(df, n, price='Close'): """ Relative Volatility Index Original Calculation Returns: list of floats = jhta.RVIOC(df, n, price='Close') Source: https://www.fmlabs.com/reference/default.htm?url=RVIoriginal.htm """ rvioc_list = [] upavg = .0 dnavg = .0 for i in range(len(df[price])): if i + 1 < n or i < 9: rvioc = float('NaN') else: start = i + 1 - n end = i + 1 y_list = df[price][start:end] stdev = jhta.STDEV({'y': y_list}, 9, 'y')[-1] if df[price][i] > df[price][i - 1]: up = stdev dn = 0 else: up = 0 dn = stdev upavg = (upavg * (n - 1) + up) / n dnavg = (dnavg * (n - 1) + dn) / n rvioc = 100 * upavg / (upavg + dnavg) rvioc_list.append(rvioc) return rvioc_list
def RVI(df, n, high='High', low='Low'): """ Relative Volatility Index """ rvi_list = [] h_upavg = .0 h_dnavg = .0 l_upavg = .0 l_dnavg = .0 i = 0 while i < len(df[low]): if i + 1 < n or i < 9: h_rvi = float('NaN') l_rvi = float('NaN') else: start = i + 1 - n end = i + 1 h = df[high][start:end] h_stdev = jhta.STDEV({'h': h}, 9, 'h')[-1] if df[high][i] > df[high][i - 1]: h_up = h_stdev h_dn = 0 else: h_up = 0 h_dn = h_stdev h_upavg = (h_upavg * (n - 1) + h_up) / n h_dnavg = (h_dnavg * (n - 1) + h_dn) / n h_rvi = 100 * h_upavg / (h_upavg + h_dnavg) l = df[low][start:end] l_stdev = jhta.STDEV({'l': l}, 9, 'l')[-1] if df[low][i] > df[low][i - 1]: l_up = l_stdev l_dn = 0 else: l_up = 0 l_dn = l_stdev l_upavg = (l_upavg * (n - 1) + l_up) / n l_dnavg = (l_dnavg * (n - 1) + l_dn) / n l_rvi = 100 * l_upavg / (l_upavg + l_dnavg) rvi = (h_rvi + l_rvi) / 2 rvi_list.append(rvi) i += 1 return rvi_list
def RVI(df, n, high='High', low='Low'): """ Relative Volatility Index Returns: list of floats = jhta.RVI(df, n, high='High', low='Low') Source: https://www.fmlabs.com/reference/default.htm?url=RVI.htm """ rvi_list = [] h_upavg = .0 h_dnavg = .0 l_upavg = .0 l_dnavg = .0 for i in range(len(df[low])): if i + 1 < n or i < 9: h_rvi = float('NaN') l_rvi = float('NaN') else: start = i + 1 - n end = i + 1 h = df[high][start:end] h_stdev = jhta.STDEV({'h': h}, 9, 'h')[-1] if df[high][i] > df[high][i - 1]: h_up = h_stdev h_dn = 0 else: h_up = 0 h_dn = h_stdev h_upavg = (h_upavg * (n - 1) + h_up) / n h_dnavg = (h_dnavg * (n - 1) + h_dn) / n h_rvi = 100 * h_upavg / (h_upavg + h_dnavg) l = df[low][start:end] l_stdev = jhta.STDEV({'l': l}, 9, 'l')[-1] if df[low][i] > df[low][i - 1]: l_up = l_stdev l_dn = 0 else: l_up = 0 l_dn = l_stdev l_upavg = (l_upavg * (n - 1) + l_up) / n l_dnavg = (l_dnavg * (n - 1) + l_dn) / n l_rvi = 100 * l_upavg / (l_upavg + l_dnavg) rvi = (h_rvi + l_rvi) / 2 rvi_list.append(rvi) return rvi_list
def BBANDW(df, n, f=2, high='High', low='Low', close='Close'): """ Bollinger Band Width Returns: list of floats = jhta.BBANDW(df, n, f=2, high='High', low='Low', close='Close') Source: https://www.fmlabs.com/reference/default.htm?url=BollingerWidth.htm """ bbandw_list = [] tp_dict = {'tp': jhta.TYPPRICE(df, high, low, close)} stdev_list = jhta.STDEV(tp_dict, n, 'tp') for i in range(len(df[close])): if i + 1 < n: bbandw = float('NaN') else: bbandw = 2 * f * stdev_list[i] bbandw_list.append(bbandw) return bbandw_list
def BBANDW(df, n, f=2, high='High', low='Low', close='Close'): """ Bollinger Band Width """ bbandw_list = [] tp_dict = {'tp': jhta.TYPPRICE(df, high, low, close)} stdev_list = jhta.STDEV(tp_dict, n, 'tp') i = 0 while i < len(df[close]): if i + 1 < n: bbandw = float('NaN') else: bbandw = 2 * f * stdev_list[i] bbandw_list.append(bbandw) i += 1 return bbandw_list
def BBANDW(df, n, f=2): """ Bollinger Band Width """ bbandw_list = [] tp_dict = {'tp': jhta.TYPPRICE(df)} stdev_list = jhta.STDEV(tp_dict, n, 'tp') i = 0 while i < len(df['Close']): if i + 1 < n: bbandw = float('NaN') else: bbandw = 2 * f * stdev_list[i] bbandw_list.append(bbandw) i += 1 return bbandw_list
def STANDARDIZE(df, price='Close'): """ Standardize Returns: list of floats = jhta.STANDARDIZE(df, price='Close') Source: https://machinelearningmastery.com/normalize-standardize-time-series-data-python/ """ standardize_list = [] start = None for i in range(len(df[price])): if df[price][i] != df[price][i] or i < 1: standardize = float('NaN') else: if start is None: start = i x = df[price][start:] mean = jhta.MEAN({'x': x}, len(x), 'x')[-1] standard_deviation = jhta.STDEV({'x': x}, len(x), 'x')[-1] standardize = (df[price][i] - mean) / standard_deviation standardize_list.append(standardize) return standardize_list
def STANDARDIZE(df, price='Close'): """ Standardize """ standardize_list = [] i = 0 start = None while i < len(df[price]): if df[price][i] != df[price][i] or i < 1: standardize = float('NaN') else: if start is None: start = i x = df[price][start:] mean = jhta.MEAN({'x': x}, len(x), 'x')[-1] standard_deviation = jhta.STDEV({'x': x}, len(x), 'x')[-1] standardize = (df[price][i] - mean) / standard_deviation standardize_list.append(standardize) i += 1 return standardize_list
def INFO(df, price='Close'): """ Print df Information """ print('{:_<28}:{:_>22}'.format('DF PRICE COLUMN', price)) print('{:_<28}:{:_>22d}'.format('LEN', len(df[price]))) print('{:_<28}:{:_>28.5f}'.format('MIN', jhta.MIN(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format('MAX', jhta.MAX(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format('SUM', jhta.SUM(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format('MEAN', jhta.MEAN(df, len(df[price]), price)[-1])) # print ('{:_<28}:{:_>28.5f}'.format('HARMONIC_MEAN', jhta.HARMONIC_MEAN(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN', jhta.MEDIAN(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN_LOW', jhta.MEDIAN_LOW(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN_HIGH', jhta.MEDIAN_HIGH(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'MEDIAN_GROUPED', jhta.MEDIAN_GROUPED(df, len(df[price]), price)[-1])) # print ('{:_<28}:{:_>28.5f}'.format('MODE', jhta.MODE(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'PSTDEV', jhta.PSTDEV(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'PVARIANCE', jhta.PVARIANCE(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'STDEV', jhta.STDEV(df, len(df[price]), price)[-1])) print('{:_<28}:{:_>28.5f}'.format( 'VARIANCE', jhta.VARIANCE(df, len(df[price]), price)[-1]))
def BBANDS(df, n, f=2, high='High', low='Low', close='Close'): """ Bollinger Bands Returns: dict of lists of floats = jhta.BBANDS(df, n, f=2, high='High', low='Low', close='Close') Source: https://www.fmlabs.com/reference/default.htm?url=Bollinger.htm """ bbands_dict = {'midband': [], 'upperband': [], 'lowerband': []} tp_dict = {'tp': jhta.TYPPRICE(df, high, low, close)} sma_list = jhta.SMA(tp_dict, n, 'tp') stdev_list = jhta.STDEV(tp_dict, n, 'tp') for i in range(len(df[close])): if i + 1 < n: midband = float('NaN') upperband = float('NaN') lowerband = float('NaN') else: midband = sma_list[i] upperband = midband + f * stdev_list[i] lowerband = midband - f * stdev_list[i] bbands_dict['midband'].append(midband) bbands_dict['upperband'].append(upperband) bbands_dict['lowerband'].append(lowerband) return bbands_dict
def BBANDS(df, n, f=2, high='High', low='Low', close='Close'): """ Bollinger Bands """ bbands_dict = {'midband': [], 'upperband': [], 'lowerband': []} tp_dict = {'tp': jhta.TYPPRICE(df, high, low, close)} sma_list = SMA(tp_dict, n, 'tp') stdev_list = jhta.STDEV(tp_dict, n, 'tp') i = 0 while i < len(df[close]): if i + 1 < n: midband = float('NaN') upperband = float('NaN') lowerband = float('NaN') else: midband = sma_list[i] upperband = midband + f * stdev_list[i] lowerband = midband - f * stdev_list[i] bbands_dict['midband'].append(midband) bbands_dict['upperband'].append(upperband) bbands_dict['lowerband'].append(lowerband) i += 1 return bbands_dict