def port_average_directional_movement_index(asset_indicator, high_arr, low_arr, close_arr, n, n_ADX): """Calculate the port Average Directional Movement Index for given data. :param asset_indicator: the indicator of beginning of the stock :param high_arr: high price of the bar, expect series from cudf :param low_arr: low price of the bar, expect series from cudf :param close_arr: close price of the bar, expect series from cudf :param n: time steps to do EWM average :param n_ADX: time steps to do EWM average of ADX :return: Average Directional Movement Index in cudf.Series """ UpI, DoI = upDownMove(high_arr.data.to_gpu_array(), low_arr.data.to_gpu_array()) tr = port_true_range(asset_indicator.to_gpu_array(), high_arr.data.to_gpu_array(), low_arr.data.to_gpu_array(), close_arr.data.to_gpu_array()) ATR = PEwm(n, tr, asset_indicator).mean() PosDI = division(PEwm(n, UpI, asset_indicator).mean(), ATR) NegDI = division(PEwm(n, DoI, asset_indicator).mean(), ATR) NORM = division(abs_arr(substract(PosDI, NegDI)), summation(PosDI, NegDI)) port_mask_nan(asset_indicator.data.to_gpu_array(), NORM, -1, 0) ADX = cudf.Series(PEwm(n_ADX, NORM, asset_indicator).mean()) return ADX
def true_strength_index(close_arr, r, s): """Calculate True Strength Index (TSI) for given data. :param close_arr: close price of the bar, expect series from cudf :param r: r time steps :param s: s time steps :return: True Strength Index in cudf.Series """ M = diff(close_arr, 1) aM = abs_arr(M) EMA1 = Ewm(r, M).mean() aEMA1 = Ewm(r, aM).mean() EMA2 = Ewm(s, EMA1).mean() aEMA2 = Ewm(s, aEMA1).mean() TSI = division(EMA2, aEMA2) return cudf.Series(TSI)
def port_true_strength_index(asset_indicator, close_arr, r, s): """Calculate port True Strength Index (TSI) for given data. :param asset_indicator: the indicator of beginning of the stock :param close_arr: close price of the bar, expect series from cudf :param r: r time steps :param s: s time steps :return: True Strength Index in cudf.Series """ M = diff(close_arr, 1) port_mask_nan(asset_indicator.data.to_gpu_array(), M, 0, 1) aM = abs_arr(M) EMA1 = PEwm(r, M, asset_indicator).mean() aEMA1 = PEwm(r, aM, asset_indicator).mean() EMA2 = PEwm(s, EMA1, asset_indicator).mean() aEMA2 = PEwm(s, aEMA1, asset_indicator).mean() TSI = division(EMA2, aEMA2) return cudf.Series(TSI)
def average_directional_movement_index(high_arr, low_arr, close_arr, n, n_ADX): """Calculate the Average Directional Movement Index for given data. :param high_arr: high price of the bar, expect series from cudf :param low_arr: low price of the bar, expect series from cudf :param close_arr: close price of the bar, expect series from cudf :param n: time steps to do EWM average :param n_ADX: time steps to do EWM average of ADX :return: Average Directional Movement Index in cudf.Series """ UpI, DoI = upDownMove(high_arr.to_gpu_array(), low_arr.to_gpu_array()) last_ele = len(high_arr) - 1 tr = true_range(high_arr.to_gpu_array(), low_arr.to_gpu_array(), close_arr.to_gpu_array()) ATR = Ewm(n, tr).mean() PosDI = division(Ewm(n, UpI).mean(), ATR) NegDI = division(Ewm(n, DoI).mean(), ATR) NORM = division(abs_arr(substract(PosDI, NegDI)), summation(PosDI, NegDI)) NORM[last_ele] = math.nan ADX = cudf.Series(Ewm(n_ADX, NORM).mean(), nan_as_null=False) return ADX