Close Navigation
Learn more about IBKR accounts
Download Futures Data with Yahoo Finance Library in Python

Download Futures Data with Yahoo Finance Library in Python

Posted July 28, 2022
José Carlos Gonzáles Tanaka via QuantInsti Blog
QuantInsti

The first thing you must do to code a futures strategy is to learn to download futures data in Python! This blog explains the complete process and helps you gain an understanding of the various steps required.

Futures trading is inherently leveraged. One has to be cautious while using leverage in a portfolio. A good way to manage leverage is to trade algorithmically, and that is where this blog will be of importance.

It covers:

  • Yahoo! Finance Library Installation
  • Import Libraries
  • Download Futures Data
  • Plot the Close Prices
  • Download Data for Multiple Futures
  • Plot Multiples Futures Data

Yahoo! Finance Library Installation

Python is one of the easiest programming languages in the world. Python makes it easy for new learners to acquire programming skills quicker. It is also quite easy to set up your own Python trading system.

We are going to use a Jupyter notebook. Let’s code! In case you haven’t already installed this famous library in your machine, here you have the code:


Import Libraries

So now, we want to import a library for data visualisation. We can use “matplotlib”. This library is the most used library in Python for this purpose.

In case you are new to this library, do you think we are going to leave you alone on this? No way! Please have a look at matplotlib before you dive into our next code.

We now want to import the two libraries. Here’s the code:


Download Futures Data

Before you dig into the many possibilities that futures trading has to offer, do note that, in the yahoo finance library, you will find continuous futures data. Futures are offered in the market for specific dates and due dates.

However, you can join all the futures in a single data line and this is called Futures continuation. You should also be aware of continuous futures contract.

For now, you just need to call the download method, and inside the parenthesis pass the ticker_symbol (of the futures contract) as “ticker_symbol = F”, the start date, and the end date to specify the span of the dataset. The list provided in this article will help you to find the correct symbol for the futures asset you want to download.

For illustration, let us fetch the continuous platinum futures with the symbol “PL=F”. We select the 2017-2021 span. We change the index column to datetime type keeping in mind that this will later be used for graphical visualisation and manipulation. Lastly, we present the last five rows of the dataframe.

DateOpenHighLowCloseAdj CloseVolume
2021-12-27968.90968.90968.90968.90968.90120
2021-12-28979.10979.10979.10979.10979.10120
2021-12-29968.20968.20968.20968.20968.203280
2021-12-30967.30971.00955.80963.60963.601356
2021-12-31962.50965.30949.60964.40964.40109

This is a futures contract for which the underlying asset is a commodity. There are futures for currencies, stocks, interest rate futures, index futures, etc.


Plot the Close Prices

Now let’s plot the Gold Futures close prices so we can have a better idea of its price trends:

Download Data for Multiple Futures

Let’s do something else. Let’s download the Gold (GC) and Copper (HG) Futures data for the period 2020-2021. To fetch more than one asset, you just need to put the futures tickers inside brackets. We use brackets since to download two or more assets you need to pass the ticker names together as a list.

Data columns will be presented by asset and then by type of price. For this, we just grouped the data first by ‘tickers’ with the group_by method. Let’s code:

GC=F
DateOpenHighLowCloseAdj CloseVolume
2021-12-271810.001812.101807.001808.101808.10150
2021-12-281812.001818.001805.501810.201810.20146
2021-12-291803.201805.101791.401805.101805.10623
2021-12-301801.701816.001796.001812.701812.70306
2021-12-311825.101827.801821.401827.501827.5080
HG=F
DateOpenHighLowCloseAdj CloseVolume
2021-12-274.384.474.374.474.47421
2021-12-284.464.474.424.424.42543
2021-12-294.414.414.404.404.40695
2021-12-304.414.424.384.384.38832
2021-12-314.394.464.384.464.46373

Visit QuantInsti to read the full article: https://blog.quantinsti.com/download-futures-data-yahoo-finance-library-python/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

Disclosure: Futures Trading

Futures are not suitable for all investors. The amount you may lose may be greater than your initial investment. Before trading futures, please read the CFTC Risk Disclosure. A copy and additional information are available at ibkr.com.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.