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 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 relative_strength_index(high_arr, low_arr, n): """Calculate Relative Strength Index(RSI) 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 n: time steps to do EWM average :return: Relative Strength Index in cudf.Series """ UpI, DoI = upDownMove(high_arr.to_gpu_array(), low_arr.to_gpu_array()) UpI_s = shift(UpI, 1) UpI_s[0] = 0 DoI_s = shift(DoI, 1) DoI_s[0] = 0 PosDI = Ewm(n, UpI_s).mean() NegDI = Ewm(n, DoI_s).mean() RSI = division(PosDI, summation(PosDI, NegDI)) return cudf.Series(RSI, nan_as_null=False)
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_relative_strength_index(asset_indicator, high_arr, low_arr, n): """Calculate Relative Strength Index(RSI) 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 n: time steps to do EWM average :return: Relative Strength Index in cudf.Series """ UpI, DoI = upDownMove(high_arr.data.to_gpu_array(), low_arr.data.to_gpu_array()) UpI_s = shift(UpI, 1) UpI_s[0] = 0 UpI_s = cudf.Series(UpI_s) * (1.0 - asset_indicator) DoI_s = shift(DoI, 1) DoI_s[0] = 0 DoI_s = cudf.Series(DoI_s) * (1.0 - asset_indicator) PosDI = PEwm(n, UpI_s, asset_indicator).mean() NegDI = PEwm(n, DoI_s, asset_indicator).mean() RSI = division(PosDI, summation(PosDI, NegDI)) return cudf.Series(RSI)
def donchian_channel(high_arr, low_arr, n): """Calculate donchian channel of given pandas data frame. :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 n: time steps :return: donchian channel in cudf.Series """ max_high = Rolling(n, high_arr).max() min_low = Rolling(n, low_arr).min() dc_l = substract(max_high, min_low) dc_l[:n - 1] = 0.0 donchian_chan = shift(dc_l, n - 1) return cudf.Series(donchian_chan)
def port_shift(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: shift in cu.Series """ M = shift(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 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_donchian_channel(asset_indicator, high_arr, low_arr, n): """Calculate port donchian channel of given pandas data frame. :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 n: time steps :return: donchian channel in cudf.Series """ max_high = Rolling(n, high_arr).max() port_mask_nan(asset_indicator.data.to_gpu_array(), max_high, 0, n - 1) min_low = Rolling(n, low_arr).min() port_mask_nan(asset_indicator.data.to_gpu_array(), min_low, 0, n - 1) dc_l = substract(max_high, min_low) # dc_l[:n-1] = 0.0 port_mask_zero(asset_indicator.data.to_gpu_array(), dc_l, 0, n - 1) donchian_chan = shift(dc_l, n - 1) port_mask_nan(asset_indicator.data.to_gpu_array(), donchian_chan, 0, n - 1) return cudf.Series(donchian_chan)
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))