Back to Community
Simple calendar-spread trade with natural gas contracts (FUTURES ALGO EXAMPLE)

The strategy below (inspired by this paper) begins with the assumption that there is a predictable commercial or institutional interest in a particular futures contract. This is a result of seasonal inventory build-and-draw cycles. In this case, the contracts of interest were natural gas futures. The price of natural gas futures contracts with delivery in summer and fall is typically lower than the price of natural gas futures contracts with delivery in winter. However, because there is no immediately visible pattern in the price of these futures contracts, cost of carry can be used as a signal.

Cost of carry in the context of natural gas is the cost of taking delivery of natural gas and storing it. I hypothesized that when cost of carry is in the highest quantile (e.g. cost of carry is more expensive) relative to the past 30 days of data, the price of the natural gas contract would decline and a short position should be taken. On the other hand, when cost of carry is in the lowest quantile relative to the past 30 days of data, the price of the contract would increase and a long position should be taken.

Translating this research into an algorithm came with some speed bumps because the data.history() method available in backtesting isn’t identical to the same method in research. The most significant limitation was not being able to use data.history() to access previously active futures contracts’ maturity dates. These maturity dates are crucial values in calculating cost of carry. So, as an alternative, I built a queue that would store the previous 30 days of cost of carry values. This approach limited the algorithm to starting 30 trading days after the beginning of the backtest. You can find the queue in this block of code:

context.cost_of_carry_data = []  
context.cost_of_carry_quantiles = []  

I’d love to hear some feedback from the community on how I can improve the performance and risk metrics of the algorithm. Thank you in advance.

Loading notebook preview...
Notebook previews are currently unavailable.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

7 responses

Here is the corresponding backtest.

Clone Algorithm
310
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 591f33fddf1e3a61bb9e0bc7
There was a runtime error.

I'm curious that whether the daily price of ETF and Futures are recorded in the same minutes or same seconds or not.
I think it might be better to get the spot index value and look into the intraday data.

looks like the strategy does not works for 2 futures with different expiration dates.

Alina, that was clever SEO spam. Can someone delete this and Alina's comment?

Thank you for flagging, it has been removed.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

@Jeremy,

I think the strategy would do well with a simple stop loss, but I haven't had much luck in programming the stop loss.

Clone Algorithm
48
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 59792206d0c2774e171aced9
There was a runtime error.

This strategy doesn't make sense to me. "Spot price" should be Prompt contract and "Future Price" should be a winter contract (Most likely January). That would make it the true intrinsic value of buying gas in the summer and selling it in the winter, which is what real storage operators like myself do.

The UNG ETF only buys and hold the front contract. It has nothing to do with the actual carry of the underlying commodity. I would love to see this strategy re-programmed using only the futures. I'm trying to learn Python to test various NG spread trading strategies.