def test_ticker_selection(self): policy = StockSelectionPolicy() policy.ignore_new_stock_period = dt.timedelta(days=360) policy.select_st = False selector = StockTickerSelector(policy=policy, db_interface=self.db_interface) dates = [dt.datetime(2020, 1, 7), dt.datetime(2020, 12, 28)] ret = selector.generate_index(dates=dates) print(ret)
def test_compact_factor(self): compact_factor = CompactFactor('证券名称', self.db_interface) print(compact_factor.get_data(dates=[dt.datetime(2015, 5, 15)])) policy = StockSelectionPolicy(select_st=True) print( compact_factor.get_data( dates=[dt.datetime(2015, 5, 15)], ticker_selector=StockTickerSelector(policy)))
def setUp(self): set_global_config('config.json') self.data_reader = AShareDataReader() forward_return = self.data_reader.forward_return factors = self.data_reader.log_cap ticker_selector = StockTickerSelector(StockSelectionPolicy()) market_cap = self.data_reader.stock_free_floating_market_cap start_date = dt.datetime(2020, 8, 1) end_date = dt.datetime(2021, 2, 1) dates = TradingCalendar().first_day_of_month(start_date, end_date) self.t = CrossSectionalPortfolioAnalysis(forward_return, factors=factors, dates=dates, market_cap=market_cap, ticker_selector=ticker_selector) self.t.cache()
def test_beta_factor(self): ids: Union[str, Sequence[str]] = ['000001.SZ', '600000.SH'] dates: Sequence[dt.datetime] = [ dt.datetime(2020, 1, 15), dt.datetime(2020, 5, 13) ] look_back_period: int = 60 min_trading_days: int = 40 policy = StockSelectionPolicy( ignore_new_stock_period=dt.timedelta(days=365), ignore_st=True) ticker_selector = StockTickerSelector(policy) beta_factor = BetaFactor(db_interface=self.db_interface) print( beta_factor.get_data(dates, ids, look_back_period=look_back_period, min_trading_days=min_trading_days)) print( beta_factor.get_data(dates, ticker_selector=ticker_selector, look_back_period=look_back_period, min_trading_days=min_trading_days))
import datetime as dt import sys from AShareData import AShareDataReader, set_global_config from AShareData.factor_compositor import FactorPortfolio, FactorPortfolioPolicy from AShareData.utils import StockSelectionPolicy if __name__ == '__main__': set_global_config(sys.argv[1]) data_reader = AShareDataReader() stock_selection_policy = StockSelectionPolicy() stock_selection_policy.ignore_new_stock_period = 244 stock_selection_policy.ignore_st = True stock_selection_policy.ignore_pause = True policy = FactorPortfolioPolicy() policy.bins = [5, 10] policy.stock_selection_policy = stock_selection_policy policy.start_date = dt.datetime(2010, 1, 1) policy.industry = data_reader.industry('申万', 1) policy.weight = data_reader.stock_free_floating_market_cap policy.name = data_reader.beta.name policy.factor = data_reader.beta sub_port = FactorPortfolio(factor_portfolio_policy=policy) sub_port.update()