Beispiel #1
0
def _do_schedule(_schedule):
    market = _schedule.market
    ticker = _schedule.ticker
    collection_name = _schedule.collection_name
    collection = db.get_collection(collection_name,
                                   codec_options=codec_options)
    sleep(1)
    while True:
        if ticker == BinanceClient.KLINE_INTERVAL_1MINUTE or ticker == BinanceClient.KLINE_INTERVAL_5MINUTE:
            sleep(randrange(2))
        elif ticker == BinanceClient.KLINE_INTERVAL_15MINUTE or BinanceClient.KLINE_INTERVAL_30MINUTE:
            sleep(randrange(20))
        else:
            sleep(randrange(200))
        if _schedule.exchange == "binance":
            try:
                klines = try_get_klines(_schedule.exchange, market, ticker,
                                        get_binance_interval_unit(ticker))
                klines = klines[:
                                -1]  # we skip the last kline on purpose to have for it a crawling volume
            except Exception as err:
                traceback.print_tb(err.__traceback__)
                logger.exception("{} {} {}".format(_schedule.exchange,
                                                   collection_name,
                                                   err.__traceback__))
                sleep(randrange(30))
                klines = get_binance_klines(market, ticker,
                                            get_binance_interval_unit(ticker))
                klines = klines[:
                                -1]  # we skip the last kline on purpose to have for it a crawling volume
        elif _schedule.exchange == "kucoin":
            try:
                klines = try_get_klines(_schedule.exchange, market, ticker,
                                        get_kucoin_interval_unit(ticker))
            except Exception:
                traceback.print_tb(err.__traceback__)
                logger.exception("{} {} {}".format(_schedule.exchange,
                                                   collection_name,
                                                   err.__traceback__))
                sleep(randrange(30))
                klines = get_kucoin_klines(market, ticker,
                                           get_kucoin_interval_unit(ticker))
        current_klines = filter_current_klines(klines, collection_name,
                                               collection)
        sleep(15)
        bd, sd = get_average_depths(_schedule.depth_crawl, _schedule.no_depths)
        list(map(lambda x: x.add_buy_depth(bd), current_klines))
        list(map(lambda x: x.add_sell_depth(sd), current_klines))
        list(map(lambda x: x.add_market(market), current_klines))
        if _schedule.exchange == "binance":
            list(map(lambda x: set_trade_volume(_schedule, x), current_klines))
        list(map(lambda x: x.add_exchange(_schedule.exchange), current_klines))
        persist_klines(current_klines, collection)
        logger.info("Stored to collection : {} : {} ".format(
            _schedule.exchange, collection_name))
        _sleep_seed = 100
        if _schedule.market == "BTCUSDT" and _schedule.ticker == "15m":
            sleep(2 * 60 + randrange(10))
        else:
            sleep(_schedule.sleep + randrange(100))
def _do_schedule(_schedule):
    market = _schedule.market
    ticker = _schedule.ticker
    collection_name = _schedule.collection_name
    collection = db.get_collection(collection_name, codec_options=codec_options)
    sleep(1)

    klines_socket[market] = {}
    klines_socket[market][ticker] = {}
    klines_socket[market][ticker]['properties'] = [_schedule, collection_name, collection]
    klines_socket[market][ticker]['klines'] = []

    bm = BinanceSocketManager(binance_obj.client)
    bm.start_kline_socket(market, process_kline_socket_message, interval=ticker)
    bm.start()

    if _schedule.exchange == "binance":
        try:
            klines = try_get_klines(_schedule.exchange, market, ticker, get_binance_interval_unit(ticker))
            klines = klines[:-1]  # we skip the last kline on purpose to have for it a crawling volume
        except Exception as err:
            traceback.print_tb(err.__traceback__)
            logger.exception("{} {} {}".format(_schedule.exchange, collection_name, err.__traceback__))
            sleep(randrange(30))
            klines = get_binance_klines(market, ticker, get_binance_interval_unit(ticker))
            klines = klines[:-1]  # we skip the last kline on purpose to have for it a crawling volume

    handle_klines(klines, _schedule, collection_name, collection)