Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Multi-SID Example Algorithm

In our help documentation, we provide both a very basic algorithm to use as a sample and a multi-SID example. This is the multi-SID example.

You should press the "Clone Algorithm" button below, and that will clone the code into your "My Algorithms" page. You can then hack it to your heart's content! Try using different stocks, different buy/sell logic, etc.

The comments in this algorithm are fairly detailed, and should be helpful in setting up your own multi-SID algorithm.

This algo also demonstrates how to write logs, import datetime, and use timezone (pytz).

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.

6 responses

Hey Dan, I know this sounds like newb question but I'm confused by the first for statement. When we are iterating and setting the price, notional, and tradeday how do the variables keep the scope until they are used in the second for statement? Wouldn't Python set the three variables for Apple but then just replace the values when the stock changes to Alcoa?

Let me know if I missed a major part, I'm still reading the docs.

Xavier,

Thanks for the question. Your reading of the code is correct. The notional limit here is on the total notional value of the portfolio, rather than on the individual positions. So, you are correct that we are over-writing a single notional value.

Position limits would be a much more effective tool - you could easily update this code to use a single loop rather than two, and to keep a dictionary of notional values keyed by the stock.

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.

A new notional line in the second For statement will contain the individual security position size. Or does that muck something up with the logs?
Also, what is the best way to compare the contributive performance of individual securities?

Thanks for any help. Rob

Rob, If I'm understanding you correctly, that new line you want to add shouldn't mess up anything with the logs.

I think there are a few ways to compare the contributive performance. In this example I use record() to plot each position's value from Dan's algorithm above. You could also log.info() each position's value, record each security's price, etc. Does that help?

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 Gus, I did not know how to plot position sizes like that. Is it possible to plot the individual stocks' realized p/l? without pasting pages of back-test transaction data into Excel, that is.

best,

It should be possible. So you have to compute the average profit on winning trades and the average profit on losing trades, then just divide them and use record(). I'm not totally familiar with realized profit/loss, but Quantopian has already taken commission into account on trades so I think profit/loss here is automatically realized profit/loss, but I could be wrong. Anyway, your process order should be something like this:

  1. Detect when a trade begins and store the starting price.
  2. Detect when a trade ends, and calculate either the gain or loss. Maybe have two lists, one with gains and one with losses that you append results of each trade to.
  3. With those lists you can calculate p/l.

With multiple stocks it gets more complicated, but it can still be done with two dimensional lists, etc. I suggest looking into either deques or Quantopian's batch_transform to work with data more easily.

Does that help? Let me know if you have any more questions.