• QQ空间
  • 回复
  • 收藏

【宽客策略源码】网格交易(期货)

geek168 数据策略 2019-8-11 15:52 190512人围观

本策略首先计算了过去300个价格数据的均值和标准差,并根据均值加减1和2个标准差得到网格的区间分界线,,并分别配以0.3和0.5的仓位权重
然后根据价格所在的区间来配置仓位(+/-40为上下界,无实际意义):
(-40,-3],(-3,-2],(-2,2],(2,3],(3,40](具体价格等于均值+数字倍标准差)
[-0.5, -0.3, 0.0, 0.3, 0.5](资金比例,此处负号表示开空仓)

回测数据为:SHFE.rb1801的1min数据
回测时间为:2017-07-01 08:00:00到2017-10-01 16:00:00


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



路过

雷人

握手

鲜花

鸡蛋
原作者: abctrader
关注微信