Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Created January 20, 2020 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonplatter/43510ad39c0b0d11d2a5a2791b75d062 to your computer and use it in GitHub Desktop.
Save westonplatter/43510ad39c0b0d11d2a5a2791b75d062 to your computer and use it in GitHub Desktop.
from datetime import datetime, timedelta, date
import pytz
from os import environ, path, makedirs
import pandas as pd
import quandl
import fasterparquet
start_date = "1995-01-01"
today = date.today()
last_market_date = today - timedelta(days=3)
end_date = last_market_date.strftime("%Y-%m-%d")
def save_futures_data(
symbol: str, month: str, sd: str, ed: str, overwrite: bool = False
):
identifier = f"CHRIS/CME_{symbol}{month}"
data_path = f"./data/{identifier}.parquet"
def save_file(identifier, data_path, sd, ed):
dirs = "/".join(data_path.split("/")[:-1])
makedirs(dirs, exist_ok=True)
df = quandl.get(identifier, start_date=sd, end_date=ed)
df.to_parquet(data_path, compression="gzip")
try:
df = pd.read_parquet(data_path)
print(f"Reading {data_path}")
except FileNotFoundError as e:
print(f"API calling {data_path}")
save_file(identifier, data_path, start_date, end_date)
if overwrite:
print(f"Overwriting data.")
save_file(identifier, data_path, start_date, end_date)
months_out_on_curve = [str(i) for i in range(1, 12)]
instruments = ["CL"]
for instrument in instruments:
for month in months_out_on_curve:
sd = start_date
ed = end_date
print(
f"instrument={instrument}, month_out={month}. start_date={sd}. end_date={ed}"
)
save_futures_data(instrument, month, sd, ed, overwrite=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment