Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Minute data + daily SMA = error

I am using zipline within a quantopian notebook to test one of my algorithms

In the 'handle_data' function, i am calculating a SMA

when I calculate it using '1m':

SMA1 = data.history(context.aapl,'price',2,'1m').mean()  

the algorithm works fine

But when i want to get a daily SMA using '1d':

SMA1 = data.history(context.aapl,'price',2,'1d').mean()  

I get the following error:

ValueError: max() arg is an empty sequence  

Note: the same 'initialize' and 'handle_data' functions work fine if I use quantopian's backtester (running it as a quantopian 'Algorithm')

Here is the full code

import zipline  
import pytz  
from datetime import datetime  
import matplotlib.pyplot as pyplot  
from collections import defaultdict

from zipline import TradingAlgorithm  
from zipline.api import order_target, record, symbol, history, order_target_percent  
import numpy as np

aapl_weights = .50  
spy_weights = .50

def initialize(context):  
    context.aapl = symbol('aapl')  
    context.spy = symbol('spy')  
    context.aapl_weights = aapl_weights  
    context.spy_weights = spy_weights  
    context.first_time = True  
    context.i=0  
def handle_data(context, data):  
    #: Only order on the first bar  
    context.i += 1  
    if context.i < 2*390:  
        return  
    SMA1 = data.history(context.aapl,'price',2,'1d').mean()


    if context.first_time:  
        order_target_percent(context.aapl, context.aapl_weights)  
        order_target_percent(context.spy, context.spy_weights)  
        context.first_time = False

data = get_pricing(  
    ['AAPL', 'SPY'],  
    start_date='2014-01-01',  
    end_date = '2015-02-15',  
    frequency='minute'  
)

algo_obj = TradingAlgorithm(  
    initialize=initialize,  
    handle_data=handle_data  
)

#: See this perf_manual object? I'm going to use that to get ending portfolio value  
perf_manual = algo_obj.run(data.transpose(2,1,0))

#: Get the sharpe ratio  
sharpe = (perf_manual.returns.mean()*252)/(perf_manual.returns.std() * np.sqrt(252))  
print "The Sharpe ratio is %0.6f" % sharpe  
2 responses

Hey Sebastian,

Did you find a solution to this?

Cheers,

Daniel

Hi Daniel,

I still haven't found a way to circumvent this issue.

Any Ideas from the community?

Cheers,
Sebastián