- ago
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)
2
1,938
Solved
55 Replies

Reply

Bookmark

Sort
Cone8
 ( 6.71% )
- ago
#1
QUOTE:
staged MOC trades cancel any active limit trades that would cause this conflict
Makes sense!

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.
0
- ago
#2
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.)
0
Cone8
 ( 6.71% )
- ago
#3
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?

0
Cone8
 ( 6.71% )
- ago
#4
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

1
- ago
#5
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.
0
Cone8
 ( 6.71% )
- ago
#6
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;

0
- ago
#7
@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.
0
Cone8
 ( 6.71% )
- ago
#8
QUOTE:
when the Market trade is placed while the Limit is still active. Is that what you were referring to?
Yes.

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.

QUOTE:
Also, are you describing placing a Market or MOC trade?
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.
0
- ago
#9
Thanks for the clarification. That helps.
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).
0
- ago
#10
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.
0
Cone8
 ( 6.71% )
- ago
#11
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".
0
- ago
#12
QUOTE:
If I'm reading you right, you're entering 2 exit orders for the same position.
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.
QUOTE:
We can't make a broker's MOC order work differently than how it's implemented at the broker.
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.
QUOTE:
What's your solution exactly?
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.
QUOTE:
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 *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.
0
Cone8
 ( 6.71% )
- ago
#13
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.
0
- ago
#14
QUOTE:
Probably. CancelationCode cancels another order with the same code when the one of them is filled.
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.
QUOTE:
The missing part: All open orders for the same symbol should be canceled automatically before placing the market order.
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.
0
Cone8
 ( 6.71% )
- ago
#15
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.
0
- ago
#16
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?
0
Cone8
 ( 6.71% )
- ago
#17
No need, we're tracking it and need Glitch to weigh in.
0
Cone8
 ( 6.71% )
- ago
#18
We've got it done for Build 31!
0
Best Answer
- ago
#19
Fantastic! Thanks!
0
- ago
#20
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?
0
Cone8
 ( 6.71% )
- ago
#21
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...
0
- ago
#22
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");             }          }
0
Cone8
 ( 6.71% )
- ago
#23
Thanks, we'll look into it.
0
Cone8
 ( 6.71% )
- ago
#24
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.
0
- ago
#25
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?
0
Cone8
 ( 6.71% )
- ago
#26
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.)
0
- ago
#27
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.


0
Cone8
 ( 6.71% )
- ago
#28
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.
0
- ago
#29
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.

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.
1
- ago
#30
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.
2
- ago
#31
The MOC + Limit trade structure using CancellationCode has been working except for the last week. I've had a couple of failures in a row, where the market order in my IRA account was rejected because apparently the limit order was not cancelled.

I reviewed the logs from IB and it appears that IB never received the Limit cancellation request for the two positions in my IRA account. Strangely, it looks like it did receive cancellations for the non-IRA account, as indicated in the IB audit report, which also notes the cancellation. The audit report shows no indication of a cancellation request for the Limit trade being received.

It looks like the Order Manager was pending the market orders properly and executed them on time. The audit does show cancellation requests and confirmations for the two non-IRA positions.

Both of these accounts are running the identical strategy code. I have not made any changes to strategy code for a few months.

I remembered that the recent build revision (36) had a change in the Miscellaneous Trading Settings preferences relating to MOC. Is it possible that this has something to do with the recent change in behavior?
0
Cone8
 ( 6.71% )
- ago
#32
Anything's possible, but the MOC/LOC change only switched the TransactionType from MarketClose (or LimitClose) to Market (or Limit) before placing the orders in the Signal Block when reaching the seconds before the session close.

And the behavior didn't change - it worked for one account, but not another, once. There must be a difference. We need to find it and correct it, if necessary.

Were both trade instances running on the same machine? If not, check for differences in the Trading preferences.
0
- ago
#33
QUOTE:
And the behavior didn't change - it worked for one account, but not another, once.

Good point. Hard to believe the code would disciminate against an IRA account.
QUOTE:
Were both trade instances running on the same machine?

Yes

Do you think the WL log might shed some light on this? Unfortunately, WL has been restarted since and log info is not available for yesterday.

One more question. I generate the CancellationCode algorithmically. It's possible it's the same CC for both instances, and they are the same symbols. Could that be an issue if instances are using different broker accounts?
0
Cone8
 ( 6.71% )
- ago
#34
Like I said, anything's possible, but I don't think so because the orders would be in different signal blocks.
0
- ago
#35
@Cone today I watched both Order Manager window and the IB Orders panel when the trigger time hit. I have 2 positions in each my IRA account and non-IRA account with stacked Limit / MOC.

One of the positions in the IRA account hit the limit and was sold. MOC was cancelled. The remaining 3 positions did not hit their limit. In the non-IRA account, the Limit was cancelled at the Pending time, Market Order was sent and executed. In the IRA account, at the Pending time the MOC disappeared from the Order Manager window, but the Limit was not cancelled. MOC was sent but rejected at IB because Limit was not cancelled.

In summary, it looks like the root of the problem is that Limits are not being cancelled by MOC, but only in my IRA account. Let me know if there's something you suggest I try to troubleshoot. Again, this is a recent problem.
0
Cone8
 ( 6.71% )
- ago
#36
To WealthLab, there's no difference in account types, so that can't matter.

QUOTE:
the Pending time the MOC disappeared from the Order Manager window, but the Limit was not cancelled. MOC was sent but rejected at IB because Limit was not cancelled.
Probably the order disappeared because you have Auto-Remove: ☑ Errored

I'll set up some tests for this today. Meanwhile, you can unchecked all the Auto-Removes to get more information for the orders in question.
0
Cone8
 ( 6.71% )
- ago
#37
I did the test twice near the market close. Worked perfectly for me with the paper margin account, however, I made a video that indicates the Market orders were placed (and actually Filled) before the Limit orders were fully Canceled. I'm not sure if it's just a display thing or not yet. We'll keep investigating.

0
- ago
#38
QUOTE:
Probably the order disappeared because you have Auto-Remove: ☑ Errored

Hmmm... Only Completed box is checked on Order Manager. Why do you think Errored was checked?
QUOTE:
I made a video that indicates the Market orders were placed (and actually Filled) before the Limit orders were fully Canceled.

Is there a way for WL to confirm a cancellation order has been completed?
0
Glitch8
 ( 11.74% )
- ago
#39
Yes, the status changes from CancelPending to Canceled.
0
- ago
#40
Does the code logic wait for Cancelled status for Limit order before placing Market order?
0
- ago
#41
BTW, the explanation for the problem only occurring with IRA might be explained be as follows:

The behavior in failure mode is identical for both accounts. However, it is not noticed in non-IRA account because the Sell Market order can be accepted and executed while Limit is still active, assuming sufficient funds including margin. When the Market order executes, WL Order Manager automatically cancels the Limit order.

It happens so fast (within a second) that we can't really see that's what happened.

What do you think?
0
Cone8
 ( 6.71% )
- ago
#42
I think you're probably right.
0
- ago
#43
Today it failed in both accounts. I pushed ahead the trigger time to be 30 min before close, so I could get a look at what was happening. The market order was placed and executed for one of the symbols in the non-IRA account, apparently because there was enough cash to cover a "short."

It turns out that the Limit was not cancelled by the broker for the positions in *either* account, as the Limits were Expired by the broker at market close *in both accounts*. There are no entries in the IB log that indicate the broker received a Limit cancel request from either account. There *are* entries noting the acceptance and execution of one market order in the non-IRA account and the rejection in the IRA account of the other.
0
Cone8
 ( 6.71% )
- ago
#44
As you suspected the change did affect the logic. A long story short, the switch from MarketClose to Market order was done too early. We'll have it fixed right for B39. Thanks for information and my apologies for the trouble.
0
- ago
#45
Oh, wow. Glad you found it. That's great. No worries!
1
Cone8
 ( 6.71% )
- ago
#46
Build 39 is ready with the fix. I don't have a way to test it for a cash account, but I'm pretty confident it will work!
0
- ago
#47
It should get its first test tomorrow. I'll let you know what happens.
1
- ago
#48
@Cone Re: B39
Stacked trade worked successfully today in both cash and margin accounts with one position in each. I will have another like trade tomorrow and will update you with results.
1
Cone8
 ( 6.71% )
- ago
#49
Great!
0
- ago
#50
No stacked trade test today. Limit reached.
0
Cone8
 ( 6.71% )
- ago
#51
Even better! :)
0
- ago
#52
Haha. Maybe financially but not from a technical troubleshooting perspective. Not sure which one is more important to me right now. ;)
1
- ago
#53
Stacked Limit / MOC worked properly today on a symbol where limit was not hit. However, on the symbol where limit was hit, the position was sold but Pending MOC was not cancelled (in cash account, OK in non-cash account). However, WL cancelled Pending MOC at market close since position no longer existed.

I will watch for a recurrence of this unexpected behavior.
0
- ago
#54
Stacked Limit / MOC worked properly today on a symbol where limit was not hit. Seems to be working reliably. I will only report failures going forward. Hope I will have nothing to report.
0
Cone8
 ( 6.71% )
- ago
#55
Great, thanks for identifying the mistake and verifying the fix!
0

Reply

Bookmark

Sort