BigQuant使用文档

R-Breaker日内策略-期货分钟_new

由qxiao创建,最终由qxiao 被浏览 29 用户

策略介绍

R-Breaker日内策略,R-Breaker是一种短线日内交易策略。

策略流程

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)

空仓时:突破策略 空仓时,当盘中价格>突破买入价,则认为上涨的趋势还会继续,开仓做多; 空仓时,当盘中价格<突破卖出价,则认为下跌的趋势还会继续,开仓做空。

持仓时:反转策略 持多单时:当日内最高价>观察卖出价后,盘中价格回落,跌破反转卖出价构成的支撑线时,采取反转策略,即做空; 持空单时:当日内最低价<观察买入价后,盘中价格反弹,超过反转买入价构成的阻力线时,采取反转策略,即做多。

策略回测:回测时间为2020-01-21 09:00:00至2020-04-01 15:15:00。

策略实现

输入特征模块

  • 特征:open, close, high, low
  • 过滤条件中进行期货合约的筛选:instrument in ('rb2005.SHF')

数据抽取模块

  • 将数据抽取出来,在这当中设置起始时间为2020-01-21 09:00:00,结束时间为2020-04-01 15:15:00。

BigTrader模块

  • m3”BigTrader“模块中,实现交易逻辑,依据条件进行买卖。
  • K线处理函数

# 交易引擎:bar数据处理函数,每个时间单位执行一次
def bigquant_run(context, data):
    import pandas as pd 
    from bigtrader.constant  import Direction 
    from bigtrader.constant  import OrderType
    from datetime import datetime,timedelta
    import numpy as np
    #获取当前时间
    cur_date =  data.current_dt.strftime('%Y-%m-%d %H:%M:%S')
    now_data = context.data[context.data.date==cur_date]
    if len(now_data)==0:
        return
    for instr in context.ins:
        
        long_position = context.get_account_position(instr, direction=Direction.LONG).avail_qty#多头持仓
        short_position = context.get_account_position(instr, direction=Direction.SHORT).avail_qty#空头持仓
        
        in_data = now_data[now_data['instrument']==instr]
        if(len(in_data)==0):
            return
        # 最新价格
        price = in_data['close'].values[0]
        high = in_data['high'].values[0]
        low = in_data['low'].values[0]
        
        closetime_nightshift = (datetime.strptime(context.closetime_night,'%H:%M') + timedelta(minutes = 30)).strftime('%H:%M')
 
    
        # 尾盘平仓
        if((cur_date>=context.closetime_day and cur_date<="15:00") or (cur_date>=context.closetime_night and cur_date<=closetime_nightshift)):
            if(long_position != 0):
                rv = context.sell_close(instr, long_position, price, order_type=OrderType.MARKET)
                msg = "{} 尾盘平多 for {}  最新价={} 下单函数返回={}".format(cur_date,instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
            if(short_position != 0):
                rv = context.buy_close(instr, short_position, price, order_type=OrderType.MARKET)
                msg = "{} 尾盘平空 for {}  最新价={} 下单函数返回={}".format(cur_date,instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
            #尾盘不开新仓,直接返回
            return
        
        # 突破策略:
        if long_position == 0 and short_position == 0:  # 空仓条件下
            if price > context.bBreak:
                # 在空仓的情况下,如果盘中价格超过突破买入价,则采取趋势策略,即在该点位开仓做多
                rv = context.buy_open(instr, context.order_num, price, order_type=OrderType.MARKET)
                msg = "{} 开多 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
                context.open_position_price = price
            elif price < context.sBreak:
                # 在空仓的情况下,如果盘中价格跌破突破卖出价,则采取趋势策略,即在该点位开仓做空
                rv = context.sell_open(instr, context.order_num, price, order_type=OrderType.MARKET)
                msg = "{} 开空 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
                context.open_position_price = price
                
        # 设置止损条件
        else:  # 有持仓时
            # 开仓价与当前行情价之差大于止损点则止损
            if (long_position != 0 and context.open_position_price - price >= context.stopLossPrice) or (short_position != 0 and price - context.open_position_price >= context.stopLossPrice):
                context.write_log('达到止损点,全部平仓', stdout=1) #输出关键日志
                if(long_position != 0):
                    rv = context.sell_close(instr, long_position, price, order_type=OrderType.MARKET)
                    msg = "{} 止损平多 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                    context.write_log(msg, stdout=1) #输出关键日志
                if(short_position != 0):
                    rv = context.buy_close(instr, short_position, price, order_type=OrderType.MARKET)
                    msg = "{} 止损平空 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                    context.write_log(msg, stdout=1) #输出关键日志
                    
        # 反转策略:
        if long_position != 0:  # 多仓条件下
            if context.max_high > context.sSetup and price < context.sEnter:
                # 多头持仓,当日内最高价超过观察卖出价后
                # 盘中价格出现回落,且进一步跌破反转卖出价构成的支撑线时
                # 采取反转策略,即在该点位反手做空
                context.write_log('多头持仓,当日内最高价超过观察卖出价后跌破反转卖出价: 反手做空', stdout=1) #输出关键日志
                rv = context.sell_close(instr, long_position, 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.sell_open(instr, context.order_num, price, order_type=OrderType.MARKET)
                context.open_position_price = price
                msg = "{} 开空 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
        elif short_position != 0:  # 空头持仓
            if context.max_low < context.bSetup and price > context.bEnter:
                # 空头持仓,当日内最低价低于观察买入价后,
                # 盘中价格出现反弹,且进一步超过反转买入价构成的阻力线时,
                # 采取反转策略,即在该点位反手做多
                context.write_log('空头持仓,当日最低价低于观察买入价后超过反转买入价: 反手做多', stdout=1) #输出关键日志
                rv = context.buy_close(instr, short_position, price, order_type=OrderType.MARKET)
                msg = "{} 平空 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志
                
                rv = context.buy_open(instr, context.order_num, price, order_type=OrderType.MARKET)
                context.open_position_price = price
                msg = "{} 开多 for {}  最新价={} 下单函数返回={}".format(str(cur_date),instr,str(price),str(rv))
                context.write_log(msg, stdout=1) #输出关键日志

策略源码

https://bigquant.com/codesharev2/e47b4298-def6-4d1d-97c9-d0a00b0bf380

\

标签

期货交易
{link}