Back to Community
order_target_percent bug

If you invoke order_target_percent on the same security in the same minute through two different schedule functions the end result is unexpected.

scheduled function 1: order_target_percent AAPL 0.1
scheduled function 2: order_target_percent AAPL 0.2

Expected result:
AAPL target percent 0.2

Given result:
AAPL target_percent 0.3

5 responses

Had an error with order_target_percent in my backtest recently. A position that was already in the portfolio at the beginning of the day was overbought when I used order_target_percent to "buy" the same position at the end of the day. Had a position go from X to 2X to 3X over a three day period, should have remained at X.

Behind the scenes, order_target_percent gets converted to an order for a fixed number of shares. That number is determined right away when you call order_target_percent and it it makes the conversion to shares without regard for any open orders. If you have open orders for the same asset, then you will likely end up with a larger position than you expected. My best suggestion would be to use get_open_orders to check if you have any open orders before placing a new order for a particular asset.

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.

OK, probably a bug with my coding.

@jamie I am afraid that is not what the function was designed for. If you wanted to purchase shares there is a different function for that.

Issuing order_target_percent you expect that the end state will be whatever leverage you specify as the parameter and the function should perform overrides etc to ensure that result. The get_open_orders fix you suggest feels like it should be a part of the function.

I'm not sure what Quantopian is doing with their Q fund broker, but back in the day when retail trading was supported, my understanding is that orders were sent to the broker as they were encountered in the code, but price & volume data and portfolio state were updated on the whole minute. So, the flow was:

Get OHLCV bar and broker data ---> Determine new portfolio ---> Submit set of orders ---> Wait for new OHLCV bar and broker data

If you don't wait for the result of the first set of orders, and submit additional orders, then bad things can happen.

@ Jamie - are there some guards for this problem when using this code:

objective = opt.TargetWeights(weights)

constraints = []

order_optimal_portfolio(  
                objective=objective,  
                constraints=constraints,  
                )  

How does order_optimal_portfolio manage multiple calls within a trading minute? Or would it behave the same as order_target_percent?