Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 04-27-2012, 08:08 AM   #1
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default How to get order status

Hello, I'm in the middle of developing my strategy and I got stuck at the part where I need to close all orders on opposite signal. Orders could be in pending stage or already triggered, and I'm using multiple orders with different TPs. Is there a way to query an order to see whether it exists or not ? i.e. if its on the market or it hasn't been triggered yet ? Because as soon as stop order on market it get Filled OrderState, and if TP hits for that order its the same state. So depending on whether it was triggered or not I need to use cancel order function or exitlong for example please correct me if I'm wrong. Anyone can suggest please on this topic anything ? maybe some sample of the code where there is some decision making login based on if the order was triggered or not ?or some logic to close all long orders (pending and currently running ones).
vitaliys is offline  
Reply With Quote
Old 04-27-2012, 08:28 AM   #2
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello vitaliys,
Welcome to our forum and I am happy to assist you.

While submitting the order you can add an IOrder object to it and check it later on for the order state

Code:
IOrder order = EnterLongLimit(limitPrice);
Code:
if (order.OrderState != OrderState.Filled)
{
  //do something
}
http://www.ninjatrader.com/support/h...nt7/iorder.htm

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-27-2012, 10:27 AM   #3
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you for your reply. I was trying to use iOrder object, but i'm getting stuck on the part where OrderState.Filled is true. So my order entered market and its currently open lets say the second order in same direction (buy stop) is not triggered yet, (i have two iOrder objects). To close both of those orders I need to apply ExitLong to the first order and CancelOrder to the second one right ? So if I have the third order which reached TP and has been closed it will have OrderState.Filled state untill I clear iOrder object. So at the point where one order has reached TP and closed and second still working they both will have OrderState.Filled. How can I distinguish between currently open and running order vs the one that hit TP ? Lets say I need to close 3 orders (1-pending buy stop, 2- currently active market order, 3 - Order that reached TP and requires iOrder object to be nulled )?

P.S. I null iOrder objects in execution function if orderstate.Filled or cancelled. But then I can't refer to open order to close it.
Last edited by vitaliys; 04-27-2012 at 11:08 AM.
vitaliys is offline  
Reply With Quote
Old 04-27-2012, 11:34 AM   #4
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello vitaliys,
To clarify further, the third order closed which entry order. the market order or the buystop entry order?

I look forward to assisting you further.
.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-27-2012, 11:54 AM   #5
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

Sorry for little messy explanation, third order is buystop order which has been triggered and hit TP so its a closed order. so it looks like this:

1) Order1 - buystop - pending to be triggered
2) Order2 - buystop - that has been triggered and currently open
3) Order3 - buystop - TP was hit and order closed.

so basically I need to cancel first order & null iOrder, exit second order & null iOrder, and null iOrder third one.
vitaliys is offline  
Reply With Quote
Old 04-27-2012, 12:44 PM   #6
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello vitaliys,
Thanks for the clarification.

I will use unique names for the entry and exit codes and will nullify the objects in the OnExecution event when the exit order gets filled.

for example, if the entry is defined by
Code:
IOrder long3 = EnterLong("long3")
and the exit order is defined by
Code:
ExitLong("exitlong3", "long3")
then nullify the entry order object in the OnExecution event when it is squared off:

Code:
 
protected override void OnExecution(IExecution execution)
{
 
   if (execution.Order != null && execution.Order.Name == "exitlong3" && execution.order.OrderState == OrderState.Filled)
   {
       long3 = null;
   }
}
This way you now have the 2 orders to take care off as the 3rd order is already set to null.

http://www.ninjatrader.com/support/helpGuides/nt7/index.html?onexecution.htm

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-27-2012, 12:54 PM   #7
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you for the example,
What about if order hit TP which was set by SetTargetPrice ? there wouldn't be ExitLong, is there a way to do something as you mentioned below for order that hit TP ?
Last edited by vitaliys; 04-27-2012 at 01:10 PM.
vitaliys is offline  
Reply With Quote
Old 04-27-2012, 01:22 PM   #8
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello vitaliys,
Please use the below overload for the target orders
Quote:
SetProfitTarget(stringfromEntrySignal, CalculationModemode, doublevalue)
http://www.ninjatrader.com/support/helpGuides/nt7/index.html?setprofittarget.htm

Now you can monitor the targets in the OnExecution event.

Code:
 
if (execution.Order != null && execution.Order.Name = "Profit target" && execution.Order.FromEntrySignal = "long3" && execution.Order.OrderState == Filled)
{
   //do something
}
Please refer to this sample code for further reference http://ninjatrader.com/support/forum/showthread.php?t=5790

Please let me know if I can assist you any further.

NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-29-2012, 11:14 AM   #9
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello Joydeep,

I got now confused by the use of SetProfitTarget by you.

SetProfitTarget(stringfromEntrySignal, CalculationModemode, doublevalue)

stringfromEntrySignal - argument would be the be signal Name that would be assigned to the entry unique name right ? so how would I refer to that target price or SL in OnExecution function ?

My entry code is:

Order1 = EnterLongStop(0,true,LotSize1, ePrice, "Order1");
Order2 = EnterLongStop(0,true,LotSize2, ePrice, "Order2");
Order3 = EnterLongStop(0,true,LotSize3, ePrice, "Order3");

so each order has its own Signal name,

then there is
protected override void OnExecution(IExecution execution)
{
if(Order1!=null && Order1.OrderState == OrderState.Filled)
{
//***********************TARGET PRICE FOR ORDER1*****************************

if (tpType == 1 || tpType == 0)
{
SetProfitTarget("Order1", CalculationMode.Ticks, TP1); // I Assign TP for Order1, I do almost same same for the next two orders based on different conditions
}


// I add the following code to clear Order1,2,3 objects after TP is hit

if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order1" && execution.Order.OrderState == OrderState.Filled)
{
Print ("Order1 hit TP and iOrder was nulled");
Order1 = null;
}
if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order2" && execution.Order.OrderState == OrderState.Filled)
{
Print ("Order2 hit TP and iOrder was nulled");
Order2 = null;
}
if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order3" && execution.Order.OrderState == OrderState.Filled)
{
Print ("Order3 hit TP and iOrder was nulled");
Order3 = null;
}



}

But execution.Order.Name is always either Sell or Order1,2,3. So there is some error in how do I attach TP(assuming all orders might have different entry price) to the order itself could you please clarify this part or let me know if you need all my code (it kind of lengthy and messy so I got only relevant code in here).
Last edited by vitaliys; 04-29-2012 at 08:56 PM.
vitaliys is offline  
Reply With Quote
Old 04-30-2012, 04:41 AM   #10
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello vitaliys,
An order submitted via SetProfitTarget() will have a name of "Profit target".
Please refer to this sample code for more details http://ninjatrader.com/support/forum...ead.php?t=5790

Since you are submitting the target orders in the OnExecution event, and it is filtered by more conditions please make sure your target orders are being placed.

You can also add the code TraceOrders in the Initialize section of the strategy to find out which order is getting filled etc
http://www.ninjatrader.com/support/h...raceorders.htm

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-30-2012, 06:05 AM   #11
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

So Joydeep how do I relate SetTargetPrice to the particular order ? Or if I set it in execution event for Order1 it will be automatically assigned to Order1 ?
vitaliys is offline  
Reply With Quote
Old 04-30-2012, 06:15 AM   #12
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello vitaliys,
You have already done so via the code
Code:
if (execution.Order != null && execution.Order.Name == "Profit target" && execution.Order.FromEntrySignal == "Order1" && execution.Order.OrderState == OrderState.Filled)
Have you set the SetProfitTarget for all the orders. From your code snippets I could only see you have only set the profit target for the order "Order1". Please make sure you have set the target order for all the orders (Order2 and Order3).

Further you code filters like
Code:
if(Order1!=null && Order1.OrderState == OrderState.Filled)
Now if Order1 is null then it fails to get the print and set the other orders to null (order2 and order3).

Please remove that filter or make case-by case filter for each order.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-30-2012, 10:23 AM   #13
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank your for clarifying that, so TP is not attached to the entry order directly but through the entry price. If I resubmit SetTargetPrice with same name but different TP price, will it move TP or create a new one ? Also is there a good logic to deal with Partial Fills ? As I understand order with partial fill may not get filled completely at all so it creates a little bit tricky conditions.
vitaliys is offline  
Reply With Quote
Old 04-30-2012, 11:07 AM   #14
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 553 times in 545 posts
Default

Hello,
Yes, if you reassign the SetProfitTarget() then it will change the target price for that particular order.

You have to custom code any part filled logic. It depends entirely on you how you want to handle such situations.

You may also like to look into this sample coded http://ninjatrader.com/support/forum...ead.php?t=7499

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 04-30-2012, 04:20 PM   #15
vitaliys
Junior Member
 
Join Date: Jan 2012
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks a lot! I have looked at the sample of the code that you provided and got a few question, what is the difference between SetStopLoss and ExitLongStop for example in terms of how the order will be closed? I understand that those two functions have slightly different properties in it but result looks the same. Also second question is I set my stop loss as

SetStopLoss("Order1", CalculationMode.Ticks, slSize, false);

Will the SetStopLoss stay at the same level of entry after entry signal "Order1" order is closed ? Do i have to reset the SetStopLoss function every time to avoid lets say stop loss being higher then TP situations ? if that's the case can ExitLongStop for example be used as a SL and what is the pros and concerns of using ExitLongStop instead on SetStopLoss function ?
vitaliys 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
Order Status outside of strategy monpere Strategy Development 1 06-09-2010 06:36 AM
Order Status murfinp Automated Trading 4 04-03-2009 08:20 AM
Order Status - Another Question murfinp Automated Trading 1 04-03-2009 07:13 AM
account order status Creamers Indicator Development 7 12-08-2008 03:54 PM
Order / Position Status pmaglio Strategy Analyzer 1 03-12-2008 08:34 PM


All times are GMT -6. The time now is 08:32 PM.

FULL RISK DISCLOSURE: Futures trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.