Check for NaNs before returning historical intraday data

This commit is contained in:
moshferatu 2024-11-13 07:28:41 -08:00
parent 575e106f32
commit 4e36de7bf0

View File

@ -8,6 +8,8 @@ from os import getenv
load_dotenv()
columns = ['Date', 'High', 'Low', 'Open', 'Close', 'Total Volume', 'Period Volume']
def read_data_from_socket(sock, recv_buffer = 4096):
buffer = b''
data = b''
@ -93,11 +95,13 @@ def get_historical_data(symbol: str, interval: int, date: datetime = None,
data = data.replace(',\n','\n')[:-1]
data = StringIO(data)
data = pd.read_csv(data, index_col = False,
names=['Date', 'High', 'Low', 'Open', 'Close', 'Total Volume', 'Period Volume'])
if not data.empty:
data['Date'] = pd.to_datetime(data['Date'])
data['Date'] = data['Date'].dt.date
data = pd.read_csv(data, index_col = False, names = columns)
if data[columns].isna().any(axis = 1).any() or data.empty:
return pd.DataFrame(columns = columns)
data['Date'] = pd.to_datetime(data['Date'])
data['Date'] = data['Date'].dt.date
return data
if __name__ == '__main__':