def kst_oscillator(close_arr, r1, r2, r3, r4, n1, n2, n3, n4): """Calculate KST Oscillator for given data. :param close_arr: close price of the bar, expect series from cudf :param r1: r1 time steps :param r2: r2 time steps :param r3: r3 time steps :param r4: r4 time steps :param n1: n1 time steps :param n2: n2 time steps :param n3: n3 time steps :param n4: n4 time steps :return: KST Oscillator in cudf.Series """ M1 = diff(close_arr, r1 - 1) N1 = shift(close_arr, r1 - 1) M2 = diff(close_arr, r2 - 1) N2 = shift(close_arr, r2 - 1) M3 = diff(close_arr, r3 - 1) N3 = shift(close_arr, r3 - 1) M4 = diff(close_arr, r4 - 1) N4 = shift(close_arr, r4 - 1) term1 = Rolling(n1, division(M1, N1)).sum() term2 = scale(Rolling(n2, division(M2, N2)).sum(), 2.0) term3 = scale(Rolling(n3, division(M3, N3)).sum(), 3.0) term4 = scale(Rolling(n4, division(M4, N4)).sum(), 4.0) KST = summation(summation(summation(term1, term2), term3), term4) return cudf.Series(KST)
def force_index(close_arr, volume_arr, n): """Calculate Force Index for given data. :param close_arr: close price of the bar, expect series from cudf :param volume_arr: volume the bar, expect series from cudf :param n: time steps :return: Force Index in cudf.Series """ F = multiply(diff(close_arr, n), diff(volume_arr, n)) return cudf.Series(F)
def port_force_index(asset_indicator, close_arr, volume_arr, n): """Calculate port Force Index 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 volume_arr: volume the bar, expect series from cudf :param n: time steps :return: Force Index in cudf.Series """ F = multiply(diff(close_arr, n), diff(volume_arr, n)) port_mask_nan(asset_indicator.data.to_gpu_array(), F, 0, n) return cudf.Series(F)
def coppock_curve(close_arr, n): """Calculate Coppock Curve for given data. :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: Coppock Curve in cudf.Series """ M = diff(close_arr, int(n * 11 / 10) - 1) N = shift(close_arr, int(n * 11 / 10) - 1) ROC1 = division(M, N) M = diff(close_arr, int(n * 14 / 10) - 1) N = shift(close_arr, int(n * 14 / 10) - 1) ROC2 = division(M, N) Copp = Ewm(n, summation(ROC1, ROC2)).mean() return cudf.Series(Copp)
def momentum(close_arr, n): """Calculate the momentum for the given data. :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: momentum in cu.Series """ return cudf.Series(diff(close_arr, n))
def momentum(close_arr, n): """Calculate the momentum for the 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 n: time steps :return: momentum in cu.Series """ return cudf.Series(diff(close_arr, n))
def ease_of_movement(high_arr, low_arr, volume_arr, n): """Calculate Ease of Movement 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 volume_arr: volume the bar, expect series from cudf :param n: time steps :return: Ease of Movement in cudf.Series """ high_arr_gpu = high_arr.data.to_gpu_array() low_arr_gpu = low_arr.data.to_gpu_array() EoM = division( multiply(summation(diff(high_arr_gpu, 1), diff(low_arr_gpu, 1)), substract(high_arr_gpu, low_arr_gpu)), scale(volume_arr.data.to_gpu_array(), 2.0)) Eom_ma = Rolling(n, EoM).mean() return cudf.Series(Eom_ma)
def rate_of_change(close_arr, n): """ Calculate the rate of return :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: rate of change in cu.Series """ M = diff(close_arr, n - 1) N = shift(close_arr, n - 1) return cudf.Series(division(M, N))
def port_kst_oscillator(asset_indicator, close_arr, r1, r2, r3, r4, n1, n2, n3, n4): """Calculate port KST Oscillator 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 r1: r1 time steps :param r2: r2 time steps :param r3: r3 time steps :param r4: r4 time steps :param n1: n1 time steps :param n2: n2 time steps :param n3: n3 time steps :param n4: n4 time steps :return: KST Oscillator in cudf.Series """ M1 = diff(close_arr, r1 - 1) N1 = shift(close_arr, r1 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), M1, 0, r1 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), N1, 0, r1 - 1) M2 = diff(close_arr, r2 - 1) N2 = shift(close_arr, r2 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), M2, 0, r2 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), N2, 0, r2 - 1) M3 = diff(close_arr, r3 - 1) N3 = shift(close_arr, r3 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), M3, 0, r3 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), N3, 0, r3 - 1) M4 = diff(close_arr, r4 - 1) N4 = shift(close_arr, r4 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), M4, 0, r4 - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), N4, 0, r4 - 1) term1 = Rolling(n1, division(M1, N1)).sum() port_mask_nan(asset_indicator.data.to_gpu_array(), term1, 0, n1 - 1) term2 = scale(Rolling(n2, division(M2, N2)).sum(), 2.0) port_mask_nan(asset_indicator.data.to_gpu_array(), term2, 0, n2 - 1) term3 = scale(Rolling(n3, division(M3, N3)).sum(), 3.0) port_mask_nan(asset_indicator.data.to_gpu_array(), term3, 0, n3 - 1) term4 = scale(Rolling(n4, division(M4, N4)).sum(), 4.0) port_mask_nan(asset_indicator.data.to_gpu_array(), term4, 0, n4 - 1) KST = summation(summation(summation(term1, term2), term3), term4) return cudf.Series(KST)
def port_coppock_curve(asset_indicator, close_arr, n): """Calculate port Coppock Curve 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 n: time steps :return: Coppock Curve in cudf.Series """ M = diff(close_arr, int(n * 11 / 10) - 1) N = shift(close_arr, int(n * 11 / 10) - 1) port_mask_nan(asset_indicator.to_gpu_array(), M, 0, int(n * 11 / 10) - 1) port_mask_nan(asset_indicator.to_gpu_array(), N, 0, int(n * 11 / 10) - 1) ROC1 = division(M, N) M = diff(close_arr, int(n * 14 / 10) - 1) N = shift(close_arr, int(n * 14 / 10) - 1) port_mask_nan(asset_indicator.to_gpu_array(), M, 0, int(n * 14 / 10) - 1) port_mask_nan(asset_indicator.to_gpu_array(), N, 0, int(n * 14 / 10) - 1) ROC2 = division(M, N) Copp = PEwm(n, summation(ROC1, ROC2), asset_indicator).mean() return cudf.Series(Copp, nan_as_null=False)
def port_ease_of_movement(asset_indicator, high_arr, low_arr, volume_arr, n): """Calculate port Ease of Movement 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 volume_arr: volume the bar, expect series from cudf :param n: time steps :return: Ease of Movement in cudf.Series """ high_arr_gpu = high_arr.data.to_gpu_array() low_arr_gpu = low_arr.data.to_gpu_array() EoM = division( multiply(summation(diff(high_arr_gpu, 1), diff(low_arr_gpu, 1)), substract(high_arr_gpu, low_arr_gpu)), scale(volume_arr.data.to_gpu_array(), 2.0)) port_mask_nan(asset_indicator.data.to_gpu_array(), EoM, 0, 1) Eom_ma = Rolling(n, EoM).mean() port_mask_nan(asset_indicator.data.to_gpu_array(), Eom_ma, 0, n - 1) return cudf.Series(Eom_ma)
def port_diff(asset_indicator, close_arr, n): """ Calculate the port diff :param asset_indicator: the indicator of beginning of the stock :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: diff in cu.Series """ M = diff(close_arr.data.to_gpu_array(), n) if n >= 0: port_mask_nan(asset_indicator.data.to_gpu_array(), M, 0, n) else: port_mask_nan(asset_indicator.data.to_gpu_array(), M, n, 0) return cudf.Series(M)
def accumulation_distribution(high_arr, low_arr, close_arr, vol_arr, n): """Calculate Accumulation/Distribution 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 vol_arr: volume of the bar, expect series from cudf :param n: time steps :return: Accumulation/Distribution in cudf.Series """ ad = (2.0 * close_arr - high_arr - low_arr) / (high_arr - low_arr) * vol_arr M = diff(ad, n - 1) N = shift(ad, n - 1) return cudf.Series(division(M, N))
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_rate_of_change(asset_indicator, close_arr, n): """ Calculate the port rate of return :param asset_indicator: the indicator of beginning of the stock :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: rate of change in cu.Series """ M = diff(close_arr, n - 1) N = shift(close_arr, n - 1) out = division(M, N) if n - 1 >= 0: port_mask_nan(asset_indicator.data.to_gpu_array(), out, 0, n - 1) else: port_mask_nan(asset_indicator.data.to_gpu_array(), out, n - 1, 0) return cudf.Series(out)
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 port_accumulation_distribution(asset_indicator, high_arr, low_arr, close_arr, vol_arr, n): """Calculate port Accumulation/Distribution 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 vol_arr: volume of the bar, expect series from cudf :param n: time steps :return: Accumulation/Distribution in cudf.Series """ ad = (2.0 * close_arr - high_arr - low_arr) / (high_arr - low_arr) * vol_arr M = diff(ad, n - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), M, 0, n - 1) N = shift(ad, n - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), N, 0, n - 1) return cudf.Series(division(M, N))