Esempio n. 1
0
def QA_fetch_stock_day(code,
                       start,
                       end,
                       format='numpy',
                       frequence='day',
                       collections=DATABASE.stock_day):
    '获取股票日线'
    start = str(start)[0:10]
    end = str(end)[0:10]

    if QA_util_date_valid(end) == True:

        __data = []
        cursor = collections.find({
            'code': str(code)[0:6],
            "date_stamp": {
                "$lte": QA_util_date_stamp(end),
                "$gte": QA_util_date_stamp(start)
            }
        })
        if format in ['json', 'dict']:
            return [QA_util_dict_remove_key(data, '_id') for data in cursor]

        for item in cursor:
            __data.append([
                str(item['code']),
                float(item['open']),
                float(item['high']),
                float(item['low']),
                float(item['close']),
                float(item['vol']), item['date']
            ])

        # 多种数据格式
        if format in ['n', 'N', 'numpy']:
            __data = numpy.asarray(__data)
        elif format in ['list', 'l', 'L']:
            __data = __data
        elif format in ['P', 'p', 'pandas', 'pd']:

            __data = DataFrame(__data,
                               columns=[
                                   'code', 'open', 'high', 'low', 'close',
                                   'volume', 'date'
                               ])

            __data['date'] = pd.to_datetime(__data['date'])
            __data = __data.set_index('date', drop=False)

        return __data
    else:
        QA_util_log_info('something wrong with date')
Esempio n. 2
0
def QA_fetch_account(message={}, db=DATABASE):
    """get the account

    Arguments:
        query_mes {[type]} -- [description]

    Keyword Arguments:
        collection {[type]} -- [description] (default: {DATABASE})

    Returns:
        [type] -- [description]
    """
    collection = DATABASE.account
    return [QA_util_dict_remove_key(res, '_id') for res in collection.find(message)]
Esempio n. 3
0
def QA_fetch_user(user_cookie, db=DATABASE):
    """
    get the user

    Arguments:
        user_cookie : str the unique cookie_id for a user
    Keyword Arguments:
        db: database for query

    Returns:
        list ---  [ACCOUNT]
    """
    collection = DATABASE.account

    return [QA_util_dict_remove_key(res, '_id') for res in collection.find({'user_cookie': user_cookie})]
Esempio n. 4
0
def GQSignal_fetch_flu_cunsum_min(start,
                                end,
                                frequency='60min',
                                market_type=QA.MARKET_TYPE.STOCK_CN,
                                portfolio='myportfolio', 
                                format='numpy',
                                ui_log=None, 
                                ui_progress=None):
    """
    '获取全市场股票指标日线处于上升形态的数量'

    Keyword Arguments:
        client {[type]} -- [description] (default: {DATABASE})
    """
    start = str(start)[0:10]
    end = str(end)[0:10]
    #code= [code] if isinstance(code,str) else code

    client = QASETTING.client[AKA.SYSTEM_NAME]
    # 同时写入横表和纵表,减少查询困扰
    #coll_day = client.get_collection(
    #        'indices_{}'.format(datetime.date.today()))
    try:
        if (market_type == QA.MARKET_TYPE.STOCK_CN):
            #coll_indices = client.stock_cn_indices_min
            coll_indices = client.get_collection('stock_cn_indices_{}'.format(portfolio))
        elif (market_type == QA.MARKET_TYPE.INDEX_CN):
            #coll_indices = client.index_cn_indices_min
            coll_indices = client.get_collection('index_cn_indices_{}'.format(portfolio))
        elif (market_type == QA.MARKET_TYPE.FUND_CN):
            #coll_indices = client.future_cn_indices_min
            coll_indices = client.get_collection('fund_cn_indices_{}'.format(portfolio))
        elif (market_type == QA.MARKET_TYPE.FUTURE_CN):
            #coll_indices = client.future_cn_indices_min
            coll_indices = client.get_collection('future_cn_indices_{}'.format(portfolio))
        elif (market_type == QA.MARKET_TYPE.CRYPTOCURRENCY):
            #coll_indices = client.cryptocurrency_indices_min
            coll_indices = client.get_collection('cryptocurrency_indices_{}'.format(portfolio))
        else:
            QA_util_log_info('WTF IS THIS! \n ', ui_log=ui_log)
            return False
    except Exception as e:
        QA_util_log_info(e)
        QA_util_log_info('WTF IS THIS! \n ', ui_log=ui_log)
        return False

    if QA_util_date_valid(end):
        cursor = coll_indices.aggregate([{
            '$match': {
                'time_stamp': {
                    "$lte": QA_util_date_stamp(end),
                    "$gte": QA_util_date_stamp(start)
                    },
                'type': frequency,
                } 
             },
             {"$group" : {
                 '_id': {
                     'datetime': "$datetime",
                     },
                 'datetime': {
                     '$first': "$datetime"},
                 'time_stamp': {
                     '$first': "$time_stamp"},
                 FLD.FLU_POSITIVE: {                            # 上涨趋势通道个股家数
                     '$sum': '${}'.format(FLD.FLU_POSITIVE)},  
                 FLD.ML_FLU_TREND: {                            # “技术调整”个股家数
                     '$sum': '${}'.format(FLD.ML_FLU_TREND)}, 
                 ST.BOOTSTRAP_I: {
                     '$sum': { 
                         '$cond': ['${}'.format(ST.BOOTSTRAP_I), 1, 0] }},
                 ST.DEADPOOL: {
                     '$sum': { 
                         '$cond': ['${}'.format(ST.DEADPOOL), 1, 0] }},
                 FLD.FLU_POSITIVE_MASTER: {
                     '$sum': { 
                         '$cond': ['${}'.format(FLD.FLU_POSITIVE_MASTER), 1, 0] }},
                 FLD.FLU_NEGATIVE_MASTER: {
                     '$sum': { 
                         '$cond': ['${}'.format(FLD.FLU_NEGATIVE_MASTER), 1, 0] }},
                 ST.VOLUME_FLOW_BOOST: {
                     '$sum': '${}'.format(ST.VOLUME_FLOW_BOOST)},  
                 ST.VOLUME_FLOW_BOOST_BONUS: {
                     '$sum': { 
                         '$cond': ['${}'.format(ST.VOLUME_FLOW_BOOST_BONUS), 1, 0] }},
                 ST.CLUSTER_GROUP_TOWARDS: {
                     '$sum': '${}'.format(ST.CLUSTER_GROUP_TOWARDS)},
                 FLD.ATR_Stopline: {
                     '$sum': '${}'.format(FLD.ATR_Stopline)},
                 FLD.ATR_SuperTrend: {
                     '$sum': '${}'.format(FLD.ATR_SuperTrend)},
                 FLD.DEA: {
                     '$sum': '${}'.format(FLD.DEA)},
                 FLD.MAXFACTOR_CROSS: {
                     '$sum': '${}'.format(FLD.MAXFACTOR_CROSS)},
                 FLD.TALIB_PATTERNS: {
                     '$sum': '${}'.format(FLD.TALIB_PATTERNS)},
                 FLD.ADXm_Trend: {
                     '$sum': '${}'.format(FLD.ADXm_Trend)},
                 FLD.Volume_HMA: {
                     '$sum': '${}'.format(FLD.Volume_HMA)},
                 FLD.RENKO_TREND_S: {
                     '$sum': '${}'.format(FLD.RENKO_TREND_S)},
                 FLD.RENKO_TREND_L: {
                     '$sum': '${}'.format(FLD.RENKO_TREND_L)},
                 'total': {                                   # 全市场个股家数
                     '$sum': 1}                                             
                 }
              },
              {'$sort':{"_id.datetime":1}}])
        try:
            res = pd.DataFrame([QA_util_dict_remove_key(item, '_id') for item in cursor])
            res = res.assign(datetime=pd.to_datetime(res.datetime)).drop_duplicates((['datetime'])).set_index('datetime',
                                    drop=False)
        except:
            res = None
        if format in ['P', 'p', 'pandas', 'pd']:
            return res
        elif format in ['json', 'dict']:
            return QA_util_to_json_from_pandas(res)
        # 多种数据格式
        elif format in ['n', 'N', 'numpy']:
            return numpy.asarray(res)
        elif format in ['list', 'l', 'L']:
            return numpy.asarray(res).tolist()
        else:
            print("QA Error GQSignal_fetch_flu_cunsum_min format parameter %s is none of  \"P, p, pandas, pd , json, dict , n, N, numpy, list, l, L, !\" " % format)
            return None
    else:
        QA_util_log_info('QA Error GQSignal_fetch_flu_cunsum_min data parameter start=%s end=%s is not right' % (start,
               end))