Is it correct that before_trading_start() doesn't run until the second day of a backtest? Shouldn't it run the first day?
Is it correct that before_trading_start() doesn't run until the second day of a backtest? Shouldn't it run the first day?
Grant, I'm not sure how the backtest you posted illustrates your point.
I just tested it, and as far as I can tell, before_trading_start() is, in fact, called on the first day of the backtest.
I did discover during my testing that there appears to be an issue where if a backtest generates fewer than three log messages, the log messages don't show up in the UI. Perhaps that's what was causing you to believe before_trading_start() wasn't being called on the first day. I've filed an internal defect report about this issue. Hopefully we'll be able to figure out what's going on and fix it soon.
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 Jonathan,
When I run the code above, the output is:
2015-10-08 PRINT AAPL
2015-10-08 PRINT QQQ
2015-10-08 PRINT SPY
2015-10-09 PRINT AAPL
2015-10-09 PRINT QQQ
2015-10-09 PRINT SPY
Shouldn't it start printing on 2015-10-07, when the backtest starts?
This code shows that
before_trading_start does get called the first day of a backtest. However, I'm still confused, since it appears that data is empty until the second day, since this loop has no output the first day:
for stock in data:
print stock.symbol
Shouldn't the universe get updated upon the first call to update_universe and since I have context.aapl = sid(24) in initialize, even if update_universe doesn't take effect immediately, shouldn't AAPL show up in data?
Backtest log output:
2015-10-07 PRINT 2015-10-07 00:00:00+00:00
2015-10-08 PRINT 2015-10-08 00:00:00+00:00
2015-10-08 PRINT AAPL
2015-10-08 PRINT QQQ
2015-10-08 PRINT SPY
2015-10-09 PRINT 2015-10-09 00:00:00+00:00
2015-10-09 PRINT AAPL
2015-10-09 PRINT QQQ
2015-10-09 PRINT SPY
Hey Grant,
The issue is that on the first day of the backtest, before_trading_start doesn't know what your universe is, so it doesn't know what SIDs to get data for. Assuming you update your universe on that first day, by the second day it has data. This is something we will be fixing, for now it's just something you have to work around if you want to use data in BTS.
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 Karen,
I don't understand how you'll fix this behavior. If I understand correctly, a stock doesn't actually get added to
data until the first trade is encountered for the day. So, if there is at least one trade over the first minute of the market, a stock will show up immediately in data. However, there can be a delay for stocks that are thinly traded. Do I understand this behavior correctly?
This goes back to a dated post, https://www.quantopian.com/posts/thinly-traded-stocks-why-no-gaps, which discusses how you forward fill. The problem I see is that you can forward fill from the prior trade day to have data available in before_trading_start. However, if the call to before_trading_start is on the start_date for a given security, then what will you do? You can't backward fill, since it would introduce a look-ahead bias, right? Or would you backward fill with NaNs?
This would seem to be relevant to pipeline, since I think where you want to land is that a coarse filter could be applied using pipeline using daily data, and then it could be refined using minutely data, by calling history, after the call to update_universe. If my understanding is correct, you'd have a problem, since within before_trading_start, you'd have one set of stocks, and as the trading day went on, you'd have another. I suppose this is no show-stopper, just a pitfall, but users just have to know to always check if a stock in in data before using it as a key.
Under live trading, how do you handle all of this? I'm guessing that you just get a stream of trades, from which you create minute bars, and as soon as you can create a bar, you add the security to data. It is the same behavior as for the backtester. You have no knowledge of whether or not a given stock is tradeable on a given day.
On a separate but related topic, are you planning to support order submission from within before_trading_start? It would be a natural progression. Filter stocks using pipeline. Refine the selection using history. Place orders to be filled when the market opens at 9:30 am.
I have run into difficulties trying to place orders first thing in the morning for illiquid stocks. Checking if a stock is in data before placing an order for it is one of the most frustrating things in Quantopian, since there is absolutely no reason one shouldn't be able to place an order for something that hasn't yet traded today.
Simon,
I think the problem may be that Q just gets trades from Nanex, but I could be wrong. If it is only trades, then the injestor spits out only bars. Until the first bar is received for a given stock, there's no way of knowing that the stock is even listed for trading. At least that's my working hypothesis at this point. But maybe Q knows ahead of time which stocks could possibly trade on a given day? Murky at this point.
It does seem whacky. Why not just fill the bars with NaNs and put the order on ice (backtesting) or send it on its merry way to IB (live trading). One implication, I think, is that using Quantopian, one could never be the first to buy a stock when it is first issued.
Do you see the problem only when you try to place orders on the
start_date for the stock? Or is the problem unrelated to the start_date?
Grant