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 port_trix(asset_indicator, close_arr, n): """Calculate the port trix. :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: expoential weighted moving average in cu.Series """ EX1 = PEwm(n, close_arr, asset_indicator).mean() EX2 = PEwm(n, EX1, asset_indicator).mean() EX3 = PEwm(n, EX2, asset_indicator).mean() return rate_of_change(cudf.Series(EX3), 2)
def port_mass_index(asset_indicator, high_arr, low_arr, n1, n2): """Calculate the port Mass 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 n1: n1 time steps :param n1: n2 time steps :return: Mass Index in cudf.Series """ Range = high_arr - low_arr EX1 = PEwm(n1, Range, asset_indicator).mean() EX2 = PEwm(n1, EX1, asset_indicator).mean() Mass = division(EX1, EX2) MassI = Rolling(n2, Mass).sum() port_mask_nan(asset_indicator.data.to_gpu_array(), MassI, 0, n2 - 1) return cudf.Series(MassI)
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_exponential_moving_average(asset_indicator, close_arr, n): """Calculate the exponential weighted moving average for the given data. :param close_arr: close price of the bar, expect series from cudf :param n: time steps :return: expoential weighted moving average in cu.Series """ EMA = PEwm(n, close_arr, asset_indicator).mean() return cudf.Series(EMA)
def port_macd(asset_indicator, 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 = 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), MACDsign=cudf.Series(MACDsign), MACDdiff=cudf.Series(MACDdiff))
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 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 port_stochastic_oscillator_d(asset_indicator, high_arr, low_arr, close_arr, n): """Calculate port stochastic oscillator D 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: stochastic oscillator D in cudf.Series """ SOk = stochastic_oscillator_k(high_arr, low_arr, close_arr) SOd = PEwm(n, SOk, asset_indicator).mean() return cudf.Series(SOd)
def port_average_true_range(asset_indicator, high_arr, low_arr, close_arr, n): """Calculate the port Average True Range See https://www.investopedia.com/terms/a/atr.asp for details :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: average true range indicator """ tr = port_true_range(asset_indicator.to_gpu_array(), high_arr.to_gpu_array(), low_arr.to_gpu_array(), close_arr.to_gpu_array()) ATR = PEwm(n, tr, asset_indicator).mean() return cudf.Series(ATR, nan_as_null=False)
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)