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.