掘金量化的一个代码,对本人写策略避免入坑有重要意义

  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. def init(context):
  6. # 选择的两个合约
  7. context.symbol = ['DCE.j1901', 'DCE.jm1901']
  8. # 订阅历史数据
  9. subscribe(symbols=context.symbol,frequency='1d',count=11,wait_group=True)
  10. def on_bar(context, bars):
  11. # 数据提取
  12. j_close = context.data(symbol=context.symbol[0],frequency='1d',fields='close',count=31).values
  13. jm_close = context.data(symbol=context.symbol[1],frequency='1d',fields='close',count=31).values
  14. # 提取最新价差
  15. new_price = j_close[-1] - jm_close[-1]
  16. # 计算历史价差,上下限,止损点
  17. spread_history = j_close[:-2] - jm_close[:-2]
  18. context.spread_history_mean = np.mean(spread_history)
  19. context.spread_history_std = np.std(spread_history)
  20. context.up = context.spread_history_mean + 0.75 * context.spread_history_std
  21. context.down = context.spread_history_mean - 0.75 * context.spread_history_std
  22. context.up_stoppoint = context.spread_history_mean + 2 * context.spread_history_std
  23. context.down_stoppoint = context.spread_history_mean - 2 * context.spread_history_std
  24. # 查持仓
  25. position_jm_long = context.account().position(symbol=context.symbol[0],side=1)
  26. position_jm_short = context.account().position(symbol=context.symbol[0],side=2)
  27. # 设计买卖信号
  28. # 设计开仓信号
  29. if not position_jm_short and not position_jm_long:
  30. if new_price > context.up:
  31. print('做空价差组合')
  32. order_volume(symbol=context.symbol[0],side=OrderSide_Sell,volume=1,order_type=OrderType_Market,position_effect=1)
  33. order_volume(symbol=context.symbol[1], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Open)
  34. if new_price < context.down:
  35. print('做多价差组合')
  36. order_volume(symbol=context.symbol[0], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Open)
  37. order_volume(symbol=context.symbol[1], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Open)
  38. # 设计平仓信号
  39. # 持jm多仓时
  40. if position_jm_long:
  41. if new_price >= context.spread_history_mean:
  42. # 价差回归到均值水平时,平仓
  43. print('价差回归到均衡水平,平仓')
  44. order_volume(symbol=context.symbol[0], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  45. order_volume(symbol=context.symbol[1], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  46. if new_price < context.down_stoppoint:
  47. # 价差达到止损位,平仓止损
  48. print('价差超过止损点,平仓止损')
  49. order_volume(symbol=context.symbol[0], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  50. order_volume(symbol=context.symbol[1], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  51. # 持jm空仓时
  52. if position_jm_short:
  53. if new_price <= context.spread_history_mean:
  54. # 价差回归到均值水平时,平仓
  55. print('价差回归到均衡水平,平仓')
  56. order_volume(symbol=context.symbol[0], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  57. order_volume(symbol=context.symbol[1], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  58. if new_price > context.up_stoppoint:
  59. # 价差达到止损位,平仓止损
  60. print('价差超过止损点,平仓止损')
  61. order_volume(symbol=context.symbol[0], side=OrderSide_Buy, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  62. order_volume(symbol=context.symbol[1], side=OrderSide_Sell, volume=1, order_type=OrderType_Market, position_effect=PositionEffect_Close)
  63. if __name__ == '__main__':
  64. '''
  65. strategy_id策略ID,由系统生成
  66. filename文件名,请与本文件名保持一致
  67. mode实时模式:MODE_LIVE回测模式:MODE_BACKTEST
  68. token绑定计算机的ID,可在系统设置-密钥管理中生成
  69. backtest_start_time回测开始时间
  70. backtest_end_time回测结束时间
  71. backtest_adjust股票复权方式不复权:ADJUST_NONE前复权:ADJUST_PREV后复权:ADJUST_POST
  72. backtest_initial_cash回测初始资金
  73. backtest_commission_ratio回测佣金比例
  74. backtest_slippage_ratio回测滑点比例
  75. '''
  76. run(strategy_id='strategy_id',
  77. filename='main.py',
  78. mode=MODE_BACKTEST,
  79. token='token',
  80. backtest_start_time='2018-02-01 08:00:00',
  81. backtest_end_time='2018-12-31 16:00:00',
  82. backtest_adjust=ADJUST_PREV,
  83. backtest_initial_cash=2000000,
  84. backtest_commission_ratio=0.0001,
  85. backtest_slippage_ratio=0.0001)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值