Tongdaxin trading interface realizes core strategy sharing of turtle trading rules

Today I will talk about the famous turtle trading rules, verify the trading ideas and methods, test its performance in the Chinese market and share simple strategy codes

The core trading rule of the Turtle is Donchian’s trend breakthrough trading method. This indicator shows the volatility of the market price by showing the highest price and the lowest price of the target within 20 days. The upper and lower lines form a channel, and follow this channel to break through and enter the market And the positioning of buying and selling signals for breaking down and leaving the market.

Here is a simple code to share:

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

The above is a simple sharing of the Turtle trading rules. Those who want to test can try it with the support of the real trading interface. Pay attention to find some that can provide trials to avoid waste. There are thousands of strategies, and there is always one that suits you. If you are interested, you can add a business card for consultation.

Guess you like

Origin blog.csdn.net/LegendCode/article/details/128614173