一、小市值测量
0.前言
非常naive的想法,选取市值最低的20只股票,每次交易先平仓,再按比例平均买入这些股票
两种写法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| start = '2013-01-01' # 回测起始时间 end = '2014-01-01' # 回测结束时间 universe = DynamicUniverse('A').apply_filter(Factor.LCAP.nsmall(20)) # 证券池,支持股票、基金、期货、指数四种资产 benchmark = 'HS300' # 策略参考标准 freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测 refresh_rate = Monthly(1) # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟 # 配置账户信息,支持多资产多账户 accounts = { 'fantasy_account': AccountConfig(account_type='security', capital_base=10000000) } def initialize(context): pass # 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次 def handle_data(context):
account = context.get_account('fantasy_account') universe = context.get_universe(exclude_halt=False) account.close_all_positions() for stock in universe: account.order_pct_to(stock,0.05)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| start = '2013-01-01' # 回测起始时间 end = '2014-01-01' # 回测结束时间 universe = DynamicUniverse('A') # 证券池,支持股票、基金、期货、指数四种资产 benchmark = 'HS300' # 策略参考标准 freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测 refresh_rate = Monthly(1) # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟 # 配置账户信息,支持多资产多账户 accounts = { 'fantasy_account': AccountConfig(account_type='security', capital_base=10000000) } def initialize(context): pass # 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次 def handle_data(context): account = context.get_account('fantasy_account') universe = context.get_universe(exclude_halt=False) lcap_data = DataAPI.MktStockFactorsOneDayGet(context.previous_date,secID=universe,ticker=u"",field=['secID','LCAP'],pandas="1") lcap_data.set_index('secID',inplace=True) lcap_data.sort_values(by='LCAP',inplace=True) buy_list = lcap_data[:20].index account.close_all_positions() for stock in buy_list: account.order_pct_to(stock,0.05)
|