PDF下载 下载

网格交易(期货)

阅读 27830
  1. # coding=utf-8
  2. from __future__ import print_function, absolute_import, unicode_literals
  3. import numpy as np
  4. import pandas as pd
  5. from gm.api import *
  6. '''
  7. 本策略首先计算了SHFE.rb1801过去300个1min收盘价的均值和标准差
  8. 并用均值加减2和3个标准差得到网格的区间分界线,分别配以0.3和0.5的仓位权重
  9. 然后根据价格所在的区间来配置仓位:
  10. (n+k1*std,n+k2*std],(n+k2*std,n+k3*std],(n+k3*std,n+k4*std],(n+k4*std,n+k5*std],(n+k5*std,n+k6*std]
  11. (n为收盘价的均值,std为收盘价的标准差,k1-k6分别为[-40, -3, -2, 2, 3, 40],其中-40和40为上下界,无实际意义)
  12. [0.5, 0.3, 0.0, -0.3, -0.5](资金比例,此处负号表示开空仓)
  13. 回测数据为:SHFE.rb1801的1min数据
  14. 回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00
  15. '''
  16. def init(context):
  17. context.symbol = 'SHFE.rb1801'
  18. # 订阅SHFE.rb1801, bar频率为1min
  19. subscribe(symbols=context.symbol, frequency='60s')
  20. # 获取过去300个价格数据
  21. timeseries = history_n(symbol=context.symbol, frequency='60s', count=300, fields='close', fill_missing='Last',
  22. end_time='2017-07-01 08:00:00', df=True)['close'].values
  23. # 获取网格区间分界线
  24. context.band = np.mean(timeseries) + np.array([-40, -3, -2, 2, 3, 40]) * np.std(timeseries)
  25. # 设置网格的仓位
  26. context.weight = [0.5, 0.3, 0.0, 0.3, 0.5]
  27. def on_bar(context, bars):
  28. bar = bars[0]
  29. # 根据价格落在(-40,-3],(-3,-2],(-2,2],(2,3],(3,40]的区间范围来获取最新收盘价所在的价格区间
  30. grid = pd.cut([bar.close], context.band, labels=[0, 1, 2, 3, 4])[0]
  31. # 获取多仓仓位
  32. position_long = context.account().position(symbol=context.symbol, side=PositionSide_Long)
  33. # 获取空仓仓位
  34. position_short = context.account().position(symbol=context.symbol, side=PositionSide_Short)
  35. # 若无仓位且价格突破则按照设置好的区间开仓
  36. if not position_long and not position_short and grid != 2:
  37. # 大于3为在中间网格的上方,做空
  38. if grid >= 3:
  39. order_target_percent(symbol=context.symbol, percent=context.weight[grid], order_type=OrderType_Market,
  40. position_side=PositionSide_Short)
  41. print(context.symbol, '以市价单开空仓到仓位', context.weight[grid])
  42. if grid <= 1:
  43. order_target_percent(symbol=context.symbol, percent=context.weight[grid], order_type=OrderType_Market,
  44. position_side=PositionSide_Long)
  45. print(context.symbol, '以市价单开多仓到仓位', context.weight[grid])
  46. # 持有多仓的处理
  47. elif position_long:
  48. # 大于3为在中间网格的上方,做空
  49. if grid >= 3:
  50. order_target_percent(symbol=context.symbol, percent=0, order_type=OrderType_Market,
  51. position_side=PositionSide_Long)
  52. print(context.symbol, '以市价单全平多仓')
  53. order_target_percent(symbol=context.symbol, percent=context.weight[grid], order_type=OrderType_Market,
  54. position_side=PositionSide_Short)
  55. print(context.symbol, '以市价单调开空仓到仓位', context.weight[grid])
  56. # 等于2为在中间网格,平仓
  57. elif grid == 2:
  58. order_target_percent(symbol=context.symbol, percent=0, order_type=OrderType_Market,
  59. position_side=PositionSide_Long)
  60. print(context.symbol, '以市价单全平多仓')
  61. # 小于1为在中间网格的下方,做多
  62. elif grid <= 1:
  63. order_target_percent(symbol=context.symbol, percent=context.weight[grid], order_type=OrderType_Market,
  64. position_side=PositionSide_Long)
  65. print(context.symbol, '以市价单开多仓到仓位', context.weight[grid])
  66. # 持有空仓的处理
  67. elif position_short:
  68. # 小于1为在中间网格的下方,做多
  69. if grid <= 1:
  70. order_target_percent(symbol=context.symbol, percent=0, order_type=OrderType_Market,
  71. position_side=PositionSide_Short)
  72. print(context.symbol, '以市价单全平空仓')
  73. order_target_percent(symbol=context.symbol, percent=context.weight[grid], order_type=OrderType_Market,
  74. position_side=PositionSide_Long)
  75. print(context.symbol, '以市价单调多仓到仓位', context.weight[grid])
  76. # 等于2为在中间网格,平仓
  77. elif grid == 2:
  78. order_target_percent(symbol=context.symbol, percent=0, order_type=OrderType_Market,
  79. position_side=PositionSide_Short)
  80. print(context.symbol, '以市价单全平空仓')
  81. # 大于3为在中间网格的上方,做空
  82. elif grid >= 3:
  83. order_target_percent(symbol=context.symbol, percent=context.weight[grid], order_type=OrderType_Market,
  84. position_side=PositionSide_Short)
  85. print(context.symbol, '以市价单开空仓到仓位', context.weight[grid])
  86. if __name__ == '__main__':
  87. '''
  88. strategy_id策略ID,由系统生成
  89. filename文件名,请与本文件名保持一致
  90. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
  91. token绑定计算机的ID,可在系统设置-密钥管理中生成
  92. backtest_start_time回测开始时间
  93. backtest_end_time回测结束时间
  94. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
  95. backtest_initial_cash回测初始资金
  96. backtest_commission_ratio回测佣金比例
  97. backtest_slippage_ratio回测滑点比例
  98. '''
  99. run(strategy_id='strategy_id',
  100. filename='main.py',
  101. mode=MODE_BACKTEST,
  102. token='token_id',
  103. backtest_start_time='2017-07-01 08:00:00',
  104. backtest_end_time='2017-10-01 16:00:00',
  105. backtest_adjust=ADJUST_PREV,
  106. backtest_initial_cash=10000000,
  107. backtest_commission_ratio=0.0001,
  108. backtest_slippage_ratio=0.0001)

0 篇笔记