
Python for Algorithmic Trading Cookbook
By :

Traders use continuous futures data for backtesting trading strategies. Futures traders use the roll from one contract to another as a potential opportunity for profit. Some traders simply pick a date before expiration to roll to the next contract, while others use sophisticated techniques involving open interest. This basis trade is persistently one of the most popular trading strategies for futures traders. These traders want control over the data that’s used to compute the basis trade, so acquiring individual contract data is important. This recipe will guide you through the process of using the OpenBB Platform to fetch individual futures contract data.
By now, you should have the OpenBB Platform installed in your virtual environment. If not, go back to the beginning of this chapter and get it set up.
We’ll use the futures functionality in the OpenBB Platform to download individual futures data for free:
import pandas as pd from openbb import obb obb.user.preferences.output_type = "dataframe"
data = obb.derivatives.futures.curve(symbol="VX")
print(data)
Running the preceding code generates the futures curve for the VIX futures contract:
Figure 1.6: Settlement prices for the forward Eurodollar futures contracts
data.index = pd.to_datetime(data.expiration) data.plot()
By running the proceeding code, we plot the VIX futures curve:
Figure 1.7: VIX futures curve
You can use the obb.derivatives.futures.historical
method to get historical data for an individual expiration. Stitching together data across a range of years can provide insight into the market’s expectation of supply and demand of the underlying commodity:
expirations = [ "2024-12", "2025-12", "2026-12", "2027-12", "2028-12", "2029-12", "2030-12", ]
contracts = [] for expiration in expirations: df = ( obb .derivatives .futures .historical( symbol="CL", expiration=expiration, start_date="2020-01-01", end_date="2022-12-31" ) ).rename(columns={ "close": expiration }) contracts.append(df[expiration])
rename
method, change the column name from "close"
to the expiration date. Finally, append the newly created pandas DataFrame to a list of DataFrames:historical = ( pd .DataFrame(contracts) .transpose() .dropna() )
transpose
method, and drop any records with no data using the dropna
method. Inspect the resulting DataFrame:print(historical)
By printing the DataFrame, we will see the historical settlement prices:
Figure 1.8: Historic settlement prices for the December CL futures contract
The result is the historical data between January 2020 and December 2022 for each of the December expirations between 2023 and 2030:
historical.iloc[-1].plot()
Here's the output:
Figure 1.9: Futures curve for the December CL contract
For more on the OpenBB Platform futures functionality, you can browse the following documentation:
curve
method: https://docs.openbb.co/platform/reference/derivatives/futures/curvecurve
method: https://docs.openbb.co/platform/reference/derivatives/futures/historical