Files
strategy-lab/to_explore/pyquantnews/59_StoringDataHDF5.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

7.6 KiB

No description has been provided for this image

This code retrieves stock and options data for the ETF "SPY" and futures data for the symbol "ES". It uses the OpenBB Terminal SDK to load the data, including historical prices and options chains. The retrieved data is then stored in HDF5 files for easy access and persistence. This is useful for financial analysis and modeling, enabling efficient data storage and manipulation. The code concludes by reading the stored data and printing it for verification.

In [ ]:
import pandas as pd
from openbb_terminal.sdk import openbb
In [ ]:
STOCKS_DATA_STORE = "stocks.h5"
FUTURES_DATA_STORE = "futures.h5"
In [ ]:
ticker = "SPY"
root = "ES"

Retrieve stock price data for the ticker "SPY"

In [ ]:
spy_equity = openbb.stocks.load(ticker)

Retrieve options expiration dates for the ticker "SPY"

In [ ]:
spy_expirations = openbb.stocks.options.expirations(ticker)

Retrieve historical options data for a specific expiration date

In [ ]:
spy_historic = openbb.stocks.options.hist(
    ticker, 
    spy_expirations[1], 
    440
)

Retrieve options chains for the ticker "SPY"

In [ ]:
spy_chains = openbb.stocks.options.chains(ticker)

Store the retrieved stock and options data in an HDF5 file

In [ ]:
with pd.HDFStore(STOCKS_DATA_STORE) as store:
    store.put("equities/spy/stock_prices", spy_equity)
    store.put("equities/spy/options_prices", spy_historic)
    store.put("equities/spy/chains", spy_chains)

Retrieve and store futures data for the symbol "ES" for specific expiration dates

In [ ]:
with pd.HDFStore(FUTURES_DATA_STORE) as store:
    for i in range(23, 31):
        expiry = f"20{i}-12"
        df = openbb.futures.historical(
            symbols=[root],
            expiry=expiry,
            start_date="2020-01-01",
            end_date="2022-12-31"
        )
        df.rename(
            columns={
                "Adj Close": expiry
            },
            inplace=True
        )
        prices = df[expiry]
        store.put(f'futures/{root}/{expiry}', prices)

Load stored stock prices, options prices, and options chains from the HDF5 file

In [ ]:
with pd.HDFStore(STOCKS_DATA_STORE) as store:
    spy_prices = store["equities/spy/stock_prices"]
    spy_options = store["equities/spy/options_prices"]
    spy_chains = store["equities/spy/chains"]

Load stored futures prices for a specific expiration date from the HDF5 file

In [ ]:
with pd.HDFStore(FUTURES_DATA_STORE) as store:
    es_prices = store[f"futures/{root}/2023-12"]

Print the loaded stock prices for verification

In [ ]:
print(spy_prices)

Print the loaded options prices for verification

In [ ]:
print(spy_options)

Print the loaded options prices for verification

In [ ]:
print(spy_options)

Print the loaded futures prices for verification

In [ ]:
print(es_prices)

PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.