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

I've read this post: https://www.quantopian.com/posts/weird-behaviour-of-stoploss

It doesn't seem to help.

So, in my handle_data I have my code executing a limit order if a certain condition is met. Further down I am placing a stop order if that limit order is filled. I get that I can't place a stoporder on a position not yet filled. So I have it executing on the portfolio object:

        if context.portfolio.positions[s].amount > 0:  
            order(s, context.portfolio.positions[s].amount)  
            message = '{stock}: Stop Order {orderNumber} {time} {price}'  
            message = message.format(stock=s, orderNumber=context.order_id, time=exchange_time, price=l[s][-2])  
            log.info(message)  

The frustrating part is the build error I'm getting is this:

Runtime exception: TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'

The line for this error is in here:

                    if exchange_time >= oo.dt + timedelta(days=2):  
                        cancel_order(oo.id)  
                        message = '{stock} Order {order_id} cancelled due to two-day rule'  
                        message = message.format(stock=oo.sid.symbol, order_id=oo.id)  
                        log.info(message)  

So, to me, it would seem the exchange_time piece is were my code is breaking. But after spending a day trying to figure out what in the hell I was doing wrong I isolated the problem to the first piece of code posted above.

In other words, if I remove the stopOrder code, the program runs just fine. So I know it's an issue with the stopOrder.

What I don't know is why in the world it's doing this... To me it seems very simple - I'm checking for open positions on the s (security) object. I have some. I want to place a stop order. But the program does not want to run with that one line to place the stop order. If I comment out that order line the code runs without issues and every time I get into that if block I have open positions.

What am I missing???

1 response

Hi Tim,
It's not immediately apparent to me what's going on here. Just looking at that error, can you add log.info(type(oo.dt)) just before if exchange_time >= oo.dt + timedelta(days=2): ? It seems like it's recognizing oo.dt as something one time, and then something else another time. Either that or different code is getting run. You can then test in both scenarios you described above, and see if a) that block of code is getting run the correct amount of times and b) what the type is.

Let me know how that works and what you see.

Gus

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.