Excerpt
The mechanics of trading calendar spreads
To backtest an intraday calendar spread strategy for crude oil futures (symbol CL), I first collect 1-minute bid/ask bars for all CL futures contracts from Interactive Brokers.
After loading the prices into a pandas
DataFrame, I use the function get_contract_nums_reindexed_like
to obtain a DataFrame showing each contract’s numerical sequence in the contract chain as of any given date:
>>> from quantrocket.master import get_contract_nums_reindexed_like
>>> contract_nums = get_contract_nums_reindexed_like(bids, limit=3)
>>> contract_nums.head()
ConId CLM9 CLZ9 CLK9 CLJ9 CLN9
Date
2019-03-04 3.0 NaN 2.0 1.0 NaN
2019-03-05 3.0 NaN 2.0 1.0 NaN
2019-03-06 3.0 NaN 2.0 1.0 NaN
2019-03-07 2.0 NaN 1.0 NaN 3.0
2019-03-08 2.0 NaN 1.0 NaN 3.0
2019-03-11 2.0 NaN 1.0 NaN 3.0
I isolate the bids and asks for contract months 1 and 2 by masking the prices with the respective contract nums and taking the mean of each row. In taking the mean, I rely on the fact that the mask leaves only one non-null observation per row, thus the mean simply gives us that observation.
are_month_1_contracts = contacts_nums == 1
month_1_bids = bids.where(are_month_1_contracts).mean(axis=1)
month_1_asks = asks.where(are_month_1_contracts).mean(axis=1)
are_month_2_contracts = contacts_nums == 2
month_2_bids = bids.where(are_month_2_contracts).mean(axis=1)
month_2_asks = asks.where(are_month_2_contracts).mean(axis=1)
I then use the bids and asks to compute the calendar spread. To reflect the fact that I must buy at the ask and sell at the bid, I compute the spread differently for the purpose of identifying long vs short opportunities:
# Buying the spread means buying the month 1 contract at the ask and
# selling the month 2 contract at the bid
spreads_for_buys = month_1_asks - month_2_bids
...
# Selling the spread means selling the month 1 contract at the bid
# and buying the month 2 contract at the ask
spreads_for_sells = month_1_bids - month_2_asks
I use the spreads to construct Bollinger Bands set two standard deviations away from the spread’s 60-minute moving average, and I buy (sell) the spread when it moves below (above) its lower (upper) band.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.
Visit QuantRocket website to read the full article and to download the code:
https://www.quantrocket.com/blog/intraday-futures-calendar-spreads
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 QuantRocket LLC and is being posted with permission from QuantRocket LLC. The views expressed in this material are solely those of the author and/or QuantRocket LLC 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.
Disclosure: Margin Trading
Trading on margin is only for sophisticated investors with high risk tolerance. You may lose more than your initial investment.
For additional information regarding margin loan rates, see ibkr.com/interest
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 ibkr.com.