Back to Community
Simple Future Algo crashes

Hallo,
I have created a very simple Future algorithm and it works when I set the starting date for the backtesting to 2011 but if I try to use anything longer it will crash.

When I define starting date to 2003 it crashes on optimize API if I change starting date to 2010 it crashes with a keyerror.
It this a data problem or something on my code?

Regards,
Lucas

Clone Algorithm
6
Loading...
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
import pandas as pd
from quantopian.algorithm import order_optimal_portfolio
import quantopian.optimize as opt

def initialize(context):
    context.rolling_period = 30 #10 days
    context.long_positions = 5
    context.short_positions = 5
    #List of all tradable future stocks at quantopian
    context.universe = [continuous_future('CL', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('XB', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('BO', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('CM', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('CN', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('ET', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('FF', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('FI', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('FV', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('MS', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('MW', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('OA', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('RR', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('SM', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('SY', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('TN', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('TU', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('TY', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('UB', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('US', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('WC', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('YM', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('VX', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('AD', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('AI', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('BP', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('CD', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('EC', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('ED', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('EE', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('ES', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('EU', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('FC', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('JE', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('JY', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('LB', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('LC', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('LH', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('ME', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('MI', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('NK', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('NQ', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('NZ', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('SF', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('SP', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('GC', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('HG', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('SV', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('YX', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('CL', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('HO', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('NG', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('PA', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('PL', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('QG', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('QM', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('XB', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('EI', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('MG', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('XG', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('YS', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('SB', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('RM', offset=0, roll='volume', adjustment='mul'),
                        continuous_future('ER', offset=0, roll='volume', adjustment='mul')
                       ]
    schedule_function(rebalance, date_rule=date_rules.month_start() , time_rule=time_rules.market_open())
    
    
def rebalance(context, data):
    hist = data.history(context.universe, 'price', context.rolling_period, '1d')
    returns = hist.pct_change().sum().order()
    cp1 = data.current(returns.index[-1], 'contract')
    cp2 = data.current(returns.index[-2], 'contract')
    cp3 = data.current(returns.index[-3], 'contract')
                           
    
    cm1 = data.current(returns.index[0], 'contract')
    cm2 = data.current(returns.index[1], 'contract')
    cm3 = data.current(returns.index[2], 'contract')
    
    weights = {cp1:0.2, cp2:0.2, cp3:0.1, cm1:-0.2, cm2:0.2, cm3:0.1}
    
    order_optimal_portfolio(objective=opt.TargetWeights(weights), constraints=[])
There was a runtime error.