Pharos
TAMath ops

MAXINDEX - 最大值索引

函数说明

返回指定周期内最大值的索引位置(距离当前位置的K线数)。

语法

python
result = TA.MAXINDEX(records, timeperiod)

参数

参数名类型说明
recordsarrayK线数组或数值数组
timeperiodint时间周期

返回值

返回最大值索引数组,值为0到timeperiod-1的整数,表示最大值距离当前的位置

计算方法

在过去timeperiod个数据中,找到最大值的位置,返回其距离当前位置的偏移量

使用场景

  1. 识别近期高点位置
  2. Donchian通道突破验证
  3. 回调深度分析
  4. 波段高点判断

基础示例

python
def main():
    records = exchange.GetRecords()
    if len(records) < 20:
        return
    
    highs = [r['High'] for r in records]
    
    # 找到过去20根K线中最高价的位置
    max_idx = TA.MAXINDEX(highs, 20)
    
    # 最新值
    bars_since_high = max_idx[-1]
    
    Log(f"最高点出现在 {bars_since_high} 根K线之前")
    
    if bars_since_high == 0:
        Log("刚创新高!")
    elif bars_since_high < 5:
        Log("接近高点")
    else:
        Log(f"已经{bars_since_high}根K线未创新高")

高级应用

1. Donchian通道突破确认

python
def donchian_breakout():
    records = exchange.GetRecords()
    highs = [r['High'] for r in records[-50:]]
    closes = [r['Close'] for r in records[-50:]]
    
    # 20周期Donchian通道
    period = 20
    max_idx = TA.MAXINDEX(highs, period)
    
    current_high = highs[-1]
    max_value = max(highs[-period:])
    bars_since_high = max_idx[-1]
    
    # 突破判断
    if current_high >= max_value and bars_since_high == 0:
        Log("突破Donchian上轨!强势信号")
        return "BUY"
    
    # 高点老化(超过15根K线未创新高)
    if bars_since_high > 15:
        Log("高点已老化,趋势可能转弱")
        return "CAUTION"
    
    return "HOLD"

2. 回调深度分析

python
def pullback_analysis():
    records = exchange.GetRecords()
    highs = [r['High'] for r in records[-30:]]
    closes = [r['Close'] for r in records[-30:]]
    
    # 找到最高点位置
    max_idx = TA.MAXINDEX(highs, 30)[-1]
    
    if max_idx == 0:
        Log("当前正在高点")
        return
    
    # 计算回调幅度
    peak_price = highs[-max_idx - 1]  # 注意:索引是倒数的
    current_price = closes[-1]
    pullback_pct = (peak_price - current_price) / peak_price * 100
    
    Log(f"距离高点 {max_idx} 根K线")
    Log(f"回调幅度: {pullback_pct:.2f}%")
    
    # 判断回调是否是买入机会
    if 5 <= pullback_pct <= 10 and max_idx >= 3:
        Log("健康回调,可能是买入机会")
        return True
    elif pullback_pct > 15:
        Log("深度回调,趋势可能反转")
        return False
    
    return None

3. 波段高点确认

python
def swing_high_detection():
    records = exchange.GetRecords()
    highs = [r['High'] for r in records[-50:]]
    
    # 使用不同周期
    short_period = 10
    long_period = 30
    
    short_max_idx = TA.MAXINDEX(highs, short_period)[-1]
    long_max_idx = TA.MAXINDEX(highs, long_period)[-1]
    
    # 波段高点特征:短周期和长周期的高点都在同一位置
    if short_max_idx == long_max_idx and short_max_idx > 5:
        Log(f"确认波段高点在 {short_max_idx} 根K线前")
        Log("可作为阻力位参考")
        
        # 计算阻力位
        resistance = highs[-short_max_idx - 1]
        current = highs[-1]
        
        if current >= resistance * 0.98:
            Log("正在测试前期高点阻力")
            return resistance
    
    return None

4. 动态止损位置

python
def trailing_stop_loss():
    records = exchange.GetRecords()
    highs = [r['High'] for r in records[-20:]]
    lows = [r['Low'] for r in records[-20:]]
    
    # 找到最近高点位置
    max_idx = TA.MAXINDEX(highs, 20)[-1]
    
    if max_idx <= 3:
        # 最近创新高,使用紧密止损
        recent_low = min(lows[-3:])
        Log(f"紧密止损: {recent_low}")
        return recent_low
    else:
        # 高点较远,使用宽松止损
        swing_low = min(lows[-max_idx:])
        Log(f"宽松止损: {swing_low} (基于高点后的最低点)")
        return swing_low

5. 趋势强度评估

python
def trend_strength():
    records = exchange.GetRecords()
    highs = [r['High'] for r in records[-50:]]
    
    # 统计不同周期的高点位置
    max_idx_10 = TA.MAXINDEX(highs, 10)[-1]
    max_idx_20 = TA.MAXINDEX(highs, 20)[-1]
    max_idx_50 = TA.MAXINDEX(highs, 50)[-1]
    
    # 强势特征:所有周期的高点都在最近
    if max_idx_10 <= 2 and max_idx_20 <= 5 and max_idx_50 <= 10:
        Log("强势上涨趋势:持续创新高")
        return "STRONG_UPTREND"
    
    # 弱势特征:长周期高点很远
    elif max_idx_50 > 30:
        Log("趋势转弱:长期未创新高")
        return "WEAK"
    
    # 盘整
    elif max_idx_20 > 15:
        Log("横盘整理")
        return "CONSOLIDATION"
    
    return "NEUTRAL"

注意事项

  • 返回值是索引(0表示当前位置),不是K线的绝对位置
  • 访问具体值时注意索引换算:array[-index-1]
  • Python中可直接使用:max_idx = closes.index(max(closes[-period:]))
  • 多个最大值时返回最近的那个

相关函数