1. # coding=utf-8
      2. from __future__ import print_function, absolute_import, unicode_literals
      3. from gm.api import *
      4. import numpy as np
      5. '''
      6. 本策略根据计算滚动的.过去的30个1min的bar的均值正负2个标准差得到布林线
      7. 并在最新价差上穿上轨来做空价差,下穿下轨来做多价差
      8. 并在回归至上下轨水平内的时候平仓
      9. 回测数据为:SHFE.rb1801和SHFE.hc1801的1min数据
      10. 回测时间为:2017-09-01 08:00:00到2017-10-01 16:00:00
      11. '''
      12. def init(context):
      13. # 进行套利的品种
      14. context.goods = ['SHFE.rb1801', 'SHFE.hc1801']
      15. # 订阅行情
      16. subscribe(symbols=context.goods, frequency='60s', count=31, wait_group=True)
      17. def on_bar(context, bars):
      18. # 获取两个品种的时间序列
      19. data_rb = context.data(symbol=context.goods[0], frequency='60s', count=31, fields='close')
      20. close_rb = data_rb.values
      21. data_hc = context.data(symbol=context.goods[1], frequency='60s', count=31, fields='close')
      22. close_hc = data_hc.values
      23. # 计算价差
      24. spread = close_rb[:-1] - close_hc[:-1]
      25. # 计算布林带的上下轨
      26. up = np.mean(spread) + 2 * np.std(spread)
      27. down = np.mean(spread) - 2 * np.std(spread)
      28. # 计算最新价差
      29. spread_now = close_rb[-1] - close_hc[-1]
      30. # 无交易时若价差上(下)穿布林带上(下)轨则做空(多)价差
      31. position_rb_long = context.account().position(symbol=context.goods[0], side=PositionSide_Long)
      32. position_rb_short = context.account().position(symbol=context.goods[0], side=PositionSide_Short)
      33. if not position_rb_long and not position_rb_short:
      34. if spread_now > up:
      35. order_target_volume(symbol=context.goods[0], volume=1, order_type=OrderType_Market,
      36. position_side=PositionSide_Short)
      37. print(context.goods[0], '以市价单开空仓一手')
      38. order_target_volume(symbol=context.goods[1], volume=1, order_type=OrderType_Market,
      39. position_side=PositionSide_Long)
      40. print(context.goods[1], '以市价单开多仓一手')
      41. if spread_now < down:
      42. order_target_volume(symbol=context.goods[0], volume=1, order_type=OrderType_Market,
      43. position_side=PositionSide_Long)
      44. print(context.goods[0], '以市价单开多仓一手')
      45. order_target_volume(symbol=context.goods[1], volume=1, order_type=OrderType_Market,
      46. position_side=PositionSide_Short)
      47. print(context.goods[1], '以市价单开空仓一手')
      48. # 价差回归时平仓
      49. elif position_rb_short:
      50. if spread_now <= up:
      51. order_close_all()
      52. print('价格回归,平所有仓位')
      53. # 跌破下轨反向开仓
      54. if spread_now < down:
      55. order_target_volume(symbol=context.goods[0], volume=1, order_type=OrderType_Market,
      56. position_side=PositionSide_Long)
      57. print(context.goods[0], '以市价单开多仓一手')
      58. order_target_volume(symbol=context.goods[1], volume=1, order_type=OrderType_Market,
      59. position_side=PositionSide_Short)
      60. print(context.goods[1], '以市价单开空仓一手')
      61. elif position_rb_long:
      62. if spread_now >= down:
      63. order_close_all()
      64. print('价格回归,平所有仓位')
      65. # 涨破上轨反向开仓
      66. if spread_now > up:
      67. order_target_volume(symbol=context.goods[0], volume=1, order_type=OrderType_Market,
      68. position_side=PositionSide_Short)
      69. print(context.goods[0], '以市价单开空仓一手')
      70. order_target_volume(symbol=context.goods[1], volume=1, order_type=OrderType_Market,
      71. position_side=PositionSide_Long)
      72. print(context.goods[1], '以市价单开多仓一手')
      73. if __name__ == '__main__':
      74. '''
      75. strategy_id策略ID,由系统生成
      76. filename文件名,请与本文件名保持一致
      77. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
      78. token绑定计算机的ID,可在系统设置-密钥管理中生成
      79. backtest_start_time回测开始时间
      80. backtest_end_time回测结束时间
      81. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
      82. backtest_initial_cash回测初始资金
      83. backtest_commission_ratio回测佣金比例
      84. backtest_slippage_ratio回测滑点比例
      85. '''
      86. run(strategy_id='strategy_id',
      87. filename='main.py',
      88. mode=MODE_BACKTEST,
      89. token='token_id',
      90. backtest_start_time='2017-09-01 08:00:00',
      91. backtest_end_time='2017-10-01 16:00:00',
      92. backtest_adjust=ADJUST_PREV,
      93. backtest_initial_cash=500000,
      94. backtest_commission_ratio=0.0001,
      95. backtest_slippage_ratio=0.0001)

    跨品种套利(期货)

    原文: https://www.myquant.cn/docs/python_strategyies/106