ranaroussi / quantstats

Portfolio analytics for quants, written in Python
Apache License 2.0
4.59k stars 809 forks source link

Futures Data (CME, EUREX, etc) #313

Open jortiz12 opened 8 months ago

jortiz12 commented 8 months ago

Dear Community, Is it possible to supply QuantStats with external futures data saved in .csv or .xlsx format. Typical data providers (e.g. YFinance) don´t have data I'm looking for (mostly bond futures at CME, EUREX).

Kind Regards Jose

git-shogg commented 7 months ago

Hi @jortiz12,

Yes this is possible. I would also recommend you have a look at alternatives that could be used as a substitute to make life a bit easier (e.g. there might be ETFs that track the same instruments?). However, if you are after a higher level of accuracy you can simply download the data for the timeframe you are after (direct from CME, EUREX, etc), save it to .csv or .xlsx format (example below uses csv), place this file in the same folder as your python script, import that into your script and use pandas to convert the datetime string column to real datetime and capture the percentage change of the instrument daily over that time. An example of this shown below.

import quantstats as qs
import pandas as pd

instrument_daily_close_price = pd.read_csv("instrument_daily_close_price.csv")
instrument_daily_close_price['Date'] = pd.to_datetime(instrument_daily_close_price['Date'], format='%d/%m/%Y')
instrument_daily_close_price['pct_change'] = instrument_daily_close_price['Price'].pct_change()
instrument_daily_close_price = instrument_daily_close_price[['Date','pct_change']]  # Drop out all unrequired columns
instrument_daily_close_price = instrument_daily_close_price.set_index('Date')
instrument_daily_close_price_series = instrument_daily_close_price.squeeze()    # Convert to pandas series
instrument_daily_close_price_series.index = instrument_daily_close_price_series.index.tz_localize(None)

comparison_instrument = qs.utils.download_returns('SPY')    # Download historical prices for benchmark.
comparison_instrument.index = comparison_instrument.index.tz_localize(None)
qs.reports.html(instrument_daily_close_price_series, comparison_instrument, output=1, download_filename="output.html")

Let me know if this answers your question?

Kind regards, git-shogg