Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why does data.current(index,'open') return nan values from time to time?

Could anyone help me to figure out why data.current(index,'open') returns nan values from time to time? I thought the function would return the last open price (I schedule the main trading function 45 minutes after market open).

The following is the code and a slice of output.

    schedule_function(allocate,date_rules.every_day(),time_rules.market_open(hours=0,minutes=45))  
    schedule_function(record_vars,date_rules.every_day(),time_rules.market_open(hours=0,minutes=45))  
    log.info(('context.bull_trade_amt=%s')%(context.bull_trade_amt))  
    log.info(('context.bull_open_price={price}').format(price=data.current(context.bull,'open')))  
    log.info('context.bull_last_minute_price={price}'.format(price=data.history(context.bull,'price',1,'1m')[0]))  
    log.info(('context.bear_trade_amt=%s')%(context.bear_trade_amt))  
    log.info(('context.bear_open_price={price}').format(price=data.current(context.bear,'open')))  
    log.info('context.bear_last_minute_price={price}'.format(price=data.history(context.bear,'price',1,'1m')[0]))  

2010-12-09 09:15 allocate:45 INFO context.bull_trade_amt=-128.766417718
2010-12-09 09:15 allocate:46 INFO context.bull_open_price=nan
2010-12-09 09:15 allocate:48 INFO context.bull_last_minute_price=38.83
2010-12-09 09:15 allocate:49 INFO context.bear_trade_amt=-121.802679659
2010-12-09 09:15 allocate:50 INFO context.bear_open_price=nan
2010-12-09 09:15 allocate:52 INFO context.bear_last_minute_price=41.05

Thanks.

2 responses

Only price is forward-filled, not fields like open/close/volume. The details are in the help doc.

It was a deliberate choice to do it that way. The logic is that something always has a "current price," even if it's not actually trading this minute. The forward-filled last-traded price is the best approximation of the current price. But, if the stock never traded, then there is no open or close, it's something that doesn't exist. There isn't a good reason to forward fill the open from a previous bar, and, worse, it could be misleading.

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.

@ Dan, thanks for the reply. I'm still a little confused. Since I call the data.current() function 45 minutes after market open, I thought that open price of that day has been filled since we already have the open price. Isn't that the case?

And it might not be relevant to the question, but I also wonder what data.history() return if I set frequency to be '1m' and bar_count to be like 45. Does it give me the price of the last 45 minute? If this is the case, then the first number of the price series is open price, right?

Thanks.