复制链接
克隆策略

    {"description":"实验创建于2021/12/6","graph":{"edges":[{"to_node_id":"-227:instruments","from_node_id":"-216:data"}],"nodes":[{"node_id":"-216","module_id":"BigQuantSpace.instruments.instruments-v2","parameters":[{"name":"start_date","value":"2021-10-01","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"2022-10-01","type":"Literal","bound_global_parameter":null},{"name":"market","value":"CN_STOCK_A","type":"Literal","bound_global_parameter":null},{"name":"instrument_list","value":"000001.SZA","type":"Literal","bound_global_parameter":null},{"name":"max_count","value":0,"type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"rolling_conf","node_id":"-216"}],"output_ports":[{"name":"data","node_id":"-216"}],"cacheable":true,"seq_num":1,"comment":"","comment_collapsed":true},{"node_id":"-227","module_id":"BigQuantSpace.hftrade.hftrade-v2","parameters":[{"name":"start_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"end_date","value":"","type":"Literal","bound_global_parameter":null},{"name":"initialize","value":"# 交易引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n print(\"initialize\") \n context.ins = context.instruments[0]#从传入参数中获取需要交易的合约\n context.order_num = 4000#下单手数\n context.set_universe(context.ins)#设置需要处理的合约\n context.last_grid = 0 # 储存前一个网格所处区间,用来和最新网格所处区间作比较\n context.closetime_day = \"14:58\"#日内策略白盘平仓时间,一般14:58\n context.set_stock_t1(0)\n context.set_commission(PerOrder(buy_cost=0, sell_cost=0, min_cost=0))\n \n \n \n\n","type":"Literal","bound_global_parameter":null},{"name":"before_trading_start","value":"# 交易引擎:每个单位时间开盘前调用一次。\ndef bigquant_run(context, data):\n context.subscribe(context.ins) #注册合约\n\n # 记录上一次交易时网格范围的变化情况(例如从4区到5区,记为4,5)\n context.grid_change_last = [0,0]\n # 以前一日的收盘价为中枢价格\n context.center = data.history(context.ins,[\"close\"],1,\"1d\").iat[0,0]\n \n ","type":"Literal","bound_global_parameter":null},{"name":"handle_tick","value":"# 交易引擎:tick数据处理函数,每个tick执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_data","value":"def bigquant_run(context, data):\n cur_date = data.current_dt\n cur_hm = cur_date.strftime('%H:%M') #time \n \n # 分别获取持仓\n position = context.get_position(context.ins)\n \n # 获取当前价格\n price = data.current(context.ins, \"close\")\n\n cash = context.portfolio.cash\n cash_num = context.order_num * price\n \n# #尾盘平仓\n# if cur_hm>=context.closetime_day and cur_hm<=\"15:00\" :\n# if(position.current_qty != 0):\n# rv = context.order_target_percent(context.ins, 0, None, order_type=OrderType.MARKET)\n# msg = \"{} 尾盘平仓 for {} 最新价={} 下单函数返回={}\".format(str(data.current_dt),context.ins,str(price),str(rv))\n# context.write_log(msg, stdout=1) #输出关键日志\n# #尾盘不开新仓,直接返回\n# return\n \n # 设置网格和当前价格所处的网格区域\n band = np.arange(0.9, 1.011, 0.001)* context.center\n #band = np.array([0.982, 0.984, 0.986, 0.988, 0.990, 0.992, 0.994, 0.996, 0.998, 1, 1.002, 1.004, 1.006, 1.008, 1.01, 1.012, 1.014, 1.016, 1.018]) * context.center\n grid = pd.cut([price], band, labels=np.arange(1,len(band)))[0]\n # 如果价格超出网格设置范围,则提示调节网格宽度和数量\n# print(position)\n# print(position.current_qty)\n# print('*'*100)\n# print(price)\n# print('#'*100)\n# if np.isnan(grid):\n# context.write_log(\"价格波动超过网格范围,可适当调节网格宽度和数量\", stdout=1) #输出关键日志\n # 如果新的价格所处网格区间和前一个价格所处的网格区间不同,说明触碰到了网格线,需要进行交易\n # 如果新网格大于前一天的网格,做空或平多\n if context.last_grid < grid:\n # 记录新旧格子范围(按照大小排序)\n grid_change_new = [context.last_grid,grid]\n # 几种例外:\n # 当last_grid = 0 时是初始阶段,不构成信号\n # 如果此时grid = 3,说明当前价格仅在开盘价之下的3区域中,没有突破网格线\n # 如果此时grid = 4,说明当前价格仅在开盘价之上的4区域中,没有突破网格线\n if context.last_grid == 0:\n context.last_grid = grid\n return\n if context.last_grid != 0:\n # 如果前一次开仓是4-5,这一次是5-4,算是没有突破,不成交\n if grid_change_new != context.grid_change_last:\n # 更新前一次的数据\n context.last_grid = grid\n context.grid_change_last = grid_change_new\n # 如果有仓位\n if position.current_qty != 0:\n rv = context.order_target_percent(context.ins, 0, None, order_type=OrderType.MARKET)\n# msg = \"{} 平仓 for {} 最新价={} 下单函数返回={}\".format(str(data.current_dt),context.ins,str(price),str(rv))\n# context.write_log(msg, stdout=1) #输出关键日志\n \n \n # 如果新网格小于前一天的网格,开仓\n if context.last_grid > grid:\n # 记录新旧格子范围(按照大小排序)\n grid_change_new = [grid,context.last_grid]\n # 几种例外:\n # 当last_grid = 0 时是初始阶段,不构成信号\n # 如果此时grid = 3,说明当前价格仅在开盘价之下的3区域中,没有突破网格线\n # 如果此时grid = 4,说明当前价格仅在开盘价之上的4区域中,没有突破网格线\n if context.last_grid == 0:\n context.last_grid = grid\n return\n if context.last_grid != 0:\n # 如果前一次开仓是4-5,这一次是5-4,算是没有突破,不成交\n if grid_change_new != context.grid_change_last:\n # 更新前一次的数据\n context.last_grid = grid\n context.grid_change_last = grid_change_new\n \n if cash > cash_num+1000:\n rv = context.order(context.ins, context.order_num, price, order_type=OrderType.MARKET)\n # msg = \"{} 开仓 for {} 最新价={} 下单函数返回={} 可用现金{}\".format(str(data.current_dt),context.ins,str(price),str(rv),str(context.portfolio.cash))\n # context.write_log(msg, stdout=1) #输出关键日志\n \n # 设计一个止盈条件:当持仓量达到10手,全部平仓\n if position.current_qty == 10*context.order_num:\n context.write_log('触发止盈,全部平仓', stdout=1) #输出关键日志\n if(position.current_qty != 0):\n rv = context.order_target_percent(context.ins, 0, None, order_type=OrderType.MARKET)\n# msg = \"{} 止盈 for {} 最新价={} 下单函数返回={}\".format(str(data.current_dt),context.ins,str(price),str(rv))\n# context.write_log(msg, stdout=1) #输出关键日志\n \n","type":"Literal","bound_global_parameter":null},{"name":"handle_trade","value":"# 交易引擎:成交回报处理函数,每个成交发生时执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"handle_order","value":"# 交易引擎:委托回报处理函数,每个委托变化时执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"after_trading","value":"# 交易引擎:盘后处理函数,每日盘后执行一次\ndef bigquant_run(context, data):\n pass\n","type":"Literal","bound_global_parameter":null},{"name":"capital_base","value":"90000","type":"Literal","bound_global_parameter":null},{"name":"frequency","value":"minute","type":"Literal","bound_global_parameter":null},{"name":"price_type","value":"真实价格","type":"Literal","bound_global_parameter":null},{"name":"product_type","value":"股票","type":"Literal","bound_global_parameter":null},{"name":"before_start_days","value":"0","type":"Literal","bound_global_parameter":null},{"name":"volume_limit","value":1,"type":"Literal","bound_global_parameter":null},{"name":"order_price_field_buy","value":"open","type":"Literal","bound_global_parameter":null},{"name":"order_price_field_sell","value":"close","type":"Literal","bound_global_parameter":null},{"name":"benchmark","value":"000001.HIX","type":"Literal","bound_global_parameter":null},{"name":"plot_charts","value":"True","type":"Literal","bound_global_parameter":null},{"name":"disable_cache","value":"False","type":"Literal","bound_global_parameter":null},{"name":"replay_bdb","value":"False","type":"Literal","bound_global_parameter":null},{"name":"show_debug_info","value":"False","type":"Literal","bound_global_parameter":null},{"name":"backtest_only","value":"False","type":"Literal","bound_global_parameter":null}],"input_ports":[{"name":"instruments","node_id":"-227"},{"name":"options_data","node_id":"-227"},{"name":"history_ds","node_id":"-227"},{"name":"benchmark_ds","node_id":"-227"}],"output_ports":[{"name":"raw_perf","node_id":"-227"}],"cacheable":false,"seq_num":2,"comment":"","comment_collapsed":true}],"node_layout":"<node_postions><node_position Node='-216' Position='257,66,200,200'/><node_position Node='-227' Position='185.7496337890625,206,200,200'/></node_postions>"},"nodes_readonly":false,"studio_version":"v2"}
    In [3]:
    # 本代码由可视化策略环境自动生成 2022年10月21日 17:50
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 交易引擎:初始化函数,只执行一次
    def m2_initialize_bigquant_run(context):
        # 加载预测数据
        print("initialize")  
        context.ins = context.instruments[0]#从传入参数中获取需要交易的合约
        context.order_num = 4000#下单手数
        context.set_universe(context.ins)#设置需要处理的合约
        context.last_grid = 0 # 储存前一个网格所处区间,用来和最新网格所处区间作比较
        context.closetime_day = "14:58"#日内策略白盘平仓时间,一般14:58
        context.set_stock_t1(0)
        context.set_commission(PerOrder(buy_cost=0, sell_cost=0, min_cost=0))
        
        
       
    
    
    # 交易引擎:每个单位时间开盘前调用一次。
    def m2_before_trading_start_bigquant_run(context, data):
        context.subscribe(context.ins) #注册合约
    
        # 记录上一次交易时网格范围的变化情况(例如从4区到5区,记为4,5)
        context.grid_change_last = [0,0]
        # 以前一日的收盘价为中枢价格
        context.center = data.history(context.ins,["close"],1,"1d").iat[0,0]
       
       
    # 交易引擎:tick数据处理函数,每个tick执行一次
    def m2_handle_tick_bigquant_run(context, data):
        pass
    
    def m2_handle_data_bigquant_run(context, data):
        cur_date =  data.current_dt
        cur_hm = cur_date.strftime('%H:%M') #time  
        
        # 分别获取持仓
        position = context.get_position(context.ins)
       
        # 获取当前价格
        price = data.current(context.ins, "close")
    
        cash = context.portfolio.cash
        cash_num = context.order_num * price
        
    #     #尾盘平仓
    #     if cur_hm>=context.closetime_day and cur_hm<="15:00" :
    #         if(position.current_qty != 0):
    #             rv = context.order_target_percent(context.ins, 0, None, order_type=OrderType.MARKET)
    #             msg = "{} 尾盘平仓 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
    #             context.write_log(msg, stdout=1) #输出关键日志
    #         #尾盘不开新仓,直接返回
    #         return
            
        # 设置网格和当前价格所处的网格区域
        band = np.arange(0.9, 1.011, 0.001)* context.center
        #band = np.array([0.982, 0.984, 0.986, 0.988, 0.990, 0.992, 0.994, 0.996, 0.998, 1, 1.002, 1.004, 1.006, 1.008, 1.01, 1.012, 1.014, 1.016, 1.018]) * context.center
        grid = pd.cut([price], band, labels=np.arange(1,len(band)))[0]
        # 如果价格超出网格设置范围,则提示调节网格宽度和数量
    #     print(position)
    #     print(position.current_qty)
    #     print('*'*100)
    #     print(price)
    #     print('#'*100)
    #     if np.isnan(grid):
    #         context.write_log("价格波动超过网格范围,可适当调节网格宽度和数量", stdout=1) #输出关键日志
        # 如果新的价格所处网格区间和前一个价格所处的网格区间不同,说明触碰到了网格线,需要进行交易
        # 如果新网格大于前一天的网格,做空或平多
        if context.last_grid < grid:
            # 记录新旧格子范围(按照大小排序)
            grid_change_new = [context.last_grid,grid]
            # 几种例外:
            # 当last_grid = 0 时是初始阶段,不构成信号
            # 如果此时grid = 3,说明当前价格仅在开盘价之下的3区域中,没有突破网格线
            # 如果此时grid = 4,说明当前价格仅在开盘价之上的4区域中,没有突破网格线
            if context.last_grid == 0:
                context.last_grid = grid
                return
            if context.last_grid != 0:
                # 如果前一次开仓是4-5,这一次是5-4,算是没有突破,不成交
                if grid_change_new != context.grid_change_last:
                    # 更新前一次的数据
                    context.last_grid = grid
                    context.grid_change_last = grid_change_new
                    # 如果有仓位
                    if position.current_qty != 0:
                        rv = context.order_target_percent(context.ins, 0, None, order_type=OrderType.MARKET)
    #                     msg = "{} 平仓 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
    #                     context.write_log(msg, stdout=1) #输出关键日志
                  
                        
        # 如果新网格小于前一天的网格,开仓
        if context.last_grid > grid:
            # 记录新旧格子范围(按照大小排序)
            grid_change_new = [grid,context.last_grid]
            # 几种例外:
            # 当last_grid = 0 时是初始阶段,不构成信号
            # 如果此时grid = 3,说明当前价格仅在开盘价之下的3区域中,没有突破网格线
            # 如果此时grid = 4,说明当前价格仅在开盘价之上的4区域中,没有突破网格线
            if context.last_grid == 0:
                context.last_grid = grid
                return
            if context.last_grid != 0:
                # 如果前一次开仓是4-5,这一次是5-4,算是没有突破,不成交
                if grid_change_new != context.grid_change_last:
                    # 更新前一次的数据
                    context.last_grid = grid
                    context.grid_change_last = grid_change_new
                
                    if cash > cash_num+1000:
                        rv = context.order(context.ins, context.order_num, price, order_type=OrderType.MARKET)
        #                 msg = "{} 开仓 for {}  最新价={} 下单函数返回={} 可用现金{}".format(str(data.current_dt),context.ins,str(price),str(rv),str(context.portfolio.cash))
        #                 context.write_log(msg, stdout=1) #输出关键日志
                            
        # 设计一个止盈条件:当持仓量达到10手,全部平仓
        if position.current_qty == 10*context.order_num:
            context.write_log('触发止盈,全部平仓', stdout=1) #输出关键日志
            if(position.current_qty != 0):
                rv = context.order_target_percent(context.ins, 0, None, order_type=OrderType.MARKET)
    #             msg = "{} 止盈 for {}  最新价={} 下单函数返回={}".format(str(data.current_dt),context.ins,str(price),str(rv))
    #             context.write_log(msg, stdout=1) #输出关键日志
        
    
    # 交易引擎:成交回报处理函数,每个成交发生时执行一次
    def m2_handle_trade_bigquant_run(context, data):
        pass
    
    # 交易引擎:委托回报处理函数,每个委托变化时执行一次
    def m2_handle_order_bigquant_run(context, data):
        pass
    
    # 交易引擎:盘后处理函数,每日盘后执行一次
    def m2_after_trading_bigquant_run(context, data):
        pass
    
    
    m1 = M.instruments.v2(
        start_date='2021-10-01',
        end_date='2022-10-01',
        market='CN_STOCK_A',
        instrument_list='000001.SZA',
        max_count=0
    )
    
    m2 = M.hftrade.v2(
        instruments=m1.data,
        start_date='',
        end_date='',
        initialize=m2_initialize_bigquant_run,
        before_trading_start=m2_before_trading_start_bigquant_run,
        handle_tick=m2_handle_tick_bigquant_run,
        handle_data=m2_handle_data_bigquant_run,
        handle_trade=m2_handle_trade_bigquant_run,
        handle_order=m2_handle_order_bigquant_run,
        after_trading=m2_after_trading_bigquant_run,
        capital_base=90000,
        frequency='minute',
        price_type='真实价格',
        product_type='股票',
        before_start_days='0',
        volume_limit=1,
        order_price_field_buy='open',
        order_price_field_sell='close',
        benchmark='000001.HIX',
        plot_charts=True,
        disable_cache=False,
        replay_bdb=False,
        show_debug_info=False,
        backtest_only=False
    )
    
    initialize
    
    • 收益率-2.49%
    • 年化收益率-2.48%
    • 基准收益率-15.81%
    • 阿尔法0.04
    • 贝塔0.46
    • 夏普比率-0.29
    • 胜率0.41
    • 盈亏比0.52
    • 收益波动率15.16%
    • 信息比率0.06
    • 最大回撤14.64%
    bigcharts-data-start/{"__type":"tabs","__id":"bigchart-6275e8fdcbf5456cbce0e35047e6ea38"}/bigcharts-data-end