Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Inputs are all NaN

Hi, i was trying to do my algorithm with BBands, however, whenever i try to make the backtest, there was an error said "inputs are all NaN
". This was the code.

"""
This is a template algorithm on Quantopian for you to adapt and fill in.  
"""
import quantopian.algorithm as algo  
import talib as t  
import numpy as np  
import pandas as pd 


def initialize(context):  
    """  
    Called once at the start of the algorithm.  
    """  
    # Rebalance every day, 1 hour after market open.  
    algo.schedule_function(  
        rebalance,  
        algo.date_rules.every_day(),  
        algo.time_rules.market_open(hours=1),  
    )

    # Record tracking variables at the end of each day.  
    algo.schedule_function(  
        record_vars,  
        algo.date_rules.every_day(),  
        algo.time_rules.market_close(),  
        )  
        #PARAMETROS  

    context.EMA=15  
    context.desv=2  
    context.delta=0.01  
    context.assetVector=[sid(16841)  
                         ,sid(44692)  
                         ,sid(23709)  
                         ,sid(47833)  
                         ,sid(42950)  
                         ,sid(5061)  
                         ,sid(4922)  
                         ,sid(114)  
                         ,sid(45815)  
                         ,sid(6683)  
                         ,sid(28051)  
                         ,sid(25006)  
                         ,sid(32146)  
                         ,sid(4707)  
                         ,sid(62)  
                         ,sid(43694)  
                         ,sid(25301)  
                         ,sid(266)  
                         ,sid(300)  
                         ,sid(24)  
                         ,sid(25753)  
                         ,sid(903)  
                         ,sid(734)  
                         ,sid(754)  
                         ,sid(4151)  
                         ,sid(838)  
                         ,sid(32689)  
                         ,sid(23112)  
                         ,sid(1900)  
                         ,sid(22139)  
                         ,sid(1787)  
                         ,sid(33729)  
                         ,sid(8347)  
                         ,sid(40430)  
                         ,sid(48780)  
                         ,sid(3499)  
                         ,sid(23920)  
                         ,sid(33472)  
                         ,sid(3496)]  
    context.numHits=0  
    context.numTrades=0  
    context.hittingRatio=0  
    context.numAssetTraded=0  
    context.numActivos=len(context.assetVector)  
    context.preciosApertura=pd.Series(np.zeros(context.numActivos),  
                                      index=context.assetVector)  
    context.preciosActual=pd.Series(np.zeros(context.numActivos),  
                                      index=context.assetVector)  
    context.bbands=pd.DataFrame(np.zeros((context.numActivos,3)),  
                                index=context.assetVector,  
                                columns=list(['lower','middle','upper']))

def before_trading_start(context, data):  
    for asset in context.assetVector:  
        prices=data.history(asset,'close',context.EMA+1,'1d')  
        upperband,middleband,lowerband=t.BBANDS(prices,  
                                                timeperiod=context.EMA,  
                                                nbdevup=context.desv,  
                                                nbdevdn=context.desv)  
        context.bbands['upper'][asset]=upperband[-1]  
        context.bbands['middle'][asset]=middleband[-1]  
        context.bbands['lower'][asset]=lowerband[-1]  
        context.preciosActual[asset]=data.current(asset,'close')  
        if  context.preciosActual[asset]<context.bbands['lower'][asset]:  
            print('Asset: %s,Close: %f, Upper: %f, middle: %f, lower: %f' %(asset,  
                                                                 context.preciosActual[asset],  
                                                                 context.bbands['upper'][asset],  
                                                                 context.bbands['middle'][asset],  
                                                                 context.bbands['lower'][asset]))

If anyone could tell me my mistake, that would be very helpfull.  
Thanks  
1 response

@ Valentina,

Try this way:

def before_trading_start(context, data):  
    for asset in context.assetVector:  
        if not data.can_trade(asset):  
            continue  
        prices = data.history(asset,'close',context.EMA + 1,'1d')