コード例 #1
0
ファイル: StockCN_realtime.py プロジェクト: mylovage/GolemQT
def GQ_fetch_stock_realtime_adv(
    code=None,
    num=1,
    collections=client.get_collection('realtime_{}'.format(date.today())),
    verbose=True,
    suffix=False,
):
    '''
    返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量
    :param code:
    :param num:
    :param collections:  realtime_XXXX-XX-XX 每天实时时间
    :param suffix:  股票代码是否带沪深交易所后缀
    :return: DataFrame
    '''
    if code is not None:
        # code 必须转换成list 去查询数据库,因为五档数据用一个collection保存了股票,指数及基金,所以强制必须使用标准化代码
        if isinstance(code, str):
            code = [normalize_code(code)]
        elif isinstance(code, list):
            code = [normalize_code(symbol) for symbol in code]
            pass
        else:
            print(
                "QA Error GQ_fetch_stock_realtime_adv parameter code is not List type or String type"
            )
        #print(verbose, code)
        items_from_collections = [
            item for item in collections.find({'code': {
                '$in': code
            }},
                                              limit=num * len(code),
                                              sort=[('datetime',
                                                     pymongo.DESCENDING)])
        ]
        if (items_from_collections is None) or \
            (len(items_from_collections) == 0):
            if verbose:
                print(
                    "QA Error GQ_fetch_stock_realtime_adv find parameter code={} num={} collection={} return NOne"
                    .format(code, num, collections))
            return
        data = pd.DataFrame(items_from_collections)
        if (suffix == False):
            # 返回代码数据中是否包含交易所代码
            data['code'] = data.apply(lambda x: x.at['code'][:6], axis=1)
        data_set_index = data.set_index(['datetime', 'code'],
                                        drop=False).drop(['_id'], axis=1)

        return data_set_index
    else:
        print("QA Error GQ_fetch_stock_realtime_adv parameter code is None")
コード例 #2
0
ファイル: sub.py プロジェクト: waytrylee/GolemQ
def formater_l1_tick(code: str, l1_tick: dict) -> dict:
    """
    处理分发 Tick 数据,新浪和tdx l1 tick差异字段格式化处理
    """
    if ((len(code) == 6) and code.startswith('00')):
        l1_tick['code'] = normalize_code(code, l1_tick['now'])
    else:
        l1_tick['code'] = normalize_code(code)
    l1_tick['servertime'] = l1_tick['time']
    l1_tick['datetime'] = '{} {}'.format(l1_tick['date'], l1_tick['time'])
    l1_tick['price'] = l1_tick['now']
    l1_tick['vol'] = l1_tick['volume']
    del l1_tick['date']
    del l1_tick['time']
    del l1_tick['now']
    del l1_tick['name']
    del l1_tick['volume']
    #print(l1_tick)
    return l1_tick