Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
help with runtime error

I'm trying out the Quantopian backtester with illiquid/thinly traded securities and running into problems. I can build the algorithm but the Full Backtest generates "There was a runtime error" with no additional details. All of the securities in the list appear to be active over the period of the backtest, based on Google finance.

Seems to be some trickiness to running Quantopian on securities that don't trade every minute/day...

Algorithm runs with

context.stocks = [sid(8554),sid(19920),sid(22739)]  

but not with

context.stocks = [sid(41290),sid(41425),sid(39479),sid(33972),sid(41159)]  

Here's the algorithm, in case it doesn't get posted:


import datetime  
import pytz

def initialize(context):

    #Full Backtest outputs "There was a runtime error"  
    context.stocks = [sid(41290),sid(41425),sid(39479),sid(33972),sid(41159)]  
    #Full Backtest completes without errors  
    #context.stocks = [sid(8554),sid(19920),sid(22739)]  
    context.price = {}  
    context.volume = {}  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0  
    utc = pytz.timezone('UTC')  
    context.d=datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)

def handle_data(data, context):  
    notional = 0.0  
    for stock in context.stocks:  
        price = data[stock].price  
        volume = data[stock].volume  
        notional = notional + data.portfolio.positions[stock].amount * price  
        tradeday = data[stock].datetime  
        log.debug(stock)  
        log.debug(tradeday)  
        log.debug(volume)  
    if (context.d + datetime.timedelta(days=1)) < tradeday:  
        log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y'))  
        context.d = tradeday  
8 responses

Hi @Grant,

Thanks for reporting this, and sorry for the trouble.
I'm looking into why the error was improperly reported to the UI, but the problem is that your backtest start date is before the first trade date of some of your securities. The error is:

NoTradeDataAvailable: {"symbol":"FLN","property":"price","first_traded":"Apr 20, 2011 2:04 PM UTC","sid":41290}  

When I cloned your algorithm and set the dates to start in 2002, the UI automatically reset the date to 5/13/2012 and highlights the start date in blue. If you drop down the date control from the blue highlight, there is contextual help that tells you the trade date ranges for all the securities in your algorithm. You can still override the start date to be before the earliest trade, but you need to check that the security is available before accessing its properties. There's a helper function for this purpose so that in handle_data you can do:

data.available(sid(41290))  

The available function is described in the help documentation here: https://www.quantopian.com/help#api-available

Please let me know if you encounter any other issues, and we will look into why the error reporting failed here.

thanks,
fawce

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks Fawce,

I followed your instructions above and confirmed the fix. However, strangely, if I manually set the full backtest range as 2012-01-03 to 2012-07-30, the full back test fails, giving "There was a runtime error." Please let me know if you get the same thing, or if I am missing something.

@Grant, actually you discovered a new case we are dealing with now. Currently we check the first day of trading for each sid, and adjust the date range so that it only starts after all sids began trading. However, we don't do any checks for sids that have days with no trading. In your example, your first day of trading was actually after all sids started trading, but the first day itself was a day when one did not trade.

The system actually detects this and raises a special exception, so we are fixing by reporting the exception more clearly.

Fawce,

Not sure that I understand the problem here. Since the full backtest is based on minutely data, why should it care about a daily time scale? Is it because it is doing daily calculations in the background, for reporting purposes? Why would it be required that every SID in the backtest trade at least once for every day of the backtest?

@Grant,

The problem isn't with the duration of the bar, it is just that the first bar sent to your algorithm is one in which a sid did not trade at all. As a result, the data parameter has no value for the sid. When your algorithm loops over all sids, and fetches the price for each, the backtester raises a special exception for missing data. The corner case you found is that the sid started trading before your backtest start date, but does not trade in the first bar of that backtest.

The backtester itself does not care about missing data, however most algorithms assume all sids are available in the data parameter. That's why we provide the available function - it lets you guard against missing data points.

thanks,
fawce

Thanks Fawce,

I'll try the available function when I get the chance.

Fawce & all,

I modified the for-loop above to read:

for stock in context.stocks:  
        if data.available(stock):  

This fixes the problem (and would seem to be good coding practice for all such loops over SIDS).

I'm still confused why this check needs to be performed at the algorithm level and is not embedded in the backtester. Shouldn't the backtester just skip minutes with no trade events for a given security?

@Grant,

The reason we send the event to your algorithm is that some of your sids have traded. Rather than skip minutes when only a partial list of your sids trade, we decided to send you the subset. We do, however, skip minutes in which none of your sids have traded.

thanks,
fawce