logo资料库

经典策略-掘金量化.pdf

第1页 / 共44页
第2页 / 共44页
第3页 / 共44页
第4页 / 共44页
第5页 / 共44页
第6页 / 共44页
第7页 / 共44页
第8页 / 共44页
资料共44页,剩余部分请下载后查看
双均线策略(期货)
alpha对冲(股票+期货)
集合竞价选股(股票)
多因子选股(股票)
网格交易(期货)
指数增强(股票)
跨品种套利(期货)
跨期套利(期货)
日内回转交易(股票)
做市商交易(期货)
海龟交易法(期货)
行业轮动(股票)
机器学习(股票)
目 录 双均线策略(期货) alpha对冲(股票+期货) 集合竞价选股(股票) 多因子选股(股票) 网格交易(期货) 指数增强(股票) 跨品种套利(期货) 跨期套利(期货) 日内回转交易(股票) 做市商交易(期货) 海龟交易法(期货) 行业轮动(股票) 机器学习(股票) 本文档使用 掘金量化 构建 - 1 -
双均线策略(期货) 双均线策略(期货) 1. # coding=utf-8 2. from __future__ import print_function, absolute_import 3. from gm.api import * 4. import talib 5. import time 6. 7. ''' 8. 本策略以DCE.i1801为交易标的,根据其一分钟(即60s频度)bar数据建立双均线模型, 9. 短周期为30,长周期为60,当短期均线由上向下穿越长期均线时做空, 10. 当短期均线由下向上穿越长期均线时做多,每次开仓前先平掉所持仓位,再开仓。 11. 回测数据为:DCE.i1801的60s频度bar数据 12. 回测时间为:2017-09-01 09:00:00到2017-09-30 15:00:00 13. ''' 14. 15. def init(context): 16. context.FAST = 30 # 短周期 17. context.SLOW = 60 # 长周期 18. context.symbol = 'DCE.i1801' # 订阅&交易标的 19. context.period = context.SLOW + 1 # 订阅数据滑窗长度 20. subscribe(context.symbol, '60s', count=context.period) # 订阅行情 21. 22. def on_bar(context, bars): 23. print (bars[0].bob) 24. # 获取数据 25. prices = context.data('DCE.i1801', '60s', context.period, fields='close') 26. # 计算长短周期均线 27. fast_avg = talib.SMA(prices.values.reshape(context.period), context.FAST) 28. slow_avg = talib.SMA(prices.values.reshape(context.period), context.SLOW) 29. 30. # 均线下穿,做空 31. if slow_avg[-2] < fast_avg[-2] and slow_avg[-1] >= fast_avg[-1]: 32. # 平多仓 33. order_target_percent(symbol=context.symbol, percent=0, position_side=1, order_type=2) 34. # 开空仓 35. order_target_percent(symbol=context.symbol, percent=0.1, position_side=2, order_type=2) 36. 37. # 均线上穿,做多 38. if fast_avg[-2] < slow_avg[-2] and fast_avg[-1] >= slow_avg[-1]: 39. # 平空仓 40. order_target_percent(symbol=context.symbol, percent=0, position_side=2,order_type=2) 41. # 开多仓 42. order_target_percent(symbol=context.symbol, percent=0.1, position_side=1,order_type=2) 43. 44. 45. def on_execution_report(context, execrpt): 46. # 打印委托执行回报 47. print(execrpt) 本文档使用 掘金量化 构建 - 2 -
双均线策略(期货) 48. 49. 50. if __name__ == '__main__': 51. ''' 52. strategy_id策略ID,由系统生成 53. filename文件名,请与本文件名保持一致 54. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST 55. token绑定计算机的ID,可在系统设置-密钥管理中生成 56. backtest_start_time回测开始时间 57. backtest_end_time回测结束时间 58. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST 59. backtest_initial_cash回测初始资金 60. backtest_commission_ratio回测佣金比例 61. backtest_slippage_ratio回测滑点比例 62. ''' 63. run(strategy_id='strategy_id', 64. filename='main.py', 65. mode=MODE_BACKTEST, 66. token='token_id', 67. backtest_start_time='2017-09-01 09:00:00', 68. backtest_end_time='2017-09-30 15:00:00', 69. backtest_adjust=ADJUST_NONE, 70. backtest_initial_cash=10000000, 71. backtest_commission_ratio=0.0001, 72. backtest_slippage_ratio=0.0001) 本文档使用 掘金量化 构建 - 3 -
alpha对冲(股票+期货) alpha对冲(股票+期货) 1. # coding=utf-8 2. from __future__ import print_function, absolute_import, unicode_literals 3. from gm.api import * 4. 5. ''' 6. 本策略每隔1个月定时触发计算SHSE.000300成份股的过去的EV/EBITDA并选取EV/EBITDA大于0的股票 7. 随后平掉排名EV/EBITDA不在最小的30的股票持仓并等权购买EV/EBITDA最小排名在前30的股票 8. 并用相应的CFFEX.IF对应的真实合约等额对冲 9. 回测数据为:SHSE.000300和他们的成份股和CFFEX.IF对应的真实合约 10. 回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00 11. ''' 12. 13. 14. def init(context): 15. # 每月第一个交易日09:40:00的定时执行algo任务 16. schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00') 17. 18. # 设置开仓在股票和期货的资金百分比(期货在后面自动进行杠杆相关的调整) 19. context.percentage_stock = 0.4 20. context.percentage_futures = 0.4 21. 22. 23. def algo(context): 24. # 获取当前时刻 25. now = context.now 26. # 获取上一个交易日 27. last_day = get_previous_trading_date(exchange='SHSE', date=now) 28. # 获取沪深300成份股 29. stock300 = get_history_constituents(index='SHSE.000300', start_date=last_day, 30. end_date=last_day)[0] ['constituents'].keys() 31. # 获取上一个工作日的CFFEX.IF对应的合约 32. index_futures = get_continuous_contracts(csymbol='CFFEX.IF', start_date=last_day, end_date=last_day)[-1]['symbol'] 33. # 获取当天有交易的股票 34. not_suspended_info = get_history_instruments(symbols=stock300, start_date=now, end_date=now) 35. not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']] 36. # 获取成份股EV/EBITDA大于0并为最小的30个 37. fin = get_fundamentals(table='tq_sk_finindic', symbols=not_suspended_symbols, 38. start_date=now, end_date=now, fields='EVEBITDA', 39. filter='EVEBITDA>0', order_by='EVEBITDA', limit=30, df=True) 40. fin.index = fin.symbol 41. # 获取当前仓位 42. positions = context.account().positions() 43. # 平不在标的池或不为当前股指期货主力合约对应真实合约的标的 44. for position in positions: 45. symbol = position['symbol'] 46. sec_type = get_instrumentinfos(symbols=symbol)[0]['sec_type'] 本文档使用 掘金量化 构建 - 4 -
alpha对冲(股票+期货) 47. # 若类型为期货且不在标的池则平仓 48. if sec_type == SEC_TYPE_FUTURE and symbol != index_futures: 49. order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market, 50. position_side=PositionSide_Short) 51. print('市价单平不在标的池的', symbol) 52. elif symbol not in fin.index: 53. order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market, 54. position_side=PositionSide_Long) 55. print('市价单平不在标的池的', symbol) 56. 57. # 获取股票的权重 58. percent = context.percentage_stock / len(fin.index) 59. # 买在标的池中的股票 60. for symbol in fin.index: 61. order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market, 62. position_side=PositionSide_Long) 63. print(symbol, '以市价单调多仓到仓位', percent) 64. 65. # 获取股指期货的保证金比率 66. ratio = get_history_instruments(symbols=index_futures, start_date=last_day, end_date=last_day)[0]['margin_ratio'] 67. # 更新股指期货的权重 68. percent = context.percentage_futures * ratio 69. # 买入股指期货对冲 70. order_target_percent(symbol=index_futures, percent=percent, order_type=OrderType_Market, 71. position_side=PositionSide_Short) 72. print(index_futures, '以市价单调空仓到仓位', percent) 73. 74. 75. if __name__ == '__main__': 76. ''' 77. strategy_id策略ID,由系统生成 78. filename文件名,请与本文件名保持一致 79. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST 80. token绑定计算机的ID,可在系统设置-密钥管理中生成 81. backtest_start_time回测开始时间 82. backtest_end_time回测结束时间 83. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST 84. backtest_initial_cash回测初始资金 85. backtest_commission_ratio回测佣金比例 86. backtest_slippage_ratio回测滑点比例 87. ''' 88. run(strategy_id='strategy_id', 89. filename='main.py', 90. mode=MODE_BACKTEST, 91. token='token_id', 92. backtest_start_time='2017-07-01 08:00:00', 93. backtest_end_time='2017-10-01 16:00:00', 94. backtest_adjust=ADJUST_PREV, 95. backtest_initial_cash=10000000, 96. backtest_commission_ratio=0.0001, 97. backtest_slippage_ratio=0.0001) 本文档使用 掘金量化 构建 - 5 -
alpha对冲(股票+期货) 本文档使用 掘金量化 构建 - 6 -
集合竞价选股(股票) 集合竞价选股(股票) 1. # coding=utf-8 2. from __future__ import print_function, absolute_import, unicode_literals 3. from gm.api import * 4. 5. ''' 6. 本策略通过获取SHSE.000300沪深300的成份股数据并统计其30天内 7. 开盘价大于前收盘价的天数,并在该天数大于阈值10的时候加入股票池 8. 随后对不在股票池的股票平仓并等权配置股票池的标的,每次交易间隔1个月. 9. 回测数据为:SHSE.000300在2015-01-15的成份股 10. 回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00 11. ''' 12. 13. 14. def init(context): 15. # 每月第一个交易日的09:40 定时执行algo任务 16. schedule(schedule_func=algo, date_rule='1m', time_rule='09:40:00') 17. # context.count_bench累计天数阙值 18. context.count_bench = 10 19. # 用于对比的天数 20. context.count = 30 21. # 最大交易资金比例 22. context.ratio = 0.8 23. 24. 25. def algo(context): 26. # 获取当前时间 27. now = context.now 28. # 获取上一个交易日 29. last_day = get_previous_trading_date(exchange='SHSE', date=now) 30. # 获取沪深300成份股 31. context.stock300 = get_history_constituents(index='SHSE.000300', start_date=last_day, 32. end_date=last_day)[0] ['constituents'].keys() 33. # 获取当天有交易的股票 34. not_suspended_info = get_history_instruments(symbols=context.stock300, start_date=now, end_date=now) 35. not_suspended_symbols = [item['symbol'] for item in not_suspended_info if not item['is_suspended']] 36. 37. trade_symbols = [] 38. if not not_suspended_symbols: 39. print('没有当日交易的待选股票') 40. return 41. 42. for stock in not_suspended_symbols: 43. recent_data = history_n(symbol=stock, frequency='1d', count=context.count, fields='pre_close,open', 44. fill_missing='Last', adjust=ADJUST_PREV, end_time=now, df=True) 45. diff = recent_data['open'] - recent_data['pre_close'] 本文档使用 掘金量化 构建 - 7 -
集合竞价选股(股票) 46. # 获取累计天数超过阙值的标的池.并剔除当天没有交易的股票 47. if len(diff[diff > 0]) >= context.count_bench: 48. trade_symbols.append(stock) 49. 50. print('本次股票池有股票数目: ', len(trade_symbols)) 51. # 计算权重 52. percent = 1.0 / len(trade_symbols) * context.ratio 53. # 获取当前所有仓位 54. positions = context.account().positions() 55. # 如标的池有仓位,平不在标的池的仓位 56. for position in positions: 57. symbol = position['symbol'] 58. if symbol not in trade_symbols: 59. order_target_percent(symbol=symbol, percent=0, order_type=OrderType_Market, 60. position_side=PositionSide_Long) 61. print('市价单平不在标的池的', symbol) 62. 63. # 对标的池进行操作 64. for symbol in trade_symbols: 65. order_target_percent(symbol=symbol, percent=percent, order_type=OrderType_Market, 66. position_side=PositionSide_Long) 67. print(symbol, '以市价单调整至权重', percent) 68. 69. 70. if __name__ == '__main__': 71. ''' 72. strategy_id策略ID,由系统生成 73. filename文件名,请与本文件名保持一致 74. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST 75. token绑定计算机的ID,可在系统设置-密钥管理中生成 76. backtest_start_time回测开始时间 77. backtest_end_time回测结束时间 78. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST 79. backtest_initial_cash回测初始资金 80. backtest_commission_ratio回测佣金比例 81. backtest_slippage_ratio回测滑点比例 82. ''' 83. run(strategy_id='strategy_id', 84. filename='main.py', 85. mode=MODE_BACKTEST, 86. token='token_id', 87. backtest_start_time='2017-07-01 08:00:00', 88. backtest_end_time='2017-10-01 16:00:00', 89. backtest_adjust=ADJUST_PREV, 90. backtest_initial_cash=10000000, 91. backtest_commission_ratio=0.0001, 92. backtest_slippage_ratio=0.0001) [ 本文档使用 掘金量化 构建 - 8 -
分享到:
收藏