def market_data_panel(self, freq="D"): start, end = self.date_range df = DataAPI.candle(self.universe, start=start, end=end, freq=freq).transpose(2, 1, 0) if freq == "D": df.major_axis = df.major_axis.normalize() return df
def read_benchmark(start, end, index_code="000300.XSHG", freq="D"): """ 获取指数行情数据 :param start:起始时间(datetime) :param end:结束时间(datetime) :param index_code:指数代码(str) :param freq:数据频率(str) :return:指数行情。返回一个benchmark对象 包含五个属性。"index", "close", "open", "high", "low"。 "index":一个MultiIndex Series的序列。索引为date (level 0) 和 asset (level 1),包含一列factor值。其中asset的值固定是index_code, factor值固定是1。用于fxdayu_alphaman.selector.selector_analysis.get_stocklist_mean_return中 作为stock_list参数来计算指数收益。 "close":收盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的收盘价。 "open":开盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的开盘价。 "high":最高价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最高价。 "low":最低价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最低价。 """ benchmark = Benchmark() benchmark_value = DataAPI.candle((index_code,), freq=freq, start=start, end=end) benchmark.open = benchmark_value.minor_xs("open") if len(benchmark.open)==0: raise ValueError benchmark.high = benchmark_value.minor_xs("high") benchmark.low = benchmark_value.minor_xs("low") benchmark.close = benchmark_value.minor_xs("close") benchmark.open.index.name = "date" benchmark.high.index.name = "date" benchmark.low.index.name = "date" benchmark.close.index.name = "date" benchmark.index = pd.DataFrame(data=benchmark.open.index) benchmark.index["asset"] = index_code benchmark.index["factor"] = 1 benchmark.index = benchmark.index.set_index(["date", "asset"]) return benchmark
def get_price_data(pool, start, end, max_window=10): data = DataAPI.candle( tuple(pool), "D", start=start - datetime.timedelta(days=max_window), end=end + datetime.timedelta(days=max_window)) data = data.replace(to_replace=0, value=np.NaN) return data
def read_LFLO(symbols, start, end): """ 获取对数流通市值 :param symbols: 一组股票代码(list),形式为通用标准(编码.交易所 如["000001.XSHE","600000.XSHG"]) :param start: 开始时间(datetime) :param end:结束时间(datetime) :return: 对数流通市值。(pandas.Dataframe) index为datetime,columns为股票代码,值为对应股票对应时间的对数流通市值。 """ LFLO = DataAPI.factor(tuple(symbols), fields="LFLO", start=start, end=end).minor_xs("LFLO") return LFLO
def market_data(self): start, end = self.date_range df = DataAPI.candle(self.universe, fields=["close"], start=start, end=end, freq=self._freq).loc[:, :, "close"].T df.index.name = "order_book_id" series = df.stack() series.name = "close" return series
def benchmark_rets(self, code="000300.XSHG", freq="D", isIndex=True): start, end = self.date_range adjust = None if not isIndex: adjust = "before" benchmark = DataAPI.candle(code, freq=freq, fields="close", start=start, end=end, adjust=adjust) benchmark_rets = benchmark["close"].pct_change() benchmark_rets.name = "benchmark_rets" if freq == "D": benchmark_rets.index = benchmark_rets.index.normalize() return benchmark_rets
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
def read_benchmark(start, end, index_code="000300.XSHG", freq="D"): """ 获取指数行情数据 :param start:起始时间(datetime) :param end:结束时间(datetime) :param index_code:指数代码(str) :param freq:数据频率(str) :return:指数行情。返回一个benchmark对象 包含五个属性。"index", "close", "open", "high", "low"。 "index":一个MultiIndex Series的序列。索引为date (level 0) 和 asset (level 1),包含一列factor值。其中asset的值固定是index_code, factor值固定是1。用于fxdayu_alphaman.selector.selector_analysis.get_stocklist_mean_return中 作为stock_list参数来计算指数收益。 "close":收盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的收盘价。 "open":开盘价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的开盘价。 "high":最高价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最高价。 "low":最低价。(pandas.Dateframe ),index为datetime,column.name 为index_code,值为对应指数的最低价。 """ benchmark = Benchmark() try: benchmark_value = DataAPI.candle((index_code, ), freq=freq, start=start, end=end) benchmark.open = benchmark_value.minor_xs("open") if len(benchmark.open) == 0: raise ValueError benchmark.high = benchmark_value.minor_xs("high") benchmark.low = benchmark_value.minor_xs("low") benchmark.close = benchmark_value.minor_xs("close") benchmark.open.index.name = "date" benchmark.high.index.name = "date" benchmark.low.index.name = "date" benchmark.close.index.name = "date" except: index_value = ts.get_k_data(code=index_code[0:6], start=start.strftime("%Y-%m-%d"), end=end.strftime("%Y-%m-%d"), ktype=freq, index=True) date = index_value.pop('date') index_value["date"] = pd.to_datetime(date + " 15:00:00", format='%Y-%m-%d %H:%M:%S') benchmark.close = index_value[["date", "close"]] benchmark.open = index_value[["date", "open"]] benchmark.high = index_value[["date", "high"]] benchmark.low = index_value[["date", "low"]] benchmark.close.columns = ["date", index_code] benchmark.open.columns = ["date", index_code] benchmark.high.columns = ["date", index_code] benchmark.low.columns = ["date", index_code] benchmark.close = benchmark.close.set_index("date") benchmark.open = benchmark.open.set_index("date") benchmark.high = benchmark.high.set_index("date") benchmark.low = benchmark.low.set_index("date") benchmark.index = pd.DataFrame(data=benchmark.open.index) benchmark.index["asset"] = index_code benchmark.index["factor"] = 1 benchmark.index = benchmark.index.set_index(["date", "asset"]) return benchmark
from fxdayu_data import DataAPI print(DataAPI.candle("000001.XSHE"))