View Full Version : Strategy order with name "Close Position"
ThePatientOne
05-29-2007, 08:10 AM
My NinjaScript strategy is placing correct Profit Target and Stop Loss orders when it is executing but I am getting another order named "Close Position" popping up. What is this order, why is it placed, how can I prevent it from being placed?
Thanks.
NinjaTrader_Ray
05-29-2007, 08:17 AM
This order is internally generated when you have an open position and you cross to the other side.
For example:
You are long 1.
Then your code calls EnterShort().
It will generate an order tagged with Close Position to close the long and then enter the short position. The reverse is true from short to long.
ThePatientOne
05-29-2007, 08:24 AM
OK. I can understand that. However, when my orginal position (say I was Long) hits my profit target, this Close Position order stays open along with my other order to go short (which is above my original long profit target).
When does this order cancel?
This order is screwing up my system. How can I prevent this or cancel this order from my strategy?
NinjaTrader_Ray
05-29-2007, 08:48 AM
For clarification.
You are long.
You have SetProfitTarget() called and an order is working.
What other orders are working? An entry and close order generated by an EnterShortLimit() ?
Assuming the above is accurate, your target order is filled so you then become flat but both the Close and Enter short orders are still working?
Thanks
ThePatientOne
05-29-2007, 09:29 AM
My strategy manages its own profit target and stop loss. Sometimes I have BOTH a buy limit AND a sell limit working at the same time. Here is an example:
1. Enter buy limit and/or sell limit orders if conditions exist for either or both
2. Determine if I am long or short
3. If long, place sell limit and sell stop limit orders
(entry sell short limit order can still be valid along with the triggered sell to close and sell to stop orders).
4. If short, do the same as #3 but for shorts.
I am continuously adjusting the entry orders based on market conditions but I *can* be long and still have a short entry order working. I know where this level is and automatically adjust my targets/stops accordingly.
So managing my own targets/stops causes this "Close Position" order to hang out there because Profit Target is never getting hit as it is never getting entered.
NinjaTrader_Ray
05-29-2007, 09:47 AM
Thanks for the clarification.
Right now, orders expire (cancel) at the close of a bar unless re-submitted.
So for final clarification, when your target or stop is hit, the EnterShortLimit() is still working (which is what you want) but the associated close position order generated by the EnterShortLimit() is also still working but you want it cancelled as soon as the position goes flat.
Assuming I understand this correctly, I will take a look to see what can be done.
ThePatientOne
05-29-2007, 10:25 AM
I think that may be the issue. I think the "Close Position" order is being submitted upon my EnterLongLimit() order being filled but after my long position is closed, this order is being maintained because my EnterShortLimit() order is still working. The "Close Position" order should be tied to the long position since that is what generated that order. Once that position is closed, the "Close Position" order should be cancelled/closed as well.
There should be an override for this as I see the need for it (for simple strategies) but I also see (and am experiencing) it as a nuisance.
ThePatientOne
05-29-2007, 09:08 PM
This is what I am now doing in my strategy code for profit target and stop loss order handling after the prior discussions:
================================================== ====
if (Position.MarketPosition == MarketPosition.Flat)
{ // Set default profit/stop loss
myProfitTarget = Math.Max(Math.Abs(longTargetPrice - longEntryPrice), Math.Abs(shortEntryPrice - shortTargetPrice)) / TickSize;
SetProfitTarget(CalculationMode.Ticks, myProfitTarget);
myStopLoss = Math.Min(Math.Abs(longEntryPrice - longStopPrice), Math.Abs(shortStopPrice - shortEntryPrice)) / TickSize;
SetStopLoss(CalculationMode.Ticks, myStopLoss);
}
if (Position.MarketPosition == MarketPosition.Long)
{
myProfitTarget = (longTargetPrice - longEntryPrice) / TickSize;
SetProfitTarget(CalculationMode.Ticks, myProfitTarget);
myStopLoss = (longEntryPrice - longStopPrice) / TickSize;
SetStopLoss(CalculationMode.Ticks, myStopLoss);
}
if (Position.MarketPosition == MarketPosition.Short)
{
myProfitTarget = (shortEntryPrice - shortTargetPrice) / TickSize;
SetProfitTarget(CalculationMode.Ticks, myProfitTarget);
myStopLoss = (shortStopPrice - shortEntryPrice) / TickSize;
SetStopLoss(CalculationMode.Ticks, myStopLoss);
}
================================================== ====
I am now using SetProfitTarget and SetStopLoss for managing orders instead of manually doing it. I am still getting the "Close Position" order remaining open once the OCO pair has closed for the position.
I am not setting SetProfitTarget or SetStopLoss in the Initialize() method because I do not know what these levels will be at the time of initialization. It depends on the market and chart interval selected. Should I even be setting these to defaults when the position is flat?
Remember, my strategy *can* be long and simultaneously have a limit order entered waiting to go short. I make sure that the ProfitTarget for the long is the same as or less than the short limit order. Therefore, the "Close Position" order is already duplicating the process of getting flat before reversing.
Anything I should be doing differently?
Would the "Close Position" order go away when the position flattened if I used the overloaded method for SetStopLoss to include the order name?
NinjaTrader_Ray
05-30-2007, 07:15 AM
You should always call the SetStopLoss() and equivalent methods prior to the opening of any position. That is the requirement since at this time, these methods must be called prior to the opening of a position so that protective orders are submitted.
Regarding the "Close position" order. We are still thinking about what can be done here. There is no way to get around having this order cancelled on the close of a position at this time.
- With your current approach, even if the order was cancelled, you still run the risk of getting double filled if the market moved fast enough and filled both the short entry limit and close position order before NT could cancel it. If you get double filled, your strategy logic will be off and you will have bigger issues.
Other options -
- Wait until the target limit is filled before submitting the short entry limit
- Use short entry market
ThePatientOne
05-30-2007, 07:33 AM
1) Waiting until the target is filled before entering the short limit order gives me a very poor position in the order depth at that price.
2) Entering a market order doesn't gaurantee me my price or better ... only possibly my price or worse.
You ARE correct in that this issue is causing me all sorts of other problems. Why do you even have to kick off this order? Can't you create some property or method in the strategy object that I can set to inhibit the generation of this order? There has *got* to be a way of getting this done.
When I run this strategy and keep cancelling this "Close Position" order when it is generated, my strategy runs without issue and works nicely.
ThePatientOne
06-01-2007, 11:00 AM
Have you guys had any progress with this issue? My strategy runs perfectly if I sit here and keep cancelling the "Close Position" order.
Can you give me a programatic way of performing this from my strategies?
Thanks.
NinjaTrader_Dierk
06-01-2007, 11:02 AM
Nope, sorry not yet. We still are not sure how to 100% reliably resolve this issue. We still have it on our list.
NTWolfe
04-01-2010, 12:20 AM
I created a simple strategy using the Strategy Wizard that basically reverses position when criteria are met. So on historical backtesting it looks something like this:
Enter Long (Buy) 5 contracts
Exit Long (Sell) 5 contracts
Enter Short (SellShort) 5 contracts
The last 2 orders occur on the same condition at the same price.
On Live testing I get the following:
Enter Long (Buy) 5 contracts
Exit Long (Sell) 5 contracts
Close Position (Sell) 5 contracts
Enter Short (SellShort) 5 contracts
I get this extra order labelled "Close Position" that ends up doubling the size of my short position, so that I am short 10 contracts rather than 5 contracts. What is this extra order that is generated, and how do you get it to stop generating it. This would be very frightening if trading live.
Thank you for your help.
NinjaTrader_Bertrand
04-01-2010, 04:46 AM
NTWolfe, welcome to our forums - if you want to just reverse on signals triggered, just use the Enter() methods only (and no Exit()'s as in your snippet) as they would revese automatically for you.
Please also ensure you're properly synched up as you start the strategy for realtime trading then -
http://www.ninjatrader-support.com/HelpGuideV6/StrategyPositionVsAccountPosition.html
NTWolfe
04-01-2010, 07:51 PM
Thanks I thought it would be something simple to correct.
NTWolfe
04-11-2010, 06:31 PM
I did walk-forward testing (on 4-9-10) again using a strategy created in the strategy wizard. I still got an erroneous "Close position" order submitted that ended up doubling and tripling my position in the wrong direction.
The strategy is different, not a Stop and Reverse. It has exits that are different than the opposite entry direction. Could it be that this occurs when the exit (to close a position) and the entry (to open a position in the opposite direction) happen to occur on the same bar?
NinjaTrader_Ben
04-11-2010, 10:44 PM
Hello,
I will have someone reply to you on Monday. Thank you for your patience.
NinjaTrader_Bertrand
04-12-2010, 11:11 AM
NTWolfe, this could happen if you issue duplicative orders - you would likely need to analyze what you do / issue with our TraceOrders feature -
http://www.ninjatrader-support.com/HelpGuideV6/TraceOrders.html
If you like you can also send a strategy exhibiting this to support at ninjatrader and my Attn and I will take a look here on my Market Replay.
Thanks
NTWolfe
04-14-2010, 07:10 PM
It happened again today, could the strategy wizard be flawed when it creates a strategy. I don't think it is as I have unlocked the code created by the strategy wizard and there is nothing that is labelled "close positon" within the code, maybe it is the platform itself or the bar type, I don't know.
What is the actual e-mail address I should send a copy of the strategy to?
Once I add the TraceOrders code what do I do with the information it generates?
One other thing:
Under the options category in strategies there is a button for "On starting a real-time strategy" that gives the option to either "Wait until flat before executing live" or "Immediately submit live working historical orders". I use market orders and have the Immediately submit live working historical orders button checked. Could this be the reason for getting the extra "close position" order submitted along with my predefined exit order.
NinjaTrader_Bertrand
04-15-2010, 05:32 AM
NTWolfe, you can contact me at support at ninjatrader dot com Attn Bertrand.
Close Position is needed to reverse, for example when you short 1 contract to go net long 1 contract you need to buy 2, one to close the open short, the second to get you long then.
Are you 100% sure you're synced up properly as you start trading it?
http://www.ninjatrader-support.com/HelpGuideV6/StrategyPositionVsAccountPosition.html
Yes, if you select the 'Immediately Submit' option and do not take care to sync, the issue could easily show up, have you tried to WaitUntilFlat to then the next freshly generated entry from the strategy?
TraceOrders info can be found here -
http://www.ninjatrader-support2.com/vb/showthread.php?t=3627
Trace
NTWolfe
04-15-2010, 09:00 PM
This seems to have corrected the situation, today on live data I did not recieve the extra order "close position"
For any others that may have the same issue:
Under the options category in strategies there is a button for "On starting a real-time strategy" that gives the option to either "Wait until flat before executing live" or "Immediately submit live working historical orders". I use market orders and have the Immediately submit live working historical orders button checked. Switching it to "Wait until flat before executing live" seems to have stopped it on today's run through.
Thank you for your patience Bertrand.
NTWolfe
04-26-2010, 06:56 PM
I allowed the strategy to run on live data today and got some pretty bizarre behavior (see attached photo), anyone ever seen anything like this?
On this it still generated the infamous "close position" order and the sim 101 account was in sync (flat) before the strategy began to generate orders. This order still doubled the position in the wrong direction.
One more question. I ran the same strategy on 4 different symbols. Two of these symbols submitted these erratic orders as seen on the photo, the other two submitted no orders at all. Does Ninjatrader only allow a certain number of symbols to be strategy-tested at the same time.?
NTWolfe
04-26-2010, 08:40 PM
In reference to the two charts that had a strategy running but did not generate trades.
CL is one and SI is the other. Orders created in the stategy wizard are Market Orders (Enter Long or Enter Short).
However I noticed that even when trying to enter a market order manually it does not get filled. In Control Center under the Orders Tab in the column listed "State" it just continues to say "Working".
I am using the Sim101 account and a live data feed via Barchart.com.
NinjaTrader_Bertrand
04-27-2010, 07:12 AM
NTWolfe, there's no limit # of strategies you could monitor.
Are you getting live Level 1 data in the Control Center for the CL and SI expiries you choose to trade?
NTWolfe
05-01-2010, 10:07 PM
No I checked and it is delayed data, thanks for your insight. Will update to real-time for those 2 symbols.
NinjaTrader_Bertrand
05-02-2010, 10:55 AM
You're welcome, please let me know how it goes then.
NTWolfe
07-20-2010, 05:21 PM
I am using NT 6.5 with a Zen-Fire data feed (real-time data). The strategy was created in Strategy Wizard, I am not a programmer. For the last 2 days I have allowed my strategy to run on about 5 different symbols. I am using Range charts. The Sim 101 account, and positions, are flat before starting the strategies (in sync). The orders are market orders.
This order with the name "Close Position" continues to get submitted at some point in the day which ends up doubling or tripling my position on successive entries.
This is what occurs: (Order Action, # contracts, Name of order)
1. Sell- it is a short Entry order 1 contract and the name is what I have created in my strategy "SE"
2. Buy- the next order submitted is a short Exit order for 1 contract and the name is "Close Position" (instead of "SX" which is the order that should be submitted according to the strategy.
At this point I am Flat as I should be position-wise. Next:
3. Buy- a long Entry order for 1 contract and the name is what I have created "LE"
4. Sell- an exit order for 1 contract and the name is what I have created in my strategy "LX"
5. Sell- a short Entry order for 1 contract and it is the "Close Positon" order (this is not part of my strategy)
6. Sell- another short Entry order, the correct one, for 1 contract at the same price as the previous order and it is what I have created in my strategy "SE"
At this point now I am short 2 contracts and this continues to happen throughout the day.
It is not part of my strategy, I simply have Long Entry/Exit and Short Entry/Exit orders.
I am at a loss as to why this happens.
NinjaTrader_Austin
07-21-2010, 07:56 AM
NTWolfe, could you please post the code you're using? If you have both ExitShort() and EnterShort() in your strategy, there could definitely be situations where you get short two contracts if the exit and entry are called on the same bar.
NTWolfe
08-03-2010, 05:05 PM
To keep it simple I only used the Donchian Short and Quantity variables when running the strategy with real-time data, the other variables are not used.
I saved it as an MS Word document and added it to a zip file.
NinjaTrader_Bertrand
08-04-2010, 07:07 AM
Could you please reattach the zip or send it directly to support at ninjatrader dot com? Thanks
NTWolfe
09-03-2010, 12:16 PM
I will e-mail the stategy to you at support@ninjatrader.com Attn: Bertrand.
Running the strategy in Sim account generated the same extra order for YM 09-10 on a 10 minute chart.
It has happened with other strategies as well, all are created in the strategy wizard.