Esempio n. 1
0
    def __init__(self,
                 security_type=SecurityType.coin,
                 exchanges=['binance'],
                 codes=None,
                 batch_size=10,
                 force_update=False,
                 sleeping_time=5,
                 fetching_style=TimeSeriesFetchingStyle.end_size,
                 default_size=2000,
                 contain_unfinished_data=False,
                 level=TradingLevel.LEVEL_1DAY,
                 one_shot=False,
                 start_timestamp=None) -> None:
        self.data_schema = get_kdata_schema(security_type=security_type,
                                            level=level)

        self.ccxt_trading_level = to_ccxt_trading_level(level)
        self.start_timestamp = to_pd_timestamp(start_timestamp)

        super().__init__(security_type,
                         exchanges,
                         codes,
                         batch_size,
                         force_update,
                         sleeping_time,
                         fetching_style,
                         default_size,
                         contain_unfinished_data,
                         level,
                         one_shot,
                         kdata_use_begin_time=True)
Esempio n. 2
0
def coin_finished_timestamp(timestamp: pd.Timestamp, level: TradingLevel):
    timestamp = to_pd_timestamp(timestamp)

    if timestamp.microsecond != 0:
        return False

    return timestamp.minute % level.to_minute() == 0
Esempio n. 3
0
def is_in_trading(security_type, exchange, timestamp):
    current = now_pd_timestamp()
    timestamp = to_pd_timestamp(timestamp)
    if is_same_date(current, timestamp):
        for start, end in get_trading_intervals(security_type=security_type,
                                                exchange=exchange):
            if current > date_and_time(
                    current, start) and current < date_and_time(current, end):
                return True
    return False
Esempio n. 4
0
def china_stock_finished_timestamp(timestamp: pd.Timestamp,
                                   level: TradingLevel):
    timestamp = to_pd_timestamp(timestamp)

    if timestamp.microsecond != 0:
        return False

    return to_time_str(timestamp, fmt=TIME_FORMAT_MINUTE1
                       ) in china_stock_level_map_finished_timestamps.get(
                           level.value)
Esempio n. 5
0
    def request(self, url=None, method='get', param=None, path_fields=None):
        security_item = param['security_item']
        start_timestamp = param['start_timestamp']
        size = param['size']
        ccxt_level = param['ccxt_level']
        level = param['level']
        ccxt_account: CCXTAccount = param['ccxt_account']

        ccxt_exchange = ccxt_account.get_ccxt_exchange(security_item.exchange)

        if ccxt_exchange.has['fetchOHLCV']:
            limit = ccxt_account.get_kdata_limit(security_item.exchange)

            limit = min(size, limit)

            kdata_list = []

            try:
                if ccxt_account.exchange_conf[
                        security_item.exchange]['support_since']:
                    kdatas = ccxt_exchange.fetch_ohlcv(security_item.code,
                                                       timeframe=ccxt_level,
                                                       since=start_timestamp)
                else:
                    kdatas = ccxt_exchange.fetch_ohlcv(security_item.code,
                                                       timeframe=ccxt_level,
                                                       limit=limit)

                # always ignore the latest one,because it's not finished
                for kdata in kdatas[0:-1]:
                    current_timestamp = kdata[0]
                    if level == TradingLevel.LEVEL_1DAY:
                        current_timestamp = to_time_str(current_timestamp)

                    kdata_json = {
                        'timestamp': to_pd_timestamp(current_timestamp),
                        'open': kdata[1],
                        'high': kdata[2],
                        'low': kdata[3],
                        'close': kdata[4],
                        'volume': kdata[5],
                        'name': security_item.name,
                        'provider': 'ccxt',
                        'level': level.value
                    }
                    kdata_list.append(kdata_json)

                return kdata_list
            except Exception as e:
                logger.exception("record_kdata for security:{} failed".format(
                    security_item.id))
        else:
            logger.warning("exchange:{} not support fetchOHLCV".format(
                security_item.exchange))
Esempio n. 6
0
    def generate_request_param(self, security_item, start, end, size,
                               timestamp):
        if self.start_timestamp:
            start = max(self.start_timestamp, to_pd_timestamp(start))

        return {
            'security_item': security_item,
            'start_timestamp': to_time_str(start),
            'size': size,
            'level': self.level,
            'ccxt_level': self.ccxt_trading_level
        }
Esempio n. 7
0
    def generate_request_param(self, security_item, start, end, size,
                               timestamp):
        if self.start_timestamp:
            start = max(self.start_timestamp, to_pd_timestamp(start))
        if size < 20:
            size = 20

        return {
            'security_item': security_item,
            'start_timestamp': to_time_str(start),
            'size': size
        }
Esempio n. 8
0
def is_trading_date(security_type, exchange, timestamp: pd.Timestamp):
    if type(security_type) == str:
        security_type = SecurityType(security_type)
    if type(timestamp) == str:
        timestamp = to_pd_timestamp(timestamp)

    # just ignore 00:00
    # the_date = date_and_time(timestamp, '09:00')

    if security_type == SecurityType.stock:
        return (timestamp.weekday() != 5) and (timestamp.weekday() != 6)

    return True
Esempio n. 9
0
    def request(self, url=None, method='get', param=None, path_fields=None):
        security_item = param['security_item']
        size = param['size']
        ccxt_account: CCXTAccount = param['ccxt_account']

        ccxt_exchange = ccxt_account.get_ccxt_exchange(security_item.exchange)

        if ccxt_exchange.has['fetchTrades']:
            limit = ccxt_account.get_tick_limit(security_item.exchange)

            limit = min(size, limit)

            kdata_list = []

            try:
                trades = ccxt_exchange.fetch_trades(security_item.code,
                                                    limit=limit)

                # always ignore the latest one,because it's not finished
                for trade in trades[0:-1]:
                    kdata_json = {
                        'securityId': security_item.id,
                        'name': security_item.name,
                        'provider': 'ccxt',
                        # 'id': trade['id'],
                        'level': 'tick',
                        'order': trade['order'],
                        'timestamp': to_pd_timestamp(trade['timestamp']),
                        'price': trade['price'],
                        'volume': trade['amount'],
                        'direction': trade['side'],
                        'orderType': trade['type'],
                        'turnover': trade['price'] * trade['amount']
                    }
                    kdata_list.append(kdata_json)

                return kdata_list
            except Exception as e:
                logger.exception("record_kdata for security:{} failed".format(
                    security_item.id))
        else:
            logger.warning("exchange:{} not support fetchOHLCV".format(
                security_item.exchange))
Esempio n. 10
0
def coin_finished_timestamp(timestamp: pd.Timestamp, level: TradingLevel):
    timestamp = to_pd_timestamp(timestamp)

    return timestamp.minute % level.to_minute() == 0