Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
simple day-based buy & sell

I'm tinkering with how Quantopian handles time. I think what's going on in the background is for days earlier than the 15th of the month, the backtester is buying SPY, and for the 15th and later, it is selling SPY.

Does my algorithm execute an SPY order every minute the market is open?

11 responses

@Grant,
Thanks for the share, and for the question.

You are correct that the day field is the day of month. The datetime property is a python datetime object: http://docs.python.org/library/datetime.html#datetime-objects

Your algorithm will buy on every event. In the IDE, we send one event per day. N the full backtest we send one event per minute.

Sound right?

Thanks,
fawce

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.

Hello Fawce,

I'd like to understand how you define an event. On your help page, you state that a minute bar is a summary of the trading activity for a security for a one-minute period. So, what if there was no historical trading activity (even though the security was listed and available for trading)? Will there still be events every minute (the "event" being that nothing happened)?

In my example above, did the algorithm place an order simply because a minute had ticked by, or because historically there was SPY trading activity during the minute?

Grant, I'll jump in since I think Fawce is out tonight. I think I can explain this best with an example. Let's say it's minute 0. Your algo will place an order to buy one share (or sell, depending on the day). That trade is not executed in minute 0 because that would permit look-ahead bias. In minute 1, your algo will place another order to buy 1 share. Also, the backtester will attempt to fill the order from minute 0. If there were no shares traded in minute 1, then the order remains open and unfilled. In minute 2, your algo will place another order to buy 1 share. The backtester will attempt to fill any unfilled orders - certainly the minute 1 order is open, since it just came in, and maybe the minute 0 is still open. That process repeats every minute - an order is placed, any existing orders are filled as possible. Every day at the close of trading all open orders are closed.

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.

Thanks Dan,

Is there any additional logic applied to trigger an order? For example, let's say that at minute 0 I place an order for 1000 shares of security X. Historically, only 100 shares were transacted during minute 1 (an order-triggering event). Will the backtester order all 1000 shares of X in minute 1, or fewer?

Hi Grant - Last week we gave members the ability to control how the backtester behaves in a situation like that, and we rolled out new documentation with that. I should have pointed that out to you last night. Belatedly: slippage help

In answer to your question: In the default settings, 1000 shares ordered in minute 0 and historically 100 shares traded in minute 1, the backtester will fill only 25% of the available volume, meaning 25 shares, and leave the remaining 975 open. The price of the 25 shares will be adjusted upwards over the historical value, too, because your purchase would probably affect the market. In the next minute, the process would repeat all over.

That scenario, of course, is a very tough one to actually model. A huge order in a thinly traded stock is very tough to predict. The backtester should be better when the order size is smaller relative to the size of the traded volume.

Grant, talking this over with the team, I made a mistake in my description. Correction: When there is no data for a stock in a frame, when it was not traded, the algorithm skips that frame entirely - it "compresses time." Revised example:

  • minute 0: There is trade data. Your algo places an order for 1 share.
  • minute 1: There is trade data. Your algo places an order for 1 share. The order from minute 0 is filled.
  • minute 2: There is no trade data. Nothing happens.
  • minute 3: There is no trade data. Nothing happens.
  • minute 4: There is trade data. Your algo places an order for 1 share. The order from minute 1 is filled.

I hope that helps rather than confuses!

Thanks Dan,

I'll have to read the slippage help reference. Regarding order placement and order fulfilment, as I understand from your example above, both require a trade data entry (an "event") to trigger an action, otherwise nothing happens. Makes sense.

Correct!

Hi Dan & all,

Seems like there ought to be some way in Quantopian to measure the liquidity of a security (over the prior N minutes/hours/days of market activity), right? Also, for screening/research purposes, it might be interesting to find securities that are sorta illiquid to see if their properties differ from the run-of-the-mill liquid ones.

I think that you're right. We're looking at ways to expose what the dollar-volume of stocks are, and how a given stock's dollar-volume compares to the big picture. I believe we will expose that in a week or two, provided this feature comes out the way I hope it will.

Hello Dan and all,

I'm still kinda confused how your backtester works when there is no trade/price data for a given minute. For example, in

def handle_data(data, context):  

what if I have two algorithms, one for security #1 and the other for security #2. For a given minute, suppose there are no trade/price data for #1, but trade/price data are available for #2. What happens? Does handle_data still get processed for the minute to execute the algorithm for security #2?

Also, what happens if I make a calculation based on data from security #1 and security #2 (e.g. volume, price, etc.). If no data exists for #1, but I have it for #2, how does the backtester handle it?