I am looking to calculate daily highs and lows that are updated as they happen (intraday). Meaning that the first value of the day is taken as the high/low then updated as new highs/lows are achieved. The values should reset each day.
I saw some code in Pinescript that does exactly what I need but I have been unable to figure out how to do this in Python:
//New day bars calculation
is_new_day() =>
d = dayofweek
na(d[1]) or d != d[1]
bar_num = int(barssince(is_new_day()))
// Find the highest and lowest price of the day
sofar_high = 0.0
sofar_low = 9999999999.9
for i = 0 to bar_num
sofar_high := high[i] > sofar_high ? high[i] : sofar_high
for i = 0 to bar_num
sofar_low := low[i] < sofar_low ? low[i] : sofar_low
The piece of Python Code that I have come up with is below. I take the return values of each function and outer join into my main dataframe, that part is working at least..
import pandas as pd
class DailyRange:
def __init__(self, high, low, length, time, day):
self.high = high
self.low = low
self.length = length
self.time = time
self.day = day
def daily_high(self):
keys = [] # List of keys or i to be used later to outer join to main dataframe
highs = []
times = []
date_times = []
current_high = 0
for i in range(1, self.length - 1, 1):
# New Day
if self.day[i] > self.day[i-1] and \
self.day[i] == self.day[i+1] and \
self.high[i] > current_high:
current_high = self.high[i]
keys.append(i-1)
highs.append(current_high)
times.append(self.day[i])
date_times.append(self.time[i])
# Same Day
elif self.day[i] == self.day[i-1] and \
self.high[i] > current_high:
current_high = self.high[i]
keys.append(i-1)
highs.append(current_high)
times.append(self.day[i])
date_times.append(self.time[i])
else:
continue
high_data = {'Key': keys,
'Current Daily High': highs,
'Day of Month - High': times,
'Datetime - High': date_times}
hd_df = pd.DataFrame(high_data, columns=['Key',
'Current Daily High',
'Day of Month - High',
'Datetime - High'])
return hd_df
def daily_low(self):
keys = [] # List of keys or i to be used later to outer join to main dataframe
lows = []
times = []
date_times = []
current_low = 999999999
for i in range(1, self.length - 1, 1):
# New Day
if self.day[i] > self.day[i - 1] and \
self.day[i] == self.day[i+1] and \
self.low[i] < current_low:
current_low = self.low[i]
keys.append(i-1)
lows.append(current_low)
times.append(self.day[i])
date_times.append(self.time[i])
# Same Day
elif self.day[i] == self.day[i-1] and \
self.low[i] < current_low:
current_low = self.low[i]
keys.append(i-1)
lows.append(current_low)
times.append(self.day[i])
date_times.append(self.time[i])
else:
continue
low_data = {'Key': keys,
'Current Daily Low': lows,
'Day of Month - Low': times,
'Datetime - Low': date_times}
ld_df = pd.DataFrame(low_data, columns=['Key',
'Current Daily Low',
'Day of Month - Low',
'Datetime - Low'])
return ld_df
I have a few issues with this code and I think they are related to my if statements and the day[i-1] arguments but I have been unable to crack it.
The days are not resetting which I thought would have been taken care of by if self.day[i] > self.day[i - 1] and self.day[i] == self.day[i+1]
The appending of the [i] values happens on the preceding row for some reason.
I am fairly new to Python and would appreciate any leads. I can't help but think there is a more simple way to do what I have tried to do above.