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

I'm openning an order and using a second order with the method order_target to set an automatic closure for my trade in case the price touches a target value:

# Enter trade  
order_id = order(s, 100, style=StopOrder(stop_price=(open_price * 0.95), exchange=IBExchange.SMART))  
log.debug(order_id)  # prints a code

 # Set the target to close the trade  
order_id = order_target(s, 0, style=LimitOrder(limit_price=close_price, exchange=IBExchange.SMART))  
log.debug(order_id)  # prints None  

Please ignore variables s, open_price and close_price, as I have tested them and are ok.

Where is the problem? It is not documented in the API that order_target (nor any other order method) can return None.

4 responses

I think this relates to your previous question which I forgot to answer. But anyway, yes, order* functions returning none has been a pet peeve of mine as well, in addition to orders which have a valid id but result in an order object which is null. I believe it relates to the face that if the order would be for 0 shares, it doesn't place the order or return an order id. Here, you've placed the order for 100 shares, but that order hasn't yet been filled. In backtest and quantopian paper trading, that order won't be filled until the close of the following bar. In IB live trading, I am not sure if it starts working immediately or if that too waits until at least your python callback has returned.

In any case, at the point where your second order is placed, in backtest, your position in that stock has not yet been accumulated, so the order_target is a no-op, since the order_target functions do not take into account open orders. It just looks at your positions, sees that you don't have any positions, and therefore to reach 0 shares it need not order anything at all.

Since you are doing some rather detailed order management, I have to warn you about some other tedious details:

  • in live trading, all orders are cancelled at the end of the day, so if you have stops/limit orders you want to persist, you need to replace them in the morning
  • however, you can't just place them at the same price that you did yesterday, since the stock may have split overnight. If you care about dividends impacting the price, you are simply SOL, you can't deal with that in live trading. For splits, you can at least replace the order via some percentage offset from your historical price as you fetch it today when you originally placed the order. So, if today the stock price is $40, and you want to place a stop at 5% below, record the date (and maybe time) you place the order, and the percentage offset, and every morning, replace the stop using the percentage offset from the results of the history() function looking for that date. Alternatively, it's possible you could use the average cost of your portfolio holding, I expect (hope!) that Quantopian/zipline split-adjusts that.
  • good luck!

Ok I get it. The short answer is that since first order is not filled until the end of the current candlestick, the second order is not placed since there is no need for action to get to 0 shares.

I understand that limitation (could be better documented though), but the real problem is that Quantopian does not provide profit targets (or at least I have been unable to find them at the documentation), and provided stop losses are not reliable in my recent experience. Therefore, I had to make up my own solution, which is more error-prone and much less elegant than providing internal stops and targets.

In the attached code, I was trying to backtest a simple strategy:
1. detect open gaps
2. classify them in 3 groups depending on the openning price and the pivot points in order to trade/not to trade them
3. trade them with risk/reward 1:1 or 1.5:1 depending on the type of gap. This means using a profit target for yesterday's close price (to close the gap) and a stop loss
4. 5 minutes after market closes, close open positions to market (if profit target nor stop loss have been reached)

Nevertheless, when backtest was completed, I saw that many days the step 4 was triggered to close positions which had passes the StopLoss, so they are not reliable. This is a big issue!

So I coded both profit target and stop loss like the code attacked.

Other problem I detected is the scheduled function has limitations, so I can go for the minute 1 as soonest, and to minute 5 before closure at most. The problem with minute 1 as soonest, is that the trade actually takes place at minute 2, which can be too late for my system. I don't unserstand this limitation either.

So, the important question is: isn't there an easy way of using simple, elegant stop losses and profit targets?

You might want to try trading it with fixed lot sizes to start, I suspect that order_percent won't do what you expect. This looks like a nice system, reminds me of some of the Master The Gap stuff, but I don't know how well it will work on Quantopian where as you say your first order must wait until 9:31 at which point much of the reversion may have already happened.

It is indeed based in the gap fade system described in Mastering the Gap, good catch! But John Carter classified the gaps according to the volume of key stocks in the premarket, which can't be done here in Q (as long as I know), so I'm trying to classify them depending on where the opening price is realted to the pivot points.

I still need to check if the stop loss and profit targets are working as expected (I suspect they are not), but I was expecting better results since this is described as a trade which works 80% of times according to both John Carter and Bulkowski.

Any comments and suggestions are welcome!