I saw a code on reddit and I am trying to figure out how the author got the end result. I tried display(df), but could only get VWAP to show, but not the upper and lower vwap.
I just wanted to clarify, the issue is that the upper and lower vwap are not extracted at all. I don't think it's because the code is not showing the full thing.
I have also tried print(key), print(day_data), print(df.loc) etc.
Open High Low Close \
Datetime
2021-10-21 09:30:00-04:00 856.000000 861.515015 855.504578 859.280029
2021-10-21 09:31:00-04:00 859.568970 862.219910 857.000000 861.499878
2021-10-21 09:32:00-04:00 861.240295 863.729980 859.500000 863.090027
2021-10-21 09:33:00-04:00 863.489990 865.330017 862.000000 862.929993
2021-10-21 09:34:00-04:00 868.000000 869.199890 868.000000 869.199890
... ... ... ... ...
2021-10-21 15:55:00-04:00 894.380005 894.750000 894.260071 894.489990
2021-10-21 15:56:00-04:00 894.510010 894.559998 894.200073 894.369995
2021-10-21 15:57:00-04:00 894.299988 894.469971 894.075989 894.210022
2021-10-21 15:58:00-04:00 894.197021 894.469971 894.000000 894.140015
2021-10-21 15:59:00-04:00 894.140015 894.150024 893.500000 893.599976
Volume Dividends Stock Splits vwap
Datetime
2021-10-21 09:30:00-04:00 908134 0 0 858.766541
2021-10-21 09:31:00-04:00 247621 0 0 859.082215
2021-10-21 09:32:00-04:00 192159 0 0 859.513382
2021-10-21 09:33:00-04:00 241155 0 0 860.106245
2021-10-21 09:34:00-04:00 256713 0 0 861.315370
... ... ... ... ...
2021-10-21 15:55:00-04:00 94604 0 0 888.946133
2021-10-21 15:56:00-04:00 74274 0 0 888.959541
2021-10-21 15:57:00-04:00 132418 0 0 888.982735
2021-10-21 15:58:00-04:00 145736 0 0 889.007794
2021-10-21 15:59:00-04:00 189346 0 0 889.037185
https://www.reddit.com/r/algotrading/comments/gman9v/calculating_vwap_bands/
Thank you.
def caclulate_vwap_bands(self, df, curr_date, num_days):
key = 'VWAP'
def stdev(df):
return df[key].values.std(ddof=0)
curr_position = datetime.datetime.combine(curr_date, time(hour=9, minute=31))
df.loc[str(curr_position), 'UPPER_VWAP'] = None
df.loc[str(curr_position), 'LOWER_VWAP'] = None
df.loc[str(curr_position), 'STD_VWAP'] = None
for _ in range(0, num_days):
day_data = df[curr_date == df.index.date].copy()
for _ in range(31, 60):
try:
std = stdev(day_data[day_data.index.time <= curr_position.time()])
df.loc[str(curr_position), 'UPPER_VWAP'] = df.loc[str(curr_position), key] + (2 * std)
df.loc[str(curr_position), 'LOWER_VWAP'] = df.loc[str(curr_position), key] - (2 * std)
df.loc[str(curr_position), 'STD_VWAP'] = std
except:
print(curr_position, "doesn't exist, skipping")
curr_position += timedelta(minutes=1)
for _ in range(10, 17):
while True:
if curr_position.time() >= time(hour=16, minute=1):
break
std = stdev(day_data[day_data.index.time <= curr_position.time()])
try:
df.loc[str(curr_position), 'UPPER_VWAP'] = df.loc[str(curr_position), key] + (2 * std)
df.loc[str(curr_position), 'LOWER_VWAP'] = df.loc[str(curr_position), key] - (2 * std)
df.loc[str(curr_position), 'STD_VWAP'] = std
except:
print(curr_position, "doesn't exist, skipping")
curr_position += timedelta(minutes=1)
curr_position += timedelta(days=1)
curr_position = curr_position.replace(hour=9, minute=31)
End result should be this
VWAP UPPER_VWAP LOWER_VWAP STD_VWAP
date
2020-05-15 09:31:00 282.4667 282.466700 282.466700 0.000000
2020-05-15 09:32:00 282.4727 282.483300 282.462100 0.005300
2020-05-15 09:33:00 282.5034 282.514517 282.492283 0.005558
2020-05-15 09:34:00 282.5159 282.526233 282.505567 0.005166
2020-05-15 09:35:00 282.4693 282.479148 282.459452 0.004924
... ... ... ... ...
2020-05-15 15:56:00 284.1254 285.181526 283.069274 0.528063
2020-05-15 15:57:00 284.1421 285.201229 283.082971 0.529565
2020-05-15 15:58:00 284.1532 285.215345 283.091055 0.531072
2020-05-15 15:59:00 284.1712 285.236408 283.105992 0.532604
2020-05-15 16:00:00 284.2078 285.276242 283.139358 0.534221