- ago
I used the following code construct to place a Sell Limit together with a Sell MOC using IB/TWS API.

CODE:
            Backtester.CancelationCode = (int)bars.DateTimes[idx].DayOfWeek;             PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target, "Limit Sell Target");             if (isNextBarLastDayOfWeek)             {                Backtester.CancelationCode = (int)bars.DateTimes[idx].DayOfWeek;                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, "Market Close EOW");             }


The Limit was hit and executed but the Sell MOC is still in WaitForClose status in Order Manager instead of being cancelled. Am I using this the wrong way?
0
459
Solved
25 Replies

Reply

Bookmark

Sort
Cone7
 ( 30.93% )
- ago
#1
It's a special case because the MOC order hasn't yet activated to be canceled at the time the other order filled. We'll probably have to work on this special case.

Note -
Either of the PortfolioSync Trading Preferences (Reduce Size.. or Always Set.. ) will still prevent the order from actually being transmitted if shares no longer exist in the account.
0
- ago
#2
Thanks for the explanation. Should I create a feature request?
0
Cone7
 ( 30.93% )
- ago
#3
At the moment I'm considering it a bug until Glitch puts in his 2 cents.
0
Cone7
 ( 30.93% )
- ago
#4
This is fixed for Build 17.
... as long as this is used in a Daily+ (not intraday) strategy.

OrderType.MarketClose [currently] creates a transaction for the bar's closing price. In an intraday context, that's the close of the current bar. Intraday traders who really want to a true "MOC" order must be careful to place/signal a MarketClose order on the penultimate bar of the day, and, it needs to be at least 5 or 10 minutes before the close, NASDAQ or NYSE, respectively to be accepted. Otherwise, you can use the pseudo-MarketClose functionality of the Order Manager to place a market order n-seconds before the close.
0
Best Answer
- ago
#5
Thanks for the heads-up. I think originally, I expected the MOC (Pending) to be removed from the Order Manager when the limit was hit, but it wasn't. I presume this is what was fixed?

However, in the time that's passed, I've become aware of f other constraints that make my original coding (above) still not usable.

1. An MOC order will not be accepted by IB broker if there is a limit order pending. Therefore, if the limit has not been hit, the MOC will be rejected (e.g., if transmitted 15 minutes before close).
2. There is currently no way to cancel the Limit order to make room for the MOC until there is a function such as Limit Good-till-date (time) or if an MOC automatically closes all outstanding orders of the same type (Buy or Sell) before placing MOC.
3. If LL functionality of WL is changed so a MOC order placed before or during market is sent immediately to broker (as discussed elsewhere) instead of being pended, then will still have the conflict with the limit order, only sooner. Behavior of stacking Limit / MOC will have revisited if MOC is no longer pended. For example, in #2 I mentioned MOC cancelling Limit. That wouldn't work without a pending, as well as immediate, placement option.

It gets complicated. The net is that there is still not a way to "stack" an MOC together with a Limit order. I still think that is a viable trading pattern, but WL would have to work around the broker constraints as mentioned above.
0
Cone7
 ( 30.93% )
- ago
#6
QUOTE:
I expected the MOC (Pending) to be removed from the Order Manager when the limit was hit, but it wasn't. I presume this is what was fixed?
In your scenario, when the limit is hit the MOC order is Canceled. That works now.

QUOTE:
The net is that there is still not a way to "stack" an MOC together with a Limit order. I still think that is a viable trading pattern, but WL would have to work around the broker constraints as mentioned above.
WL already works around it by holding the MOC order until the "N Seconds before Close" that you specify in the Trading Preferences. The only issue was that you wanted both orders active at the same time in case WL couldn't complete its job due to some system failure.
0
- ago
#7
QUOTE:
The only issue was that you wanted both orders active at the same time in case WL couldn't complete its job due to some system failure.

Actually, that was a different issue https://www.wealth-lab.com/Discussion/MOC-Order-Pending-8572 which I presume is on some list for implementation (unless you consider it a feature request).

The issue relative to this discussion is that an MOC cannot be placed while a Limit is active and there is no way I am aware of to automatically or programmatically cancel the limit so the MOC can be placed without being rejected by the broker if the limit is still active near market close.
0
Cone7
 ( 30.93% )
- ago
#8
QUOTE:
The issue relative to this discussion is that an MOC cannot be placed while a Limit is active
What do you mean exactly? You can definitely do that in TWS, and here's the current Wealth-Lab equivalent.



Are you saying that the Limit order is canceled when WL places the MOC with the broker N-seconds before the close?
0
- ago
#9
No, I'm saying that the MOC will be rejected by the broker when WL tries to place it (because Limit is active), and the Limit will cancel EOD if not executed (per WL rules). But the position will remain open, which is not the objective of the strategy.
0
Cone7
 ( 30.93% )
- ago
#10
QUOTE:
the MOC will be rejected by the broker when WL tries to place it (because Limit is active)
This is not my experience.

0
Cone7
 ( 30.93% )
- ago
#11
Strangely, IB filled this MarketClose order about 20 seconds before the closing bell. I'm not sure why, but maybe it has something to do with the Paper account???

You can see that the Limit was canceled when the MOC filled (but the timestamps are when the orders were placed).

0
- ago
#12
I'm not completely sure, but that rings a bell. I seem to recall that I went merrily along stacking Limits & MOCs. in my strategy when using IB paper account. Never an issue.

Once I transitioned to a live account, the issue showed up the first time a Limit was not hit.

The IB paper account, as with most brokers, has limitations synthesizing live trading. In addition to market data being wacko, I don't think all the back-end broker rules and restrictions are active with a paper account.

The upshot is that no matter how much testing you do with paper trading, you should be prepared for a few surprises when you transition to live trading.

You can do a simple test on a live account by placing a limit order way out of the money in TWS and then seeing if it provides the option to place an MOC on same symbol (e.g.., with OCO construct). If it doesn't provide that option, or if does and then rejects it, it's a good indication it will fail with API.

0
Cone7
 ( 30.93% )
- ago
#13
You can use the non-wacko live data with a Paper account too. There's an option to enable that somewhere in the account settings (as I recall).

I use IB live everyday - 2 accounts. So far, the only difference I've seen between live and paper was this MOC fill 20 seconds before the close.

Now the difference w.r.t. to exiting a Position with two orders could be that you're using a cash account and not margin. Of course this will be a limitation because you can't oversell (short), so no broker will allow you to have 2 orders live to exit the same shares in a Cash account.
0
- ago
#14
Good thought but not correct. I have margin accounts. As a general statement, brokers do not allow MOC with other open orders on same symbol. WL has a way to manage that, potentially.

Have you tried placing MOC order with a limit way out of money and see what I'm saying?
0
Cone7
 ( 30.93% )
- ago
#15
Is it required that I have to try it differently? It works, just try it. If the broker is rejecting your order because of some other limitation, then we're done here.
0
- ago
#16
QUOTE:
It works, just try it.

I'm afraid we may not be talking about the same thing. What is it specifically you want me to try and how do you want me to try it?

Are you saying that in a **live** account you are able to place a MOC and limit order for a symbol through the IB API and:
1. both orders are accepted
2. the MOC is executed if limit is not hit or cancelled?

I'm trying to understand if the examples you shared in posts 10 & 11 were done in live or paper accounts.

0
Cone7
 ( 30.93% )
- ago
#18
** live ** account
This gif is on a loop:
1. the Limit order starts Active and the MOC is WaitForClose.
2. WL placed the MOC and it goes Active
3. The Limit order Fills and the MOC is canceled in response.

0
- ago
#19
Thank you for taking the time to do this for me. I'm surprised and wondering why this didn't work for me.

When you have the time, could you please send me your code snippet that places the limit and MOC transactions together and incorporates the Cancellation Code? I will try again early in the week when market is open and let you know what happens.
0
Cone7
 ( 30.93% )
- ago
#20
There's really no difference in the code from what you posted in #1, although it's probably better to use a constant than a variable for the CancelationCode for the orders you want to associate.

CODE:
if (HasOpenPosition(bars, PositionType.Long)) {             Position pos = LastPosition;             Backtester.CancelationCode = 51;             PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.Close[idx] * 1.005, "Limit Sell Target");             if (true)             {                Backtester.CancelationCode = 51;                PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, "Market Close EOW");             } }
0
- ago
#21
I tried to short-cut a way to test stacking Sell Limt/MOC by manually entering trades from the Order Manager. I was surprised to see that the second trade cancelled the first, regardless of entry order, so I couldn't get both placed on same symbol / qty using the Order Manager.

Is that expected behavior?
0
Cone7
 ( 30.93% )
- ago
#22
Yes, expected.

Both orders must be entered simultaneously from a strategy with the same CancelationCode. There's no shortcut.
0
- ago
#23
@Cone, I see now what my source of confusion on this issue has been. I now realize the "failure" first happened with my IRA account. It was because, even though it is a (limited) margin account, shorting, as you know, is not allowed in IRA account. Therefore, IB broker rejected pended MOC when Limit was still active, to prevent the possibility of a buying short if limit happened to be hit after MOC was placed. I know you understand all this already.

I didn't realize it was because of the IRA account and apparently didn't try it with a non-IRA account. Sorry for the spin but I understand the limitations much better now, in my continuing education on automated trading, brokers, API's regulations, etc.

Limit/MOC stacking works just fine on a non-IRA account, assuming it is a margin account. Of course, currently, we still have the possibility of buying an unwanted short, but I know you are aware of that.

Thanks for helping me unravel this.
0
Cone7
 ( 30.93% )
- ago
#24
Sure. And thanks to you, canceling MOC works better now.
0
- ago
#25
:)
0

Reply

Bookmark

Sort