In automated daily trading it is sometimes advantageous to place an MOC order and a limit order for the same symbol. In In the case of a Sell Limit, this would take advantage of potential intraday market movement but if the limit is not hit exit the position at the end of the day (or week) to limit potential losses.
With non-margin accounts (such as IRA's) brokers won't allow placing an MOC order if a limit order (for the same symbol and action) is already in place. This is the broker's way preventing a non-margin account creating a short position (not allowed) in the case that the limit is hit but the MOC is not cancelled.
WL8 has a potential workaround in that trade preferences allow for control of MOC placement by staging an MOC to be place n minutes before market close. However, the staged trade will be rejected (in a non-margin account) if a limit for the same trade is currently active.
This feature request is to have staged MOC trades cancel any active limit trades that would cause this conflict, further utilizing the built-in automation capabilities of WL8 to provide addition "broker friendly" trade structures for MOC similar to OCO.
(Note: this Feature Requests has overlap with three others, https://www.wealth-lab.com/Discussion/GTD-Order-8169 , https://www.wealth-lab.com/Discussion/TWS-OCA-and-Trade-Stacking-8115 and https://www.wealth-lab.com/Discussion/Limit-MOC-8293)
With non-margin accounts (such as IRA's) brokers won't allow placing an MOC order if a limit order (for the same symbol and action) is already in place. This is the broker's way preventing a non-margin account creating a short position (not allowed) in the case that the limit is hit but the MOC is not cancelled.
WL8 has a potential workaround in that trade preferences allow for control of MOC placement by staging an MOC to be place n minutes before market close. However, the staged trade will be rejected (in a non-margin account) if a limit for the same trade is currently active.
This feature request is to have staged MOC trades cancel any active limit trades that would cause this conflict, further utilizing the built-in automation capabilities of WL8 to provide addition "broker friendly" trade structures for MOC similar to OCO.
(Note: this Feature Requests has overlap with three others, https://www.wealth-lab.com/Discussion/GTD-Order-8169 , https://www.wealth-lab.com/Discussion/TWS-OCA-and-Trade-Stacking-8115 and https://www.wealth-lab.com/Discussion/Limit-MOC-8293)
Rename
QUOTE:Makes sense!
staged MOC trades cancel any active limit trades that would cause this conflict
The simulated MOC/LOC orders are actually staged in the "WaitForClose" state. When reaching "n-seconds before the Close" the Orders Manager would first check for and cancel any active orders in the account for the same symbol before placing the market (or limit) order.
Thanks!
Currently, does a WaitForClose MOC cancel itself before placement if the position has already been sold? I'm wondering whether a CancellationCode construct would be required in case the Limit is hit before close, and if so, how this handles WaitForClose trades that have not yet executed. (In a non-margin account, the Broker would reject the Sell order and make a cancellation moot, so this question assumes a margin account.)
Currently, does a WaitForClose MOC cancel itself before placement if the position has already been sold? I'm wondering whether a CancellationCode construct would be required in case the Limit is hit before close, and if so, how this handles WaitForClose trades that have not yet executed. (In a non-margin account, the Broker would reject the Sell order and make a cancellation moot, so this question assumes a margin account.)
In the exit strategy, a Limit/Stop order that fills will automatically cancel the WaitForClose order with the same Backtester.CancelationCode - good call!
That's a 99.9% solution. If the WaitForClose is a market order, it will have the same effect - the live Limit order will be cancelled as soon as the market order fills.
Considering the limit order didn't fill "all day" - the chances of it happening within the 1 second that the market order goes live may even be less than 0.1%, imho. What do you think?
That's a 99.9% solution. If the WaitForClose is a market order, it will have the same effect - the live Limit order will be cancelled as soon as the market order fills.
Considering the limit order didn't fill "all day" - the chances of it happening within the 1 second that the market order goes live may even be less than 0.1%, imho. What do you think?
Just tested it to make sure... and found a bug...
The Limit fill does cancel the WaitForClose order - that part's good.
The bug is that the MarketClose order in the WaitForClose order doesn't activate unless "Use MOC" is enabled. However enabling it is "Behavior 1" which places a real MOC order.
We need to get that bug fixed (Build 27) and then I think we have the 99.99% solution with CancelationCode.
--- fixed for Build 27
The Limit fill does cancel the WaitForClose order - that part's good.
The bug is that the MarketClose order in the WaitForClose order doesn't activate unless "Use MOC" is enabled. However enabling it is "Behavior 1" which places a real MOC order.
We need to get that bug fixed (Build 27) and then I think we have the 99.99% solution with CancelationCode.
--- fixed for Build 27
QUOTE:
Considering the limit order didn't fill "all day" - the chances of it happening within the 1 second that the market order goes live may even be less than 0.1%, imho. What do you think?
Yes, very unlikely. In this scenario, as a practical matter, I don't think it means that much because the likely movement in one minute would put the price of the market order pretty close to the limit order.
I'm not sure what you mean about putting the price of the market order close to the limit, but, it's a sub-1-second operation. It would go something like this on IB - assuming the n-seconds before close preference is set to 10 seconds...
CODE:
09:30:00.000 - Sell At Limit: Active; Sell At MarketClose: WaitForClose 15:59:50.000 - Sell At MarketClose -> Sell At Market activates 15:59:50.250 - Market order Filled -> WealthLab cancels limit order 15:59:50.500 - Sell At Limit: Canceled;
@Cone I don't think I understood the scenario you were describing. I thought you were asking hypothetically what the chances were that the limit would be filled if cancelled, if it hadn't been filled all day and what the opportunity loss would be. Now I'm wondering if you are concerned about the 1 sec. when the Market trade is placed while the Limit is still active. Is that what you were referring to?
If so, my thinking was that the Limit would be cancelled by WL *before* the MOC is placed (or the Broker may not accept it). Also, are you describing placing a Market or MOC trade? The latter requires placement 5 or ten minutes before close. I know you know that so I must be missing something. Are thinking WL would convert MOC to Market order to circumvent the time restriction?
Please clarify for me.
If so, my thinking was that the Limit would be cancelled by WL *before* the MOC is placed (or the Broker may not accept it). Also, are you describing placing a Market or MOC trade? The latter requires placement 5 or ten minutes before close. I know you know that so I must be missing something. Are thinking WL would convert MOC to Market order to circumvent the time restriction?
Please clarify for me.
QUOTE:Yes.
when the Market trade is placed while the Limit is still active. Is that what you were referring to?
QUOTE:For a Cash account, that's true. So it's the 99.99% solution for margin accounts.
If so, my thinking was that the Limit would be cancelled by WL *before* the MOC is placed (or the Broker may not accept it).
QUOTE:There are 2 behaviors for MarketClose orders. I'm describing "Behavior 2". (See Help > Preferences > Trading > Special Order Types)
Also, are you describing placing a Market or MOC trade?
"Behavior 1" is a "real" MOC order that needs to be placed earlier.
Thanks for the clarification. That helps.
I understand now. I didn't realize that pending MOC orders are not really MOC orders. I thought it would place an MOC for any time you select (>15 or <5 min) and let the Broker reject it if <5 min. I thought it was up to the user to manage this with Trading Preferences settings. I suspect there could be a pricing difference between Market and MOC orders at close of market, particularly if there is a big bid/ask gap, though I don't know the internals of how brokerages handle this.
In this feature request I wrote:
By that I meant WL would *first cancel* conflicting trades at the brokerage, and *then* place MOC (or MarketClose).
QUOTE:
There are 2 behaviors for MarketClose orders. I'm describing "Behavior 2". (See Help > Preferences > Trading > Special Order Types)
"Behavior 1" is a "real" MOC order that needs to be placed earlier.
I understand now. I didn't realize that pending MOC orders are not really MOC orders. I thought it would place an MOC for any time you select (>15 or <5 min) and let the Broker reject it if <5 min. I thought it was up to the user to manage this with Trading Preferences settings. I suspect there could be a pricing difference between Market and MOC orders at close of market, particularly if there is a big bid/ask gap, though I don't know the internals of how brokerages handle this.
QUOTE:
QUOTE:
If so, my thinking was that the Limit would be cancelled by WL *before* the MOC is placed (or the Broker may not accept it).
For a Cash account, that's true. So it's the 99.99% solution for margin accounts.
In this feature request I wrote:
QUOTE:
This feature request is to have staged MOC trades cancel any active limit trades that would cause this conflict
By that I meant WL would *first cancel* conflicting trades at the brokerage, and *then* place MOC (or MarketClose).
Today I placed a stacked Limit / MOC sell trade with CancellationCode logic and:
- with a Margin account (not IRA) it works fine
- with an IRA account (no margin) the MOC trade was rejected (broker views as a short because the Limit was still active), and the Limit remained active.
If the placement of a sell MOC would cancel any conflicting trades (the Sell Limit, in this case) this trade structure would work in non-margin / IRA accounts. Hence the title of this feature request
Alternatively, the feature request https://www.wealth-lab.com/Discussion/GTD-Order-8169 looks like it would also permit this functionality.
- with a Margin account (not IRA) it works fine
- with an IRA account (no margin) the MOC trade was rejected (broker views as a short because the Limit was still active), and the Limit remained active.
If the placement of a sell MOC would cancel any conflicting trades (the Sell Limit, in this case) this trade structure would work in non-margin / IRA accounts. Hence the title of this feature request
Alternatively, the feature request https://www.wealth-lab.com/Discussion/GTD-Order-8169 looks like it would also permit this functionality.
If I'm reading you right, you're entering 2 exit orders for the same position. We can't make a broker's MOC order work differently than how it's implemented at the broker.
What's your solution exactly?
The only way I can see this working for the cash account is if at a designated time the limit order is canceled and replaced with the MOC - which needs to be at least 5 minutes before the market close.
ALTERNATIVELY, you can use WealthLab's MOC Behavior 2, which is NOT to use the broker's MOC and allows WealthLab to place a market order N seconds before the close. To make this happen, the strategy must Place a MarketClose order AND you disable "Use MOC when Possible".
What's your solution exactly?
The only way I can see this working for the cash account is if at a designated time the limit order is canceled and replaced with the MOC - which needs to be at least 5 minutes before the market close.
ALTERNATIVELY, you can use WealthLab's MOC Behavior 2, which is NOT to use the broker's MOC and allows WealthLab to place a market order N seconds before the close. To make this happen, the strategy must Place a MarketClose order AND you disable "Use MOC when Possible".
QUOTE:Yes. It's a broker-unsupported variant of an OCO trade. The only reason it works for margin accounts is that the broker looks at the second order as a potential short position, which is allowed. I haven't found a broker that supports MOC in OCO.
If I'm reading you right, you're entering 2 exit orders for the same position.
QUOTE:Of course. However, since WL WaitForClose pends an MOC order (which is actually a market order), there is the possibility ability to do something that the broker can't support with manual trading.
We can't make a broker's MOC order work differently than how it's implemented at the broker.
QUOTE:My solution is additional logic so that before a pending MOC order (B2) transmits to the broker, it would first cancel any active orders at the broker that duplicate the sell or buy action. This will eliminate the rejection.
What's your solution exactly?
QUOTE:I *am* using Behavior 2. My understanding is that WL has in fact been attempting to place market orders (not MOC) because of the B2 logic. But they are rejected because the Limit is still active. I'm unclear how disable "Use MOC...." can change this equation. Apparently, CancellationCode logic cancels the companion trade *after* its companion is placed, not before.
ALTERNATIVELY, you can use WealthLab's MOC Behavior 2, which is NOT to use the broker's MOC and allows WealthLab to place a market order N seconds before the close. To make this happen, the strategy must Place a MarketClose order AND you disable "Use MOC when Possible".
I think I'm hearing that the Limit order isn't canceled automatically before the Behavior 2 Market order is placed. Probably. CancelationCode cancels another order with the same code when the one of them is filled.
Here's how it needs to go:
Use MOC enabled - (Behavior 1)
MOC orders are placed immediately. If you have an open [stop/limit] order for the same symbol, your [cash] account probably won't support multiple orders simultaneously.
Use MOC disabled - (Behavior 2)
OrderType.MarketClose orders are placed as Market orders N seconds before the close of the session. The missing part: All open orders for the same symbol should be canceled automatically before placing the market order.
Here's how it needs to go:
Use MOC enabled - (Behavior 1)
MOC orders are placed immediately. If you have an open [stop/limit] order for the same symbol, your [cash] account probably won't support multiple orders simultaneously.
Use MOC disabled - (Behavior 2)
OrderType.MarketClose orders are placed as Market orders N seconds before the close of the session. The missing part: All open orders for the same symbol should be canceled automatically before placing the market order.
QUOTE:That appears to be the case. It looks like that is the way it's designed to work and the way it works. It also makes sense as a type of OCO, which would also wait for a transaction before cancelling another order. In this use case, we require a cancel without a triggering transaction, but rather a pending one.
Probably. CancelationCode cancels another order with the same code when the one of them is filled.
QUOTE:If you are asking me if believe that is a concise description of the requirement, I originally thought might not be narrow enough. But then I realized that if you are closing a (entire) position with a market order there is no reason for other orders on the same symbol (whether they would cause a broker rejection or not) to remain open. That is perhaps, unless someone wanted to do a partial close of a position. With that caveat, I think it is a good concise summary that addresses the vast majority of the real-world (vs. theoretical) use cases. However, I am possibly not aware of more exotic trading structures some WL users employ.
The missing part: All open orders for the same symbol should be canceled automatically before placing the market order.
To allow more flexibility, I'll change the statement to ... "All open orders for the same symbol with the same CancelationCode should be canceled automatically before placing the market order."
In other words, if the MarketClose order has the same CancelationCode as other open orders, it makes sense to cancel them because the market order is going to be filled.
In other words, if the MarketClose order has the same CancelationCode as other open orders, it makes sense to cancel them because the market order is going to be filled.
I like your approach. It's very explicit, so there shouldn't be any trader surprises. Another benefit of implementing this is that, for margin accounts, it will eliminate the small possibility of opening an unwanted short position.
Is there anything you want me to do, such as modifying this feature request or starting a new one?
Is there anything you want me to do, such as modifying this feature request or starting a new one?
No need, we're tracking it and need Glitch to weigh in.
We've got it done for Build 31!
Fantastic! Thanks!
I'm pleased to let you know I tested this today and it worked fine!
There was one glitch, that although related, may be off topic. I found that the WaitForClose market order disappeared from the Order Manager about an hour before market close and before triggering. I had to rerun strategy to get it back.
This was unusual, and I realized that I had just received a notification of updated version 32 availability. I wonder if that could have triggered deletion of the market order. To be safe would it be best to limit notifications to after-market hours?
There was one glitch, that although related, may be off topic. I found that the WaitForClose market order disappeared from the Order Manager about an hour before market close and before triggering. I had to rerun strategy to get it back.
This was unusual, and I realized that I had just received a notification of updated version 32 availability. I wonder if that could have triggered deletion of the market order. To be safe would it be best to limit notifications to after-market hours?
I can't even begin to imagine how a notification could have that effect, but maybe Glitch does?
Edit ---
... although I'm beginning to imagine that the links to Wealth$im could have something to do with it...
Edit ---
... although I'm beginning to imagine that the links to Wealth$im could have something to do with it...
QUOTE:
I found that the WaitForClose market order disappeared from the Order Manager about an hour before market close and before triggering.
This has happened 3x now, each time I stack a Limit Sell with MOC. The WaitForClose disappears from the Order Manager before the trigger time (now set at 20 minutes). I'm unclear how to reproduce this and looking for suggestions on how to troubleshoot this to narrow down the cause.
Here are my Trade Preference settings.
Here's the snippet code setting up the cancellation code.
CODE:
posCnt++; if (HasOpenPosition(bars, PositionType.Long)) { //dev8 double target = LastOpenPosition.EntryPrice * sellMove; Backtester.CancelationCode = posCnt; PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, target, "Limit Sell Target"); if (LastOpenPosition.ProfitPctAsOf(idx) < exitMove || isNextBarLastDayOfWeek) { Backtester.CancelationCode = posCnt; PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, "Market Close Exit"); } }
Thanks, we'll look into it.
I used as much as your code as I could, modifying so that it would run. For me, it worked perfectly. I had the Limit Active and WaitForClose orders in the Order Manager for 2 hours, and just now, 20 minutes before the close (I had set it to 1200 seconds as you had), the Limit order was Canceled and the MarketClose order Activated at Market and Filled.
Probably you need to give me all of the initial conditions and an entire Strategy that I can use to duplicate what you're seeing.
Probably you need to give me all of the initial conditions and an entire Strategy that I can use to duplicate what you're seeing.
Today I had two Limit + MOC trades active. The first one sold at limit and its MOC was cancelled by WL. With the second one, the Limit was not reached but the MOC disappeared before the WaitForClose (WFC) time, again.
MOC was still active on second position after the first position executed at Limit (about 80 minutes before WFC). But when I came back to computer about 5 min before WFC, the MOC had disappeared. The Limit was still active.
Don't see any activity in the IB log acknowledging the MOC or rejecting it.
So far this problem has been repeatable for Limit + MOC when the Limit is not hit before WFC. What can I try or send you to help you replicate?
MOC was still active on second position after the first position executed at Limit (about 80 minutes before WFC). But when I came back to computer about 5 min before WFC, the MOC had disappeared. The Limit was still active.
Don't see any activity in the IB log acknowledging the MOC or rejecting it.
So far this problem has been repeatable for Limit + MOC when the Limit is not hit before WFC. What can I try or send you to help you replicate?
Were there any messages in the log viewer for IB? Connection lost maybe? (I don't even think that would do it, but we need to find something.)
Today I had 4 positions with Limit + MOC. Two were in IRA and 2 were in margin account. The Limit was hit on one of the positions in the margin account and its companion MOC went away. On the other three positions, the MOC didn't disappear today, but it also didn't place trade or remove limit n seconds before market close (20 minutes). Snaps below taken after market close.

I've looked at it, tested it, and can't find an issue.
I noticed that you put in the order on the evening of the previous session. That's a difference in what I've tested, but it shouldn't matter. As long as the order status is MarketClose, it will be processed with the signal block.
What do you do with your machine overnight? Does it sleep or hibernate? I'm going to try to duplicate your environment for tomorrow.
Please post the version of the strategy you're using so that I can use that too.
I noticed that you put in the order on the evening of the previous session. That's a difference in what I've tested, but it shouldn't matter. As long as the order status is MarketClose, it will be processed with the signal block.
What do you do with your machine overnight? Does it sleep or hibernate? I'm going to try to duplicate your environment for tomorrow.
Please post the version of the strategy you're using so that I can use that too.
It worked properly one day last week, cancelling limit sell 20 min before close and successfully placing market close. I'd like to have a few trading days with this setup to see if this is an intermittent problem. (I hope not.) I will let you know what I find.
I don't sleep or hibernate this computer; only screen turns off after an hour or so of non-use. That doesn't keep strategies from running on schedule. I do not close WL between placing the overnight trade and market close. After market close, I allow TWS to do its daily refresh followed by WL restart.
QUOTE:.
What do you do with your machine overnight? Does it sleep or hibernate?
I don't sleep or hibernate this computer; only screen turns off after an hour or so of non-use. That doesn't keep strategies from running on schedule. I do not close WL between placing the overnight trade and market close. After market close, I allow TWS to do its daily refresh followed by WL restart.
I had two Limit+MOC trades today, one in margin, one not. Both executed successfully when limit was not reached. I'll continue to monitor and report any failures.
Your Response
Post
Edit Post
Login is required