From 9aadf5af003f5c85be936573b1b96d08e2e7447a Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 5 Dec 2023 06:16:22 -0800 Subject: [PATCH] Add script to backfill OHLC data --- ohlc/backfill_ohlc.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 ohlc/backfill_ohlc.py diff --git a/ohlc/backfill_ohlc.py b/ohlc/backfill_ohlc.py new file mode 100644 index 0000000..982c55b --- /dev/null +++ b/ohlc/backfill_ohlc.py @@ -0,0 +1,31 @@ +import pandas as pd + +from database.ohlc import insert +from datetime import datetime#, date, timedelta +from iqfeed import get_daily_data, get_historical_data, minutes + +symbols_and_timeframes = [] + +start_date = datetime(2023, 12, 1) +# yesterday = date.today() - timedelta(days=1) + +for symbol, timeframe in symbols_and_timeframes: + if timeframe == '1d': + data = get_daily_data(symbol, + start_date = datetime.combine(start_date, datetime.min.time()), + end_date = datetime.combine(start_date, datetime.max.time())) + else: # Assuming minutes for now. + data = get_historical_data(symbol, minutes(int(timeframe[:-1])), + start_date = datetime.combine(start_date, datetime.min.time()), + end_date = datetime.combine(start_date, datetime.max.time())) + + data['Symbol'] = symbol + data['Timeframe'] = timeframe + data['Timestamp'] = pd.to_datetime(data['Date']) + data['Date'] = data['Timestamp'].dt.date + data = data.rename(columns={ + 'Period Volume': 'Volume' + }) + data = data[['Symbol', 'Date', 'Timeframe', 'Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']] + + insert(data) \ No newline at end of file