Pharos
TAKline

二合一形态 (2+1组合检测)

多个TA.CDL函数 - 组合应用策略

返回K线形态目录


策略思路

在实战中,单一K线形态可能产生假信号。组合使用多个形态可以提高准确率:

组合方案

1. 反转形态组合

同时检测多个反转形态,增强信号可信度:

python
def onTick():
    exchange.SetContractType("swap")
    records = exchange.GetRecords()
    
    if len(records) < 100:
        return
    
    # 同时检测多个看涨反转形态
    hammer = TA.CDLHAMMER(records)
    engulfing = TA.CDLENGULFING(records)
    piercing = TA.CDLPIERCING(records)
    morning_star = TA.CDLMORNINGSTAR(records)
    
    # 统计看涨信号数量
    bullish_count = 0
    signals = []
    
    if hammer[-1] == 100:
        bullish_count += 1
        signals.append("锤子线")
    
    if engulfing[-1] == 100:
        bullish_count += 1
        signals.append("看涨吞没")
    
    if piercing[-1] == 100:
        bullish_count += 1
        signals.append("刺透形态")
    
    if morning_star[-1] == 100:
        bullish_count += 1
        signals.append("早晨之星")
    
    # 两个或以上信号才交易
    if bullish_count >= 2:
        rsi = TA.RSI(records, 14)
        
        if rsi[-1] < 40:
            Log(f"检测到{bullish_count}个看涨信号:{', '.join(signals)}")
            
            exchange.SetDirection("buy")
            exchange.Buy(-1, bullish_count * 0.5)  # 信号越多仓位越大

2. 形态 + 趋势确认

形态配合趋势指标:

python
def check_reversal_with_trend():
    records = exchange.GetRecords()
    
    # K线形态
    hammer = TA.CDLHAMMER(records)
    
    # 趋势指标
    ma20 = TA.MA(records, 20)
    ma60 = TA.MA(records, 60)
    macd = TA.MACD(records, 12, 26, 9)
    rsi = TA.RSI(records, 14)
    
    if hammer[-1] == 100:  # 出现锤子线
        # 确认下跌趋势中
        if records[-1].Close < ma20[-1] < ma60[-1]:
            # 超卖状态
            if rsi[-1] < 30:
                # MACD即将金叉
                if macd[0][-1] > macd[0][-2]:  # DIF上升
                    Log("锤子线 + 超卖 + MACD走强 = 强烈买入信号")
                    return True
    
    return False

3. 反转 + 持续形态结合

识别趋势的不同阶段:

python
# 全局变量
position_stage = None  # None, 'entry', 'add'

def onTick():
    global position_stage
    
    records = exchange.GetRecords()
    
    # 反转形态
    morning_star = TA.CDLMORNINGSTAR(records)
    
    # 持续形态
    rising_three = TA.CDLRISEFALL3METHODS(records)
    strike3 = TA.CDL3LINESTRIKE(records)
    
    # 阶段1:反转入场
    if position_stage is None and morning_star[-1] == 100:
        Log("检测到早晨之星,反转入场")
        exchange.SetDirection("buy")
        exchange.Buy(-1, 1)
        position_stage = 'entry'
    
    # 阶段2:持续加仓
    elif position_stage == 'entry':
        if rising_three[-1] == 100 or strike3[-1] == 100:
            Log("检测到持续形态,加仓")
            exchange.SetDirection("buy")
            exchange.Buy(-1, 0.5)
            position_stage = 'add'

注意事项

  1. 避免过度拟合:不要同时用太多条件
  2. 信号冲突处理:看涨和看跌信号同时出现时观望
  3. 仓位管理:信号越强,仓位越大
  4. 止损保护:形态失败及时止损

返回K线形态目录 | 返回TA指标