def mac(candles: np.ndarray, fast_interval: int = 20, slow_interval: int = 50, sequential=False) -> Union[float, np.ndarray]: """ Moving Average Cross Port of: https://www.tradingview.com/script/PcWAuplI-Moving-Average-Cross/ :param candles: np.ndarray :param fast_interval: int - default: 20 :param slow_interval: int - default: 50 :param sequential: bool - default: False :return: Union[float, np.ndarray] Output: 1: yellow market, -1: red market """ if not sequential and len(candles) > 240: candles = candles[-240:] candles_high = candles[:, 3] candles_low = candles[:, 4] # Fast MAs upper_fast = talib.EMA(candles_high, timeperiod=fast_interval) lower_fast = talib.EMA(candles_low, timeperiod=fast_interval) # Slow MAs upper_slow = talib.EMA(candles_high, timeperiod=slow_interval) lower_slow = talib.EMA(candles_low, timeperiod=slow_interval) # Crosses crosses_lf_us = utils.crossed(lower_fast, upper_slow, direction=None, sequential=True) crosses_uf_ls = utils.crossed(upper_fast, lower_slow, direction=None, sequential=True) dir_1 = np.where(crosses_lf_us, 1, np.nan) dir_2 = np.where(crosses_uf_ls, -1, np.nan) dir = np.where(dir_1 == 1, dir_1, np.nan) dir = np.where(dir_2 == -1, dir_2, dir_1) res = pd.Series(dir).fillna(method="ffill").to_numpy() if sequential: return res else: return res[-1]
def should_long(self): qty = utils.size_to_qty(self.capital, self.price, 3, fee_rate=self.fee_rate) if utils.crossed( self.rsi, 35, direction="above" ) and qty > 0 and self.available_margin > (qty * self.price): return True
def test_crossed(): candles = np.array(mama_candles) cross_100 = utils.crossed(candles[:, 2], 100) assert cross_100 == False cross_120 = utils.crossed(candles[:, 2], 120) assert cross_120 == True cross_120 = utils.crossed(candles[:, 2], 120, direction="below") assert cross_120 == True cross_120 = utils.crossed(candles[:, 2], 120, direction="above") assert cross_120 == False seq_cross_200 = utils.crossed(candles[:, 2], 200, direction="below", sequential=True) assert seq_cross_200[-5] == True seq_cross_200 = utils.crossed(candles[:, 2], 200, direction="above", sequential=True) assert seq_cross_200[-5] == False seq_cross_120 = utils.crossed(candles[:, 2], 120, sequential=True) assert seq_cross_120[-1] == True array_array_cross_above = utils.crossed(np.array([1., 2, 3, 4, 5, 6]), np.array([3., 3, 3, 3, 3, 3]), direction="above", sequential=True) assert array_array_cross_above[-3] == True array_array_cross_below = utils.crossed(np.array([1., 2, 3, 2, 1, 6]), np.array([3., 3, 3, 3, 3, 3]), direction="below", sequential=True) assert array_array_cross_below[-3] == True array_array_cross = utils.crossed(np.array([1., 2, 3, 4, 1, 2]), np.array([3., 3, 3, 3, 3, 3]), sequential=True) assert array_array_cross[-3] == True assert array_array_cross[-2] == True
def test_crossed(): candles = np.array(mama_candles) cross_100 = utils.crossed(candles[:, 2], 100) assert cross_100 == False cross_120 = utils.crossed(candles[:, 2], 120) assert cross_120 == True cross_120 = utils.crossed(candles[:, 2], 120, direction="below") assert cross_120 == True cross_120 = utils.crossed(candles[:, 2], 120, direction="above") assert cross_120 == False seq_cross_200 = utils.crossed(candles[:, 2], 200, direction="below", sequential=True) assert seq_cross_200[-5] == True seq_cross_200 = utils.crossed(candles[:, 2], 200, direction="above", sequential=True) assert seq_cross_200[-5] == False seq_cross_120 = utils.crossed(candles[:, 2], 120, sequential=True) assert seq_cross_120[-1] == True
def update_position(self): if utils.crossed(self.rsi, self.hp['xparam'], direction="below") or utils.crossed( self.rsi, 10, direction="below"): self.liquidate()