BigQuant使用文档

菲阿里四价策略-期货分钟_new

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

策略介绍

菲阿里四价指的是:昨日高点、昨日低点、昨天收盘、今天开盘四个价格。 菲阿里四价上下轨的计算非常简单。昨日高点为上轨,昨日低点为下轨。当价格突破上轨时,买入开仓;当价格突破下轨时,卖出开仓。

策略流程

  1. 筛选条件:菲阿里四价上下轨的计算非常简单。昨日高点为上轨,昨日低点为下轨。当价格突破上轨时,买入开仓;当价格突破下轨时,卖出开仓。
  2. 策略回测:回测时间为2023-04-01 09:00:00至2023-05-01 15:15:00。

策略实现

输入特征模块

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

\

数据抽取模块

  • 将数据抽取出来,在这当中设置起始时间为2023-04-01 09:00:00,结束时间为2023-05-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 
    
    #获取当前时间
    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]
        if context.flag == 0:
            context.today_open = in_data['open'].values[0]
        # 均线
        ma = in_data['ma'].values[0]
        # 前一分钟均线/价格
        ma_1 = in_data['ma_1'].values[0]
        price_1 = in_data['close_1'].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 price < context.today_open:
            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) 
            
        elif short_position != 0 and price > context.today_open:
            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)
            
        if long_position == 0 and price < high and context.count<=1:
            rv = context.buy_open(instr, context.order_num, price, order_type=OrderType.MARKET)
            msg = "{} 开多 for {}  最新价={} 下单函数返回={}".format(cur_date,instr,str(price),str(rv))
            context.count += 1
            context.write_log(msg, stdout=1) 
            
        elif short_position == 0 and price < low and context.count<=1:
            rv = context.sell_open(instr, context.order_num, price, order_type=OrderType.MARKET)
            msg = "{} 开多 for {}  最新价={} 下单函数返回={}".format(cur_date,instr,str(price),str(rv))
            context.count += 1
            context.write_log(msg, stdout=1)
   

策略源码

https://bigquant.com/codesharev2/d2fb8c9d-40d7-4e40-8998-76dd54773013

\

标签

期货交易策略回测
{link}