identify_fractals.py 588 B

123456789101112131415
  1. def identify_fractals(df):
  2. """
  3. 标记顶底分型
  4. :param df: 无包含关系的 K 线 DataFrame
  5. :return: 顶底分型的索引和类型列表 [(index, 'Top'), (index, 'Bottom')]
  6. """
  7. fractals = []
  8. for i in range(1, len(df) - 1):
  9. # 顶分型
  10. if df.loc[i, 'High'] > df.loc[i-1, 'High'] and df.loc[i, 'High'] > df.loc[i+1, 'High']:
  11. fractals.append((i, 'Top'))
  12. # 底分型
  13. elif df.loc[i, 'Low'] < df.loc[i-1, 'Low'] and df.loc[i, 'Low'] < df.loc[i+1, 'Low']:
  14. fractals.append((i, 'Bottom'))
  15. return fractals