I asked about this in another post, started a fresh one because I have more data.
Running a strategy that exits on Friday, today it attempted to place an order for a market exit on close. It placed just fine.
Then it tried to also place a limit order at a certain price, and it was rejected.
I cancelled both orders and flipped the order of execution to see what happened. The limit order was accepted no problem this time.
So, it feels like first order in is what works, second one fails.
Wild guess that if it used an OCO it would work?
I have not exited/restarted WL8 this AM in case wee need more information.
I am using Tradestation as the broker.
Strategy is One Percent Per Week.



Running a strategy that exits on Friday, today it attempted to place an order for a market exit on close. It placed just fine.
Then it tried to also place a limit order at a certain price, and it was rejected.
I cancelled both orders and flipped the order of execution to see what happened. The limit order was accepted no problem this time.
So, it feels like first order in is what works, second one fails.
Wild guess that if it used an OCO it would work?
I have not exited/restarted WL8 this AM in case wee need more information.
I am using Tradestation as the broker.
Strategy is One Percent Per Week.
Rename
It was probably rejected because you were trying to place two exit orders for the same position, one limit and one market close. In cases like this I hold off on placing the market close. Then, shortly before market close, if the limit order has not filled, I replace it with a market close order.
Scenario:
Limit and/or Stop order(s) that, if not filled are followed by a MOC order.
Just yesterday I was creating a programming pattern to automate this scenario with At-Close Processing. I'll post the solution when I have confidence that it works.
Limit and/or Stop order(s) that, if not filled are followed by a MOC order.
Just yesterday I was creating a programming pattern to automate this scenario with At-Close Processing. I'll post the solution when I have confidence that it works.
Thanks so much guys!!
I was actually going to create a topic about this today myself, but in a slightly different form. I had 2 orders filled, and now I’ve got a limit and a MOC order standing. The problem is that if the first one gets executed, then at market close I’ll end up with a short position, which is much worse. Although during the backtest the second one will be cancelled.
The behavior depends on the broker, it’s certainly something to carefully monitor.
@www5 - the difference is that you're trading a margin account.
Multiple orders for the same shares aren't allowed in a cash account (unless they're OCO).
Multiple orders for the same shares aren't allowed in a cash account (unless they're OCO).
As it relates to OPPW, we want to prevent placing the MarketClose order with the Limit order on "Friday". Here's the solution that works for me.
First, depending on which version of OPPW, you'll have 2 blocks of code with this snippet. The variable NextBarIsLastDayOfWeek might be also bars.TomorrowIsLastTradingDayOfWeek(idx). It's the same thing.
Replace that block (2 places) with this:
And add this routine to the script:
1. Save the Script.
2. If you have a Workspace with this running in the Strategy Monitor, Deactivate the Strategy, right click, "Refresh Selected Strategies...".
3. Configure... enable At-Close Processing at least 5 seconds before the close, but if you have a broker that's slow to activate orders, you might need to make that 15 or even 30 seconds.
4. Activate and Save the Workspace.
5. In the Data Manager, make sure you have a provider at the top of the Historical Provider list that can return a partial bar for TQQQ.
6. Since the CancelationCode doesn't change, the AtClose process will cancel the Active Limit order and Place the Market order.
As long as the Limit order cancels quickly enough, it should work! Make sure you're there on the first day it happens to verify it. If it doesn't, you'll have to take a little slippage post-market.
First, depending on which version of OPPW, you'll have 2 blocks of code with this snippet. The variable NextBarIsLastDayOfWeek might be also bars.TomorrowIsLastTradingDayOfWeek(idx). It's the same thing.
CODE:
if (NextBarIsLastDayOfWeek) { Backtester.CancelationCode = 642; PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose); }
Replace that block (2 places) with this:
CODE:
CheckExitAtClose(bars, idx);
And add this routine to the script:
CODE:
// 1. Prevents the SellAtClose on Friday morning // 2. Exits at Market with At-Close Processing private void CheckExitAtClose(BarHistory bars, int idx) { bool flag = false; if (idx + 1 < bars.Count) { if (bars.TomorrowIsLastTradingDayOfWeek(idx)) // the condition to trade "At Close" *tomorrow* flag = true; } if (flag) { PlaceTrade(bars, TransactionType.Sell, OrderType.MarketClose, 0, "Sell AtClose (Market)"); } }
1. Save the Script.
2. If you have a Workspace with this running in the Strategy Monitor, Deactivate the Strategy, right click, "Refresh Selected Strategies...".
3. Configure... enable At-Close Processing at least 5 seconds before the close, but if you have a broker that's slow to activate orders, you might need to make that 15 or even 30 seconds.
4. Activate and Save the Workspace.
5. In the Data Manager, make sure you have a provider at the top of the Historical Provider list that can return a partial bar for TQQQ.
6. Since the CancelationCode doesn't change, the AtClose process will cancel the Active Limit order and Place the Market order.
As long as the Limit order cancels quickly enough, it should work! Make sure you're there on the first day it happens to verify it. If it doesn't, you'll have to take a little slippage post-market.
Your Response
Post
Edit Post
Login is required