database-automation/ohlc/update_ohlc_data.py

52 lines
1.7 KiB
Python

import pandas as pd
import schedule
import time
from database.ohlc import insert
from datetime import datetime, date, timedelta
from iqfeed import get_daily_data, get_historical_data, minutes
# Symbols and timeframes to process.
# TODO: Store in database to make re-deployment unnecessary.
symbols_and_timeframes = [
('SPX.XO', '5m'),
('SPX.XO', '1d'),
('SPY', '5m'),
('SPY', '1d'),
('VIX.XO', '5m'),
('VIX.XO', '1d'),
('VIX1D.XO', '1d'),
('VIX9D.XO', '1d'),
('VIX3M.XO', '1d'),
('VIX6M.XO', '1d'),
('VIX1Y.XO', '1d')
]
def update_ohlc_data():
for symbol, timeframe in symbols_and_timeframes:
yesterday = date.today() - timedelta(days=1)
if timeframe == '1d':
data = get_daily_data(symbol,
start_date = datetime.combine(yesterday, datetime.min.time()),
end_date = datetime.combine(yesterday, datetime.max.time()))
else: # Assuming minutes for now.
data = get_historical_data(symbol, minutes(int(timeframe[:-1])),
start_date = datetime.combine(yesterday, datetime.min.time()),
end_date = datetime.combine(yesterday, 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)
if __name__ == '__main__':
schedule.every().day.at('01:00', 'America/Los_Angeles').do(update_ohlc_data)
while True:
schedule.run_pending()
time.sleep(1)