克隆策略

策略思想:

R-Breaker是一种短线日内交易策略。根据前一个交易日的收盘价(C)、最高价(H)和最低价(L)数据通过一定方式计算出六个价位,从大到小依次为: 突破买入价、观察卖出价、反转卖出价、反转买入、观察买入价、突破卖出价。

中心价位P = (H + C + L)/3

突破买入价 = H + 2P -2L

观察卖出价 = P + H - L

反转卖出价 = 2P - L

反转买入价 = 2P - H

观察买入价 = P - (H - L)

突破卖出价 = L - 2(H - P)

空仓时:突破策略

空仓时,当盘中价格>突破买入价,则认为上涨的趋势还会继续,开仓做多;

空仓时,当盘中价格<突破卖出价,则认为下跌的趋势还会继续,开仓做空。

持仓时:反转策略

持多单时:当日内最高价>观察卖出价后,盘中价格回落,跌破反转卖出价构成的支撑线时,采取反转策略,即做空;

持空单时:当日内最低价<观察买入价后,盘中价格反弹,超过反转买入价构成的阻力线时,采取反转策略,即做多。

In [1]:
from datetime import datetime,timedelta

STRATEGY_NAME = "STRATEGY_1m_rbreak"
        
def initialize(context):
    """初始化"""
    print("initialize")  
    context.symbol = instruments
    context.ins = context.get_conf_param("instruments")#从传入参数中获取需要交易的合约
    context.order_num = context.get_conf_param("order_num")#下单手数
    context.set_universe(context.ins) #设置需要处理的合约
    
    context.closetime_day = context.get_conf_param("closetime_day")#日内策略白盘平仓时间,一般14:58
    context.closetime_night = context.get_conf_param("closetime_night")#日内策略夜盘平仓时间,一般22:58,注意有些商品夜盘收盘时间不一样
    
def before_trading(context, data):
    """盘前处理"""
    
    context.subscribe(context.ins) #注册合约
    context.max_high = 0 #当日最高价
    context.max_low = 0 #当日最低价
    hist = data.history(context.ins, ["high","low","open","close"], 1, "1d")
    high = hist['high'].iloc[0]  # 前一日的最高价
    low = hist['low'].iloc[0]  # 前一日的最低价
    close = hist['close'].iloc[0]  # 前一日的收盘价
    pivot = (high + low + close) / 3  # 枢轴点
    price_multi = 2  #  参数
    context.bBreak = np.round(high + price_multi * (pivot - low), 2)  # 突破买入价 
    context.sSetup = np.round(pivot + (high - low), 2)  # 观察卖出价
    context.sEnter = np.round(price_multi * pivot - low, 2)  # 反转卖出价
    
    context.bEnter = np.round(price_multi * pivot - high, 2)  # 反转买入价
    context.bSetup = np.round(pivot - (high - low), 2)  # 观察买入价
    context.sBreak = np.round(low - price_multi * (high - pivot), 2)  # 突破卖出价
    cur_date =  data.current_dt
    print(cur_date, context.bBreak, context.sSetup, context.sEnter,context.bEnter , context.bSetup,context.sBreak)
    


    
def handle_data(context, data):
    """Bar行情推送"""
    cur_date =  data.current_dt
    cur_hm = cur_date.strftime('%H:%M') 
    
    #获取当日最高价和最低价
    today_high = data.current(context.ins,'high')
    today_low = data.current(context.ins,'low')
    context.max_high = max(context.max_high,today_high)
    context.max_low = max(context.max_high,today_low) 
    
    # 分别获取多头持仓,和空头持仓
    position_long = context.get_position(context.ins, Direction.LONG)
    position_short = context.get_position(context.ins, Direction.SHORT)
    
    # 获取当前价格
    price = data.current(context.ins, "close")
    
    #部分品种夜盘收盘时间不一样,此时间表示指定的尾盘平仓时间往后偏移30分钟,这段时间内不能开新仓,只能平仓。给30分钟是为了足够的冗余
    closetime_nightshift = (datetime.strptime(context.closetime_night,'%H:%M') + timedelta(minutes = 30)).strftime('%H:%M')
    #尾盘平仓
    if((cur_hm>=context.closetime_day and cur_hm<="15:00") or (cur_hm>=context.closetime_night and cur_hm<=closetime_nightshift)):
        if(position_long.current_qty != 0):
            rv = context.sell_close(context.ins, position_long.avail_qty, price, order_type=OrderType.MARKET)
            msg = "{} 尾盘平多 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
            context.write_log(msg, stdout=1) #输出关键日志
        if(position_short.current_qty != 0):
            rv = context.buy_close(context.ins, position_short.avail_qty, price, order_type=OrderType.MARKET)
            msg = "{} 尾盘平空 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
            context.write_log(msg, stdout=1) #输出关键日志
        #尾盘不开新仓,直接返回
        return
        
    # 突破策略
    if position_long.current_qty == 0 and position_short.current_qty == 0:  # 空仓条件下
        if price > context.bBreak:
            # 在空仓的情况下,如果盘中价格超过突破买入价,则采取趋势策略,即在该点位开仓做多
            rv = context.buy_open(context.ins, context.order_num, price, order_type=OrderType.MARKET)
            msg = "buybuybuy{} 开多 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
            context.write_log(msg, stdout=1) #输出关键日志
            context.open_position_price = price
        elif price < context.sBreak:
            # 在空仓的情况下,如果盘中价格跌破突破卖出价,则采取趋势策略,即在该点位开仓做空
            rv = context.sell_open(context.ins, context.order_num, price, order_type=OrderType.MARKET)
            msg = "sellsellsell{} 开空 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
            context.write_log(msg, stdout=1) #输出关键日志
            context.open_position_price = price
    # 设置止损条件
    else:   
        # 反转策略:
        if position_long.current_qty != 0:  # 多仓条件下
            if context.max_high > context.sSetup and price < context.sEnter:
                # 多头持仓,当日内最高价超过观察卖出价后
                # 盘中价格出现回落,且进一步跌破反转卖出价构成的支撑线时
                # 采取反转策略,即在该点位反手做空
                context.write_log('多头持仓,当日内最高价超过观察卖出价后跌破反转卖出价: 反手做空', stdout=1) #输出关键日志
                rv = context.sell_close(context.ins, position_long.avail_qty, price, order_type=OrderType.MARKET)
                msg = '平===多'*5+"{} 平多 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
                
                rv = context.sell_open(context.ins, context.order_num, price, order_type=OrderType.MARKET)
                context.open_position_price = price
                msg = '开===空'*5 +"{} 开空 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志  
                
        elif position_short.current_qty != 0:  # 空头持仓
            if context.max_low < context.bSetup and price > context.bEnter:
                # 空头持仓,当日内最低价低于观察买入价后,
                # 盘中价格出现反弹,且进一步超过反转买入价构成的阻力线时,
                # 采取反转策略,即在该点位反手做多
                context.write_log('空头持仓,当日最低价低于观察买入价后超过反转买入价: 反手做多', stdout=1) #输出关键日志
                rv = context.buy_close(context.ins, position_short.avail_qty, price, order_type=OrderType.MARKET)
                msg = "{} 平空 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
                
                rv = context.buy_open(context.ins, context.order_num, price, order_type=OrderType.MARKET)
                context.open_position_price = price
                msg = "{} 开多 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志   
                

def handle_order(context, order):
    """委托回报推送"""
    pass

def handle_trade(context, trade):
    """成交回报推送"""
    pass
    
instruments = "RB2201.SHF"
#需要交易者传入的参数
strategy_setting = [
    {
        "instruments": instruments,
        "order_num": 1,
        "closetime_day": "14:58",
        "closetime_night": "22:58"
    }    
]
start_date = "2021-08-01"
end_date = "2021-11-01"

md = M.hfbacktest.v1(start_date=start_date,
                     end_date=end_date,
                     instruments=[instruments], #只传入一个合约便于策略逻辑展示
                     capital_base=100000,
                     product_type=Product.FUTURE,
                     frequency=Frequency.MINUTE,
                     initialize=initialize,
                     before_trading_start=before_trading,
                     handle_data=handle_data,
                     handle_order=handle_order,
                     handle_trade=handle_trade,
                     plot_charts=True,
                     volume_limit=1.0,
                     disable_cache=0,
                     show_debug_info=1,
                     strategy_setting=strategy_setting,
                     slippage_type=SlippageType.FIXED,#滑点固定模式
                     slippage_value=1.0,#买卖双向各1个滑点
                     m_deps=np.random.rand())
2021-11-25 16:29:18.156155 run trading v1.7.9 
2021-11-25 16:29:18.157960 init history datas... 
2021-11-25 16:29:18.217576 init trading env... 
2021-11-25 16:29:18.219128 run_backtest() capital:100000, frequency:1m, product_type:future, date:2021-08-02 00:00:00 ~ 2021-11-01 00:00:00 
2021-11-25 16:29:18.225259 run_backtest() running... 
initialize
2021-07-30 20:30:00 5954.0 5890.0 5833.0 5712.0 5648.0 5591.0
2021-11-25 16:29:25.229866 strategy_20211125(bkt999,): sellsellsell2021-07-30 21:02:00 开空 for RB2201.SHF  最新价=5584.0 下单函数返回=0 
2021-11-25 16:29:25.394660 strategy_20211125(bkt999,): 2021-07-30 22:58:00 尾盘平空 for RB2201.SHF  最新价=5412.0 下单函数返回=0 
2021-11-25 16:29:25.403185 strategy_20211125(bkt999,): sellsellsell2021-08-02 09:01:00 开空 for RB2201.SHF  最新价=5457.0 下单函数返回=0 
2021-11-25 16:29:25.726530 strategy_20211125(bkt999,): 2021-08-02 14:58:00 尾盘平空 for RB2201.SHF  最新价=5427.0 下单函数返回=0 
2021-08-02 20:30:00 5982.0 5834.0 5629.0 5276.0 5128.0 4923.0
2021-08-03 20:30:00 5558.67 5493.33 5363.67 5168.67 5103.33 4973.67
2021-08-04 20:30:00 5854.67 5673.33 5578.67 5302.67 5121.33 5026.67
2021-08-05 20:30:00 5652.67 5601.33 5503.67 5354.67 5303.33 5205.67
2021-08-06 20:30:00 5622.67 5552.33 5476.67 5330.67 5260.33 5184.67
2021-08-09 20:30:00 5569.0 5495.0 5418.0 5267.0 5193.0 5116.0
2021-08-10 20:30:00 5690.33 5595.67 5546.33 5402.33 5307.67 5258.33
2021-08-11 20:30:00 5736.67 5668.33 5632.67 5528.67 5460.33 5424.67
2021-08-12 20:30:00 5688.33 5638.67 5540.33 5392.33 5342.67 5244.33
2021-08-13 20:30:00 5643.33 5572.67 5527.33 5411.33 5340.67 5295.33
2021-08-16 20:30:00 5662.0 5580.0 5454.0 5246.0 5164.0 5038.0
2021-08-17 20:30:00 5484.33 5433.67 5389.33 5294.33 5243.67 5199.33
2021-11-25 16:29:31.830483 strategy_20211125(bkt999,): sellsellsell2021-08-18 10:12:00 开空 for RB2201.SHF  最新价=5180.0 下单函数返回=0 
2021-11-25 16:29:32.039754 strategy_20211125(bkt999,): 2021-08-18 14:58:00 尾盘平空 for RB2201.SHF  最新价=5145.0 下单函数返回=0 
2021-08-18 20:30:00 5539.67 5451.33 5297.67 5055.67 4967.33 4813.67
2021-08-19 20:30:00 5321.33 5223.67 5120.33 4919.33 4821.67 4718.33
2021-08-20 20:30:00 5421.67 5273.33 5186.67 4951.67 4803.33 4716.67
2021-08-23 20:30:00 5262.33 5214.67 5158.33 5054.33 5006.67 4950.33
2021-08-24 20:30:00 5413.0 5327.0 5281.0 5149.0 5063.0 5017.0
2021-08-25 20:30:00 5347.67 5313.33 5274.67 5201.67 5167.33 5128.67
2021-11-25 16:29:35.158862 strategy_20211125(bkt999,): sellsellsell2021-08-26 14:54:00 开空 for RB2201.SHF  最新价=5123.0 下单函数返回=0 
2021-11-25 16:29:35.170446 strategy_20211125(bkt999,): 2021-08-26 14:58:00 尾盘平空 for RB2201.SHF  最新价=5110.0 下单函数返回=0 
2021-08-26 20:30:00 5408.33 5346.67 5227.33 5046.33 4984.67 4865.33
2021-08-27 20:30:00 5454.0 5346.0 5284.0 5114.0 5006.0 4944.0
2021-08-30 20:30:00 5614.0 5494.0 5424.0 5234.0 5114.0 5044.0
2021-08-31 20:30:00 5451.0 5414.0 5361.0 5271.0 5234.0 5181.0
2021-09-01 20:30:00 5422.0 5368.0 5305.0 5188.0 5134.0 5071.0
2021-09-02 20:30:00 5475.33 5411.67 5342.33 5209.33 5145.67 5076.33
2021-09-03 20:30:00 5696.33 5572.67 5490.33 5284.33 5160.67 5078.33
2021-09-06 20:30:00 5647.0 5571.0 5522.0 5397.0 5321.0 5272.0
2021-09-07 20:30:00 5638.67 5597.33 5545.67 5452.67 5411.33 5359.67
2021-09-08 20:30:00 5581.33 5534.67 5493.33 5405.33 5358.67 5317.33
2021-11-25 16:29:40.654420 strategy_20211125(bkt999,): buybuybuy2021-09-09 13:31:00 开多 for RB2201.SHF  最新价=5628.0 下单函数返回=0 
2021-11-25 16:29:40.804533 strategy_20211125(bkt999,): 2021-09-09 14:58:00 尾盘平多 for RB2201.SHF  最新价=5673.0 下单函数返回=0 
2021-09-09 20:30:00 5996.33 5844.67 5763.33 5530.33 5378.67 5297.33
2021-09-10 20:30:00 5837.0 5784.0 5712.0 5587.0 5534.0 5462.0
2021-09-13 20:30:00 5870.33 5816.67 5729.33 5588.33 5534.67 5447.33
2021-09-14 20:30:00 5813.33 5746.67 5619.33 5425.33 5358.67 5231.33
2021-09-15 20:30:00 5757.67 5669.33 5593.67 5429.67 5341.33 5265.67
2021-09-16 20:30:00 5794.33 5739.67 5640.33 5486.33 5431.67 5332.33
2021-09-22 08:30:00 5734.67 5661.33 5569.67 5404.67 5331.33 5239.67
2021-09-22 20:30:00 5928.0 5794.0 5725.0 5522.0 5388.0 5319.0
2021-09-23 20:30:00 5799.33 5743.67 5674.33 5549.33 5493.67 5424.33
2021-09-24 20:30:00 5717.33 5656.67 5562.33 5407.33 5346.67 5252.33
2021-09-27 20:30:00 5864.0 5732.0 5648.0 5432.0 5300.0 5216.0
2021-09-28 20:30:00 5758.67 5699.33 5666.67 5574.67 5515.33 5482.67
2021-09-29 20:30:00 5830.0 5765.0 5711.0 5592.0 5527.0 5473.0
2021-10-08 08:30:00 5907.33 5818.67 5762.33 5617.33 5528.67 5472.33
2021-10-08 20:30:00 5970.33 5906.67 5828.33 5686.33 5622.67 5544.33
2021-10-11 20:30:00 6004.67 5937.33 5872.67 5740.67 5673.33 5608.67
2021-11-25 16:29:49.344672 strategy_20211125(bkt999,): sellsellsell2021-10-12 13:33:00 开空 for RB2201.SHF  最新价=5607.0 下单函数返回=0 
2021-11-25 16:29:49.492433 strategy_20211125(bkt999,): 2021-10-12 14:58:00 尾盘平空 for RB2201.SHF  最新价=5557.0 下单函数返回=0 
2021-10-12 20:30:00 5990.67 5901.33 5731.67 5472.67 5383.33 5213.67
2021-10-13 20:30:00 5868.0 5717.0 5569.0 5270.0 5119.0 4971.0
2021-10-14 20:30:00 5696.0 5619.0 5542.0 5388.0 5311.0 5234.0
2021-10-15 20:30:00 5696.67 5624.33 5569.67 5442.67 5370.33 5315.67
2021-10-18 20:30:00 5753.33 5642.67 5532.33 5311.33 5200.67 5090.33
2021-10-19 20:30:00 5806.0 5688.0 5617.0 5428.0 5310.0 5239.0
2021-10-20 20:30:00 5695.0 5616.0 5460.0 5225.0 5146.0 4990.0
2021-11-25 16:29:53.385446 strategy_20211125(bkt999,): sellsellsell2021-10-21 14:53:00 开空 for RB2201.SHF  最新价=4976.0 下单函数返回=0 
2021-11-25 16:29:53.390088 strategy_20211125(bkt999,): sellsellsell2021-10-21 14:54:00 开空 for RB2201.SHF  最新价=4976.0 下单函数返回=0 
2021-11-25 16:29:53.394408 strategy_20211125(bkt999,): sellsellsell2021-10-21 14:55:00 开空 for RB2201.SHF  最新价=4976.0 下单函数返回=0 
2021-11-25 16:29:53.397793 strategy_20211125(bkt999,): sellsellsell2021-10-21 14:56:00 开空 for RB2201.SHF  最新价=4976.0 下单函数返回=0 
2021-11-25 16:29:53.400741 strategy_20211125(bkt999,): sellsellsell2021-10-21 14:57:00 开空 for RB2201.SHF  最新价=4976.0 下单函数返回=0 
2021-10-21 20:30:00 5666.0 5528.0 5252.0 4838.0 4700.0 4424.0
2021-10-22 20:30:00 5299.0 5162.0 5031.0 4763.0 4626.0 4495.0
2021-10-25 20:30:00 5050.0 5003.0 4914.0 4778.0 4731.0 4642.0
2021-10-26 20:30:00 5150.67 5048.33 4982.67 4814.67 4712.33 4646.67
2021-11-25 16:29:55.238561 strategy_20211125(bkt999,): sellsellsell2021-10-27 10:33:00 开空 for RB2201.SHF  最新价=4636.0 下单函数返回=0 
2021-11-25 16:29:55.464762 strategy_20211125(bkt999,): 2021-10-27 14:58:00 尾盘平空 for RB2201.SHF  最新价=4658.0 下单函数返回=0 
2021-10-27 20:30:00 5233.0 5105.0 4880.0 4527.0 4399.0 4174.0
2021-10-28 20:30:00 5296.0 5080.0 4896.0 4496.0 4280.0 4096.0
2021-10-29 20:30:00 4984.33 4888.67 4767.33 4550.33 4454.67 4333.33
2021-11-25 16:29:57.180864 run_backtest() finished! time cost 38.955s! 
  • 收益率3.09%
  • 年化收益率13.9%
  • 基准收益率-0.87%
  • 阿尔法0.1
  • 贝塔0.09
  • 夏普比率2.35
  • 胜率0.43
  • 盈亏比13.44
  • 收益波动率4.32%
  • 最大回撤0.24%
bigcharts-data-start/{"__type":"tabs","__id":"bigchart-9df5b37fb9c346e3a9486a3485782df9"}/bigcharts-data-end
In [ ]: