Beispiel #1
0
def QA_fetch_stock_day_adv(
        code,
        __start, __end,
        if_drop_index=False,
        collections=QA_Setting.client.quantaxis.stock_day):
    '获取股票日线'
    __start = str(__start)[0:10]
    __end = str(__end)[0:10]
    
    if isinstance(code, str):
        print(code)
        if QA_util_date_valid(__end) == True:
            __data = []
            for item in collections.find({
                'code': str(code)[0:6], "date_stamp": {
                    "$lte": QA_util_date_stamp(__end),
                    "$gte": QA_util_date_stamp(__start)}}):
                __data.append([str(item['code']), float(item['open']), float(item['high']), float(
                    item['low']), float(item['close']), float(item['vol']), item['date']])
            __data = DataFrame(__data, columns=[
                'code', 'open', 'high', 'low', 'close', 'volume', 'date'])
            __data['date'] = pd.to_datetime(__data['date'])
            return QA_DataStruct_Stock_day(__data.query('volume>1').set_index(['date', 'code'], drop=if_drop_index))
        else:
            QA_util_log_info('something wrong with date')
    elif isinstance(code, list):
        return QA_DataStruct_Stock_day(pd.concat(QA_fetch_stocklist_day(code, [__start, __end])).query('volume>1').set_index(['date', 'code'], drop=if_drop_index))
def QA_fetch_stock_day_adv(
        code,
        start='all', end=None,
        if_drop_index=False,
        collections=DATABASE.stock_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())

    if isinstance(code, str):
        if QA_util_date_valid(end) == True:
            __data = []
            for item in collections.find({
                'code': str(code)[0:6], "date_stamp": {
                    "$lte": QA_util_date_stamp(end),
                    "$gte": QA_util_date_stamp(start)}}):
                __data.append([str(item['code']), float(item['open']), float(item['high']), float(
                    item['low']), float(item['close']), float(item['vol']), float(item['amount']), item['date']])
            __data = DataFrame(__data, columns=[
                'code', 'open', 'high', 'low', 'close', 'volume', 'amount', 'date'])
            __data['date'] = pd.to_datetime(__data['date'])
            return QA_DataStruct_Stock_day(__data.query('volume>1').set_index(['date', 'code'], drop=if_drop_index).sort_index())
        else:
            QA_util_log_info('something wrong with date')
    elif isinstance(code, list):
        return QA_DataStruct_Stock_day(pd.concat(QA_fetch_stocklist_day(code, [start, end])).query('volume>1').set_index(['date', 'code'], drop=if_drop_index))
def QA_fetch_index_quant_pre_adv(code, start="all", end=None, format='pd'):
    '获取股票量化机器学习数据查询接口'
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    # code checking
    if start == 'all' or start == None:
        start = '2008-01-01'
        end = QA_util_today_str()
        data = QA_fetch_index_quant_pre(code, start, end)
        return QA_DataStruct_Stock_day(data)
    else:
        data = QA_fetch_index_quant_pre(code, start, end)
        return QA_DataStruct_Stock_day(data)
Beispiel #4
0
    def get_stock_day_qfq_adv(self, codelist, start, end):

        res = self.get_stock_day_qfq(codelist, start, end).reset_index()
        return QA_DataStruct_Stock_day(
            res.assign(amount=res.total_turnover,
                       code=res.order_book_id).set_index(['date', 'code']),
            if_fq='qfq')
Beispiel #5
0
def QA_fetch_stocklist_day_adv(
        code,
        __start, __end,
        if_drop_index=False,
        collections=QA_Setting.client.quantaxis.stock_day):
    '获取股票日线'
    return QA_DataStruct_Stock_day(pd.concat(QA_fetch_stocklist_day(code, [__start, __end])).query('volume>1').set_index(['date', 'code'], drop=if_drop_index))
Beispiel #6
0
def QA_fetch_stock_day_adv(code,
                           start='all',
                           end=None,
                           if_drop_index=False,
                           collections=DATABASE.stock_day):
    '''
    :param code:  股票代码
    :param start: 开始日期
    :param end:   结束日期
    :param if_drop_index:
    :param collections: 默认数据库
    :return: 如果股票代码不存 或者开始结束日期不存在 在返回 None ,合法返回 QA_DataStruct_Stock_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_stock_day(code, start, end, format='pd')
    if res is None:
        print("code=%s , start=%s, end=%s QA_fetch_stock_day return None" %
              (code, start, end))
        return None
    else:
        return QA_DataStruct_Stock_day(
            res.set_index(['date', 'code'], drop=if_drop_index))
def QA_fetch_stock_fianacial_adv(
    code,
    start='all',
    end=None,
    if_drop_index=True,
):
    '获取财报TTM'
    #code= [code] if isinstance(code,str) else code
    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 = QA_util_today_str()

    res = QA_fetch_stock_fianacial(code, start, end, format='pd')
    if res is None:
        #  todo 报告是代码不合法,还是日期不合法
        print(
            "QA Error QA_fetch_stock_fianacial_adv parameter code=%s , start=%s, end=%s call QA_fetch_stock_fianacial_adv 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_fianacial_adv set index 'datetime, code' return None")
        #     return
        return QA_DataStruct_Stock_day(res_reset_index)
def QA_fetch_stock_day_adv(
        code,
        start='all', end=None,
        if_drop_index=False,
        # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉
        collections=DATABASE.stock_day):
    '''

    :param code:  股票代码
    :param start: 开始日期
    :param end:   结束日期
    :param if_drop_index:
    :param collections: 默认数据库
    :return: 如果股票代码不存 或者开始结束日期不存在 在返回 None ,合法返回 QA_DataStruct_Stock_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_stock_day(code, start, end, format='pd')
    if res is None:
        # 🛠 todo 报告是代码不合法,还是日期不合法
        print("💢 Error QA_fetch_stock_day_adv parameter code=%s , start=%s, end=%s call QA_fetch_stock_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("💢 Error QA_fetch_stock_day_adv set index 'datetime, code' return None")
        #     return None
        return QA_DataStruct_Stock_day(res_reset_index)
Beispiel #9
0
def QA_fetch_stocklist_day_adv(
        code,
        start, end=None,
        if_drop_index=False,
        collections=DATABASE.stock_day):
    '获取股票日线'
    return QA_DataStruct_Stock_day(pd.concat(QA_fetch_stocklist_day(code, [start, end])).query('volume>1').set_index(['date', 'code'], drop=if_drop_index).sort_index())
def QA_fetch_stock_target_adv(code,
                              start="all",
                              end=None,
                              type='close',
                              format='pd'):
    '获取股票量化机器学习数据查询接口'
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    # code checking
    if start == 'all' or start == None:
        start = '2008-01-01'
        end = QA_util_today_str()
        data = QA_fetch_stock_target(code, start, end, type=type)
        return QA_DataStruct_Stock_day(data)
    else:
        data = QA_fetch_stock_target(code, start, end, type=type)
        return QA_DataStruct_Stock_day(data)
def QA_fetch_stocklist_day_adv(
        code,
        start='all', end=None,
        if_drop_index=False,
        collections=DATABASE.stock_day):
    '获取股票日线'

    if start == 'all':
        start = '1990-01-01'
        end = str(datetime.date.today())
    return QA_DataStruct_Stock_day(pd.concat(QA_fetch_stocklist_day(code, [start, end])).query('volume>1').set_index(['date', 'code'], drop=if_drop_index).sort_index())
def QA_fetch_index_alpha_adv(code,
                             start="all",
                             end=None,
                             format='pd',
                             collections=DATABASE.index_alpha):
    '获取股票财报日历'
    #code= [code] if isinstance(code,str) else code
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    # code checking
    if start == 'all' or start == None:
        start = '2005-01-01'
        end = QA_util_today_str()
        data = QA_fetch_index_alpha(code, start, end)
        return QA_DataStruct_Stock_day(data)
    else:
        data = QA_fetch_index_alpha(code, start, end)
        return QA_DataStruct_Stock_day(data)
def QA_fetch_stock_quant_data_adv(code,
                                  start="all",
                                  end=None,
                                  block=True,
                                  format='pd',
                                  collections=DATABASE.stock_quant_data):
    '获取股票量化机器学习最终指标V1'
    #code= [code] if isinstance(code,str) else code
    end = start if end is None else end
    start = str(start)[0:10]
    end = str(end)[0:10]

    # code checking
    if start == 'all' or start == None:
        start = '2008-01-01'
        end = QA_util_today_str()
        data = QA_fetch_stock_quant_data(code, start, end, block)
        return QA_DataStruct_Stock_day(data)
    else:
        data = QA_fetch_stock_quant_data(code, start, end, block)
        return QA_DataStruct_Stock_day(data)
Beispiel #14
0
    def test_data_struct_stock(self):
        code = '000001'
        days = 365 * 1.2
        start = datetime.datetime.now() - datetime.timedelta(days)
        end = datetime.datetime.now() - datetime.timedelta(0)
        df = qm.get(code, start, end)
        if df is not None:
            df = df.set_index(['date', 'code'], drop=True)
        ds = qd().dataStruct(df)
        ds2 = QA_DataStruct_Stock_day(df)

        self.assertIsInstance(ds, QA_DataStruct_Stock_day)
        self.assertIsInstance(ds2, QA_DataStruct_Stock_day)
Beispiel #15
0
def QA_fetch_get_stock_indicator(code, start_date, end_date, type='day'):
    if type == 'day':
        start = QA_util_get_pre_trade_date(start_date, 200)
        rng1 = pd.Series(pd.date_range(start_date, end_date,
                                       freq='D')).apply(lambda x: str(x)[0:10])
        try:
            data = QA_fetch_stock_day_adv(code, start, end_date)
            data = data.to_qfq()
        except:
            print("No data")
    elif type == 'week':
        start = QA_util_get_pre_trade_date(start_date, 200)
        rng1 = pd.Series(pd.date_range(start_date, end_date,
                                       freq='D')).apply(lambda x: str(x)[0:10])
        try:
            data = QA_fetch_stock_day_adv(code, start, end_date)
            data = data.to_qfq()
            data = QA_DataStruct_Stock_day(
                data.data.groupby('code', sort=True).apply(ohlc, 7))
        except:
            print("No data")

    elif type == 'month':
        start = QA_util_get_pre_trade_date(start_date, 220)
        rng1 = pd.Series(pd.date_range(start_date, end_date,
                                       freq='D')).apply(lambda x: str(x)[0:10])
        try:
            data = QA_fetch_stock_day_adv(code, start, end_date)
            data = data.to_qfq()
            data = QA_DataStruct_Stock_day(
                data.data.groupby('code', sort=True).apply(ohlc, 30))
        except:
            print("No data")
    if data == None:
        return None
    else:
        data = get_indicator(data, rng1)
        return (data)
Beispiel #16
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:
        print("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)
def QA_fetch_stock_day_adv(code,
                           start='all',
                           end=None,
                           if_drop_index=False,
                           collections=DATABASE.stock_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())

    return QA_DataStruct_Stock_day(
        QA_fetch_stock_day(code, start, end,
                           format='pd').set_index(['date', 'code'],
                                                  drop=if_drop_index))
Beispiel #18
0
def QA_fetch_get_index_indicator(code, start_date, end_date, type='day'):
    if type == 'day':
        start = QA_util_get_pre_trade_date(start_date, 180)
        rng1 = pd.Series(pd.date_range(start_date, end_date,
                                       freq='D')).apply(lambda x: str(x)[0:10])
        try:
            data = QA.QA_fetch_index_day(
                code, start, end_date,
                format='pd').reset_index(drop=True).set_index(['date', 'code'])
            data = QA_DataStruct_Stock_day(data)
        except:
            print("No data")
    elif type == 'week':
        start = QA_util_get_pre_trade_date(start_date, 187)
        rng1 = pd.Series(pd.date_range(start_date, end_date,
                                       freq='D')).apply(lambda x: str(x)[0:10])
        try:
            data = QA.QA_fetch_index_day(
                code, start, end_date,
                format='pd').reset_index(drop=True).set_index(['date', 'code'])
            data = QA_DataStruct_Stock_day(
                data.groupby('code', sort=True).apply(ohlc, 7))
        except:
            print("No data")

    elif type == 'month':
        start = QA_util_get_pre_trade_date(start_date, 210)
        rng1 = pd.Series(pd.date_range(start_date, end_date,
                                       freq='D')).apply(lambda x: str(x)[0:10])
        try:
            data = QA.QA_fetch_index_day(
                code, start, end_date,
                format='pd').reset_index(drop=True).set_index(['date', 'code'])
            data = QA_DataStruct_Stock_day(
                data.groupby('code', sort=True).apply(ohlc, 30))
        except:
            print("No data")
    if data == None:
        return None
    else:
        data = get_indicator(data, rng1)
        return (data)
Beispiel #19
0
 def get_stock_day(self, codelist, start, end):
     codelist = promise_list(codelist)
     if 'XS' not in codelist[0]:
         codelist = pd.Series(codelist).apply(lambda x: x + '.XSHE' if x[
             0] != '6' else x + '.XSHG').tolist()
     columns_raw = [
         'date', 'order_book_id', 'num_trades', 'limit_up', 'limit_down',
         'open', 'high', 'low', 'close', 'volume', 'total_turnover'
     ]
     res = self.client.query_dataframe(
         "SELECT * FROM quantaxis.stock_cn_day  WHERE ((`date` >= '{}')) \
                      AND (`date` <= '{}') AND (`order_book_id` IN ({}))".
         format(start, end, "'{}'".format(
             "','".join(codelist)))).loc[:, columns_raw].drop_duplicates(
                 ['date', 'order_book_id'])
     return QA_DataStruct_Stock_day(
         res.assign(date=pd.to_datetime(res.date),
                    code=res.order_book_id,
                    amount=res.total_turnover).set_index(['date', 'code'
                                                          ]).sort_index())
def QA_fetch_stock_day_full_adv(date):
    '返回全市场某一天的数据'
    return QA_DataStruct_Stock_day(QA_fetch_stock_full(date, 'pd').set_index(['date', 'code'], drop=False))