Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why certain rows were not processed using fetch_csv?

Hi everyon,

This algo monitored the behavior for each day and print 'Buy', 'Sell', 'No trade' correspondingly. In this algo, it should sell BAC on 2006-05-29 (I tested it using local python). However, it didn't trade on that day, even not shown in the output and the backtest results.

I double checked that the datapoint on 2006-05-29 was imported into quantopian.

Anyone can help? Thank you all so much!

My code is below.

import pandas

def initialize(context):  
    # context.width = 1.9  
    # context.window = 350  
    context.BAC = sid(700)  
    context.amount = 100  
    context.longs = context.shorts = 0  
    # import the external data  
    url = 'https://docs.google.com/spreadsheets/d/1dlHaM4QhybACe20gY6Udu0LXcdoQWUQjNXQRFHcXADQ/pub?gid=1314643233&single=true&output=csv'  
    fetch_csv(url,  
        symbol='BAC',  
        date_column='Date',  
        pre_func = preview,  
        post_func = rename_col,  
        date_format='%Y-%m-%d')  
    schedule_function(BBbands,  
                      date_rules.every_day(),  
                      time_rules.market_open()  
                     )  
    # Record variables at the end of each day.  
    schedule_function(record_vars,  
                      date_rules.every_day(),  
                      time_rules.market_close())  


def preview(df):  
    log.info(' \n %s ' % df.dropna().head())  
    log.info(' \n %s ' % df.tail())  
    return df    

def rename_col(df):  
    width = 1.9  
    window = 350  
    df = df.rename(columns={'PX_CLOSE_1D': 'Close'})  
    df = df.rename(columns={'PX_OPEN': 'Open'})  
    df = df[['Open', 'Close', 'sid']]  
    df['Close_SMA'] = df['Close'].rolling(window).mean()  
    df['Close_STD'] = df['Close'].rolling(window).std()  
    df['Upperband'] = df['Close_SMA'] + width * df['Close_STD']  
    df['Lowerband'] = df['Close_SMA'] - width * df['Close_STD']  
    # df = df.fillna(method='ffill')  
    df.dropna(inplace=True)  
    # # Correct look-ahead bias in mapping data to times  
    # df = df.tshift(1, freq='b')  
    log.info(' \n %s ' % df.head())  
    log.info(' \n %s ' % df.tail())  
    return df

def handle_data(context, data):  
    pass


def BBbands(context,data):  
    upperband = data.current('BAC','Upperband')  
    lowerband = data.current('BAC','Lowerband')  
    open_price = data.current('BAC','Open')  
    log.info(get_datetime())  
    log.info('cur_price: %s --- [upperbb: %s,lowerbb: b %s]' %(open_price, upperband, lowerband))  
    if open_price < lowerband:  
        order(context.BAC, context.amount)  
        log.info('Buying %d shares of %s' %(context.portfolio.portfolio_value/open_price, 'BAC'))  
    elif open_price > upperband:  
        order(context.BAC, -context.amount)  
        log.info('Selling %d shares of %s' %(context.portfolio.portfolio_value/open_price, 'BAC'))  
    else:  
        log.info('No trade today')  
        pass  
    record(Upperband=upperband, Lowerband=lowerband, CurrentPrice=open_price)  
def record_vars(context, data):  
    """  
    This function is called at the end of each day and plots our leverage as well  
    as the number of long and short positions we are holding.  
    """

    # Check how many long and short positions we have.  
    for position in context.portfolio.positions.itervalues():  
        if position.amount > 0:  
            context.longs += 1  
        elif position.amount < 0:  
            context.shorts += 1    

    log.info('longs: %s' %context.longs)  
    log.info('shorts: %s\n\n' %context.shorts)  
1 response

Afterwards, I used the data internally from Quantopian and found it also skipped the price on 2006-05-29. I am kind of confusing here because from my own dataset is extracted from Bloomberg, which should be no issues on it's accuracy.

It might be the minor differences among different data sources. Quantopian's data seems more like data from yahoo. However, how can I fully test my local data? Is there some wrong in my coding?

Thank you all so much !