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 ppsr(high_arr, low_arr, close_arr): """Calculate Pivot Points, Supports and Resistances 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 :return: PP R1 S1 R2 S2 R3 S3 """ high_gpu = high_arr.to_gpu_array() low_gpu = low_arr.to_gpu_array() close_gpu = close_arr.to_gpu_array() PP = average_price(high_gpu, low_gpu, close_gpu) R1 = substract(scale(PP, 2.0), low_gpu) S1 = substract(scale(PP, 2.0), high_gpu) R2 = substract(summation(PP, high_gpu), low_gpu) S2 = summation(substract(PP, high_gpu), low_gpu) R3 = summation(high_gpu, scale(substract(PP, low_gpu), 2.0)) S3 = substract(low_gpu, scale(substract(high_gpu, PP), 2.0)) out = collections.namedtuple('PPSR', 'PP R1 S1 R2 S2 R3 S3') return out(PP=cudf.Series(PP, nan_as_null=False), R1=cudf.Series(R1, nan_as_null=False), S1=cudf.Series(S1, nan_as_null=False), R2=cudf.Series(R2, nan_as_null=False), S2=cudf.Series(S2, nan_as_null=False), R3=cudf.Series(R3, nan_as_null=False), S3=cudf.Series(S3, nan_as_null=False))
def port_ppsr(asset_indicator, high_arr, low_arr, close_arr): """Calculate port Pivot Points, Supports and Resistances 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 :return: PP R1 S1 R2 S2 R3 S3 """ high_gpu = high_arr.data.to_gpu_array() low_gpu = low_arr.data.to_gpu_array() close_gpu = close_arr.data.to_gpu_array() PP = average_price(high_gpu, low_gpu, close_gpu) R1 = substract(scale(PP, 2.0), low_gpu) S1 = substract(scale(PP, 2.0), high_gpu) R2 = substract(summation(PP, high_gpu), low_gpu) S2 = summation(substract(PP, high_gpu), low_gpu) R3 = summation(high_gpu, scale(substract(PP, low_gpu), 2.0)) S3 = substract(low_gpu, scale(substract(high_gpu, PP), 2.0)) out = collections.namedtuple('PPSR', 'PP R1 S1 R2 S2 R3 S3') return out(PP=cudf.Series(PP), R1=cudf.Series(R1), S1=cudf.Series(S1), R2=cudf.Series(R2), S2=cudf.Series(S2), R3=cudf.Series(R3), S3=cudf.Series(S3))
def macd(close_arr, n_fast, n_slow): """Calculate MACD, MACD Signal and MACD difference :param close_arr: close price of the bar, expect series from cudf :param n_fast: fast time steps :param n_slow: slow time steps :return: MACD MACDsign MACDdiff """ EMAfast = Ewm(n_fast, close_arr).mean() EMAslow = Ewm(n_slow, close_arr).mean() MACD = substract(EMAfast, EMAslow) average_window = 9 MACDsign = Ewm(average_window, MACD).mean() MACDdiff = substract(MACD, MACDsign) out = collections.namedtuple('MACD', 'MACD MACDsign MACDdiff') return out(MACD=cudf.Series(MACD), MACDsign=cudf.Series(MACDsign), MACDdiff=cudf.Series(MACDdiff))
def port_macd(asset_indicator, close_arr, n_fast, n_slow): """Calculate MACD, MACD Signal and MACD difference :param asset_indicator: the indicator of beginning of the stock :param close_arr: close price of the bar, expect series from cudf :param n_fast: fast time steps :param n_slow: slow time steps :return: MACD MACDsign MACDdiff """ EMAfast = PEwm(n_fast, close_arr, asset_indicator).mean() EMAslow = PEwm(n_slow, close_arr, asset_indicator).mean() MACD = substract(EMAfast, EMAslow) average_window = 9 MACDsign = PEwm(average_window, MACD, asset_indicator).mean() MACDdiff = substract(MACD, MACDsign) out = collections.namedtuple('MACD', 'MACD MACDsign MACDdiff') return out(MACD=cudf.Series(MACD, nan_as_null=False), MACDsign=cudf.Series(MACDsign, nan_as_null=False), MACDdiff=cudf.Series(MACDdiff, nan_as_null=False))
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 commodity_channel_index(high_arr, low_arr, close_arr, n): """Calculate Commodity Channel 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 :return: Commodity Channel Index in cudf.Series """ PP = average_price(high_arr.to_gpu_array(), low_arr.to_gpu_array(), close_arr.to_gpu_array()) M = Rolling(n, PP).mean() N = Rolling(n, PP).std() CCI = division(substract(PP, M), N) return cudf.Series(CCI, nan_as_null=False)
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 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_chaikin_oscillator(asset_indicator, high_arr, low_arr, close_arr, volume_arr, n1, n2): """Calculate port Chaikin Oscillator 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 volume_arr: volume the bar, expect series from cudf :param n1: n1 time steps :param n2: n2 time steps :return: Chaikin Oscillator indicator in cudf.Series """ ad = (2.0 * close_arr - high_arr - low_arr) / (high_arr - low_arr) * volume_arr first = PEwm(n1, ad, asset_indicator).mean() second = PEwm(n2, ad, asset_indicator).mean() Chaikin = cudf.Series(substract(first, second)) return Chaikin
def bollinger_bands(close_arr, n): """Calculate the Bollinger Bands. See https://www.investopedia.com/terms/b/bollingerbands.asp for details :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: b1 b2 """ MA = Rolling(n, close_arr).mean() MSD = Rolling(n, close_arr).std() close_arr_gpu = numba.cuda.device_array_like(close_arr.data.to_gpu_array()) close_arr_gpu[:] = close_arr.data.to_gpu_array()[:] close_arr_gpu[0:n - 1] = math.nan MSD_4 = scale(MSD, 4.0) b1 = division(MSD_4, MA) b2 = division(summation(substract(close_arr_gpu, MA), scale(MSD, 2.0)), MSD_4) out = collections.namedtuple('Bollinger', 'b1 b2') return out(b1=cudf.Series(b1), b2=cudf.Series(b2))
def port_commodity_channel_index(asset_indicator, high_arr, low_arr, close_arr, n): """Calculate port Commodity Channel 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 :return: Commodity Channel Index in cudf.Series """ PP = average_price(high_arr.to_gpu_array(), low_arr.to_gpu_array(), close_arr.to_gpu_array()) M = Rolling(n, PP).mean() port_mask_nan(asset_indicator.to_gpu_array(), M, 0, n - 1) N = Rolling(n, PP).std() port_mask_nan(asset_indicator.to_gpu_array(), N, 0, n - 1) CCI = division(substract(PP, M), N) return cudf.Series(CCI, 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 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
def port_bollinger_bands(asset_indicator, close_arr, n): """Calculate the port Bollinger Bands. See https://www.investopedia.com/terms/b/bollingerbands.asp for details :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: b1 b2 """ MA = Rolling(n, close_arr).mean() port_mask_nan(asset_indicator.to_gpu_array(), MA, 0, n - 1) MSD = Rolling(n, close_arr).std() port_mask_nan(asset_indicator.to_gpu_array(), MSD, 0, n - 1) close_arr_gpu = numba.cuda.device_array_like(close_arr.to_gpu_array()) close_arr_gpu[:] = close_arr.to_gpu_array()[:] close_arr_gpu[0:n - 1] = math.nan MSD_4 = scale(MSD, 4.0) b1 = division(MSD_4, MA) b2 = division(summation(substract(close_arr_gpu, MA), scale(MSD, 2.0)), MSD_4) out = collections.namedtuple('Bollinger', 'b1 b2') return out(b1=cudf.Series(b1, nan_as_null=False), b2=cudf.Series(b2, nan_as_null=False))