Download Futures Data with Yahoo Finance Library in Python

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

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:

DateOpenHighLowCloseAdj CloseVolume
DateOpenHighLowCloseAdj CloseVolume

Visit QuantInsti to read the full article:

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