NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 12-26-2010, 10:33 AM   #1
werido
Senior Member
 
Join Date: Oct 2009
Posts: 157
Thanks: 4
Thanked 0 times in 0 posts
Default Exit orders

Hi,

What is the strategy code that would send out an exit order to close a trade labeled "a" only if that trade is currently open, but would not even try to close out "a" if it is not open?

The strategy that I have now sends out the exit order for "a" whenever the conditions for the exit are met despite the fact that "a" was not even opened to begin with. This doesn't really cause any major problems since NT7 just ignores the exit order if it isn't applicable. But how would I prevent this attempted exit order execution from occurring in the first place?

What I've been using so far is:

if (Position.MarketPosition == MarketPosition.Long)

and this does prevent the exit order from trying to execute when I have no orders open. But this line of code doesn't help when I have order "b" open long, because this line of code just says to send the exit order when there is ANY position open long.

How do I limit it even further so that the condition is that -- only if "a" is long, then and only then send out the exit order: ExitLong("Exit a", "a") ?



Thank you very much.
werido is offline  
Reply With Quote
Old 12-26-2010, 07:23 PM   #2
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

Not sure that I understand what your trying to achieve here. As you would need to let the trade close itself off before you are able to start a new trade. Therefor if you never exited the trade your Positions would be off and a new trade would never be taken as you are limited by Entried Per Direction = 1 for example if this is what you have it set too.


Also, your NinjaScript strategy it sounds like you have the settings set to Wait until falt before executing live. In this case the automated strategy running on historical data would need to go flat before the entry is submitted live. Therefor in this case also you would still need the exit signal code.

I look forward to assisting you further.
NinjaTrader_Brett is offline  
Reply With Quote
Old 12-26-2010, 07:44 PM   #3
werido
Senior Member
 
Join Date: Oct 2009
Posts: 157
Thanks: 4
Thanked 0 times in 0 posts
Default

Hi Brett,

What I'm trying to do is put a condition on the exit order so that it only executes if in fact the entry order associated with that exit order is live and the position is in play.
Right now my code is something like:

if (............)
{
EnterLong(200000, "a");
}

if (Position.MarketPosition == MarketPosition.Long
&&...................., 1))
{
ExitLong("Exit a", "a");
}


The problem is that the exit order will try to execute when the rule (..............) is met and when there is currently a live Long position. It will execute the order to "Exit a" even if "a" is not an open position. As long as there is any Long position that is live, this exit order will execute. Now, NT7 won't close an order in this case and will just ignore "Exit a" if "a" is not live. But is there a way to put a condition on the exit order to only execute "Exit a" when "a" is live?
The condition I put: if (Position.MarketPosition == MarketPosition.Long works to stop the exit order when there is NO live Long position, but it will still execute the exit order (although not close anything) when there is ANY long position that is live.
How do I set a condition for the exit to only execute if "a" is live?

Thank you.
werido is offline  
Reply With Quote
Old 12-26-2010, 08:05 PM   #4
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

In this case, I would just not run on historical data is this on option?



On the top of OnBarUpdate()
{

if( Historical )
return;

}

Otherwise you would be unable to do this as there is no way for the strategy to tell the difference from what occured historically and what occured live. It simply sees the entries and the exits, this is explicit design.

Let me know if I can be of further assistance.
NinjaTrader_Brett is offline  
Reply With Quote
Old 12-26-2010, 08:32 PM   #5
werido
Senior Member
 
Join Date: Oct 2009
Posts: 157
Thanks: 4
Thanked 0 times in 0 posts
Default

Executing the exit based on the strategy applied to the chart with historical data is ok for my purposes so long as "a" is in play from the strategy (not necessarily live from having been executed by the strategy in real time). But is there any way to set a condition for the exit order to only execute when the entry order associated with that exit order actually needs to be closed as per the strategy?
The reason this is important is that I also have a signal set up to signal me when the exit order is triggered and I would only like that signal to show when that exit order is indeed relevant (i.e. "a" is needs to be closed) as opposed to just when the conditions of the exit order are met despite "a" not even being live (from historical or not, from live execution or from the historical data). I don't want a signal telling me to "exit a" when "a" doesn't need to be closed because it was never open from the strategy (live or not).
Isn't there a way to connect the exit order to the entry order in a better way so that the exit order will only go through if it's entry order was called for by the strategy?
Is there something like:
if (Position.MarketPosition == MarketPosition.a)
where the important thing is that the strategy checks to make sure that the order labeled "a" is in play (i.e. open position from the strategy), and not just check to make sure that there is ANY order that is currently Long from the strategy (which will show as Long in MarketPosition when "immediatley submit live working historical orders" is checked like I have it now).
Is there a code line that says " if 'a' is open" ......?
werido is offline  
Reply With Quote
Old 12-27-2010, 06:33 AM   #6
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
Default

Hello,

Yes, you can check to see if an entry order has been executed. With an IOrder object which gets into some more complex unmanaged order code. However this will not assist you as the IOrder object will still show a filled status on historical. So that when you switch to live it will still consider the order executed and will still need to issue the exit order as NinjaTrader will see no difference vs an order executed in Simulation/Historical or live they are both the same in NinjaTraders eye.

What you need here is the information on things to watch out for the strategy position vs the account position when this does occur, also to make sure you account is in sync.


Strategy Position vs. Account Position:
<http://www.ninjatrader-support2.com/...ead.php?t=4033>
1st solution:
You can include the following statement in your code to avoid the strategy to be calculated on historical data.
// Only run on real-time data
if (Historical)
return;
Add the statement to the top of OnBarUpdate()
2nd solution:
You can set 'On starting a real-time strategy' to 'Wait until flat before executing live'.
This option can be found at Tools-->Options-->Strategies-->NinjaScript-tab.
3rd solution, would be submitting manual orders to sync the strategy and account positions described in the link


Let me know if I can be of further assistance.
NinjaTrader_Brett is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Exit orders syscali General Programming 3 09-20-2010 08:45 AM
ATI Exit Orders for Stop dbvaello Automated Trading 4 06-23-2010 10:19 AM
Modifying Exit Orders heyligerb ATM Strategies (Discretionary Trading) 6 03-17-2010 07:30 AM
Differences in Exit Orders three faced devil Strategy Development 3 10-27-2008 10:41 AM
Exit Via Limit Orders Not Working Xepher Strategy Development 3 04-12-2008 01:59 AM


All times are GMT -6. The time now is 11:22 PM.