Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to place a limit sell once buy is filled

I want to place a limit sell $0.50 above the buy price of a stock once that order is filled. What is the best way to do this while accounting for the fact that orders are cancelled at the end of each day? I'll also likely have multiple open positions of the same stock at various prices so that seems to be complicating it a bit.

For example: I buy 50 shares of stock XYZ at $10 and 50 shares again at $9. I would like to place a limit sell at $10.50 and $9.50 that persists overnight or code that checks for open positions and places limit sells $0.50 above the limit buy price.

It feels like I should probably use something like get_open_orders() but haven't been able to figure it out.

Thanks for any help!

2 responses

This style of order management is currently tough in Quantopian, but not impossible. You'll want to use the get_order() function to track a single order position, note the amount of shares bought in that order (for example 50 shares of stock XYZ at $10). Once this order has been filled and you own the position, then you can submit a new limit order for 50 shares at $10.50. Here's an example how to detect order status (open, filled, cancelled, etc).

You can repeat this process twice: once for a limit sell at $10.50 and a second time for $9.50. Keep in mind that in live trading, all open orders are cancelled at end-of-day. You'll need to resubmit the limit orders at the following market open.

The function get_open_orders() will query for all orders that have been submitted to the broker but not yet filled. It won't return data on filled positions in your portfolio. Hope that clarifies the setup!

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.

Not to be a party pooper, but I think this sort of order management is effectively impossible with Quantopian. If you buy a stock at $10 and want a limit order to sell at 50c above your entry, there is basically no way to persist this order over multiple days, if you consider that the stock might split, or pay dividends, and the algo will never know either, and there is no record of historical transactions tied to open positions.

If your entry was at $10 last week and limit sell at $10.50, but the stock since split 2:1 and then paid a $1 dividend, it's price will now be (assuming it hasn't legitimately moved at all) $4, and you'd probably want your limit sell at $4.25 but there's no way at all to calculate that number.