View Full Version : Problem with automated close of market order
bridenour
01-30-2009, 02:21 PM
Hi,
I have a script that automatically closes market orders at 3:58pm. It has seemed to work ok, but today on a large position it only closed part of the order at 3:58.
At 3:58, it sold about 2/3rds of the position. I was watching and noticed the entire position didnt close. I then checked my live orders in MBTrading and found it to be empty. At that point, I manually closed the remainder of the position.
A few seconds later, i noticed that i now had a new live short position in the security! I went ahead and manually closed this also.
When i went back and checked in the NT logs, it appears it generated an order to close part of the position at 3:58. At 3:59, it generated an order to close the remainder of the position. Because i had already manually closed the remainder of the position, it ended up opening a new position.
My question is, why would it have only closed part of the position on the 58th minute? It is a single entry signal and a simple exitlong command i send?
Thanks for your help!
NinjaTrader_Bertrand
01-30-2009, 02:33 PM
Hi bridenour, it looks like you got only a partial fill on that exit order - please send me your trace and logs to support at ninjatrader dot com (Attn Bertrand) to investigate. Thanks!
bridenour
02-04-2009, 12:02 PM
Hi,
I have been running my live strategy for several days now, and it is frequently not closing open positions properly. All positions are opened with market orders and attempted to close with market orders.
For example, today at 1:34 is opened a position in an instrument for 1175 shares, and at 1:43 a stop was triggered, and a market sell order executed. However, it only generated a market sell order for exactly 200 shares. The remaining position is left open and completely unmanaged.
My strategy is rather complex and is multi-instrument. I do not use different enter signals for each instrument, as I am assuming the signals are contained within the context of each bar/instrument being processed.
Can you please advise what the potential causes of this could be? It is not a partial fill problem, as the quantity ninja trader is attempting to close is simply incorrect.
Thanks!
NinjaTrader_Bertrand
02-04-2009, 12:15 PM
Hi bridenour, hard to say without the strategy code - if you like you can forward this to support at ninjatrader dot com and I will have a look. I would recommend you use different entry / exit signals to reference the correct position - maybe this is of help - http://www.ninjatrader-support2.com/vb/showthread.php?t=3225
bridenour
02-04-2009, 12:27 PM
Bertrand -
Do I need to use a different entry and exit signal for each instrument in my strategy? I was assuming it was only necessary if i were making multiple trades of a single instrument.
I only have at max 1 open trade for each instrument, but i may have several different instruments open at a time. Does each insturment need its own individual entry signal?
The example you reference is only for a single instrument.
Thanks
NinjaTrader_Bertrand
02-04-2009, 12:54 PM
Hi bridenour, yes please use a unique entry name for a different entry and test the strategy again.
bridenour
02-17-2009, 04:55 AM
I have changed my multiinstrument strategy to use a unique signal for each instrument in the strategy. It will execute starting today -- I am hoping it actually begins to close positions correctly.
Thanks
NinjaTrader_Bertrand
02-17-2009, 05:09 AM
Ok great please let us know.
bridenour
02-18-2009, 09:31 AM
Unfortunately, even though i have now included distinct signals with each instrument position open, it is still only closing partial positions. When it opens the position, the order seems to be filled in chunks, and for some reason, when it decides to close the position, it only issues a close order for part of the total quantity that was opened.
I am going to send over my log that demonstrates this...
NinjaTrader_Bertrand
02-18-2009, 09:36 AM
Thanks for the update - please send the logs to support at ninjatrader dot com Attn Bertrand.
bridenour
02-18-2009, 09:39 AM
Ok i sent the log file....let me know if you will also need the trace.
thanks
NinjaTrader_Bertrand
02-18-2009, 09:57 AM
Thanks responded via mail, you will need to debug why your 'sell long' signal generates an order for 400 shares only.
bridenour
02-18-2009, 10:08 AM
I see. So, this problem never occurs in backtesting.
My exits are just a simple "Exitlong" with the original signal name. I don't even specify any amounts on the exit.
Im at a loss as to where to even start in terms of debugging this. How could anything i do influence the amount of shares the system decides to close, given the exit approach i am using? I assumed NT tracked internally the size of the position it originally opened, and then closed the correct size/amount based on using the original entry signal to close the position.
I'm very glad to debug this, but need some help/direction as to where to focus.
thanks
bridenour
02-18-2009, 10:09 AM
I would be glad to track my initial position size that i open, and then specify it explicitly when i issue the close, if that would work around this. Let me know if that would be a superior approach.
NinjaTrader_Bertrand
02-18-2009, 10:44 AM
Isn't the original size specified by pSize in the code you send me? Then try using this also in your Exit orders.
bridenour
02-18-2009, 10:46 AM
Ok, i did not notice that exit orders required quantity size to be specified. I was only specifying it on entry. I can certainly save and specify it on exit also.
I will let you know how it turns out.
thanks
bridenour
02-25-2009, 03:42 PM
Now it's gotten crazy.
I recently changed my script to maintain the size of the positioned that was opened, and use that position size to explicitly close the position via ExitLong. I close the exact amount that was originally opened.
It usually works.
However, yesterday at 3:58 my strategy called ExitLong on an open position of about 2807 shares. I had traceOrders turned on and noticed on the output window that the ExitLong order was entered with the correct sizing information.
However, what was actually placed and executed at the broker was an order for exactly 900 less shares than the amount displayed in traceorders. Also, when I check the log and trace files, they also show the order being placed for the lesser, incorrect amount. Only the traceOrders information showed the correct position size on the exit call.
I've since closed the output window and can't find the traceOrders output in any of the log files. As a result, I can't find anything now showing the discrepency between the two order amounts (the amount that was called in ExitLong vs what was actually generated and sent to the broker. Is TraceOrders output not saved anywhere?
Any idea how an explicitly called exitlong position with an explicit quantity size would generate a close order with a totally different quantity size? If not, any guidance on how I could capture data that would help from a support standpoint?
thanks
NinjaTrader_Josh
02-25-2009, 03:49 PM
If you are closing a position you can just use ExitLong() without an explicit quantity. Remember, if you have a single EnterLong(), do NOT call ExitLong() twice with separate quantities to try to exit the whole position. If you are trying to scale-out you need to scale-in first with separate entry orders.
bridenour
02-25-2009, 04:41 PM
Hi Josh,
My situation is really simple. I call enter long with a quantity to enter the position.
Originally, I was just calling exit long without a quantity to exit. The problem is, the strategy was only exiting part of the position about half of the time. I don't know why, but thats what it was doing pretty consistently.
So, at the recommendation here, i changed my ExitLong to use a quantity variable, based on the original entry quantity. I thought that fixed it, but it was apparent yesterday that it is still only exxiting part of the positions. Even when I explicitly called ExitLong for the full quantity, it is only issuing an order to my broker for a part of the quantity.
The only thing complex about my strategy is that it is heavily multi-instrument and multi-timeperiod. But, the entries and exits are just simple, one time orders using EnterLong and ExitLong.
Obviously, it being so unreliable at exiting positions is a killer. I'd be glad to gather whatever data is necessary to help solve the problem -- because my use of EnterLong and ExitLong is so simple, I'm at a bit of a loss.
Thanks
NinjaTrader_Bertrand
02-26-2009, 05:57 AM
Hi bridenour, which order handling setting do you use under Tools > Options > Strategies > NinjaScript ? Please try 'ByStrategyPosition' here.
http://www.ninjatrader-support.com/HelpGuideV6/StrategiesTab.html
bridenour
02-26-2009, 06:38 AM
I did switch it to "By Strategy Position" when you recommended it a couple of days ago. The problem occurred yesterday anyway.
NinjaTrader_Josh
02-26-2009, 07:12 AM
bridenour,
We are going to need TraceOrders = true output, log files, and trace files as well as a simple as possible, reproducible scenario.
bridenour
02-26-2009, 10:27 AM
When it happens again, i will copy the traceOrders output and send it in along with the log files. It will demonstrate the problem.
At this point, I don't have a reproducible case other than to say it happens occasionally when trading live.