Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Closing orders

Hi. I'm new to that platform, still learning. I wanted to ask about closing orders.

I had some experience with MetaTrader (4 and 5) and futures. Generaly it looks like when you open a position you have a transation opened. To close it, you have to say "I want to close that" instead of declaring opposed order.

I spent a while to figure it out how to cancel_order or to stop it, than I found that I have to just make the new order.

The question is, when I am using real broker, how you handle closing transactions? You close existing transactions and open new one? Or maybe opening and closing transactions is only futures feature (I used only that so I do not know how things happen on other markets).

2 responses

It's weird that nobody replied on this. Perhaps because the question is a bit unclear?

I'd imagine that translation from cancel_order and order_target (with zero amount) is handled by zipline or IB wrapper and in any case will result in a Cancel order sent to the broker / exchange (depending what order type is being canceled). Can anybody confirm this please?

Hi Alex,

The functions cancel_order and order_target have different objectives. If you have an open order and want to close the position, you should use order_target and more specifically, the command "order_target(stock, 0)". If you have a long position of 100 shares of AAPL in your portfolio, this submits a sell order -100 of AAPL to zipline or the broker, depending on your connectivity. The behavior is identical in simulation and with a broker.

Keep in mind that orders are asynchronous. You can submit multiple orders without waiting for a previous order to fill. To avoid over-ordering, check for open orders before submitting new orders: https://www.quantopian.com/posts/order-target-percent-ordering-too-much

The function cancel_order will terminate any open, unfilled orders. This will not impact positions that are already in your portfolio. If an order is partially filled, the unfilled number of shares will be cancelled. Hope that clarifies the different behavior.

Cheers,
Alisa

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.