def run_snapshot(self): date_str = self._cur_time.date_.strftime('%Y%m%d') equity1 = QueryTool(AShareEODPrices.S_DQ_PCTCHANGE, Engine.lu_cbaas_dev).\ filter(AShareEODPrices.S_INFO_WINDCODE == '000001.SZ').\ filter(AShareEODPrices.TRADE_DT == date_str).all() equity2 = QueryTool(AShareEODPrices.S_DQ_PCTCHANGE, Engine.lu_cbaas_dev).\ filter(AShareEODPrices.S_INFO_WINDCODE == '601398.SH').\ filter(AShareEODPrices.TRADE_DT == date_str).all() index_return = equity1[0][0] * 0.5 + equity2[0][0] * 0.5 self._result.update({self._cur_time.date_: index_return})
def get_fund_performance(engine, ftype='MF', fund_id=None, start=None, end=None, freq='d', date_col='date'): if freq not in ['d', 'w', 'm']: raise ValueError( '{!r} is not in frequency options ("d", "w", "m").'.format(freq)) if freq == 'd': table = DailyPerformance funds, start_date, end_date = _get_config_params( table, engine, ftype, fund_id, start, end) performance = QueryTool([DailyPerformance.fund_id, DailyPerformance.date_.label(date_col), (DailyPerformance.return_/100).label('return')], engine).\ filter(DailyPerformance.fund_id.in_(funds)).\ filter(DailyPerformance.date_ <= end).\ filter(DailyPerformance.date_ > start).\ all() elif freq == 'w': table = WeeklyPerformance funds, start_date, end_date = _get_config_params( table, engine, ftype, fund_id, start, end) performance = QueryTool([WeeklyPerformance.fund_id, WeeklyPerformance.month_end.label(date_col), (WeeklyPerformance.return_/100).label('return')], engine).\ filter(WeeklyPerformance.fund_id.in_(funds)).\ filter(WeeklyPerformance.month_end <= end).\ filter(WeeklyPerformance.month_end > start).\ all() elif freq == 'm': table = MonthlyPerformance funds, start_date, end_date = _get_config_params( table, engine, ftype, fund_id, start, end) performance = QueryTool([MonthlyPerformance.fund_id, MonthlyPerformance.month_end.label(date_col), (MonthlyPerformance.return_/100).label('return')], engine).\ filter(MonthlyPerformance.fund_id.in_(funds)).\ filter(MonthlyPerformance.month_end <= end).\ filter(MonthlyPerformance.month_end > start).\ all() else: raise NotImplementedError( 'Performance table with frequency of {!r} has yet implemented.'. format(freq)) return performance
def _get_fund_info(self): # cbaas_fund_info = DataFrame( # QueryTool([CBAASFund.S_INFO_WINDCODE.label('suntime_id'), # CBAASFund.id.label('cbaas_id'), # CBAASFund.strategy], # self._engine.lu_cbaas_test.cbaasquant).all() # ) # # suntime_fund_info = DataFrame( # QueryTool([SuntimeFundInfo.fund_id.label('suntime_id'), # SuntimeFundInfo.fund_name.label('fund_name'), # SuntimeFundInfo.foundation_date.label('foundation_date')], # self._engine.lu_cbaas_test.suntime) # ) # self._fund_info = cbaas_fund_info.merge(suntime_fund_info, # on='',) info = QueryTool([CBAASFund.S_INFO_WINDCODE.label('suntime_id'), CBAASFund.id.label('cbaas_id'), CBAASFund.strategy, SuntimeFundInfo.fund_id.label('suntime_id'), SuntimeFundInfo.fund_name.label('fund_name'), SuntimeFundInfo.foundation_date.label('foundation_date')], self._engine.lu_cbaas_dev).\ join(CBAASFund.S_INFO_WINDCODE == SuntimeFundInfo.fund_id).all() self._fund_info = info
def __init__(self): self._engine = Engine self._cur_time = QueryTool(TradeCalendar, self._engine.lu_cbaas_test.cbaas).\ filter(TradeCalendar.date_ == '1990-12-31').all()[0] self._fund_info = None self._get_fund_info()
def __init__(self, start, end): self._engine = Engine self._axis = QueryTool([TradeCalendar.date_, TradeCalendar.prev_trade_date, TradeCalendar.is_open, TradeCalendar.is_week_end, TradeCalendar.is_month_end, TradeCalendar.is_quarter_end, TradeCalendar.is_year_end, TradeCalendar.cal_end_of_month], self._engine.lu_cbaas_dev).\ filter(TradeCalendar.date_ >= start).\ filter(TradeCalendar.date_ < end).all()
def get_fund_info(engine, ftype=None): fund_types = _get_fund_type(ftype) return QueryTool([CBAASFund.id.label('fund_id'), CBAASFund.S_INFO_WINDCODE, CBAASFund.asset_class, CBAASFund.inception_date, CBAASFund.latest_nav_date], engine).\ filter(CBAASFund.valid == 1).\ filter(CBAASFund.asset_class.in_(fund_types)).\ all()
def _cur_avail_hf_info(self): info = QueryTool([CBAASFund.id.label('fund_id'), CBAASFund.fund_name, CBAASFund.strategy, FundCompany.id.label('company_id'), FundCompany.company_name], self._engine). \ join(CompanyFund).\ join(FundCompany).\ filter(CBAASFund.asset_class == 8).\ filter(CBAASFund.inception_date <= self.date).\ filter(CBAASFund.latest_nav_date >= self.date).all() return DataFrame(info)
def _get_config_params(performance_table, engine, ftype=None, fund_id=None, start=None, end=None): fund_types = _get_fund_type(ftype) if fund_id is None: funds = [ _id for _id, in QueryTool(CBAASFund.id, engine).filter( CBAASFund.asset_class.in_(fund_types)).filter( CBAASFund.valid == 1).all() ] elif not isinstance(fund_id, list): funds = list(fund_id) else: funds = fund_id if not start: start_date = QueryTool(func.min(performance_table.date_), engine).\ filter(performance_table.fund_id.in_(funds)).\ scaler().\ strftime('%Y-%m-%d') else: start_date = start if not end: end_date = QueryTool(func.max(performance_table.date_), engine). \ filter(performance_table.fund_id.in_(funds)). \ scaler(). \ strftime('%Y-%m-%d') else: end_date = end return funds, start_date, end_date
def _pure_rating_cal(self, funds, base_col_dict, factor_col, rating_name): cols = factor_col cols.append(base_col_dict['fund_id']) query_list = QueryTool(cols, self._engine).\ filter(base_col_dict['fund_id'].in_(funds)).\ filter(base_col_dict['date'] == self.date).\ all() if len(query_list) > 0: factor_value = DataFrame(query_list).set_index('fund_id').dropna() standardized = self._clean_and_standarize(factor_value) raw_rating = self._rating_cal(standardized.mean(axis=1), qcut, [0., .1, .325, .675, .9, 1.], labels=False, duplicates='drop') return raw_rating.to_frame(rating_name) else: return DataFrame(columns=[rating_name])
def _cur_avail_mf_info(self): info = QueryTool([CBAASFund.id.label('fund_id'), CBAASFund.fund_name, CBAASFund.strategy_category, Manager.id.label('manager_id'), Manager.first_name.label('manager_name'), FundCompany.id.label('company_id'), FundCompany.company_name], self._engine). \ join(ManagerFundHistory). \ join(Manager). \ join(FundCompany). \ filter(CBAASFund.asset_class == 10). \ filter(CBAASFund.inception_date <= self.date). \ filter(CBAASFund.latest_nav_date >= self.date). \ filter(or_(and_(ManagerFundHistory.start_date <= self.date, ManagerFundHistory.end_date >= self.date), and_(ManagerFundHistory.start_date <= self.date, ManagerFundHistory.end_date.is_(None)))).all() return DataFrame(info)
engine).\ filter(MonthlyPerformance.fund_id.in_(funds)).\ filter(MonthlyPerformance.month_end <= end).\ filter(MonthlyPerformance.month_end > start).\ all() else: raise NotImplementedError( 'Performance table with frequency of {!r} has yet implemented.'. format(freq)) return performance if __name__ == '__main__': from cbaasquant.data.utils.engine import Engine eng = Engine.lu_cbaas_dev # test = get_fund_performance(engine=eng, ftype='HF', start='2018-01-01', end='2018-01-08', freq='w') # from pandas import DataFrame # print(DataFrame(test)) test = QueryTool([CBAASFund.id, CBAASFund.asset_class, CBAASFund.inception_date, CBAASFund.latest_nav_date], eng). \ filter(CBAASFund.id == 473247). \ all() print(test)