def identify_fractals(df): """ 标记顶底分型 :param df: 无包含关系的 K 线 DataFrame :return: 顶底分型的索引和类型列表 [(index, 'Top'), (index, 'Bottom')] """ fractals = [] for i in range(1, len(df) - 1): # 顶分型 if df.loc[i, 'High'] > df.loc[i-1, 'High'] and df.loc[i, 'High'] > df.loc[i+1, 'High']: fractals.append((i, 'Top')) # 底分型 elif df.loc[i, 'Low'] < df.loc[i-1, 'Low'] and df.loc[i, 'Low'] < df.loc[i+1, 'Low']: fractals.append((i, 'Bottom')) return fractals