def execute(self, pool, start, end, data=None, data_config=None): """ 计算选股结果 :param pool: list 待选股票池 data:进行选股操作所需要的数据 start:datetime 选股索引范围起始时间 end:datetime 选股索引范围结束时间 :return:selector_result:A MultiIndex Series indexed by date (level 0) and asset (level 1), containing the values mean whether choose the asset or not. ----------------------------------- date | asset | ----------------------------------- | AAPL | 1 ----------------------- | BA | 1 ----------------------- 2014-01-01 | CMG | 1 ----------------------- | DAL | 0 ----------------------- | LULU | -1 ----------------------- """ if data is None: data = DataAPI.get(symbols=tuple(pool), start=start - datetime.timedelta(days=self.max_window), end=end, **data_config) factor = Factor_Volume003().get_factor(data, update=True) quantiles = self.get_quantiles(factor) selector_result = quantiles.copy() selector_result[:] = 0 selector_result[quantiles == self.choose_quantile] = 1 return selector_result.loc[start:end]
def execute(self, pool, start, end, data=None, data_config=None): """ 计算选股结果 :param pool: list 待选股票池 data:进行选股操作所需要的数据 start:datetime 选股索引范围起始时间 end:datetime 选股索引范围结束时间 :return:selector_result:A MultiIndex Series indexed by date (level 0) and asset (level 1), containing the values mean whether choose the asset or not. ----------------------------------- date | asset | ----------------------------------- | AAPL | 1 ----------------------- | BA | 1 ----------------------- 2014-01-01 | CMG | 1 ----------------------- | DAL | 0 ----------------------- | LULU | -1 ----------------------- """ if data is None: data = DataAPI.get(symbols=tuple(pool), start=start - datetime.timedelta(days=self.max_window), end=end, **data_config) selector_result = list( map(self.calculate_MACD_signal, data.iteritems())) selector_result = pd.concat(selector_result, axis=1).stack() selector_result.index.names = ["date", "asset"] return selector_result.loc[start:end]
# 配置选股器所在包路径 Admin.PACKAGE_NAME = "fxdayu_alphaman.examples.factors" # 初始选股范围设置 initial_codes = standard_code_style( json.load(open('test_stock_pool.json'))["test_stock_pool"]) data_config = {"freq": "D", "api": "candle", "adjust": "after"} # 测试参数设置 start = datetime.datetime(2017, 1, 1) end = datetime.datetime(2017, 4, 18, 15) periods = (1, 5, 10) # 获取数据 data = DataAPI.get(symbols=tuple(initial_codes), start=start - datetime.timedelta(days=100), end=end, **data_config) prices = data.minor_xs("close") def manage_factors_value_test(factor_name_list, data_config_dict): # admin测试 -获得多个因子结果 factor_admin = Admin(*factor_name_list) factors_dict = factor_admin.get_all_factors_value( initial_codes, start, end, all_factors_data_config_dict=data_config_dict) return factor_admin, factors_dict
def instantiate_factor_and_get_factor_value(factor_name, pool, start, end, Factor=None, data=None, data_config={"freq": "D", "api": "candle", "adjust": "after"}, para_dict=None, ): """ 计算某个因子指定时间段的因子值 (可支持直接用factor_name加载到对应因子的算法) :param factor_name: 因子名称(str) 需确保传入的factor_name、因子的类名、对应的module文件名一致(不含.后缀),因子才能正确加载 :param pool: 股票池范围(list),如:["000001.XSHE","600300.XSHG",......] :param start: 起始时间 (datetime) :param end: 结束时间 (datetime) :param Factor (optional): 因子(factor.factor.Factor object),可选.可以输入一个设计好的Factor类来执行计算. :param data (optional): 计算因子需用到的数据,根据计算需求自行指定。(可选) :param data_config (optional): 在data参数为None的情况下(不传入自定义数据), 可通过该参数调用fxdayu_data api 访问到数据 (dict), 与data参数二选一。 :param para_dict (optional): 外部指定因子里所用到的参数集(dict),为空则不修改原有参数。 形如:{"fast":5,"slow":10} :return: factor_value:因子值 格式为一个MultiIndex Series,索引(index)为date(level 0)和asset(level 1), 包含一列factor值。形如: ----------------------------------- date | asset | ----------------------------------- | AAPL | 0.5 ----------------------- | BA | -1.1 ----------------------- 2014-01-01 | CMG | 1.7 ----------------------- | DAL | -0.1 ----------------------- | LULU | 2.7 ----------------------- """ from fxdayu_data import DataAPI import datetime # 实例化因子类 if Factor is None: factor = _get_factor(factor_name, Admin.PACKAGE_NAME)() else: factor = Factor # 接收外部传入的参数 if para_dict: for para in list(para_dict.keys()): setattr(factor, para, para_dict[para]) if data is None: pn_data = DataAPI.get(symbols=tuple(pool), start=start - datetime.timedelta(days=factor.max_window), end=end, **data_config) else: pn_data = data # 因子计算结果获取 factor_value = factor.get_factor(pn_data, update=True) return factor_value