def test_add_new_candle():
    candles_manager = CandlesManager()
    candle = _gen_candles(1)[0]
    candles_manager.add_new_candle(candle)
    assert candles_manager.close_candles_index == 1
    assert len(candles_manager.close_candles) == CandlesManager.MAX_CANDLES_COUNT
    assert candles_manager.close_candles[0] == candle[PriceIndexes.IND_PRICE_CLOSE.value]
Example #2
0
    async def handle_candles_update(self,
                                    time_frame,
                                    new_symbol_candles_data,
                                    replace_all=False,
                                    partial=False):
        try:
            symbol_candles = self.symbol_candles[time_frame]
        except KeyError:
            symbol_candles = CandlesManager()
            await symbol_candles.initialize()

            if replace_all:
                symbol_candles.replace_all_candles(new_symbol_candles_data)
            else:
                pass  # TODO ask exchange to init

            self.symbol_candles[time_frame] = symbol_candles
            return

        if partial:
            symbol_candles.add_old_and_new_candles(new_symbol_candles_data)
        elif replace_all:
            symbol_candles.replace_all_candles(new_symbol_candles_data)
        else:
            symbol_candles.add_new_candle(new_symbol_candles_data)
def test_replace_all_candles():
    candles_manager = CandlesManager()
    many_candles = _gen_candles(20)[10:]
    candles_manager.add_old_and_new_candles(many_candles)
    assert candles_manager.close_candles[0] == many_candles[0][PriceIndexes.IND_PRICE_CLOSE.value]
    assert candles_manager.close_candles[9] == many_candles[9][PriceIndexes.IND_PRICE_CLOSE.value]
    new_candles = _gen_candles(10)
    candles_manager.replace_all_candles(new_candles)
    assert candles_manager.close_candles[0] == new_candles[0][PriceIndexes.IND_PRICE_CLOSE.value]
    assert candles_manager.close_candles[9] == new_candles[9][PriceIndexes.IND_PRICE_CLOSE.value]
async def symbol_data(time_frame):
    symbol_candles = CandlesManager()
    await symbol_candles.initialize()
    symbol_candles.replace_all_candles(_get_candles())
    symbol_kline = KlineManager()
    await symbol_kline.initialize()
    symbol_kline.kline_update(_get_candle(11))
    manager = ExchangeManager({}, "binance")
    symbol_data = ExchangeSymbolData(manager, "BTC/USDT")
    tf = TimeFrames(time_frame)
    symbol_data.symbol_candles[tf] = symbol_candles
    symbol_data.symbol_klines[tf] = symbol_kline
    return symbol_data
def test_get_symbol_candles_data():
    candles_manager = CandlesManager()
    _test_data(candles_manager.get_symbol_close_candles(), 0, np.nan)
    _test_data(candles_manager.get_symbol_open_candles(), 0, np.nan)
    _test_data(candles_manager.get_symbol_high_candles(), 0, np.nan)
    _test_data(candles_manager.get_symbol_low_candles(), 0, np.nan)
    _test_data(candles_manager.get_symbol_time_candles(), 0, np.nan)
    _test_data(candles_manager.get_symbol_volume_candles(), 0, np.nan)

    new_candles = _gen_candles(2)
    candles_manager.add_old_and_new_candles(new_candles)
    _test_data(candles_manager.get_symbol_close_candles(), 2, new_candles[-1][PriceIndexes.IND_PRICE_CLOSE.value])
    _test_data(candles_manager.get_symbol_open_candles(), 2, new_candles[-1][PriceIndexes.IND_PRICE_OPEN.value])
    _test_data(candles_manager.get_symbol_high_candles(), 2, new_candles[-1][PriceIndexes.IND_PRICE_HIGH.value])
    _test_data(candles_manager.get_symbol_low_candles(), 2, new_candles[-1][PriceIndexes.IND_PRICE_LOW.value])
    _test_data(candles_manager.get_symbol_volume_candles(), 2, new_candles[-1][PriceIndexes.IND_PRICE_VOL.value])
    _test_data(candles_manager.get_symbol_time_candles(), 2, new_candles[-1][PriceIndexes.IND_PRICE_TIME.value])
def test_reach_max_candles_count():
    candles_manager = CandlesManager()
    all_candles = _gen_candles(candles_manager.MAX_CANDLES_COUNT + 3)
    max_candles = all_candles[0:candles_manager.MAX_CANDLES_COUNT]
    other_candles = all_candles[candles_manager.MAX_CANDLES_COUNT:]

    assert candles_manager.reached_max is False
    candles_manager.add_old_and_new_candles(max_candles)
    assert candles_manager.reached_max is True
    _test_data(candles_manager.get_symbol_close_candles(), candles_manager.MAX_CANDLES_COUNT,
               max_candles[-1][PriceIndexes.IND_PRICE_CLOSE.value])
    assert candles_manager.close_candles_index == candles_manager.MAX_CANDLES_COUNT - 1

    # should remove oldest (first) candles and insert new ones instead
    candles_manager.add_old_and_new_candles(other_candles)
    _test_data(candles_manager.get_symbol_close_candles(), candles_manager.MAX_CANDLES_COUNT,
               other_candles[-1][PriceIndexes.IND_PRICE_CLOSE.value])
def test_add_old_and_new_candles():
    candles_manager = CandlesManager()

    # with one candle
    single_candle = _gen_candles(1)
    candles_manager.add_old_and_new_candles(single_candle)
    assert candles_manager.reached_max is False
    assert candles_manager.close_candles_index == 1
    assert len(candles_manager.close_candles) == CandlesManager.MAX_CANDLES_COUNT
    assert candles_manager.close_candles[0] == single_candle[0][PriceIndexes.IND_PRICE_CLOSE.value]

    # with many candles including first one
    many_candles = _gen_candles(10)
    candles_manager.add_old_and_new_candles(many_candles)
    assert candles_manager.reached_max is False
    assert candles_manager.close_candles_index == 10
    assert len(candles_manager.close_candles) == CandlesManager.MAX_CANDLES_COUNT
    assert candles_manager.close_candles[0] == many_candles[0][PriceIndexes.IND_PRICE_CLOSE.value]
    assert candles_manager.close_candles[9] == many_candles[9][PriceIndexes.IND_PRICE_CLOSE.value]
def test_get_symbol_prices():
    candles_manager = CandlesManager()
    candle = _gen_candles(1)[0]
    candles_manager.add_new_candle(candle)
    symbol_price = candles_manager.get_symbol_prices()
    assert symbol_price[PriceIndexes.IND_PRICE_CLOSE.value][-1] == candle[PriceIndexes.IND_PRICE_CLOSE.value]
    assert symbol_price[PriceIndexes.IND_PRICE_OPEN.value][-1] == candle[PriceIndexes.IND_PRICE_OPEN.value]
    assert symbol_price[PriceIndexes.IND_PRICE_HIGH.value][-1] == candle[PriceIndexes.IND_PRICE_HIGH.value]
    assert symbol_price[PriceIndexes.IND_PRICE_LOW.value][-1] == candle[PriceIndexes.IND_PRICE_LOW.value]
    assert symbol_price[PriceIndexes.IND_PRICE_VOL.value][-1] == candle[PriceIndexes.IND_PRICE_VOL.value]
    assert symbol_price[PriceIndexes.IND_PRICE_TIME.value][-1] == candle[PriceIndexes.IND_PRICE_TIME.value]

    second_candle = _gen_candles(2)[1]
    candles_manager.add_new_candle(second_candle)
    second_sym_price = candles_manager.get_symbol_prices(1)
    assert second_sym_price[PriceIndexes.IND_PRICE_CLOSE.value][-1] == second_candle[PriceIndexes.IND_PRICE_CLOSE.value]
    assert second_sym_price[PriceIndexes.IND_PRICE_OPEN.value][-1] == second_candle[PriceIndexes.IND_PRICE_OPEN.value]
    assert second_sym_price[PriceIndexes.IND_PRICE_HIGH.value][-1] == second_candle[PriceIndexes.IND_PRICE_HIGH.value]
    assert second_sym_price[PriceIndexes.IND_PRICE_LOW.value][-1] == second_candle[PriceIndexes.IND_PRICE_LOW.value]
    assert second_sym_price[PriceIndexes.IND_PRICE_VOL.value][-1] == second_candle[PriceIndexes.IND_PRICE_VOL.value]
    assert second_sym_price[PriceIndexes.IND_PRICE_TIME.value][-1] == second_candle[PriceIndexes.IND_PRICE_TIME.value]
Example #9
0
def create_new_candles_manager(candles=None) -> CandlesManager:
    manager = CandlesManager()
    if candles is not None:
        manager.replace_all_candles(candles)
    return manager
def test_constructor():
    candles_manager = CandlesManager()
    assert candles_manager.candles_initialized is False
    assert candles_manager.close_candles_index == 0
    assert len(candles_manager.close_candles) == CandlesManager.MAX_CANDLES_COUNT
    assert all(np.isnan(value) for value in candles_manager.close_candles)