Loading ...

Python实现TradingView中的PivotHigh和PivotLow

Author: 苏慕白, Created: 2021-12-24 09:17:00, Updated: 2021-12-24 14:14:34

直接上代码:

"""枢轴点 最高价"""
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)

More

小小梦 可以写成策略在FMZ上展示公开。