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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| from __future__ import print_function, absolute_import, unicode_literals from gm.api import * ''' 本策略每隔1个月定时触发计算SHSE.000300成份股的过去一天EV/EBITDA值并选取30只EV/EBITDA值最小且大于零的股票 对不在股票池的股票平仓并等权配置股票池的标的 并用相应的CFFEX.IF对应的真实合约等额对冲 回测数据为:SHSE.000300和他们的成份股和CFFEX.IF对应的真实合约 回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00 注意:本策略仅供参考,实际使用中要考虑到期货和股票处于两个不同的账户,需要人为的保证两个账户的资金相同。 ''' def init(context): schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00') context.percentage_stock = 0.4 context.percentage_futures = 0.4 def algo(context): now = context.now last_day = get_previous_trading_date(exchange='SHSE', date=now) stock300 = get_history_constituents(index='SHSE.000300', start_date=last_day, end_date=last_day)[0]['constituents'].keys() index_futures = get_continuous_contracts(csymbol='CFFEX.IF', start_date=last_day, end_date=last_day)[-1]['symbol'] not_suspended_info = get_history_instruments(symbols=stock300, start_date=now, end_date=now) not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']] fin = get_fundamentals(table='trading_derivative_indicator', symbols=not_suspended_symbols, start_date=now, end_date=now, fields='EVEBITDA', filter='EVEBITDA>0', order_by='EVEBITDA', limit=30, df=True) fin.index = fin.symbol positions = context.account().positions() for position in positions: symbol = position['symbol'] sec_type = get_instrumentinfos(symbols=symbol)[0]['sec_type'] if sec_type == SEC_TYPE_FUTURE and symbol != index_futures: order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market, position_side=PositionSide_Short) print('市价单平不在标的池的', symbol) elif symbol not in fin.index: order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market, position_side=PositionSide_Long) print('市价单平不在标的池的', symbol) percent = context.percentage_stock / len(fin.index) for symbol in fin.index: order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market, position_side=PositionSide_Long) print(symbol, '以市价单调多仓到仓位', percent) ratio = get_history_instruments(symbols=index_futures, start_date=last_day, end_date=last_day)[0]['margin_ratio'] percent = context.percentage_futures * ratio order_target_percent(symbol=index_futures, percent=percent, order_type=OrderType_Market, position_side=PositionSide_Short) print(index_futures, '以市价单调空仓到仓位', percent) if __name__ == '__main__': ''' strategy_id策略ID,由系统生成 filename文件名,请与本文件名保持一致 mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST token绑定计算机的ID,可在系统设置-密钥管理中生成 backtest_start_time回测开始时间 backtest_end_time回测结束时间 backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST backtest_initial_cash回测初始资金 backtest_commission_ratio回测佣金比例 backtest_slippage_ratio回测滑点比例 ''' run(strategy_id='strategy_id', filename='main.py', mode=MODE_BACKTEST, token='token_id', backtest_start_time='2017-07-01 08:00:00', backtest_end_time='2017-10-01 16:00:00', backtest_adjust=ADJUST_PREV, backtest_initial_cash=10000000, backtest_commission_ratio=0.0001, backtest_slippage_ratio=0.0001)
|