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