![]() |
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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).
|
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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
}
Please let me know if I can assist you any further.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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. .
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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")
Code:
ExitLong("exitlong3", "long3")
Code:
protected override void OnExecution(IExecution execution)
{
if (execution.Order != null && execution.Order.Name == "exitlong3" && execution.order.OrderState == OrderState.Filled)
{
long3 = null;
}
}
http://www.ninjatrader.com/support/helpGuides/nt7/index.html?onexecution.htm Please let me know if I can assist you any further.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#8 | |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
Hello vitaliys,
Please use the below overload for the target orders Quote:
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 let me know if I can assist you any further.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
|
#9 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#10 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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 ?
|
|
|
|
|
|
#12 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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) Further you code filters like Code:
if(Order1!=null && Order1.OrderState == OrderState.Filled) Please remove that filter or make case-by case filter for each order. Please let me know if I can assist you any further.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#14 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Junior Member
Join Date: Jan 2012
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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 ? |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 02:54 PM |
| Order / Position Status | pmaglio | Strategy Analyzer | 1 | 03-12-2008 08:34 PM |