Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
order_target_percent(stock, 0) can cause unexpected consequence - short position

order_target_percent(stock, 0) is causing short position by January 15th.
I think it is because "context.portfolio.positions[stock].amount > 0" is true in line 42 even after the initial "order_target_percent(stock, 0)" from the previous minute if the market is not liquid enough. In that case, you will end up sending "order_target_percent(stock, 0)" again while the first (previous minute) "order_target_percent(stock, 0)" is still partially queued up to get ride of everything.

Is there any clever way to build a variable such as "context.current_position_ind" so that it will flip once for each buy all or buy sell order in order to avoid issue described above. Also I need "context.current_position_ind" such that it can handle multiple securities. I am not so well versed in Python yet. I would think I need to build "context.current_position_ind" either as a list or pandas.

Thank you very much.

8 responses

A simple workaround suggested before is to use the Quantopianget_open_orders function to see if there are any open orders (not filled in full). There are two ways.

A call to get_open_orders() returns a dictionary of all your open orders, mapping the security ID (sid) of each security in your current universe to a list of open orders to buy or sell that security. Used in a Python condition, it is equivalent to True if you have at least one open order, regardless of security, or to False if you have no open orders at all. To do no trading until all open orders are filled, you could write:

def handle_data(context, data):  
    if not get_open_orders():  
        # decide what, buy low, sell high  

A call to get_open_orders(sid) returns a list of all your open orders for the security sid. Used in a Python condition, it is equivalent to True if you have at least one open order for that security, or to False if you have no open orders for it, regardless of orders for any other security. To do no trading in security sid until all open orders for it are filled, you could write:

def handle_data(context, data):  
    for sid in data:  
        if not get_open_orders(sid):  
            # buy sid low, sell high  

In your code, you could insert and not get_open_orders(stock) before the colon on line 42 and 49.

André, thanks for sharing your thoughts. However I couldn't get it to work. It seems like "not get_open_orders(stock)" stays true all the time. So it will never attempt to unwind.

There is a fix we are working on for this over here: https://github.com/quantopian/zipline/pull/582

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.

Thomas, Thanks for your post. But I am not sure what is going on by looking at the link you provided above. Could you please help me understand in a layman term what you are referring as "fix"? Are you trying to fix "order_target_percent" function? I don't know what you should be fixing, because all I need to know is to put the right formula to avoid entering an intended trade more than once (that is by waiting for the first "order_target_percent" function to complete its process if order size is too big and needs to spread out to the next t+1, t+2 ..., perhaps somehow using get_open_orders's amount becoming zero or something. So I think fix can be made without changing Quantopian's underlying platform set up. I think John Fawcett was getting to the same ball park although I am having difficulty implementing. https://www.quantopian.com/posts/log-dot-info-problem

Ujae, the problem is that order_target_percent() does not take open orders into account and we have internally discussed this problem for quite a while. Your proposed fix certainly works for your algorithm but I think it's better to fix this for everyone as it's rarely intended and can lead to weird effects like you observed.

Hi Thomas,

I'm no expert, but I've gathered that there is a general problem with providing more sophisticated tools to manage execution, which would seem to be best done in a real-time fashion on the broker side, versus code that would run on a Quantopian server, with only minutely updates.

In the context of the Q hedge fund, what do you expect to do? I thought I heard Jess Stauth mention in a webinar that you'd be moving away from IB to a "prime broker" so would that broker offer order execution on their side? Would that broker be available to individual, non-manager (i.e. retail) traders using Quantopian? I just figure that if you are headed toward managing $10B in capital, you'd need to be thinking well beyond tweaks to order_target_percent(), right?

Best regards,

Grant

As Captain Fawcett of the Starship Quantopian is wont to utter, "Make it so." (with regards to specific portfolio target holding percentages), so too must every algorithmic strategy commander have the power to "make it so." No doubt that as the talk of moving massive amounts of money through algorithms becomes more common than the constant banter of beta and alpha, a focus on intelligent execution will take centerstage. Maybe the Q can start to share their intents and understanding in the space now rather than after they release new functionality; such that this critical piece of fund management can be understood more thoroughly by us, your minions.

Thomas knows, bad execution can kill a strategy. What works stunningly at $100k fails miserably at $100M. The breakdown is execution. The inability to solve this breakdown will render the Q Fund a non-starter out the gate. This though is generally thought of as the realm of the broker. "Broker, I need 10,000 TSLA -- make it so!" But why would we leave such power in the hands of pure Wall Street players? Best execution? Really? How are you tackling this? What avenues will be made available to strat writers, if any? Or is the magic of "Make it so." all that you think we'll need? Looking forward to your answers, MT.

Grant,
Very good point. I fixed it this way. Thanks
Ujae