def rsi_caching_nb(ts, windows, ewms): """Caching function for `vectorbt.indicators.basic.RSI`.""" delta = generic_nb.diff_nb(ts) # otherwise ewma will be all NaN up, down = delta.copy(), delta.copy() up = generic_nb.set_by_mask_nb(up, up < 0, 0) down = np.abs(generic_nb.set_by_mask_nb(down, down > 0, 0)) # Cache cache_dict = dict() for i in range(windows.shape[0]): h = hash((windows[i], ewms[i])) if h not in cache_dict: roll_up = ma_nb(up, windows[i], ewms[i]) roll_down = ma_nb(down, windows[i], ewms[i]) cache_dict[h] = roll_up, roll_down return cache_dict
def rsi_cache_nb(close, windows, ewms, adjust): """Caching function for `vectorbt.indicators.basic.RSI`.""" delta = generic_nb.diff_nb(close) up, down = delta.copy(), delta.copy() up = generic_nb.set_by_mask_nb(up, up < 0, 0) down = np.abs(generic_nb.set_by_mask_nb(down, down > 0, 0)) # Cache cache_dict = dict() for i in range(len(windows)): h = hash((windows[i], ewms[i])) if h not in cache_dict: roll_up = ma_nb(up, windows[i], ewms[i], adjust=adjust) roll_down = ma_nb(down, windows[i], ewms[i], adjust=adjust) cache_dict[h] = roll_up, roll_down return cache_dict
def rsi_caching_nb(ts, windows, ewms): """Caching function for `vectorbt.indicators.basic.RSI`.""" delta = generic_nb.diff_nb(ts)[1:, :] # otherwise ewma will be all NaN up, down = delta.copy(), delta.copy() up = generic_nb.set_by_mask_nb(up, up < 0, 0) down = np.abs(generic_nb.set_by_mask_nb(down, down > 0, 0)) # Cache cache_dict = dict() for i in range(windows.shape[0]): h = hash((windows[i], ewms[i])) if h not in cache_dict: if ewms[i]: roll_up = generic_nb.ewm_mean_nb(up, windows[i]) roll_down = generic_nb.ewm_mean_nb(down, windows[i]) else: roll_up = generic_nb.rolling_mean_nb(up, windows[i]) roll_down = generic_nb.rolling_mean_nb(down, windows[i]) roll_up = generic_nb.prepend_nb(roll_up, 1, np.nan) # bring to old shape roll_down = generic_nb.prepend_nb(roll_down, 1, np.nan) cache_dict[h] = roll_up, roll_down return cache_dict