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

I have one of my algorithms live trading right now, and it looks like there's a slight issue with the way that order_value works, and I'm wondering if there's a reasonable way of handling it.

I had $304 in my IB account, so my algorithm placed an order_value() on a symbol with $304 as the target dollar amount. However, order_value submitted the buy order as 15 shares, but by the time the order went through, the price per share went up slightly, such that the order would have cost $310 - so IB rejected the order.

In the case of my particular algorithm it doesn't really matter if a particular purchase gets rejected (since a different purchase will be attempted on the next frame), but I do worry that IB might get upset about too many rejected orders coming through, and some algorithms may be negatively impacted by this. Also, if I did have a margin account, the order would have gone through and put my account in debt.

Is there any way for Quantopian to submit the order to IB based on maximum dollar amount, rather than based on a share count? Or any way for Quantopian to at least automatically apply a limit to a fill-or-kill order so that the share price doesn't cause the purchase price to exceed the available funds? Or something I should be doing differently from the algorithm's point of view?

Also, depending on the implementation of order_value, it might be possible to bring the computation of the share count closer to the submission of the order to reduce the window of opportunity for this race condition. Obviously there's no way to solve it perfectly, but if order_value were to get the current share price as soon as possible before calculating the number of shares, that would reduce the possibility somewhat. I don't know if you're doing this already though. :)

4 responses

Hi J,

You can add a cash buffer to your code to prevent situations like this one, where the price moves because of slippage. Here's an example with order_target_percent that uses 99% of your cash, leaving a 1% reserve (see the context.weights variable): https://www.quantopian.com/posts/rebalance-algo-9-sector-etfs. You could use a similar idea with the order_value function.

To further simulate this in backtesting, set a slippage that is most applicable for your strategy. For more information take a look at: https://www.quantopian.com/help#ide-slippage

In terms of IB-specific order types, we have a tricky balance where we want to build powerful tools for you to live trade, yet remain agnostic to expand to multiple brokers. We did a couple IB styles for order execution: RelativeOrder and VWAPBestEffort, see: https://www.quantopian.com/help#api-order-execution

Hope that helps!

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.

Okay, so basically there's no way to prevent the issue at purchase time and it's just up to the algorithm to put a buffer on the trade amount? Good to know, thanks.

Hello Alisa & J.,

Would a limit order work to avoid exceeding available cash? If the order doesn't go through, then it could be killed via the algorithm in the next minute bar, right?

Grant

A limit order would work as well, but you will need to manage creating and canceling many limit orders. I'd suggest to use a cash buffer instead, and both options can be tested in the backtester.