动量策略:
如果某只股票在前一段时期表现较好,那么下一段时期该股票扔将有良好表现。
反转策略:
如果某只股票在前一段时期表现不好,那么下一段时期该股票将会反转,即表现变好。
动量策略&反转策略
计算股票池中所有股票在前一段时间的收益率
选择收益率最大(最小)的N只股票调仓,动量策略选最大,反转策略选最小
from jqdata import * def initialize(context): set_benchmark('000300.XSHG') set_option('use_real_price', True) set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') g.N = 10 g.benchmark = '000300.XSHG' run_monthly(handle, 1) def handle(context): stocks = get_index_stocks(g.benchmark) df_close = history(30, field='close', security_list=list(stocks)).T df_close['ret'] = (df_close.iloc[:,-1]-df_close.iloc[:,0])/df_close.iloc[:,0] sorted_stocks = df_close.sort_values('ret', ascending=False).index to_hold = sorted_stocks[:g.N] for stock in context.portfolio.positions: if stock not in to_hold: order_target_value(stock, 0) to_buy = [stock for stock in to_hold if stock not in context.portfolio.positions] if len(to_buy) > 0: cash_per_stock = context.portfolio.available_cash / len(to_buy) for stock in to_buy: order_value(stock, cash_per_stock)
把里面的“sorted_stocks = df_close.sort_values('ret', ascending=False).index” False 改为True就是反转策略