Reputation: 176
I want to convert the following Pine script to python to calculate the value for vwap1 variable without plotting the results, I just want to calculate the value for vwap1:
wapScore(pds) =>
mean = sum(volume*close,pds)/sum(volume,pds)
vwapsd = sqrt(sma(pow(close-mean, 2), pds) )
(close-mean)/vwapsd
vwap1 = input(48)
plot(vwapScore(vwap1),title="ZVWAP2-48",color=#35e8ff, linewidth=2,transp=0.75)
I have tried the following:
def calculate_SMA(ser, days):
sma = ser.rolling(window=days).mean()
return sma
def calculate_Zscore(pds, volume, close):
mean = sum(volume * close, pds) / sum(volume, pds)
vwapsd = np.sqrt(calculate_SMA(pow(close - mean, 2), pds))
return (close - mean) / vwapsd
And I am using calculate_Zscore
function to calculate the value and add it to pandas dataframe
but it gives me different values rather than the values on trading view
candles["Zscroe-48"] = zscore.calculate_Zscore(48,candles["Volume"], candles["Close"])
Could someone please tell me what I am doing wrong or what I am missing ?
Upvotes: 0
Views: 2136
Reputation: 11
I think problem is in mean
.
Try this:
def calculate_SMA(ser, pds):
sma = ser.rolling(window=pds).mean()
return sma
def calculate_Zscore(pds,df):
df['mean'] = ((df['Close']*df['Volume']).rolling(pds).sum())/df['Volume'].rolling(pds).sum()
#mean = sum(volume * close, pds) / sum(volume, pds)
df['vwapsd'] = np.sqrt(calculate_SMA(pow(df['Close'] - df['mean'], 2), pds))
#df['z-scoor'] = (df['Close'] - df['mean']) / df['vwapsd']
return (df['Close'] - df['mean']) / df['vwapsd']
Upvotes: 1