示例#1
0
def QA_fetch_stock_realtime_adv(
    code=None,
    num=1,
    collections=DATABASE.get_collection('realtime_{}'.format(
        datetime.date.today())),
    verbose=True,
):
    '''
    返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量
    :param code:
    :param num:
    :param collections:  realtime_XXXX-XX-XX 每天实时时间
    :return: DataFrame
    '''
    if code is not None:
        # code 必须转换成list 去查询数据库
        if isinstance(code, str):
            code = [code]
        elif isinstance(code, list):
            pass
        else:
            QA_util_log_error(
                "QA Error QA_fetch_stock_realtime_adv parameter code is not List type or String type"
            )

        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:
                QA_util_log_error(
                    "QA Error QA_fetch_stock_realtime_adv find parameter code={} num={} collection={} return NOne"
                    .format(code, num, collections))
            return

        data = pd.DataFrame(items_from_collections)
        data_set_index = data.set_index(['datetime', 'code'],
                                        drop=False).drop(['_id'], axis=1)

        return data_set_index
    else:
        QA_util_log_error(
            "QA Error QA_fetch_stock_realtime_adv parameter code is None")
示例#2
0
def QA_fetch_index_transaction_adv(code,
                                   start,
                                   end=None,
                                   frequence='tick',
                                   if_drop_index=True,
                                   collections=DATABASE.index_transaction):
    '''

    :param code:
    :param start:
    :param end:
    :param if_drop_index:
    :param collections:
    :return:
    '''
    end = start if end is None else end
    if len(start) == 10:
        start = '{} 09:30:00'.format(start)

    if len(end) == 10:
        end = '{} 15:00:00'.format(end)

    if start == end:
        # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_stock_min, 不支持start end是相等的
        QA_util_log_error(
            "QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! "
            % (code, start, end))
        return None

    # 🛠 todo 报告错误 如果开始时间 在 结束时间之后

    res = QA_fetch_index_transaction(code,
                                     start,
                                     end,
                                     format='pd',
                                     frequence=frequence,
                                     collections=collections)
    if res is None:
        QA_util_log_error(
            "QA Error QA_fetch_index_transaction_adv parameter code=%s , start=%s, end=%s frequence=%s call QA_fetch_index_transaction return None"
            % (code, start, end, frequence))
        return None
    else:
        res_set_index = res.set_index(['datetime', 'code'], drop=if_drop_index)
        # if res_set_index is None:
        #     print("QA Error QA_fetch_stock_min_adv set index 'datetime, code' return None")
        #     return None
        return QA_DataStruct_Index_transaction(res_set_index)
示例#3
0
def QA_fetch_cryptocurrency_day_adv(code,
                                    start,
                                    end=None,
                                    if_drop_index=True,
                                    collections=DATABASE.cryptocurrency_day):
    '''
    '获取数字加密资产日线'
    :param code:
    :param start:  字符串str 开始日期 eg 2011-01-01
    :param end:  字符串str 结束日期 eg 2011-05-01
    :param if_drop_index: Ture False , dataframe drop index or not
    :param collections:  mongodb 数据库
    :return:
    '''
    '获取数字加密资产日线'
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    # 🛠 todo 报告错误 如果开始时间 在 结束时间之后
    # 🛠 todo 如果相等

    res = QA_fetch_cryptocurrency_day(code,
                                      start,
                                      end,
                                      format='pd',
                                      collections=collections)
    if res is None:
        QA_util_log_error(
            "QA Error QA_fetch_cryptocurrency_day_adv parameter symbol=%s start=%s end=%s call QA_fetch_cryptocurrency_day return None"
            % (code, start, end))
    else:
        res_set_index = res.set_index(['date', 'code'])
        return QA_DataStruct_CryptoCurrency_day(res_set_index)
示例#4
0
def QA_fetch_cryptocurrency_min_adv(code,
                                    start,
                                    end=None,
                                    frequence='1min',
                                    if_drop_index=True,
                                    collections=DATABASE.cryptocurrency_min):
    '''
    '获取数字加密资产分钟线'
    :param symbol:
    :param start:
    :param end:
    :param frequence:
    :param if_drop_index:
    :param collections:
    :return:
    '''
    if frequence in ['1min', '1m']:
        frequence = '1min'
    elif frequence in ['5min', '5m']:
        frequence = '5min'
    elif frequence in ['15min', '15m']:
        frequence = '15min'
    elif frequence in ['30min', '30m']:
        frequence = '30min'
    elif frequence in ['60min', '60m']:
        frequence = '60min'

    # __data = [] 没有使用

    end = start if end is None else end
    if len(start) == 10:
        start = '{} 00:00:00'.format(start)
    if len(end) == 10:
        end = '{} 23:59:59'.format(end)

    # 🛠 todo 报告错误 如果开始时间 在 结束时间之后

    # if start == end:
    # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_cryptocurrency_min_adv, 不支持start end是相等的
    # print("QA Error QA_fetch_cryptocurrency_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % (code, start, end))
    # return None

    res = QA_fetch_cryptocurrency_min(code,
                                      start,
                                      end,
                                      format='pd',
                                      frequence=frequence,
                                      collections=collections)
    if res is None:
        QA_util_log_error(
            "QA Error QA_fetch_cryptocurrency_min_adv parameter symbol=%s start=%s end=%s frequence=%s call QA_fetch_cryptocurrency_min return None"
            % (code, start, end, frequence))
    else:
        res_reset_index = res.set_index(['datetime', 'code'],
                                        drop=if_drop_index)
        # if res_reset_index is None:
        #     print("QA Error QA_fetch_cryptocurrency_min_adv set index 'date, code' return None")
        return QA_DataStruct_CryptoCurrency_min(res_reset_index)
示例#5
0
def QA_fetch_stock_day_full_adv(date):
    '''
    '返回全市场某一天的数据'
    :param date:
    :return: QA_DataStruct_Stock_day类 型数据
    '''
    # 🛠 todo 检查日期data参数
    res = QA_fetch_stock_full(date, 'pd')
    if res is None:
        QA_util_log_error(
            "QA Error QA_fetch_stock_day_full_adv parameter date=%s call QA_fetch_stock_full return None"
            % (date))
        return None
    else:
        res_set_index = res.set_index(['date', 'code'])
        # if res_set_index is None:
        #     print("QA Error QA_fetch_stock_day_full set index 'date, code' return None")
        return QA_DataStruct_Stock_day(res_set_index)
示例#6
0
def QA_fetch_bond_day_adv(
    code,
    start='all',
    end=None,
    if_drop_index=True,
    # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉
    collections=DATABASE.bond_day):
    '''

    :param code:  股票代码
    :param start: 开始日期
    :param end:   结束日期
    :param if_drop_index:
    :param collections: 默认数据库
    :return: 如果债券代码不存 或者开始结束日期不存在 在返回 None ,合法返回 QA_DataStruct_Bond_day 数据
    '''
    '获取债券日线'
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    if start == 'all':
        start = '1990-01-01'
        end = str(datetime.date.today())

    res = QA_fetch_bond_day(code,
                            start,
                            end,
                            format='pd',
                            collections=collections)
    if res is None:
        # 🛠 todo 报告是代码不合法,还是日期不合法
        QA_util_log_error(
            "QA Error QA_fetch_bond_day_adv parameter code=%s , start=%s, end=%s call QA_fetch_bond_day return None"
            % (code, start, end))
        return None
    else:
        res_reset_index = res.set_index(['date', 'code'], drop=if_drop_index)
        # if res_reset_index is None:
        #     print("QA Error QA_fetch_stock_day_adv set index 'datetime, code' return None")
        #     return None
        return QA_DataStruct_Bond_day(res_reset_index)
示例#7
0
def QA_fetch_index_day_adv(
    code,
    start,
    end=None,
    if_drop_index=True,
    # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉
    collections=DATABASE.index_day):
    '''
    :param code: code:  字符串str eg 600085
    :param start:  字符串str 开始日期 eg 2011-01-01
    :param end:  字符串str 结束日期 eg 2011-05-01
    :param if_drop_index: Ture False , dataframe drop index or not
    :param collections:  mongodb 数据库
    :return:
    '''
    '获取指数日线'
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    # 🛠 todo 报告错误 如果开始时间 在 结束时间之后
    # 🛠 todo 如果相等

    res = QA_fetch_index_day(code,
                             start,
                             end,
                             format='pd',
                             collections=collections)
    if res is None:
        QA_util_log_error(
            "QA Error QA_fetch_index_day_adv parameter code=%s start=%s end=%s call QA_fetch_index_day return None"
            % (code, start, end))
        return None
    else:
        res_set_index = res.set_index(['date', 'code'], drop=if_drop_index)
        # if res_set_index is None:
        #     print("QA Error QA_fetch_index_day_adv set index 'date, code' return None")
        #     return None
        return QA_DataStruct_Index_day(res_set_index)
示例#8
0
def QA_fetch_bond_min_adv(
    code,
    start,
    end=None,
    frequence='1min',
    if_drop_index=True,
    # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉
    collections=DATABASE.bond_min):
    '''
    '获取债券分钟线'
    :param code:  字符串str eg 600085
    :param start: 字符串str 开始日期 eg 2011-01-01
    :param end:   字符串str 结束日期 eg 2011-05-01
    :param frequence: 字符串str 分钟线的类型 支持 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m 类型
    :param if_drop_index: Ture False , dataframe drop index or not
    :param collections: mongodb 数据库
    :return: QA_DataStruct_Bond_min 类型
    '''
    if frequence in ['1min', '1m']:
        frequence = '1min'
    elif frequence in ['5min', '5m']:
        frequence = '5min'
    elif frequence in ['15min', '15m']:
        frequence = '15min'
    elif frequence in ['30min', '30m']:
        frequence = '30min'
    elif frequence in ['60min', '60m']:
        frequence = '60min'
    else:
        QA_util_log_error(
            "QA Error QA_fetch_bond_min_adv parameter frequence=%s is none of 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m"
            % frequence)
        return None

    # __data = [] 未使用

    end = start if end is None else end
    if len(start) == 10:
        start = '{} 09:30:00'.format(start)

    if len(end) == 10:
        end = '{} 15:00:00'.format(end)

    if start == end:
        # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_stock_min, 不支持start end是相等的
        QA_util_log_error(
            "QA Error QA_fetch_bond_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! "
            % (code, start, end))
        return None

    # 🛠 todo 报告错误 如果开始时间 在 结束时间之后

    res = QA_fetch_bond_min(code,
                            start,
                            end,
                            format='pd',
                            frequence=frequence,
                            collections=collections)
    if res is None:
        QA_util_log_error(
            "QA Error QA_fetch_bond_min_adv parameter code=%s , start=%s, end=%s frequence=%s call QA_fetch_bond_min return None"
            % (code, start, end, frequence))
        return None
    else:
        res_set_index = res.set_index(['datetime', 'code'], drop=if_drop_index)
        # if res_set_index is None:
        #     print("QA Error QA_fetch_stock_min_adv set index 'datetime, code' return None")
        #     return None
        return QA_DataStruct_Bond_min(res_set_index)
示例#9
0
 def on_ordererror(self, direction, offset, price, volume):
     QA_util_log_error('order Error ')