Exemplo n.º 1
0
 def _inner(universe, start_time, end_time):
     '''
     上证综指
     '''
     sql = sql_tmplate.replace('code', index_code).replace('price_type', price_type)
     data = fdgetter.get_db_data(sql, cols=('close', 'time'), start_time=start_time,
                                 end_time=end_time, add_stockcode=False)
     # pdb.set_trace()
     data = pd.DataFrame(np.repeat([data.close.values], len(universe), axis=0).T,
                         index=data.time, columns=sorted(universe))
     checkdata_completeness(data, start_time, end_time)
     check_indexorder(data)
     return data
Exemplo n.º 2
0
def get_adjfactor(universe, start_time, end_time):
    '''
    股票的复权因子
    '''
    sql = '''
        SELECT A.ExDiviDate, A.RatioAdjustingFactor, M.SecuCode
        FROM QT_AdjustingFactor A, SecuMain M
        WHERE
            A.InnerCode = M.InnerCode AND
            M.secuMarket in (83, 90) AND
            M.SECUCATEGORY = 1
        ORDER BY M.SecuCode ASC, A.ExDiviDate ASC
        '''
    data = fdgetter.get_db_data(sql, cols=('time', 'data', 'code'), add_stockcode=False)
    data['code'] = data.code.apply(datatoolkits.add_suffix)
    by_code = data.groupby('code')
    tds = dateshandle.get_tds(start_time, end_time)
    data = by_code.apply(datatoolkits.map_data, days=tds, fromNowOn=True,
                         fillna={'code': lambda x: x.code.iloc[0], 'data': lambda x: 1})
    data = data.reset_index(drop=True)
    data = data.pivot_table('data', index='time', columns='code')
    data = data.loc[:, sorted(universe)].fillna(1)    # 因为新股大多数情况下没有分红记录
    assert check_indexorder(data), 'Error, data order is mixed!'
    assert checkdata_completeness(data, start_time, end_time), "Error, data missed!"
    return data
Exemplo n.º 3
0
def get_ctargetprice(universe, start_time, end_time):
    '''
    获取一致预期目标价数据
    '''
    sql = '''
    SELECT TARGET_PRICE, CON_DATE, STOCK_CODE
    FROM CON_FORECAST_SCHEDULE
    WHERE
        CON_DATE >= CAST(\'{start_time}\' AS datetime) AND
        CON_DATE <= CAST(\'{end_time}\' AS datetime)
    '''
    data = fdgetter.get_db_data(sql,
                                start_time=start_time,
                                end_time=end_time,
                                cols=('data', 'time', 'code'),
                                db=fdgetter.zyyx,
                                add_stockcode=False)
    # pdb.set_trace()
    data['code'] = data.code.apply(datatoolkits.add_suffix)
    data = data.pivot_table('data', index='time', columns='code')
    data = data.loc[:, sorted(universe)]
    assert check_indexorder(data), 'Error, data order is mixed!'
    assert checkdata_completeness(data, start_time,
                                  end_time), "Error, data missed!"
    return data
Exemplo n.º 4
0
 def _inner(universe, start_time, end_time):
     data = fdgetter.get_db_data(transed_sql, cols=cols, start_time=start_time,
                                 end_time=end_time, add_stockcode=False)
     data['code'] = data.code.apply(datatoolkits.add_suffix)
     if len(data.columns) == 4:
         data.loc[data.data == 0, 'data'] = data['prevclose']
         data.drop('prevclose', inplace=True, axis=1)
     data = data.pivot_table('data', index='time', columns='code')
     data = data.loc[:, sorted(universe)]
     assert check_indexorder(data), 'Error, data order is mixed!'
     assert checkdata_completeness(data, start_time, end_time), "Error, data missed!"
     return data
Exemplo n.º 5
0
 def inner(universe, start_time, end_time):
     new_start = tds_shift(start_time, 130)
     data = fdgetter.get_db_data(sql, code=index_code, cols=('code', 'weight', 'time'),
                                 add_stockcode=False, start_time=new_start, end_time=end_time)
     data.code = data.code.apply(datatoolkits.add_suffix)
     # pdb.set_trace()
     data = data.pivot_table('weight', index='time', columns='code')
     tds = dateshandle.get_tds(new_start, end_time)
     data = datatoolkits.map_data(data.reset_index(), days=tds, fromNowOn=True)
     data = data.set_index('time')
     data_tds = dateshandle.get_tds(start_time, end_time)
     data = data.reindex(data_tds)
     data = data.loc[:, sorted(universe)] / 100
     assert check_indexorder(data), "Mixed index order"
     return data
Exemplo n.º 6
0
 def _inner(universe, start_time, end_time):
     data = fdgetter.get_db_data(transed_sql, cols=('data', 'time', 'code'),
                                 add_stockcode=False)
     data['code'] = data.code.apply(datatoolkits.add_suffix)
     data = data.drop_duplicates().sort_values(['code', 'time'])  # 此处假设若时间相同则股本数量相同
     by_code = data.groupby('code')
     tds = dateshandle.get_tds(start_time, end_time)
     data = by_code.apply(datatoolkits.map_data, days=tds, fromNowOn=True,
                          fillna={'code': lambda x: x.code.iloc[0]})
     data = data.reset_index(drop=True)
     data = data.pivot_table('data', index='time', columns='code')
     data = data.loc[:, sorted(universe)]
     assert check_indexorder(data), 'Error, data order is mixed!'
     assert checkdata_completeness(data, start_time, end_time), "Error, data missed!"
     return data
Exemplo n.º 7
0
 def _inner(universe, start_time, end_time):
     offset = 366 * n + 180
     # pdb.set_trace()
     new_start = pd.to_datetime(start_time) - pd.Timedelta('%d day' % offset)
     data = fdgetter.get_db_data(sql, start_time=new_start, end_time=end_time,
                                 cols=('update_time', 'rpt_date', 'code', 'data'),
                                 add_stockcode=False)
     data['code'] = data.code.apply(datatoolkits.add_suffix)
     data = _handle_dbdata(data, start_time, end_time, fdmutils.cal_yr, col_name='data',
                           offset=n)
     # pdb.set_trace()
     data = data.loc[:, sorted(universe)]
     assert check_indexorder(data), 'Error, data order is mixed!'
     assert checkdata_completeness(data, start_time, end_time), "Error, data missed!"
     return data
Exemplo n.º 8
0
 def _inner(universe, start_time, end_time):
     new_start = pd.to_datetime(start_time) - pd.Timedelta('540 day')
     data = fdgetter.get_db_data(sql_template, start_time=new_start, end_time=end_time,
                                 cols=('update_time', 'rpt_date', 'code', 'data'),
                                 add_stockcode=False)
     data['code'] = data.code.apply(datatoolkits.add_suffix)
     by_code = data.groupby('code')
     data = by_code.apply(fdmutils.get_observabel_data).reset_index(drop=True)
     by_cno = data.groupby(['code', 'obs_time'])
     data = by_cno.apply(fdmutils.cal_ttm, col_name='data').reset_index()\
         .rename(columns={'obs_time': 'time'})
     tds = dateshandle.get_tds(start_time, end_time)
     # pdb.set_trace()
     data = data.groupby('code').apply(datatoolkits.map_data, days=tds,
                                       fillna={'code': lambda x: x.code.iloc[0]})
     data = data.reset_index(drop=True)
     data = data.pivot_table('data', index='time', columns='code')
     data = data.loc[:, sorted(universe)]
     assert check_indexorder(data), 'Error, data order is mixed!'
     assert checkdata_completeness(data, start_time, end_time), "Error, data missed!"
     return data