Esempio n. 1
0
async def test_set_mark_price(backtesting_exchange_manager):
    prices_manager = PricesManager(backtesting_exchange_manager)
    prices_manager.set_mark_price(10)
    assert prices_manager.mark_price == 10
    assert prices_manager.mark_price_set_time == backtesting_exchange_manager.exchange.get_exchange_current_time(
    )
    assert prices_manager.valid_price_received_event.is_set()
Esempio n. 2
0
    def __init__(self, exchange_manager, symbol):
        self.symbol = symbol
        self.exchange_manager = exchange_manager

        self.order_book_manager = OrderBookManager()
        self.prices_manager = PricesManager()
        self.recent_trades_manager = RecentTradesManager()
        self.ticker_manager = TickerManager()

        self.symbol_candles = {}
        self.symbol_klines = {}

        self.logger = get_logger(f"{self.__class__.__name__} - {self.symbol}")
Esempio n. 3
0
async def test_initialize(backtesting_exchange_manager):
    prices_manager = PricesManager(backtesting_exchange_manager)
    assert prices_manager.mark_price == prices_manager.mark_price_set_time == 0
    assert not prices_manager.valid_price_received_event.is_set()

    # should be reset in init
    prices_manager.mark_price = 10
    prices_manager.mark_price_set_time = 10
    prices_manager.valid_price_received_event.set()

    await prices_manager.initialize_impl()
    assert prices_manager.mark_price == prices_manager.mark_price_set_time == 0
    assert not prices_manager.valid_price_received_event.is_set()
Esempio n. 4
0
async def test_get_mark_price(backtesting_exchange_manager):
    prices_manager = PricesManager(backtesting_exchange_manager)
    # without a set price
    with pytest.raises(asyncio.TimeoutError):
        await prices_manager.get_mark_price(0.01)
    assert not prices_manager.valid_price_received_event.is_set()

    # set price
    prices_manager.set_mark_price(10)
    assert await prices_manager.get_mark_price(0.01) == 10
    assert prices_manager.valid_price_received_event.is_set()

    # expired price
    backtesting_exchange_manager.backtesting.time_manager.current_timestamp = 66666666
    with pytest.raises(asyncio.TimeoutError):
        await prices_manager.get_mark_price(0.01)
    assert not prices_manager.valid_price_received_event.is_set()

    # reset price with this time
    prices_manager.set_mark_price(10)
    assert await prices_manager.get_mark_price(0.01) == 10
    assert prices_manager.valid_price_received_event.is_set()

    # current time move within allowed range
    backtesting_exchange_manager.backtesting.time_manager.current_timestamp = 1
    assert await prices_manager.get_mark_price(0.01) == 10
    assert prices_manager.valid_price_received_event.is_set()

    # new value
    prices_manager.set_mark_price(42.0000172)
    assert await prices_manager.get_mark_price(0.01) == 42.0000172
    assert prices_manager.valid_price_received_event.is_set()
    def __init__(self, exchange_manager, symbol):
        self.symbol = symbol
        self.exchange_manager = exchange_manager

        self.price_events_manager = PriceEventsManager()
        self.order_book_manager = OrderBookManager()
        self.prices_manager = PricesManager(self.exchange_manager)
        self.recent_trades_manager = RecentTradesManager()
        self.ticker_manager = TickerManager()
        self.funding_manager = FundingManager() if self.exchange_manager.is_margin else None

        self.symbol_candles = {}
        self.symbol_klines = {}

        self.logger = get_logger(f"{self.__class__.__name__} - {self.symbol}")
    async def handle_recent_trades_update(self, exchange: str, symbol: str,
                                          recent_trades: list):
        try:
            mark_price = PricesManager.calculate_mark_price_from_recent_trade_prices(
                [
                    float(
                        last_price[ExchangeConstantsOrderColumns.PRICE.value])
                    for last_price in recent_trades
                ])

            await self.push(symbol, mark_price)
        except Exception as e:
            self.logger.exception(f"Fail to handle recent trades update : {e}")
Esempio n. 7
0
def prices_manager(event_loop, backtesting_exchange_manager):
    return PricesManager(backtesting_exchange_manager)
Esempio n. 8
0
class ExchangeSymbolData:
    MAX_ORDER_BOOK_ORDER_COUNT = 100
    MAX_RECENT_TRADES_COUNT = 100

    def __init__(self, exchange_manager, symbol):
        self.symbol = symbol
        self.exchange_manager = exchange_manager

        self.order_book_manager = OrderBookManager()
        self.prices_manager = PricesManager()
        self.recent_trades_manager = RecentTradesManager()
        self.ticker_manager = TickerManager()

        self.symbol_candles = {}
        self.symbol_klines = {}

        self.logger = get_logger(f"{self.__class__.__name__} - {self.symbol}")

    # candle functions
    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 handle_recent_trade_update(self,
                                   recent_trades,
                                   replace_all=False,
                                   partial=False):
        if partial:
            # TODO check if initialized
            return self.recent_trades_manager.add_new_trades(recent_trades)
        elif replace_all:
            return self.recent_trades_manager.set_all_recent_trades(
                recent_trades)
        # TODO check if initialized

        # recent trades should be a dict
        return self.recent_trades_manager.add_recent_trade(recent_trades)

    def handle_mark_price_update(self, mark_price):
        self.prices_manager.set_mark_price(mark_price)

    def handle_order_book_update(self, asks, bids, is_delta=False):
        if is_delta:
            # TODO check if initialized
            self.order_book_manager.order_book_delta_update(asks, bids)
        else:
            self.order_book_manager.order_book_update(asks, bids)

    def handle_ticker_update(self, ticker):
        self.ticker_manager.ticker_update(ticker)

    async def handle_kline_update(self, time_frame, kline):
        try:
            symbol_klines = self.symbol_klines[time_frame]
        except KeyError:
            symbol_klines = KlineManager()
            try:
                await symbol_klines.initialize()
                symbol_klines.kline_update(kline)
                self.symbol_klines[time_frame] = symbol_klines
            except KeyError:
                self.logger.warning(
                    "Can't initialize kline manager : missing required candles data."
                )
                return

        symbol_klines.kline_update(kline)
class ExchangeSymbolData:
    MAX_ORDER_BOOK_ORDER_COUNT = 100
    MAX_RECENT_TRADES_COUNT = 100

    def __init__(self, exchange_manager, symbol):
        self.symbol = symbol
        self.exchange_manager = exchange_manager

        self.price_events_manager = PriceEventsManager()
        self.order_book_manager = OrderBookManager()
        self.prices_manager = PricesManager(self.exchange_manager)
        self.recent_trades_manager = RecentTradesManager()
        self.ticker_manager = TickerManager()
        self.funding_manager = FundingManager() if self.exchange_manager.is_margin else None

        self.symbol_candles = {}
        self.symbol_klines = {}

        self.logger = get_logger(f"{self.__class__.__name__} - {self.symbol}")

    # candle functions
    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)

            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 handle_recent_trade_update(self, recent_trades, replace_all=False):
        if replace_all:
            recent_trades_added = self.recent_trades_manager.set_all_recent_trades(recent_trades)
        else:
            recent_trades_added = self.recent_trades_manager.add_new_trades(recent_trades)
        self.price_events_manager.handle_recent_trades(recent_trades_added)
        return recent_trades_added

    def handle_liquidations(self, liquidations):
        self.recent_trades_manager.add_new_liquidations(liquidations)

    def handle_mark_price_update(self, mark_price, mark_price_source) -> bool:
        updated = self.prices_manager.set_mark_price(mark_price, mark_price_source)
        if updated:
            self.price_events_manager.handle_price(mark_price,
                                                   self.exchange_manager.exchange.get_exchange_current_time())
        return updated

    def handle_order_book_update(self, asks, bids):
        self.order_book_manager.handle_new_books(asks, bids)

    def handle_order_book_ticker_update(self, ask_quantity, ask_price, bid_quantity, bid_price):
        self.order_book_manager.order_book_ticker_update(ask_quantity, ask_price, bid_quantity, bid_price)

    def handle_ticker_update(self, ticker):
        self.ticker_manager.ticker_update(ticker)

    def handle_mini_ticker_update(self, mini_ticker):
        self.ticker_manager.mini_ticker_update(mini_ticker)

    async def handle_kline_update(self, time_frame, kline):
        try:
            symbol_klines = self.symbol_klines[time_frame]
        except KeyError:
            symbol_klines = KlineManager()
            try:
                await symbol_klines.initialize()
                symbol_klines.kline_update(kline)
                self.symbol_klines[time_frame] = symbol_klines
            except KeyError:
                self.logger.warning("Can't initialize kline manager : missing required candles data.")
                return

        symbol_klines.kline_update(kline)

    async def handle_funding_update(self, funding_rate, next_funding_time, timestamp):
        if self.funding_manager:
            self.funding_manager.funding_update(funding_rate, next_funding_time, timestamp)