Python实现TradingView中的PivotHigh和PivotLow
温馨提示:本文最后更新于2021年12月20日 19:45,若内容或图片失效,请在下方留言或联系博主。
直接上代码:
"""枢轴点 最高价"""
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)