Python实现TradingView中的PivotHigh和PivotLow

2021-12-20 / 0 评论 / 922 阅读
温馨提示:
本文最后更新于2021-12-20,已超过半年没有更新,若内容或图片失效,请留言反馈。

直接上代码:

"""枢轴点 最高价"""
def PivotHigh(df, left, right=0):
    right = right if right else left
    df['pivot'] = 0.0
    for i in range(len(df)):
        if i >= left+right:
            rolling = df['High'][i-right-left:i+1].values
            m = max(rolling)
            #print(GetTime(df['Time'][i], "%m-%d %H:%M"), df['High'][i-right], m, rolling)
            if df['High'][i-right] == m:
                df['pivot'].values[i] = m
    return df['pivot']

"""枢轴点 最低价"""
def PivotLow(df, left, right=0):
    right = right if right else left
    df['rollingLow'] = df['Low'].rolling(left+right).min()
    df['pivot'] = 0.0
    for i in range(len(df)):
        if i >= left+right:
            rolling = df['Low'][i-right-left:i+1].values
            m = min(rolling)
            if df['Low'][i-right] == m:
                df['pivot'].values[i] = m
    return df['pivot']

使用:

import pandas as pd
df = pd.DataFrame(r) #r为K线数据

df['PivotHigh'] = PivotHigh(df.copy(), 10, 10)
df['PivotLow'] = PivotLow(df.copy(), 10, 10)

评论一下?

OωO
取消