Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Logging and closing price, am I'm missing something?

Hi there!

Am I understanding this wrong? As per the logging below, the output of the algorithm seems to be reporting the closing price of the current day to the closing of the next day in real life.

This is the output of the program (Ticker in question is AAPL) :

2013-07-22 handle_data:17 INFO Closing price: 418.98  
2013-07-23 handle_data:17 INFO Closing price: 440.65  
2013-07-24 handle_data:17 INFO Closing price: 438.5  
2013-07-25 handle_data:17 INFO Closing price: 440.99  

But the data from finance yahoo, google and think or swim report this:

Jul 22, 2013 426.31
Jul 23, 2013 418.99
Jul 24, 2013 440.51
Jul 25, 2013 438.50

Am I missing something basic here? I went through the API reference and didn't find something that would indicate this behavior.

10 responses

You aren't missing anything - you hit a bug. We're working on it this afternoon. Sorry for the confusion!

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.

This is now fixed. The log time stamp was suffering from a UTC/EDT conversion problem, and was being cast back to "yesterday" in daily mode. The actual backtest was OK.

Sweet!

Thank you Dan.

Dan - is this still fixed, and if so is there something I need to do to make it work in my old backtests?

For example, I ran a 4-day algo on AAPL. It shows a transaction firing off on Jan 3rd:
2012-01-03 19:00:00 AAPL BUY $413.47

But, according to Yahoo, that is the approx closing price (not adjusted) for 2012-01-04.
(I understand you process dividends via the cash mechanism, so comparing actual close).

Sending the price to plot via record, it seems to be consistently off 1 day for me still.
(See my "MA Cross-over Long-only" algo, if needed.)

Let me know, and thanks!
Ken

EDIT: But, if I put in a "print price" in the handler, it prints out the date & actual closing price correctly:
2012-01-03 PRINT 411.1
2012-01-04 PRINT 413.44

So, the logs seem to be correct, yet the graph and transaction details is confusing, as noted above.
For example, at the beginning of Jan 4th, the graph is already showing the closing price, as shown in the transaction details for Jan 3rd.

Ken, what's going on here is that we're doing a bit of shorthand in the transaction tab.

When you print the price on Jan-3-12, you see the close price for that day (411.10). Your algo also places an order on Jan-3-12 (though that isn't logged anywhere specifically, as written). On Jan-4-12, your algo logs a close price of $413.44. The order you placed on Jan 3 is filled on Jan 4 - at a price of $413.47. That's the same price as the close, plus slippage and commission. That is the price that is showing in the transaction tab.

In the transaction tab, we choose to display the fills based on the order date. That can be confusing sometimes (like this one) but makes it easier to understand other contexts.

For repro: Take the algo attached below and clone it (It's essentially the very first algo you posted, minor tweaks and date changes). Run a full backtest on it. You'll see a log file that looks like this. You'll see a transaction tab like this.

OK, I undertand now. When an order is placed, it is actually filled at the close of the next day. I didn't realize that nuance.

Thanks, Dan!

Yes - that's very key to preventing look-ahead bias. You place an order with knowledge of the close price in this period; the order is filled based on the close price of the next period.

Sure, I agree to avoid that; but, you could also buy on the following open, instead of waiting 24 hours since the "signal". Or, in a pseudo-daily system, you could place an MOC order based upon the current price at say 3:45 pm, etc.

But - I hear ya, and the current system is certainly straightforward, and is very logical and consistent by always using "close".

Thanks!
Ken

So running on daily data, there is no way to buy at the open and sell at the close? Because orders only get filled on the next close...