From 7a177b38c9a2f0fab00df8db2873222519d3936f Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sun, 2 Jul 2023 05:14:43 -0700 Subject: [PATCH] Initial commit of script to automate the download of SPX options quotes --- Dockerfile | 5 +++++ download_spx_quotes.py | 48 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++++ 3 files changed, 57 insertions(+) create mode 100644 Dockerfile create mode 100644 download_spx_quotes.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..836cbc5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.10 +COPY download_spx_quotes.py . +COPY requirements.txt . +RUN pip install -r requirements.txt +CMD ["python", "./download_spx_quotes.py"] \ No newline at end of file diff --git a/download_spx_quotes.py b/download_spx_quotes.py new file mode 100644 index 0000000..d6ebc92 --- /dev/null +++ b/download_spx_quotes.py @@ -0,0 +1,48 @@ +import os +import pandas as pd +import paramiko +import schedule +import time +import zipfile + +quotes_directory = os.environ['DOWNLOAD_DIRECTORY'] +download_directory = quotes_directory + 'Zip Archive/' + +def process_file(file: str): + quote_data = pd.read_csv(file) + current_date = quote_data.iloc[0]['quote_datetime'][:10] + zero_dte_quotes = quote_data[(quote_data['expiration'] == current_date) & (quote_data['root'] == 'SPXW')] + if not zero_dte_quotes.empty: + zero_dte_quotes.to_csv(os.path.join(quotes_directory, current_date[:4], current_date + '.csv'), index=False) + +def download_quotes(): + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + client.connect( + hostname='sftp.datashop.livevol.com', + username=os.environ['USER'], + password=os.environ['PASSWORD'] + ) + sftp = client.open_sftp() + sftp.chdir('./subscriptions/' + os.environ['ORDER'] + '/' + os.environ['ITEM']) + files = sftp.listdir() + for file in files: + local_file = download_directory + file + if not os.path.exists(local_file): + print('Downloading', file) + sftp.get(file, local_file) + with zipfile.ZipFile(local_file, 'r') as zip_file: + print('Extracting', file) + zip_file.extractall(download_directory) + extracted_file = local_file.replace('.zip', '.csv') + print('Processing', extracted_file) + process_file(extracted_file) + print('Deleting', extracted_file) + os.remove(extracted_file) + client.close() + +if __name__ == '__main__': + schedule.every().day.at('04:00', 'America/Los_Angeles').do(download_quotes) + while True: + schedule.run_pending() + time.sleep(1) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..64f7c4b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +pandas +paramiko +pytz +schedule \ No newline at end of file