【宽客学院】策略止盈止损


(iQuant) #1
作者:bigquant
阅读时间:8分钟
本文由BigQuant宽客学院推出,难度标签:☆☆

导语:止盈止损是交易中比较常用的技巧,想要用好也比较困难。本文对其进行简单介绍,帮助大家可以在策略里面灵活添加止盈止损。


止损又叫 “割肉”, 指的是当一个投资组合亏损达到一定比例及时清仓出局, 以免形成更大的亏损的行为。 止盈是指当盈利大于一定数额, 及时获利了结。

止盈止损为什么重要呢?

  1. 侥幸的心理作祟,某些投资者尽管也知道趋势上已经到止盈止损位,但由于过于犹豫,总是想再涨一点或再跌一点就止盈止损出局,导致自己错过最佳止盈止损的大好时机;

  2. 价格频繁的波动会让投资者犹豫不决,经常性错误的止损止盈会给投资者留下挥之不去的记忆,从而动摇投资者下次止损止盈的决心;

  3. 过于贪婪,有些投资者在趋势已经到达止盈位置了之后总抱着还会有盈利的空间而不愿意刹车止盈。

在这里提供了几个简单的止盈止损方案的模板, 在写这些代码的时候考虑了用户的体验,用户只需将相应的代码复制粘贴到自己的策略中,并设置相关参数就能正常使用。

我们先以双均线策略为例,添加固定点数止盈条件。我们只需修改Trade回测模块中的主函数:

第一步:在主函数的起始位置加入止盈止损功能代码:

固定点数止盈

固定点数止盈代码

解释:建仓以后,当盈利固定点数,达到止盈条件,止盈出场

 #------------------------------------------止赢模块START--------------------------------------------
    date = data.current_dt.strftime('%Y-%m-%d')
    positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
    # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    current_stopwin_stock = [] 
    if len(positions) > 0:
        for i in positions.keys():
            stock_cost = positions[i] 
            stock_market_price = data.current(context.symbol(i), 'price') 
            # 赚3元就止赢
            if stock_market_price - stock_cost  >= 3:   
                context.order_target_percent(context.symbol(i),0)     
                current_stopwin_stock.append(i)
                print('日期:',date,'股票:',i,'出现止盈状况')
    #-------------------------------------------止赢模块END---------------------------------------------

就实现了在策略中添加固定点数止盈功能,模板代码我们整理到文末。

其它止盈止损方式的代码如下所示,只需要将对应的代码粘贴到回测模块的主函数头部即可实现相应的功能。

固定百分比止盈

固定百分比止盈代码

解释:建仓以后,当盈利固定百分比,达到止盈条件,止盈出场

#------------------------------------------止赢模块START--------------------------------------------
    date = data.current_dt.strftime('%Y-%m-%d')
    positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
    # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    current_stopwin_stock = [] 
    if len(positions) > 0:
        for i in positions.keys():
            stock_cost = positions[i] 
            stock_market_price = data.current(context.symbol(i), 'price') 
            # 赚10%就止赢
            if (stock_market_price - stock_cost ) / stock_cost>= 0.1:   
                context.order_target_percent(context.symbol(i),0)     
                current_stopwin_stock.append(i)
                print('日期:',date,'股票:',i,'出现止盈状况')
    #-------------------------------------------止赢模块END---------------------------------------------

固定点数止损

固定点数止损代码

解释:建仓以后,当亏损固定点数,达到止损条件,止损出场

 #------------------------------------------止损模块START--------------------------------------------
    date = data.current_dt.strftime('%Y-%m-%d')
    positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
    # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    current_stoploss_stock = [] 
    if len(positions) > 0:
        for i in positions.keys():
            stock_cost = positions[i] 
            stock_market_price = data.current(context.symbol(i), 'price') 
            # 亏1元就止损
            if stock_market_price - stock_cost  <= -1:   
                context.order_target_percent(context.symbol(i),0)     
                current_stoploss_stock.append(i)
                print('日期:',date,'股票:',i,'出现止损状况')
    #-------------------------------------------止损模块END---------------------------------------------

固定百分比止损

固定百分比止损代码

解释:建仓以后,当亏损固定百分比时,达到止损条件,止损出场

 #------------------------------------------止损模块START--------------------------------------------
    date = data.current_dt.strftime('%Y-%m-%d')
    positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
    # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    current_stoploss_stock = [] 
    if len(positions) > 0:
        for i in positions.keys():
            stock_cost = positions[i] 
            stock_market_price = data.current(context.symbol(i), 'price') 
            # 亏5%就止损
            if (stock_market_price - stock_cost) / stock_cost <= -0.05:   
                context.order_target_percent(context.symbol(i),0)     
                current_stoploss_stock.append(i)
                print('日期:',date,'股票:',i,'出现止损状况')
    #-------------------------------------------止损模块END---------------------------------------------

跟踪止损

跟踪止损代码

解释:建仓初期,确定一个初始止损,如果股票下跌,达到初始止损条件,出场。如果股票上涨,那么初始止损不再适用,而是采取止损位置不断抬高的跟踪止损,能够防止利润的大幅回吐

    #------------------------------------------止损模块START--------------------------------------------
    date = data.current_dt.strftime('%Y-%m-%d')  
    equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
    # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
    current_stoploss_stock = [] 
    if len(equities) > 0:
        for i in equities.keys():
            stock_market_price = data.current(context.symbol(i), 'price')  # 最新市场价格
            last_sale_date = equities[i].last_sale_date   # 上次交易日期
            delta_days = data.current_dt - last_sale_date  
            hold_days = delta_days.days # 持仓天数
            # 建仓以来的最高价
            highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()
            # 确定止损位置
            stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.1
            record('止损位置', stoploss_line)
            # 如果价格下穿止损位置
            if stock_market_price < stoploss_line:
                context.order_target_percent(context.symbol(i), 0)     
                current_stoploss_stock.append(i)
                print('日期:', date , '股票:', i, '出现止损状况')
    #-------------------------------------------止损模块END--------------------------------------------------

第二步:修改卖出逻辑

止赢和止损实质上是条件卖出!对于已经止盈/止损的股票我们应当使用列表记录下来,并在轮仓循环卖出逻辑中跳过以避免多次卖出造成空头持仓。如下所示,我们在循环卖出逻辑中加入判断语句:

修改卖出逻辑
    # 如果有卖出信号
    if len(stock_to_sell)>0:
        for instrument in stock_to_sell:

            #----------这里加入股票判断,如果已经止盈/止损了就跳过此股票,避免二次卖出--------
            if instrument in current_stopwin_stock:
                continue
            #----------------------------------------------------------------------------------------

            sid = context.symbol(instrument) # 将标的转化为equity格式
            cur_position = context.portfolio.positions[sid].amount # 持仓
            if cur_position > 0 and data.can_trade(sid):
                context.order_target_percent(sid, 0) # 全部卖出 
                cash_for_buy += stock_hold_now[instrument]

双均线+固定点位止损案例

克隆策略

双均线策略+固定点数止盈

    {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-50:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-57:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-50:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-102:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-102:options_data","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-57:input_data","SourceOutputPortId":"-50:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-57:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nbuy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)\nsell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2012-05-29","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-11-08","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600519.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-50","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"30","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-50"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-50"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-50","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-57","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-57"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-57"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-57","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-102","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n \n #------------------------------------------止赢模块START--------------------------------------------\n date = data.current_dt.strftime('%Y-%m-%d')\n positions = {e.symbol: p.cost_basis for e, p in context.portfolio.positions.items()}\n # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\n current_stopwin_stock = [] \n if len(positions) > 0:\n for i in positions.keys():\n stock_cost = positions[i] \n stock_market_price = data.current(context.symbol(i), 'price') \n # 赚3元就止赢\n if stock_market_price - stock_cost >= 3: \n context.order_target_percent(context.symbol(i),0) \n current_stopwin_stock.append(i)\n print('日期:',date,'股票:',i,'出现止盈状况')\n #-------------------------------------------止赢模块END---------------------------------------------\n\n \n # 获取今日的日期\n today = data.current_dt.strftime('%Y-%m-%d') \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n\n # 记录用于买入股票的可用现金,因为是早盘卖股票,需要记录卖出的股票市值并在买入下单前更新可用现金;\n # 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用\n cash_for_buy = context.portfolio.cash \n \n try:\n buy_stock = context.daily_stock_buy[today] # 当日符合买入条件的股票\n except:\n buy_stock=[] # 如果没有符合条件的股票,就设置为空\n \n try:\n sell_stock = context.daily_stock_sell[today] # 当日符合卖出条件的股票\n except:\n sell_stock=[] # 如果没有符合条件的股票,就设置为空\n \n # 需要卖出的股票:已有持仓中符合卖出条件的股票\n stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]\n # 需要买入的股票:没有持仓且符合买入条件的股票\n stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now ] \n # 需要调仓的股票:已有持仓且不符合卖出条件的股票\n stock_to_adjust=[ i for i in stock_hold_now if i not in sell_stock ]\n \n # 如果有卖出信号\n if len(stock_to_sell)>0:\n for instrument in stock_to_sell:\n if instrument in current_stopwin_stock:\n continue\n sid = context.symbol(instrument) # 将标的转化为equity格式\n cur_position = context.portfolio.positions[sid].amount # 持仓\n if cur_position > 0 and data.can_trade(sid):\n context.order_target_percent(sid, 0) # 全部卖出 \n # 因为设置的是早盘卖出早盘买入,需要根据卖出的股票更新可用现金;如果设置尾盘卖出早盘买入,则不需更新可用现金(可以删除下面的语句)\n cash_for_buy += stock_hold_now[instrument]\n \n # 如果有买入信号/有持仓\n if len(stock_to_buy)>0:\n weight = 1/len(set(stock_to_buy+stock_to_adjust)) # 每只股票的比重为等资金比例持有\n for instrument in set(stock_to_buy+stock_to_adjust):\n sid = context.symbol(instrument) # 将标的转化为equity格式\n if data.can_trade(sid):\n context.order_target_value(sid, weight*cash_for_buy) # 买入","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n df = context.options['data'].read_df()\n\n # 函数:求满足开仓条件的股票列表\n def open_pos_con(df):\n return list(df[df['buy_condition']>0].instrument)\n\n # 函数:求满足平仓条件的股票列表\n def close_pos_con(df):\n return list(df[df['sell_condition']>0].instrument)\n\n # 每日买入股票的数据框\n context.daily_stock_buy= df.groupby('date').apply(open_pos_con)\n # 每日卖出股票的数据框\n context.daily_stock_sell= df.groupby('date').apply(close_pos_con)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"1000000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-102"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-102","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='765,20,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1073,124,200,200'/><NodePosition Node='-86' Position='1078,418,200,200'/><NodePosition Node='-50' Position='1078,232,200,200'/><NodePosition Node='-57' Position='1076,327,200,200'/><NodePosition Node='-102' Position='1048,529,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
    In [11]:
    # 本代码由可视化策略环境自动生成 2019年1月24日 10:23
    # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
    
    
    # 回测引擎:每日数据处理函数,每天执行一次
    def m3_handle_data_bigquant_run(context, data):
        
     #------------------------------------------止赢模块START--------------------------------------------
        date = data.current_dt.strftime('%Y-%m-%d')
        positions = {e.symbol: p.cost_basis  for e, p in context.portfolio.positions.items()}
        # 新建当日止赢股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
        current_stopwin_stock = [] 
        if len(positions) > 0:
            for i in positions.keys():
                stock_cost = positions[i] 
                stock_market_price = data.current(context.symbol(i), 'price') 
                # 赚3元就止赢
                if stock_market_price - stock_cost  >= 3:   
                    context.order_target_percent(context.symbol(i),0)     
                    current_stopwin_stock.append(i)
                    print('日期:',date,'股票:',i,'出现止盈状况')
        #-------------------------------------------止赢模块END---------------------------------------------
    
           
        # 获取今日的日期
        today = data.current_dt.strftime('%Y-%m-%d')  
        # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
        stock_hold_now = {e.symbol: p.amount * p.last_sale_price
                     for e, p in context.portfolio.positions.items()}
    
        # 记录用于买入股票的可用现金,因为是早盘卖股票,需要记录卖出的股票市值并在买入下单前更新可用现金;
        # 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用
        cash_for_buy = context.portfolio.cash    
        
        try:
            buy_stock = context.daily_stock_buy[today]  # 当日符合买入条件的股票
        except:
            buy_stock=[]  # 如果没有符合条件的股票,就设置为空
        
        try:
            sell_stock = context.daily_stock_sell[today]  # 当日符合卖出条件的股票
        except:
            sell_stock=[] # 如果没有符合条件的股票,就设置为空
        
        # 需要卖出的股票:已有持仓中符合卖出条件的股票
        stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]
        # 需要买入的股票:没有持仓且符合买入条件的股票
        stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now ]  
        # 需要调仓的股票:已有持仓且不符合卖出条件的股票
        stock_to_adjust=[ i for i in stock_hold_now if i not in sell_stock ]
        
        # 如果有卖出信号
        if len(stock_to_sell)>0:
            for instrument in stock_to_sell:
                if instrument in current_stopwin_stock:
                    continue
                sid = context.symbol(instrument) # 将标的转化为equity格式
                cur_position = context.portfolio.positions[sid].amount # 持仓
                if cur_position > 0 and data.can_trade(sid):
                    context.order_target_percent(sid, 0) # 全部卖出 
                    # 因为设置的是早盘卖出早盘买入,需要根据卖出的股票更新可用现金;如果设置尾盘卖出早盘买入,则不需更新可用现金(可以删除下面的语句)
                    cash_for_buy += stock_hold_now[instrument]
        
        # 如果有买入信号/有持仓
        if len(stock_to_buy)>0:
            weight = 1/len(set(stock_to_buy+stock_to_adjust)) # 每只股票的比重为等资金比例持有
            for instrument in set(stock_to_buy+stock_to_adjust):
                sid = context.symbol(instrument) # 将标的转化为equity格式
                if  data.can_trade(sid):
                    context.order_target_value(sid, weight*cash_for_buy) # 买入
    # 回测引擎:准备数据,只执行一次
    def m3_prepare_bigquant_run(context):
        # 加载预测数据
        df = context.options['data'].read_df()
    
        # 函数:求满足开仓条件的股票列表
        def open_pos_con(df):
            return list(df[df['buy_condition']>0].instrument)
    
        # 函数:求满足平仓条件的股票列表
        def close_pos_con(df):
            return list(df[df['sell_condition']>0].instrument)
    
        # 每日买入股票的数据框
        context.daily_stock_buy= df.groupby('date').apply(open_pos_con)
        # 每日卖出股票的数据框
        context.daily_stock_sell= df.groupby('date').apply(close_pos_con)
    # 回测引擎:初始化函数,只执行一次
    def m3_initialize_bigquant_run(context):
    
        # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
        context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    
    
    m1 = M.input_features.v1(
        features="""# #号开始的表示注释
    # 多个特征,每行一个,可以包含基础特征和衍生特征
    buy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)
    sell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)""",
        m_cached=False
    )
    
    m2 = M.instruments.v2(
        start_date=T.live_run_param('trading_date', '2012-05-29'),
        end_date=T.live_run_param('trading_date', '2017-11-08'),
        market='CN_STOCK_A',
        instrument_list='600519.SHA',
        max_count=0
    )
    
    m7 = M.general_feature_extractor.v7(
        instruments=m2.data,
        features=m1.data,
        start_date='',
        end_date='',
        before_start_days=30
    )
    
    m8 = M.derived_feature_extractor.v3(
        input_data=m7.data,
        features=m1.data,
        date_col='date',
        instrument_col='instrument',
        drop_na=False,
        remove_extra_columns=False
    )
    
    m6 = M.dropnan.v1(
        input_data=m8.data
    )
    
    m3 = M.trade.v4(
        instruments=m2.data,
        options_data=m6.data,
        start_date='',
        end_date='',
        handle_data=m3_handle_data_bigquant_run,
        prepare=m3_prepare_bigquant_run,
        initialize=m3_initialize_bigquant_run,
        volume_limit=0.025,
        order_price_field_buy='open',
        order_price_field_sell='open',
        capital_base=1000000,
        auto_cancel_non_tradable_orders=True,
        data_frequency='daily',
        price_type='后复权',
        product_type='股票',
        plot_charts=True,
        backtest_only=False,
        benchmark=''
    )
    
    [2019-01-24 10:21:03.229337] INFO: bigquant: input_features.v1 开始运行..
    [2019-01-24 10:21:03.237439] INFO: bigquant: input_features.v1 运行完成[0.008104s].
    [2019-01-24 10:21:03.240759] INFO: bigquant: instruments.v2 开始运行..
    [2019-01-24 10:21:03.246090] INFO: bigquant: 命中缓存
    [2019-01-24 10:21:03.247002] INFO: bigquant: instruments.v2 运行完成[0.006273s].
    [2019-01-24 10:21:03.253295] INFO: bigquant: general_feature_extractor.v7 开始运行..
    [2019-01-24 10:21:03.258747] INFO: bigquant: 命中缓存
    [2019-01-24 10:21:03.259789] INFO: bigquant: general_feature_extractor.v7 运行完成[0.006449s].
    [2019-01-24 10:21:03.262993] INFO: bigquant: derived_feature_extractor.v3 开始运行..
    [2019-01-24 10:21:03.360734] INFO: derived_feature_extractor: 提取完成 buy_condition=where(mean(close_0,5)>mean(close_0,50),1,0), 0.011s
    [2019-01-24 10:21:03.371789] INFO: derived_feature_extractor: 提取完成 sell_condition=where(mean(close_0,5)<mean(close_0,50),1,0), 0.010s
    [2019-01-24 10:21:03.392186] INFO: derived_feature_extractor: /y_2012, 165
    [2019-01-24 10:21:03.416805] INFO: derived_feature_extractor: /y_2013, 238
    [2019-01-24 10:21:03.443336] INFO: derived_feature_extractor: /y_2014, 245
    [2019-01-24 10:21:03.467377] INFO: derived_feature_extractor: /y_2015, 244
    [2019-01-24 10:21:03.492467] INFO: derived_feature_extractor: /y_2016, 244
    [2019-01-24 10:21:03.518638] INFO: derived_feature_extractor: /y_2017, 207
    [2019-01-24 10:21:03.556622] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.293596s].
    [2019-01-24 10:21:03.559887] INFO: bigquant: dropnan.v1 开始运行..
    [2019-01-24 10:21:03.632438] INFO: dropnan: /y_2012, 165/165
    [2019-01-24 10:21:03.658496] INFO: dropnan: /y_2013, 238/238
    [2019-01-24 10:21:03.684874] INFO: dropnan: /y_2014, 245/245
    [2019-01-24 10:21:03.710760] INFO: dropnan: /y_2015, 244/244
    [2019-01-24 10:21:03.739511] INFO: dropnan: /y_2016, 244/244
    [2019-01-24 10:21:03.776395] INFO: dropnan: /y_2017, 207/207
    [2019-01-24 10:21:03.788098] INFO: dropnan: 行数: 1343/1343
    [2019-01-24 10:21:03.790329] INFO: bigquant: dropnan.v1 运行完成[0.230434s].
    [2019-01-24 10:21:03.805786] INFO: bigquant: backtest.v8 开始运行..
    [2019-01-24 10:21:03.808036] INFO: bigquant: biglearning backtest:V8.1.6
    [2019-01-24 10:21:06.070987] INFO: bigquant: product_type:stock by specified
    [2019-01-24 10:21:17.359534] INFO: bigquant: 读取股票行情完成:1568
    [2019-01-24 10:21:17.407634] INFO: algo: TradingAlgorithm V1.4.2
    [2019-01-24 10:21:17.639002] INFO: algo: trading transform...
    日期: 2012-07-13 股票: 600519.SHA 出现止盈状况
    日期: 2012-08-09 股票: 600519.SHA 出现止盈状况
    日期: 2012-10-09 股票: 600519.SHA 出现止盈状况
    日期: 2012-10-12 股票: 600519.SHA 出现止盈状况
    日期: 2012-10-16 股票: 600519.SHA 出现止盈状况
    日期: 2012-10-18 股票: 600519.SHA 出现止盈状况
    日期: 2013-05-07 股票: 600519.SHA 出现止盈状况
    日期: 2013-05-09 股票: 600519.SHA 出现止盈状况
    日期: 2013-05-22 股票: 600519.SHA 出现止盈状况
    日期: 2014-01-27 股票: 600519.SHA 出现止盈状况
    日期: 2014-02-14 股票: 600519.SHA 出现止盈状况
    日期: 2014-02-28 股票: 600519.SHA 出现止盈状况
    日期: 2014-03-04 股票: 600519.SHA 出现止盈状况
    日期: 2014-03-06 股票: 600519.SHA 出现止盈状况
    日期: 2014-03-17 股票: 600519.SHA 出现止盈状况
    日期: 2014-03-19 股票: 600519.SHA 出现止盈状况
    日期: 2014-03-21 股票: 600519.SHA 出现止盈状况
    日期: 2014-04-10 股票: 600519.SHA 出现止盈状况
    日期: 2014-04-18 股票: 600519.SHA 出现止盈状况
    日期: 2014-04-23 股票: 600519.SHA 出现止盈状况
    日期: 2014-06-30 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-03 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-07 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-14 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-16 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-18 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-22 股票: 600519.SHA 出现止盈状况
    日期: 2014-07-28 股票: 600519.SHA 出现止盈状况
    日期: 2014-08-01 股票: 600519.SHA 出现止盈状况
    日期: 2014-08-05 股票: 600519.SHA 出现止盈状况
    日期: 2014-09-03 股票: 600519.SHA 出现止盈状况
    日期: 2014-09-05 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-02 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-04 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-08 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-10 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-15 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-19 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-23 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-25 股票: 600519.SHA 出现止盈状况
    日期: 2014-12-31 股票: 600519.SHA 出现止盈状况
    日期: 2015-02-16 股票: 600519.SHA 出现止盈状况
    日期: 2015-02-25 股票: 600519.SHA 出现止盈状况
    日期: 2015-02-27 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-09 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-11 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-16 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-18 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-23 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-26 股票: 600519.SHA 出现止盈状况
    日期: 2015-03-30 股票: 600519.SHA 出现止盈状况
    日期: 2015-04-03 股票: 600519.SHA 出现止盈状况
    日期: 2015-04-08 股票: 600519.SHA 出现止盈状况
    日期: 2015-04-13 股票: 600519.SHA 出现止盈状况
    日期: 2015-04-16 股票: 600519.SHA 出现止盈状况
    日期: 2015-04-20 股票: 600519.SHA 出现止盈状况
    日期: 2015-05-22 股票: 600519.SHA 出现止盈状况
    日期: 2015-10-19 股票: 600519.SHA 出现止盈状况
    日期: 2015-10-22 股票: 600519.SHA 出现止盈状况
    日期: 2015-10-26 股票: 600519.SHA 出现止盈状况
    日期: 2015-11-05 股票: 600519.SHA 出现止盈状况
    日期: 2015-12-21 股票: 600519.SHA 出现止盈状况
    日期: 2016-02-26 股票: 600519.SHA 出现止盈状况
    日期: 2016-03-01 股票: 600519.SHA 出现止盈状况
    日期: 2016-03-03 股票: 600519.SHA 出现止盈状况
    日期: 2016-03-24 股票: 600519.SHA 出现止盈状况
    日期: 2016-03-28 股票: 600519.SHA 出现止盈状况
    日期: 2016-04-06 股票: 600519.SHA 出现止盈状况
    日期: 2016-05-03 股票: 600519.SHA 出现止盈状况
    日期: 2016-05-05 股票: 600519.SHA 出现止盈状况
    日期: 2016-05-09 股票: 600519.SHA 出现止盈状况
    日期: 2016-05-11 股票: 600519.SHA 出现止盈状况
    日期: 2016-05-31 股票: 600519.SHA 出现止盈状况
    日期: 2016-06-02 股票: 600519.SHA 出现止盈状况
    日期: 2016-06-27 股票: 600519.SHA 出现止盈状况
    日期: 2016-06-29 股票: 600519.SHA 出现止盈状况
    日期: 2016-07-04 股票: 600519.SHA 出现止盈状况
    日期: 2016-07-06 股票: 600519.SHA 出现止盈状况
    日期: 2016-07-11 股票: 600519.SHA 出现止盈状况
    日期: 2016-10-18 股票: 600519.SHA 出现止盈状况
    日期: 2016-10-25 股票: 600519.SHA 出现止盈状况
    日期: 2016-10-27 股票: 600519.SHA 出现止盈状况
    日期: 2016-11-08 股票: 600519.SHA 出现止盈状况
    日期: 2016-11-29 股票: 600519.SHA 出现止盈状况
    日期: 2016-12-01 股票: 600519.SHA 出现止盈状况
    日期: 2016-12-06 股票: 600519.SHA 出现止盈状况
    日期: 2016-12-08 股票: 600519.SHA 出现止盈状况
    日期: 2017-01-04 股票: 600519.SHA 出现止盈状况
    日期: 2017-01-06 股票: 600519.SHA 出现止盈状况
    日期: 2017-01-10 股票: 600519.SHA 出现止盈状况
    日期: 2017-01-12 股票: 600519.SHA 出现止盈状况
    日期: 2017-01-17 股票: 600519.SHA 出现止盈状况
    日期: 2017-02-20 股票: 600519.SHA 出现止盈状况
    日期: 2017-02-22 股票: 600519.SHA 出现止盈状况
    日期: 2017-02-24 股票: 600519.SHA 出现止盈状况
    日期: 2017-03-06 股票: 600519.SHA 出现止盈状况
    日期: 2017-03-09 股票: 600519.SHA 出现止盈状况
    日期: 2017-03-13 股票: 600519.SHA 出现止盈状况
    日期: 2017-03-15 股票: 600519.SHA 出现止盈状况
    日期: 2017-03-17 股票: 600519.SHA 出现止盈状况
    日期: 2017-03-21 股票: 600519.SHA 出现止盈状况
    日期: 2017-04-07 股票: 600519.SHA 出现止盈状况
    日期: 2017-04-12 股票: 600519.SHA 出现止盈状况
    日期: 2017-04-18 股票: 600519.SHA 出现止盈状况
    日期: 2017-04-20 股票: 600519.SHA 出现止盈状况
    日期: 2017-04-25 股票: 600519.SHA 出现止盈状况
    日期: 2017-04-27 股票: 600519.SHA 出现止盈状况
    日期: 2017-05-02 股票: 600519.SHA 出现止盈状况
    日期: 2017-05-04 股票: 600519.SHA 出现止盈状况
    日期: 2017-05-15 股票: 600519.SHA 出现止盈状况
    日期: 2017-05-18 股票: 600519.SHA 出现止盈状况
    日期: 2017-05-22 股票: 600519.SHA 出现止盈状况
    日期: 2017-06-07 股票: 600519.SHA 出现止盈状况
    日期: 2017-06-09 股票: 600519.SHA 出现止盈状况
    日期: 2017-06-14 股票: 600519.SHA 出现止盈状况
    日期: 2017-06-16 股票: 600519.SHA 出现止盈状况
    日期: 2017-06-26 股票: 600519.SHA 出现止盈状况
    日期: 2017-07-24 股票: 600519.SHA 出现止盈状况
    日期: 2017-07-28 股票: 600519.SHA 出现止盈状况
    日期: 2017-08-02 股票: 600519.SHA 出现止盈状况
    日期: 2017-08-07 股票: 600519.SHA 出现止盈状况
    日期: 2017-08-09 股票: 600519.SHA 出现止盈状况
    日期: 2017-08-14 股票: 600519.SHA 出现止盈状况
    日期: 2017-09-18 股票: 600519.SHA 出现止盈状况
    日期: 2017-09-21 股票: 600519.SHA 出现止盈状况
    日期: 2017-09-25 股票: 600519.SHA 出现止盈状况
    日期: 2017-09-28 股票: 600519.SHA 出现止盈状况
    日期: 2017-10-09 股票: 600519.SHA 出现止盈状况
    日期: 2017-10-11 股票: 600519.SHA 出现止盈状况
    日期: 2017-10-13 股票: 600519.SHA 出现止盈状况
    日期: 2017-10-19 股票: 600519.SHA 出现止盈状况
    日期: 2017-10-26 股票: 600519.SHA 出现止盈状况
    日期: 2017-11-06 股票: 600519.SHA 出现止盈状况
    日期: 2017-11-08 股票: 600519.SHA 出现止盈状况
    [2019-01-24 10:21:22.140459] INFO: Performance: Simulated 1327 trading days out of 1327.
    [2019-01-24 10:21:22.141598] INFO: Performance: first open: 2012-05-29 09:30:00+00:00
    [2019-01-24 10:21:22.142406] INFO: Performance: last close: 2017-11-08 15:00:00+00:00
    
    • 收益率211.08%
    • 年化收益率24.05%
    • 基准收益率54.82%
    • 阿尔法0.18
    • 贝塔0.26
    • 夏普比率1.02
    • 胜率0.86
    • 盈亏比0.55
    • 收益波动率20.16%
    • 信息比率0.03
    • 最大回撤19.76%
    [2019-01-24 10:21:24.860551] INFO: bigquant: backtest.v8 运行完成[21.054745s].
    

    双均线+移动止损案例

    克隆策略

    双均线策略+移动止损

      {"Description":"实验创建于2017/8/26","Summary":"","Graph":{"EdgesInternal":[{"DestinationInputPortId":"-50:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-57:features","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24:data"},{"DestinationInputPortId":"-50:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-102:instruments","SourceOutputPortId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62:data"},{"DestinationInputPortId":"-102:options_data","SourceOutputPortId":"-86:data"},{"DestinationInputPortId":"-57:input_data","SourceOutputPortId":"-50:data"},{"DestinationInputPortId":"-86:input_data","SourceOutputPortId":"-57:data"}],"ModuleNodes":[{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","ModuleId":"BigQuantSpace.input_features.input_features-v1","ModuleParameters":[{"Name":"features","Value":"# #号开始的表示注释\n# 多个特征,每行一个,可以包含基础特征和衍生特征\nbuy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)\nsell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features_ds","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-24","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":1,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","ModuleId":"BigQuantSpace.instruments.instruments-v2","ModuleParameters":[{"Name":"start_date","Value":"2012-05-29","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"end_date","Value":"2017-11-08","ValueType":"Literal","LinkedGlobalParameter":"交易日期"},{"Name":"market","Value":"CN_STOCK_A","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_list","Value":"600519.SHA","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"max_count","Value":"0","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"rolling_conf","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62"}],"OutputPortsInternal":[{"Name":"data","NodeId":"287d2cb0-f53c-4101-bdf8-104b137c8601-62","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":2,"IsPartOfPartialRun":null,"Comment":"预测数据,用于回测和模拟","CommentCollapsed":false},{"Id":"-86","ModuleId":"BigQuantSpace.dropnan.dropnan-v1","ModuleParameters":[],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-86"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-86","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":6,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-50","ModuleId":"BigQuantSpace.general_feature_extractor.general_feature_extractor-v7","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_start_days","Value":"30","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-50"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-50"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-50","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":7,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-57","ModuleId":"BigQuantSpace.derived_feature_extractor.derived_feature_extractor-v3","ModuleParameters":[{"Name":"date_col","Value":"date","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"instrument_col","Value":"instrument","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"drop_na","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"remove_extra_columns","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"user_functions","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"input_data","NodeId":"-57"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"features","NodeId":"-57"}],"OutputPortsInternal":[{"Name":"data","NodeId":"-57","OutputType":null}],"UsePreviousResults":true,"moduleIdForCode":8,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true},{"Id":"-102","ModuleId":"BigQuantSpace.trade.trade-v4","ModuleParameters":[{"Name":"start_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"end_date","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"handle_data","Value":"# 回测引擎:每日数据处理函数,每天执行一次\ndef bigquant_run(context, data):\n\n #------------------------------------------止损模块START--------------------------------------------\n date = data.current_dt.strftime('%Y-%m-%d') \n equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}\n \n # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断\n current_stoploss_stock = [] \n if len(equities) > 0:\n for i in equities.keys():\n stock_market_price = data.current(context.symbol(i), 'price') # 最新市场价格\n last_sale_date = equities[i].last_sale_date # 上次交易日期\n delta_days = data.current_dt - last_sale_date \n hold_days = delta_days.days # 持仓天数\n # 建仓以来的最高价\n highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()\n # 确定止损位置\n stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.1\n record('止损位置', stoploss_line)\n # 如果价格下穿止损位置\n if stock_market_price < stoploss_line:\n context.order_target_percent(context.symbol(i), 0) \n current_stoploss_stock.append(i)\n print('日期:', date , '股票:', i, '出现止损状况')\n #-------------------------------------------止损模块END--------------------------------------------------\n\n \n # 获取今日的日期\n today = data.current_dt.strftime('%Y-%m-%d') \n # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表\n stock_hold_now = {e.symbol: p.amount * p.last_sale_price\n for e, p in context.portfolio.positions.items()}\n\n # 记录用于买入股票的可用现金,因为是早盘卖股票,需要记录卖出的股票市值并在买入下单前更新可用现金;\n # 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用\n cash_for_buy = context.portfolio.cash \n \n try:\n buy_stock = context.daily_stock_buy[today] # 当日符合买入条件的股票\n except:\n buy_stock=[] # 如果没有符合条件的股票,就设置为空\n \n try:\n sell_stock = context.daily_stock_sell[today] # 当日符合卖出条件的股票\n except:\n sell_stock=[] # 如果没有符合条件的股票,就设置为空\n \n # 需要卖出的股票:已有持仓中符合卖出条件的股票\n stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]\n # 需要买入的股票:没有持仓且符合买入条件的股票\n stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now ] \n # 需要调仓的股票:已有持仓且不符合卖出条件的股票\n stock_to_adjust=[ i for i in stock_hold_now if i not in sell_stock ]\n \n # 如果有卖出信号\n if len(stock_to_sell)>0:\n for instrument in stock_to_sell:\n if instrument in current_stoploss_stock:\n continue\n sid = context.symbol(instrument) # 将标的转化为equity格式\n cur_position = context.portfolio.positions[sid].amount # 持仓\n if cur_position > 0 and data.can_trade(sid):\n context.order_target_percent(sid, 0) # 全部卖出 \n # 因为设置的是早盘卖出早盘买入,需要根据卖出的股票更新可用现金;如果设置尾盘卖出早盘买入,则不需更新可用现金(可以删除下面的语句)\n cash_for_buy += stock_hold_now[instrument]\n \n # 如果有买入信号/有持仓\n if len(stock_to_buy)>0:\n weight = 1/len(set(stock_to_buy+stock_to_adjust)) # 每只股票的比重为等资金比例持有\n for instrument in set(stock_to_buy+stock_to_adjust):\n sid = context.symbol(instrument) # 将标的转化为equity格式\n if data.can_trade(sid):\n context.order_target_value(sid, weight*cash_for_buy) # 买入","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"prepare","Value":"# 回测引擎:准备数据,只执行一次\ndef bigquant_run(context):\n # 加载预测数据\n df = context.options['data'].read_df()\n\n # 函数:求满足开仓条件的股票列表\n def open_pos_con(df):\n return list(df[df['buy_condition']>0].instrument)\n\n # 函数:求满足平仓条件的股票列表\n def close_pos_con(df):\n return list(df[df['sell_condition']>0].instrument)\n\n # 每日买入股票的数据框\n context.daily_stock_buy= df.groupby('date').apply(open_pos_con)\n # 每日卖出股票的数据框\n context.daily_stock_sell= df.groupby('date').apply(close_pos_con)","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"initialize","Value":"# 回测引擎:初始化函数,只执行一次\ndef bigquant_run(context):\n\n # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数\n context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))\n","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"before_trading_start","Value":"","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"volume_limit","Value":0.025,"ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_buy","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"order_price_field_sell","Value":"open","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"capital_base","Value":"1000000","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"auto_cancel_non_tradable_orders","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"data_frequency","Value":"daily","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"price_type","Value":"后复权","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"product_type","Value":"股票","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"plot_charts","Value":"True","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"backtest_only","Value":"False","ValueType":"Literal","LinkedGlobalParameter":null},{"Name":"benchmark","Value":"","ValueType":"Literal","LinkedGlobalParameter":null}],"InputPortsInternal":[{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"instruments","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"options_data","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"history_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"benchmark_ds","NodeId":"-102"},{"DataSourceId":null,"TrainedModelId":null,"TransformModuleId":null,"Name":"trading_calendar","NodeId":"-102"}],"OutputPortsInternal":[{"Name":"raw_perf","NodeId":"-102","OutputType":null}],"UsePreviousResults":false,"moduleIdForCode":3,"IsPartOfPartialRun":null,"Comment":"","CommentCollapsed":true}],"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-24' Position='765,20,200,200'/><NodePosition Node='287d2cb0-f53c-4101-bdf8-104b137c8601-62' Position='1073,124,200,200'/><NodePosition Node='-86' Position='1078,418,200,200'/><NodePosition Node='-50' Position='1078,232,200,200'/><NodePosition Node='-57' Position='1076,327,200,200'/><NodePosition Node='-102' Position='1048,529,200,200'/></NodePositions><NodeGroups /></DataV1>"},"IsDraft":true,"ParentExperimentId":null,"WebService":{"IsWebServiceExperiment":false,"Inputs":[],"Outputs":[],"Parameters":[{"Name":"交易日期","Value":"","ParameterDefinition":{"Name":"交易日期","FriendlyName":"交易日期","DefaultValue":"","ParameterType":"String","HasDefaultValue":true,"IsOptional":true,"ParameterRules":[],"HasRules":false,"MarkupType":0,"CredentialDescriptor":null}}],"WebServiceGroupId":null,"SerializedClientData":"<?xml version='1.0' encoding='utf-16'?><DataV1 xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Meta /><NodePositions></NodePositions><NodeGroups /></DataV1>"},"DisableNodesUpdate":false,"Category":"user","Tags":[],"IsPartialRun":true}
      In [12]:
      # 本代码由可视化策略环境自动生成 2019年1月24日 10:29
      # 本代码单元只能在可视化模式下编辑。您也可以拷贝代码,粘贴到新建的代码单元或者策略,然后修改。
      
      
      # 回测引擎:每日数据处理函数,每天执行一次
      def m3_handle_data_bigquant_run(context, data):
      
          #------------------------------------------止损模块START--------------------------------------------
          date = data.current_dt.strftime('%Y-%m-%d')  
          equities = {e.symbol: p for e, p in context.portfolio.positions.items() if p.amount>0}
          
          # 新建当日止损股票列表是为了handle_data 策略逻辑部分不再对该股票进行判断
          current_stoploss_stock = [] 
          if len(equities) > 0:
              for i in equities.keys():
                  stock_market_price = data.current(context.symbol(i), 'price')  # 最新市场价格
                  last_sale_date = equities[i].last_sale_date   # 上次交易日期
                  delta_days = data.current_dt - last_sale_date  
                  hold_days = delta_days.days # 持仓天数
                  # 建仓以来的最高价
                  highest_price_since_buy = data.history(context.symbol(i), 'high', hold_days, '1d').max()
                  # 确定止损位置
                  stoploss_line = highest_price_since_buy - highest_price_since_buy * 0.1
                  record('止损位置', stoploss_line)
                  # 如果价格下穿止损位置
                  if stock_market_price < stoploss_line:
                      context.order_target_percent(context.symbol(i), 0)     
                      current_stoploss_stock.append(i)
                      print('日期:', date , '股票:', i, '出现止损状况')
          #-------------------------------------------止损模块END--------------------------------------------------
      
             
          # 获取今日的日期
          today = data.current_dt.strftime('%Y-%m-%d')  
          # 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
          stock_hold_now = {e.symbol: p.amount * p.last_sale_price
                       for e, p in context.portfolio.positions.items()}
      
          # 记录用于买入股票的可用现金,因为是早盘卖股票,需要记录卖出的股票市值并在买入下单前更新可用现金;
          # 如果是早盘买尾盘卖,则卖出时不需更新可用现金,因为尾盘卖出股票所得现金无法使用
          cash_for_buy = context.portfolio.cash    
          
          try:
              buy_stock = context.daily_stock_buy[today]  # 当日符合买入条件的股票
          except:
              buy_stock=[]  # 如果没有符合条件的股票,就设置为空
          
          try:
              sell_stock = context.daily_stock_sell[today]  # 当日符合卖出条件的股票
          except:
              sell_stock=[] # 如果没有符合条件的股票,就设置为空
          
          # 需要卖出的股票:已有持仓中符合卖出条件的股票
          stock_to_sell = [ i for i in stock_hold_now if i in sell_stock ]
          # 需要买入的股票:没有持仓且符合买入条件的股票
          stock_to_buy = [ i for i in buy_stock if i not in stock_hold_now ]  
          # 需要调仓的股票:已有持仓且不符合卖出条件的股票
          stock_to_adjust=[ i for i in stock_hold_now if i not in sell_stock ]
          
          # 如果有卖出信号
          if len(stock_to_sell)>0:
              for instrument in stock_to_sell:
                  if instrument in current_stoploss_stock:
                      continue
                  sid = context.symbol(instrument) # 将标的转化为equity格式
                  cur_position = context.portfolio.positions[sid].amount # 持仓
                  if cur_position > 0 and data.can_trade(sid):
                      context.order_target_percent(sid, 0) # 全部卖出 
                      # 因为设置的是早盘卖出早盘买入,需要根据卖出的股票更新可用现金;如果设置尾盘卖出早盘买入,则不需更新可用现金(可以删除下面的语句)
                      cash_for_buy += stock_hold_now[instrument]
          
          # 如果有买入信号/有持仓
          if len(stock_to_buy)>0:
              weight = 1/len(set(stock_to_buy+stock_to_adjust)) # 每只股票的比重为等资金比例持有
              for instrument in set(stock_to_buy+stock_to_adjust):
                  sid = context.symbol(instrument) # 将标的转化为equity格式
                  if  data.can_trade(sid):
                      context.order_target_value(sid, weight*cash_for_buy) # 买入
      # 回测引擎:准备数据,只执行一次
      def m3_prepare_bigquant_run(context):
          # 加载预测数据
          df = context.options['data'].read_df()
      
          # 函数:求满足开仓条件的股票列表
          def open_pos_con(df):
              return list(df[df['buy_condition']>0].instrument)
      
          # 函数:求满足平仓条件的股票列表
          def close_pos_con(df):
              return list(df[df['sell_condition']>0].instrument)
      
          # 每日买入股票的数据框
          context.daily_stock_buy= df.groupby('date').apply(open_pos_con)
          # 每日卖出股票的数据框
          context.daily_stock_sell= df.groupby('date').apply(close_pos_con)
      # 回测引擎:初始化函数,只执行一次
      def m3_initialize_bigquant_run(context):
      
          # 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
          context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
      
      
      m1 = M.input_features.v1(
          features="""# #号开始的表示注释
      # 多个特征,每行一个,可以包含基础特征和衍生特征
      buy_condition=where(mean(close_0,5)>mean(close_0,50),1,0)
      sell_condition=where(mean(close_0,5)<mean(close_0,50),1,0)""",
          m_cached=False
      )
      
      m2 = M.instruments.v2(
          start_date=T.live_run_param('trading_date', '2012-05-29'),
          end_date=T.live_run_param('trading_date', '2017-11-08'),
          market='CN_STOCK_A',
          instrument_list='600519.SHA',
          max_count=0
      )
      
      m7 = M.general_feature_extractor.v7(
          instruments=m2.data,
          features=m1.data,
          start_date='',
          end_date='',
          before_start_days=30
      )
      
      m8 = M.derived_feature_extractor.v3(
          input_data=m7.data,
          features=m1.data,
          date_col='date',
          instrument_col='instrument',
          drop_na=False,
          remove_extra_columns=False
      )
      
      m6 = M.dropnan.v1(
          input_data=m8.data
      )
      
      m3 = M.trade.v4(
          instruments=m2.data,
          options_data=m6.data,
          start_date='',
          end_date='',
          handle_data=m3_handle_data_bigquant_run,
          prepare=m3_prepare_bigquant_run,
          initialize=m3_initialize_bigquant_run,
          volume_limit=0.025,
          order_price_field_buy='open',
          order_price_field_sell='open',
          capital_base=1000000,
          auto_cancel_non_tradable_orders=True,
          data_frequency='daily',
          price_type='后复权',
          product_type='股票',
          plot_charts=True,
          backtest_only=False,
          benchmark=''
      )
      
      [2019-01-24 10:28:38.829416] INFO: bigquant: input_features.v1 开始运行..
      [2019-01-24 10:28:38.835972] INFO: bigquant: input_features.v1 运行完成[0.006565s].
      [2019-01-24 10:28:38.838659] INFO: bigquant: instruments.v2 开始运行..
      [2019-01-24 10:28:38.842732] INFO: bigquant: 命中缓存
      [2019-01-24 10:28:38.843744] INFO: bigquant: instruments.v2 运行完成[0.005082s].
      [2019-01-24 10:28:38.849113] INFO: bigquant: general_feature_extractor.v7 开始运行..
      [2019-01-24 10:28:38.853107] INFO: bigquant: 命中缓存
      [2019-01-24 10:28:38.853925] INFO: bigquant: general_feature_extractor.v7 运行完成[0.004821s].
      [2019-01-24 10:28:38.856613] INFO: bigquant: derived_feature_extractor.v3 开始运行..
      [2019-01-24 10:28:38.934132] INFO: derived_feature_extractor: 提取完成 buy_condition=where(mean(close_0,5)>mean(close_0,50),1,0), 0.008s
      [2019-01-24 10:28:38.943099] INFO: derived_feature_extractor: 提取完成 sell_condition=where(mean(close_0,5)<mean(close_0,50),1,0), 0.008s
      [2019-01-24 10:28:38.963278] INFO: derived_feature_extractor: /y_2012, 165
      [2019-01-24 10:28:38.989117] INFO: derived_feature_extractor: /y_2013, 238
      [2019-01-24 10:28:39.025541] INFO: derived_feature_extractor: /y_2014, 245
      [2019-01-24 10:28:39.054356] INFO: derived_feature_extractor: /y_2015, 244
      [2019-01-24 10:28:39.082826] INFO: derived_feature_extractor: /y_2016, 244
      [2019-01-24 10:28:39.106189] INFO: derived_feature_extractor: /y_2017, 207
      [2019-01-24 10:28:39.155087] INFO: bigquant: derived_feature_extractor.v3 运行完成[0.298437s].
      [2019-01-24 10:28:39.158915] INFO: bigquant: dropnan.v1 开始运行..
      [2019-01-24 10:28:39.240877] INFO: dropnan: /y_2012, 165/165
      [2019-01-24 10:28:39.268540] INFO: dropnan: /y_2013, 238/238
      [2019-01-24 10:28:39.298469] INFO: dropnan: /y_2014, 245/245
      [2019-01-24 10:28:39.341826] INFO: dropnan: /y_2015, 244/244
      [2019-01-24 10:28:39.388205] INFO: dropnan: /y_2016, 244/244
      [2019-01-24 10:28:39.436448] INFO: dropnan: /y_2017, 207/207
      [2019-01-24 10:28:39.451129] INFO: dropnan: 行数: 1343/1343
      [2019-01-24 10:28:39.454232] INFO: bigquant: dropnan.v1 运行完成[0.295272s].
      [2019-01-24 10:28:39.478837] INFO: bigquant: backtest.v8 开始运行..
      [2019-01-24 10:28:39.481688] INFO: bigquant: biglearning backtest:V8.1.6
      [2019-01-24 10:28:42.204641] INFO: bigquant: product_type:stock by specified
      [2019-01-24 10:28:53.958079] INFO: bigquant: 读取股票行情完成:1568
      [2019-01-24 10:28:54.006757] INFO: algo: TradingAlgorithm V1.4.2
      [2019-01-24 10:28:54.341509] INFO: algo: trading transform...
      日期: 2012-08-16 股票: 600519.SHA 出现止损状况
      日期: 2014-03-26 股票: 600519.SHA 出现止损状况
      日期: 2014-05-05 股票: 600519.SHA 出现止损状况
      日期: 2015-01-14 股票: 600519.SHA 出现止损状况
      日期: 2015-02-02 股票: 600519.SHA 出现止损状况
      日期: 2015-05-05 股票: 600519.SHA 出现止损状况
      日期: 2015-06-04 股票: 600519.SHA 出现止损状况
      日期: 2015-06-18 股票: 600519.SHA 出现止损状况
      [2019-01-24 10:29:01.727346] INFO: Performance: Simulated 1327 trading days out of 1327.
      [2019-01-24 10:29:01.729245] INFO: Performance: first open: 2012-05-29 09:30:00+00:00
      [2019-01-24 10:29:01.739814] INFO: Performance: last close: 2017-11-08 15:00:00+00:00
      
      • 收益率474.87%
      • 年化收益率39.39%
      • 基准收益率54.82%
      • 阿尔法0.3
      • 贝塔0.31
      • 夏普比率1.43
      • 胜率0.53
      • 盈亏比5.8
      • 收益波动率22.94%
      • 信息比率0.06
      • 最大回撤16.31%
      [2019-01-24 10:29:04.332712] INFO: bigquant: backtest.v8 运行完成[24.853869s].
      

      小结: 本文为大家介绍了几种止盈止损方案模板,大家只需复制并粘贴到模板策略交易模块的主函数中即可使用,合理的利用止盈止损技巧,可以帮助大家构建更加成熟的量化策略,享有更加长远的收益。


         本文由BigQuant宽客学院推出,版权归BigQuant所有,转载请注明出处。
      


      股票被套等反弹了一天再抛,如何修改代码