動量策略:
如果某只股票在前一段時期表現較好,那么下一段時期該股票扔將有良好表現。
反轉策略:
如果某只股票在前一段時期表現不好,那么下一段時期該股票將會反轉,即表現變好。
動量策略&反轉策略
計算股票池中所有股票在前一段時間的收益率
選擇收益率最大(最小)的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就是反轉策略