分享bigquant量化技术指标的自定义策略

bigquant量化技术指标的自定义策略单纯通过计算MA多头指标,以短线 =5, MA 长线 =20, 当 MA 短线在 MA 长线上方时,买入股票,否则卖出股票。

调仓周期为 20 个工作日,而且最大允许持仓股票数为 10 只,最终得到这个策略的回测结果如下:

使用代码:

def handle_data(context, data):
    
    if context.trading_day_index < context.observation:  
        return
     
    ##########
    # 1. 资金分配
    # 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
    # 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
    is_staging = context.trading_day_index < context.next_trade_day # 是否在建仓期间(前 hold_days 天)
    cash_avg = context.portfolio.portfolio_value / context.hold_days
    cash_for_buy = context.portfolio.cash
    cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
    positions = {e.symbol: p.amount * p.last_sale_price
                 for e, p in context.portfolio.positions.items()}

    if not is_staging:
         # 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按预测的排序末位淘汰
        context.next_trade_day = context.next_trade_day + context.hold_days
        equities = {e.symbol: e for e, p in context.portfolio.positions.items()}
        #instruments = list(reversed(list(instrument)))
        for equity in equities:
            context.order_target(equity, 0)

        # 3. 生成买入订单:按机器学习算法预测的排序,买入前面的stock_count只股票
        candidates = []
        for i, instrument in enumerate(instruments):
            sid = context.symbol(instruments[i])
            price = data.current(sid, 'price') # 最新价格
            # 读取历史数据
            prices = data.history(sid, 'price', context.observation, '1d')
            
            if np.isnan(price):
                continue

            # 计算短期及长期MA
            short_mavg = data.history(sid, 'price',context.ma_short_period, '1d').mean() # 短期均线值
            long_mavg = data.history(sid, 'price',context.ma_long_period, '1d').mean() # 长期均线值

            # 计算现在portfolio中股票的仓位
            cur_position = context.portfolio.positions[sid].amount

            # 策略逻辑卖出逻辑(下穿)
            # 每次调仓周期到卖掉所有股票
            #if cur_position > 0 and data.can_trade(sid):
            #    context.order_target_value(sid, 0)

            # 买入逻辑(上穿)
            if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid):  
                #context.order(sid, int(cash/price/100)*100) # 买入
                candidates.append((sid, price))

        candidates = candidates[: context.stock_count]
        candidate_num =  len(candidates)
        if candidate_num > 0:
            context.stock_weights = T.norm([1 / candidate_num for i in range(0, candidate_num)])
            buy_cash_weights = context.stock_weights
            print(" buy_cash_weights ", " => ", buy_cash_weights)
            buy_sid_prices = list(candidates)
            #max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
            for i, instrument in enumerate(buy_sid_prices):
                cash = cash_for_buy * buy_cash_weights[i]
                if cash > 0:
                    sou =  int(cash/instrument[1]/100)*100
                    context.order(instrument[0], sou)
                    print(context.trading_day_index, " => ", instrument[0], " buy ", cash)
        else:
            print(context.trading_day_index, " => ", " no candidate ")

接口来源:https://gitee.com/l2gogogo

分析结果发现,实际回测结果比经典方案要差,初步评估是放弃了一些低权重的股票,导致选择股票比较单一 所致。

在分享bigquant量化技术指标的自定义策略过程中发现,其实直接使用量化交易接口更加方便快捷,想了解更多可以点击下方qq名片。

你可能感兴趣的:(人工智能,python)