Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Backtest vs Live Trading Issues

Issue #1
The following function is run as a pre_func in a fetch_csv call. The data being called is monthly data.

def load_data(df):  
    df = df.reindex(df.index[::-1])  
    df['last'] = df['Value']  
    df['last Date'] = df['Date']  
    return df  

In backtest mode, the below line was working fine. It would capture the late of the most recently available monthly data given the current day in the backtest.

last_date= datetime.strptime(data.current('data_series', 'last Date'),'%Y-%m-%d')  

But in live trading mode, this last line of code fails because a "NaN" value is returned from data.current. I should note that the monthly data lags by at least a month. So in live trading, if we are in November, the last available data may be from October or even September.

I actually want the date returned to be the last date available from the fetched data so that I can adjust some calculations I do with the data. How can I do this and not keep getting NaN?

Issue #2
data.current is returning the last close price but not the current price. When in the backtest, I though this was simply an aberration of backtesting. But now that the code is in live trading, it's still only returning the last close price and not the intraday current price. Am I doing something wrong is this just how this code works?

Example:
now_price = data.current(stock,'price') #where stock is any valid sip code, returns last close instead of intraday price

1 response

There are two possible fixes. One is to keep the most recent 'Value' in your context variable, check if the 'Value' for the current date is 'NaN' and if it isn't, update context. You would want to do this in before_trading_start.
​ ​Another way of getting around this would be to use pandas to forward-fill your input DataFrame in pre_func, right after you reverse the index. First you will want to add the missing dates by reindexing, this thread shows you how: (http://stackoverflow.com/a/19324591). For fill_value you want to use None, this will help with the next step. To forward-fill use pandas.DataFrame.fillna, with the method argument set to 'ffill'. Here is an example of how to do it (http://stackoverflow.com/a/27905350). This will replace NaN with the previous most recent valid 'Value'

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.