2024-11-11 17:02:51 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from pandas import concat, DataFrame
|
|
|
|
|
|
|
|
from ohlc import ohlc
|
|
|
|
|
|
|
|
def get_daily_data(symbol: str, start_date: datetime = datetime.today() - timedelta(days = 365),
|
|
|
|
end_date: datetime = datetime.today()) -> DataFrame:
|
|
|
|
daily_data = ohlc(symbol = symbol, start_date = start_date, end_date = end_date)
|
|
|
|
|
|
|
|
"""
|
|
|
|
The daily bar is not available for the current day until after market close.
|
|
|
|
It must be calculated using intraday (e.g., 5-minute) bars while the market is open.
|
|
|
|
"""
|
2024-11-11 17:37:17 +00:00
|
|
|
today = datetime.today().date()
|
2024-11-11 17:02:51 +00:00
|
|
|
if today not in daily_data['Date'].values:
|
|
|
|
intraday_data = ohlc(symbol = symbol, minutes = 5)
|
|
|
|
|
2024-11-12 15:20:13 +00:00
|
|
|
if intraday_data.empty:
|
|
|
|
return daily_data
|
|
|
|
|
2024-11-11 17:02:51 +00:00
|
|
|
open_price = intraday_data['Open'].iloc[0]
|
|
|
|
high_price = intraday_data['High'].max()
|
|
|
|
low_price = intraday_data['Low'].min()
|
|
|
|
close_price = intraday_data['Close'].iloc[-1]
|
|
|
|
total_volume = intraday_data['Total Volume'].iloc[-1]
|
|
|
|
|
|
|
|
todays_data = {
|
|
|
|
'Date': today,
|
|
|
|
'Open': open_price,
|
|
|
|
'High': high_price,
|
|
|
|
'Low': low_price,
|
|
|
|
'Close': close_price,
|
|
|
|
'Total Volume': total_volume,
|
|
|
|
'Period Volume': 0
|
|
|
|
}
|
|
|
|
|
|
|
|
daily_data = concat([daily_data, DataFrame([todays_data])], ignore_index = True)
|
|
|
|
|
|
|
|
return daily_data
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(get_daily_data(symbol = 'SPY'))
|