Back to Community
Manual selection of futures contracts instead of continuous futures

Hi, when trading futures, is it possible to manually reference a specific contract or is the only option to trade futures is through continuous futures?

The current contract for CL is CLJ19 and the next contract is CLK19. Can I subscribe to each contract's price separately?

What I am trying to do is to visualize calendar spreads. I used another method within this algo, but as you know, free accounts can't use the latest historical data, so in reality, what this algo may be doing, is using the current contract (CLJ19) and the differed month contract (CLK19) with during periods where these contracts were not the current and the next contracts. Manually selecting contracts and not continuous futures would allow me to pick a time period (typically 1-month length) and to apply the corresponding contracts to my backtest. Then I could be sure of the generated spread.

Your answer would really help :)


Clone Algorithm
Total Returns
Max Drawdown
Benchmark Returns
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
This is a template algorithm on Quantopian for you to adapt and fill in.

import numpy as np
import scipy as sp
from quantopian.algorithm import order_optimal_portfolio
import quantopian.optimize as opt

def initialize(context):
    context.future = continuous_future('CL')
    #schedule_function(rebalance, date_rule=date_rules.every_day())

def handle_data(context, data):
    # Specifying the future contract chain
    future_chain = data.current_chain(context.future)
    front_contract = future_chain[0]
    secondary_contract = future_chain[1]
    current_price_contract1 = data.current(front_contract, 'price')
    current_price_contract2 = data.current(secondary_contract, 'price')
    diff = current_price_contract1-current_price_contract2
There was a runtime error.