Back to Community
Function that is not called crashes backtest, returns() will not work either

I have a function (basically taken from help section) that works but somehow crashes my backtests (even when I'm not using it). I'm providing the code.

If you comment out the lines:

    price_history = history(bar_count=2, frequency='1d', field='price', ffill=True)  
    prev_bar = price_history[st][-2]  
    curr_bar = price_history[st][-1]  

The code will run.

It is interesting to note that the same thing happens when I'm trying to use .returns()

Deleting the records for the day of the crash makes it run:

TAS,-1,4/15/2014,4/15/2014,16,46
EDS,-1,4/15/2014,4/15/2014,17,0

But will only make it crash later on. What tickers make it crash and why?

Thankful for any suggestions!

Clone Algorithm
3
Loading...
Backtest from to with initial capital
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

def initialize(context):
    # Set lookup date to avoid confusion.
    set_symbol_lookup_date('2015-01-01')
    # import the custom CSV data file
    fetch_csv("https://dl.dropboxusercontent.com/u/124064072/tradeInfo.csv",
              pre_func=None,
              post_func=None,
              date_column='buyDate',
              universe_func=my_universe)

# my_universe returns a set of securities that define your universe.
def my_universe(context, fetcher_data):
    # set my_stocks to be every security in the fetcher_data
    my_stocks = set(fetcher_data['sid'])
    # return the securities we identified earlier
    return my_stocks

def getDirection(st):
    print st
    price_history = history(bar_count=2, frequency='1d', field='price', ffill=True) 
    prev_bar = price_history[st][-2]  
    curr_bar = price_history[st][-1]
    if curr_bar > prev_bar:
        return 1
    else:
        return 0
    
def handle_data(context,data):
    # Any content.
    for stock in data:
        # Anything
        stock
There was a runtime error.
5 responses

The log output is:

1970-01-01null:nullWARNDropped 13 rows from fetched csv.  
1970-01-01null:nullWARNrequests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.  
End of logs.  

Are you sure that fetcher and the csv are set up correctly?

This algo code hit an error because of an error in our back-end during the pandas upgrade. We're working on getting it fixed and will post back here when it's patched!

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.

That code looks like it would fail if it got no bars...?

Ah cool! What was the problem?

That code looks like it would fail if it got no bars...?

This was the reason! I thought the dropped rows were those that could not be matched to a SID and therefore returned 0 as SID (seems to be tickers that exist multiple times, confusing the internal matching.) So I didn't account for those. When I simply added a post_func it started working:

    df = df[df.sid != 0]  

I don't think I can do anything about that SSLContext warning, I suppose that's a server-side issue.