Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Number of owned shares?

Hi Everyone,

I was writing a simple algorithm to day trade some stocks for me, and I ran into a slight problem. I don't want the algorithm to purchase more than a hundred shares. Basically, it buys 1 share every minute that a certain condition is true, but I don't want it to buy a share if it already has 100 or more. Basically, I'm trying to find out how to get the number of currently owned shares. Could anyone help me out with this?

6 responses

Take a look at the trading guards where you can set restrictions on max positions, max order sizes, going long only, and more: https://www.quantopian.com/help#ide-trading-guards

You'd want the maximum position size call and can do something like:

set_max_position_size(symbol('AAPL'), max_shares=100)  
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 Pranjal,

You might consider the impact of commissions. Wouldn't you incur the $1 minimum per trade imposed by Interactive Brokers? So, if you are buying 1 share per minute, then to buy 100 shares in one day, you'll spend $100. So, you'll have to consistently make more than $100 per day just to break even (or $1 per trade, on a per trade basis).

Grant

One more thought - you can also get the positions from the portfolio object by using:

context.portfolio.positions  

This will return a dictionary of your positions in your algorithm. If you want to know the number of shares you currently own you can do,

len(context.portfolio.positions)  

Hi Alisa and Grant, thank you both very much for those suggestions, I will give them a try as soon as possible.

Alisa Deychman: Wouldn't len(context.portfolio.positions) give the positions number, not the number of shares? For instance if we had 200 shares of IBM that would still be only 1 position.

Try context.portfolio.positions[put the stock here].amount
This will give you the number of shares for whatever stock you have between the brackets.