Contributor:
QuantInsti
Visit: 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
• Plot the Close Prices
• 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:

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.

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:

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:

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.