Esempio n. 1
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}")
    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}")
Esempio n. 3
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)
Esempio n. 5
0
async def ticker_manager():
    t_manager = TickerManager()
    await t_manager.initialize()
    return t_manager