def corr_update(corr, codes): ra_index = base_ra_index.find(corr['ra_index_id']) if ra_index is None: click.echo(click.style( "unknown index [%s]for calc corr!" % (corr['ra_index_id']), fg="yellow")) return False yesterday = (datetime.now() - timedelta(days=1)); enddate = yesterday.strftime("%Y-%m-%d") # # 加载指数数据 # index_code = ra_index['ra_code'] if corr['ra_date_type'] == 1: df_nav_index = DBData.db_index_value_daily('2015-10-08', enddate, codes=[index_code]) else: df_nav_index = DBData.db_index_value('2015-10-08', enddate, codes=[index_code]) df_inc_index = df_nav_index.pct_change().fillna(0.0) # # 加载基金列表 # df_fund = base_ra_fund.load(codes=codes) data = [] with click.progressbar(length=len(df_fund.index), label=('update corr for corr %d' % (corr['globalid'])).ljust(30)) as bar: for _,fund in df_fund.iterrows(): bar.update(1) tmp = corr_update_fund(corr, fund, df_inc_index) if tmp is not None: data.append([ corr['globalid'], fund['globalid'], fund['ra_code'], "%.4f" % (tmp), ]) df_new = pd.DataFrame(data, columns=['ra_corr_id', 'ra_fund_id', 'ra_fund_code', 'ra_corr']) df_new = df_new.set_index(['ra_corr_id', 'ra_fund_id']) db = database.connection('base') # 加载旧数据 t2 = Table('ra_corr_fund', MetaData(bind=db), autoload=True) columns2 = [ t2.c.ra_corr_id, t2.c.ra_fund_id, t2.c.ra_fund_code, t2.c.ra_corr, ] stmt_select = select(columns2, (t2.c.ra_corr_id == corr['globalid'])) if codes is not None: stmt_select = stmt_select.where(t2.c.ra_fund_code.in_(codes)) df_old = pd.read_sql(stmt_select, db, index_col=['ra_corr_id', 'ra_fund_id']) if not df_old.empty: df_old['ra_corr'] = df_old['ra_corr'].map("{:.4f}".format) # 更新数据库 database.batch(db, t2, df_new, df_old, timestamp=True)
def find_asset_name(asset_id): if asset_id.strip().isdigit(): #print int(asset_id) asset_id = int(asset_id) xtype = asset_id / 10000000 if 12 == xtype: record = base_ra_index.find(asset_id) return record[2].strip() else: return None elif asset_id.strip().startswith('ERI'): record = base_exchange_rate_index.find(asset_id) return record[2].strip() else: return None
def match_asset_pool(gid): if gid.isdigit(): xtype = int(gid) / 10000000 else: xtype = re.sub(r'([\d]+)', '', gid).strip() result = 0 if xtype == 1: result = gid elif xtype == 5: xtab = { '4111010': 11110100, #大盘资产修型 '41110101': 11110100, #大盘资产修型(实验) '41110102': 11110100, #巨潮大盘指数修型 '41110105': 11110217, #价值资产修型 '41110200': 11110200, #小盘资产修型 '41110201': 11110200, #小盘资产修型(实验) '41110202': 11110200, #巨潮小盘指数修型 '41110205': 11110213, #上涨资产修型 '41110206': 11110214, #震荡资产修型 '41110207': 11110215, #下跌资产修型 '41110208': 11110216, #成长资产修型 '41120200': 11120200, #标普资产修型 '41120500': 11120500, #恒生资产修型 '41120501': 11120500, #恒生资产修型(实验) '41120502': 11120500, #恒生指数修型 '41400100': 11400100, #黄金资产修型 '41400101': 11400100, #黄金资产修型(实验) '41400102': 11400100, #黄金指数修型 } if gid in xtab: result = xtab[gid] elif xtype == 12: # # 指数资产 # xtab = { '120000001': 11110100, # 沪深300, 大盘池 '120000002': 11110200, # 中证500, 小盘池 '120000003': 11110100, # 巨潮大盘, 大盘池 '120000004': 11110200, # 巨潮小盘, 小盘池 '120000010': 11210100, # 国债指数,利率债池 '120000011': 11210200, # 中证信用债指数,信用债池 '120000014': 11400100, # 沪金指数, 黄金池 '120000028': 11400300, # 原油指数, 原油池 '120000029': 11400400, # 商品指数, 商品池 '120000031': 11400500, # 房地产指数, 房地产池 } if gid in xtab: result = xtab[gid] else: asset = base_ra_index.find(gid) name = asset['ra_name'] if '标普' in name: result = 11120200 elif '黄金' in name: result = 11400100 elif '恒生' in name: result = 11120500 elif xtype == 'ERI': xtab = { 'ERI000001': 11120200, # 标普500人民币计价指数, 标普基金池 'ERI000002': 11120500, # 恒生指数人民币计价指数, 恒生基金池 } if gid in xtab: result = xtab[gid] else: pass return result