def zscore(candles: np.ndarray, period: int = 14, matype: int = 0, nbdev: float = 1, devtype: int = 0, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ zScore :param candles: np.ndarray :param period: int - default: 14 :param matype: int - default: 0 :param nbdev: float - default: 1 :param devtype: int - default: 0 :param source_type: str - default: "close" :param sequential: bool - default: False :return: float | np.ndarray """ candles = slice_candles(candles, sequential) source = get_candle_source(candles, source_type=source_type) means = ma(source, period=period, matype=matype, sequential=True) if devtype == 0: sigmas = talib.STDDEV(source, period) * nbdev elif devtype == 1: sigmas = mean_ad(source, period, sequential=True) * nbdev elif devtype == 2: sigmas = median_ad(source, period, sequential=True) * nbdev zScores = (source - means) / sigmas return zScores if sequential else zScores[-1]
def vlma(candles: np.ndarray, min_period: int = 5, max_period: int = 50, matype: int = 0, devtype: int = 0, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ Variable Length Moving Average :param candles: np.ndarray :param min_period: int - default: 5 :param max_period: int - default: 50 :param matype: int - default: 0 :param devtype: int - default: 0 :param source_type: str - default: "close" :param sequential: bool - default: False :return: float | np.ndarray """ # Accept normal array too. if len(candles.shape) == 1: source = candles else: candles = slice_candles(candles, sequential) source = get_candle_source(candles, source_type=source_type) mean = ma(source, period=max_period, matype=matype, sequential=True) if devtype == 0: stdDev = talib.STDDEV(source, max_period) elif devtype == 1: stdDev = mean_ad(source, max_period, sequential=True) elif devtype == 2: stdDev = median_ad(source, max_period, sequential=True) a = mean - (1.75 * stdDev) b = mean - (0.25 * stdDev) c = mean + (0.25 * stdDev) d = mean + (1.75 * stdDev) res = vlma_fast(source, a, b, c, d, min_period, max_period) return res if sequential else res[-1]
def devstop(candles: np.ndarray, period: int = 20, mult: float = 0, devtype: int = 0, direction: str = "long", sequential: bool = False) -> Union[float, np.ndarray]: """ Kase Dev Stops :param candles: np.ndarray :param period: int - default: 20 :param mult: float - default: 0 :param devtype: int - default: 0 :param direction: str - default: long :param sequential: bool - default: False :return: float | np.ndarray """ candles = slice_candles(candles, sequential) high = candles[:, 3] low = candles[:, 4] AVTR = talib.SMA(talib.MAX(high, 2) - talib.MIN(low, 2), period) if devtype == 0: SD = talib.STDDEV(talib.MAX(high, 2) - talib.MIN(low, 2), period) elif devtype == 1: SD = mean_ad(talib.MAX(high, 2) - talib.MIN(low, 2), period, sequential=True) elif devtype == 2: SD = median_ad(talib.MAX(high, 2) - talib.MIN(low, 2), period, sequential=True) if direction == "long": res = talib.MAX(high - AVTR - mult * SD, period) else: res = talib.MIN(low + AVTR + mult * SD, period) return res if sequential else res[-1]
def bollinger_bands_width( candles: np.ndarray, period: int = 20, devup: float = 2, devdn: float = 2, matype: int = 0, devtype: int = 0, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ BBW - Bollinger Bands Width - Bollinger Bands Bandwidth :param candles: np.ndarray :param period: int - default: 20 :param devup: float - default: 2 :param devdn: float - default: 2 :param matype: int - default: 0 :param devtype: int - default: 0 :param source_type: str - default: "close" :param sequential: bool - default: False :return: BollingerBands(upperband, middleband, lowerband) """ candles = slice_candles(candles, sequential) source = get_candle_source(candles, source_type=source_type) if devtype == 0: dev = talib.STDDEV(source, period) elif devtype == 1: dev = mean_ad(source, period, sequential=True) elif devtype == 2: dev = median_ad(source, period, sequential=True) middlebands = ma(source, period=period, matype=matype, sequential=True) upperbands = middlebands + devup * dev lowerbands = middlebands - devdn * dev if sequential: return (upperbands - lowerbands) / middlebands else: return (upperbands[-1] - lowerbands[-1]) / middlebands[-1]
def rvi(candles: np.ndarray, period: int = 10, ma_len: int = 14, matype: int = 1, devtype: int = 0, source_type: str = "close", sequential: bool = False) -> Union[float, np.ndarray]: """ RVI - Relative Volatility Index :param candles: np.ndarray :param period: int - default: 10 :param ma_len: int - default: 14 :param matype: int - default: 1 :param source_type: str - default: "close" :param sequential: bool - default=False :return: float | np.ndarray """ candles = slice_candles(candles, sequential) source = get_candle_source(candles, source_type=source_type) if devtype == 0: dev = talib.STDDEV(source, period) elif devtype == 1: dev = mean_ad(source, period, sequential=True) elif devtype == 2: dev = median_ad(source, period, sequential=True) diff = np.diff(source) diff = same_length(source, diff) up = np.nan_to_num(np.where(diff <= 0, 0, dev)) down = np.nan_to_num(np.where(diff > 0, 0, dev)) up_avg = ma(up, period=ma_len, matype=matype, sequential=True) down_avg = ma(down, period=ma_len, matype=matype, sequential=True) result = 100 * (up_avg / (up_avg + down_avg)) return result if sequential else result[-1]