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 Third Party
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or QuantRocket LLC and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.
Disclosure: Margin Trading
Trading on margin is only for experienced 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.
Join The Conversation
For specific platform feedback and suggestions, please submit it directly to our team using these instructions.
If you have an account-specific question or concern, please reach out to Client Services.
We encourage you to look through our FAQs before posting. Your question may already be covered!