通达信交易接口实现海龟交易法则核心策略分享

今天说一下著名的海龟交易法则,验证下交易思路与方法,检验其在中国市场的表现和简单的策略代码分享

海龟的核心交易法则就是唐奇安趋势突破交易法,这个指标是在该标的20天内的最高价最低价来显示市场价格的波动性,上下两条线形成了一个通道,按照这个通道向上突破进场和向下突破离场的买卖信号定位。

下面是简单的代码分享:

def turtle(context):
    global record                                                                                
    #获取投资者账户、投资股票、价格数据、持仓数据、账户金额
    account = context.get_account('fantasy_account')                                             
    current_universe = context.get_universe(asset_type = 'stock',exclude_halt=True)                 
    security_position = account.get_positions()                                                   
    cash = account.cash  

    #构建唐奇安通道
    history = context.history(current_universe,['closePrice','lowPrice','highPrice'],time_range, rtype='array')
    for sec in current_universe:                                                                  
        close = history[sec]['closePrice']                                                          
        low = history[sec]['lowPrice']                                                            
        high  = history[sec]['highPrice']                                                           
        current_price = context.current_price(sec)                                                  

        #计算真实波幅,
        atr = ta.ATR(high, low, close, atrlength)[-1]

        #入场:突破唐奇安通道上轨,买入1单位股票
        if  current_price> high[-DC_range:-1].max() and sec not in security_position:                     
            unit = calcUnit(account.portfolio_value,atr)                                                 
            account.order_to(sec,unit)                                                                   

            #清空record中sec过期的记录
            if len(record)!=0:                                                                 
                record = record[record['symbol']!=sec]                                                               #记录股票,加仓次数及买入价格                                
            record = record.append(pd.DataFrame({'symbol':[sec],'add_time':[1],'last_buy_price':[current_price]}))     
            continue

        #加仓:若股价在上一次买入价格的基础上上涨了0.5N,则加仓1个单位的股票
        elif sec in security_position:                                                          
            try:
                last_price =float(record[record['symbol'] == sec]['last_buy_price'])             
                add_price =last_price + 0.5 * atr                                                
                add_unit = float(record[record['symbol'] == sec]['add_time'])                         

                # 加仓次数小于4次
                if current_price > add_price and add_unit < limit_unit:                                
                    unit = calcUnit(account.portfolio_value,atr)                                        
                    account.order(sec,unit)                                                                   

                    # 记录加仓次数
                    record.loc[record['symbol']== sec,'add_time'] += 1
                    record.loc[record['symbol']== sec,'last_buy_price'] = current_price                             
                #离场(止损止盈):下跌 2ATR时或当股价跌破10日唐奇安通道,清仓离场 
                elif current_price< low[-int(DC_range/2):-1].min() or current_price < (last_price - 2*atr):
                    account.order_to(sec,0)                              

                    # 将卖出股票的记录清空 
                    record = record[record['symbol']!=sec]
            except:
                pass

以上是海龟交易法则的简单分享,想要测试的小伙伴可以在实盘交易接口的支持下尝试一下。注意找一些可以提供试用的,避免浪费。策略千万种,总有适合自己的。有兴趣的可以添加名片咨询一下。

猜你喜欢

转载自blog.csdn.net/LegendCode/article/details/128614173