Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Questions on the API from a beginner

Hi,

I'm trying to understand the API and I had several questions.

  • It says handle_data is called whenever "a market event occurs for any of your algorithm's specified securities." What classifies as a market event?
  • How often is handle_data called (at most) when it's doing daily vs minute backtesting?
  • What about when there are multiple securities in your context? Multiple events could be happening for various securities; does this mean handle_data is called one for each event for every security?
  • How does a corresponding order work for handle_data when dealing with multiple securities? That is, if a market event deals with one security, do I still have to place an order for all the securities?
    • What is being plotted on the backtest graph when dealing with multiple securities? That is, I only see one graph as opposed to one graph for each security.
6 responses

I wish I could edit my post... That last bullet point was not meant to be a sub-bullet point.

Hello Vijay,

I'll fill you in where I can, and others can confirm or correct my feedback:

--It says handle_data is called whenever "a market event occurs for any of your algorithm's specified securities." What classifies as a market event?

Basically a "market event" is an entry in the Quantopian backtest database listing historical trade data for a security identified by a sid number (rather than a symbol). The available data are listed under "Event Properties" on the help page. There are actually two market event databases, one daily and one minutely. As I understand, the database entries don't correspond to actual historical market events but are valid proxies for backtesting. As a side note to the Quantopian team, I am curious how the database is derived...does your vendor openly publish the details?

--How often is handle_data called (at most) when it's doing daily vs minute backtesting?

As implied, handle_data is called once per day under daily backtesting and once per minute under minutely backtesting. However, handle_data will not be called if there are no market data for the specified securities on a given day or in a given minute.

--What about when there are multiple securities in your context? Multiple events could be happening for various securities; does this mean handle_data is called one for each event for every security?

When there are multiple securities, the handle_data method only gets called once per market tic. Within that call, your algorithm then can then loop through some or all of the specified securities.

--How does a corresponding order work for handle_data when dealing with multiple securities? That is, if a market event deals with one security, do I still have to place an order for all the securities?

To my knowledge, orders can only be placed on individual securities. You can't define a group of securities and then, for example, buy 100 shares of each with a single order. However, it is straightforward to loop through a list of securities to place orders.

--What is being plotted on the backtest graph when dealing with multiple securities? That is, I only see one graph as opposed to one graph for each security.

The backtest graph plots the overall return of the algorithm. Reportedly, Quantopian is working on the means to add additional time-series data to the graph.


A few more notes:

Regarding multiple securities, I found it instructive to run both liquid and thinly traded securities, to understand how the backtester handles these when multiple securities are specified. An example:

context.stocks = [sid(41290),sid(41425),sid(39479),sid(33972),sid(41159)]  

Additionally, the "order" method described on the help page is really a "submit_order" method. The order does not get fulfilled until the next market event for the security being transacted (and to my knowledge, there is no way to modify an order once it has been submitted, e.g. cancel it).

Grant

Thanks Grant

Thanks Grant. I think that gives me a better idea. I'm also curious as to how how the database is derived. And just to understand the whole process, let me see if I'm understanding this correctly...

Assume I have an algorithm and I'm testing for stocks X and Y on a minute-by-minute basis for a certain day.
For each minute, I receive a market event/tic for stocks X and Y consisting of "the opening price, closing price, high price, low price, and trading volume during that minute".
Then, based on that event data, I place orders for X and Y. From there, how does it determine the profit/loss from those orders? Does it simply see how much money say buying 100 shares of stock X and selling 50 shares of stock Y would result in based on the subsequent minute's data? And then keep some running total of net profit compute overall return at the end?

Hello Vijay,

I've never actually tested it, but I think, on a rolling basis, the backtester is computing the return (normalized to the start of the backtest) based on a hypothetical scenario of going to an all-cash portfolio assuming that in the next tic of the market all of the security prices are unchanged. I'm not sure how the backtester handles commissions and slippage in estimating the return. In other words, is it an estimate of the likely real return or is it idealized?

It'd be straightforward to write an algorithm to address you question...I recommend just giving it a try and posting the result here. Also, I think one could have a look at the source code for the backtester to verify the return computation.

Grant

Hi Vijay,

Unrelated to your specific questions, but now you can edit your posts by clicking the pencil icon on the far right of your name in the blue bar above each post. Hope that helps!

thanks,
Jean

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.