Strategy Library

Momentum Effect in Commodities Futures

Introduction

Commodity futures are excellent portfolio diversifiers and some of them are an effective hedge against inflation. This algorithm will explore the momentum effect in commodity futures with the momentum return.

Method

As the strategy needs the continuous futures contract, we import the custom data from Quandl. We create a universe of tradable commodity futures from all available commodity futures traded on CME and ICE. They are all liquid and active continuous contracts #1. The data from Quandl are non-adjusted price based on spot-month continuous contract calculations. The data resolution is daily.

The first step is importing the data.

from QuantConnect.Python import PythonQuandl
for symbol in self.symbols:
    self.AddData(QuandlFutures, symbol, Resolution.Daily)

class QuandlFutures(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "settle"

Here we use the indicator RateOfChange(period) to simulate the momentum return. Here the period is 12 months. As we are using the custom data, the indicator initialization should use the history request to update the value manually. All indicators are saved in the dictionary self.roc.

self.roc = {}
for symbol in self.symbols:
    self.AddData(QuandlFutures, symbol, Resolution.Daily)
    self.roc[symbol] = RateOfChange(period)
    hist = self.History([symbol], 400, Resolution.Daily).loc[symbol]
    for i in hist.itertuples():
        self.roc[symbol].Update(i.Index, i.settle)

In OnData(self, data), indicators for all futures contracts are updated every day with the settlement price.

We rank the contracts by the last 12-month return and divide them into quintiles. In the trading part, the algorithm goes long on the quintile with the highest momentum return and goes short on the quintile with the lowest momentum return. The portfolio is rebalanced each month.

Algorithm

div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">