Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Limit Order Bars since placement
Collapse
X
-
Limit Order Bars since placement
If I place a Limit order, BUT the order has not opened yet, what is the best way to keep track of how many bars have been painted since the placement of the order.Tags: None
-
Ok on the barnumber, but I need to wrap this in a if statement on the status of the position. What is that if test ? Because I am not long yet for example?
Comment
-
Hello,
Sure, you would set this on an order update if you wanted.
The easy way to do it would be this simple as well as long you dont reverse positions:
if(Position.MarketPosition == Position.Long && barNumberSinceEntry == 0)
{
barNumberSinceEntry == CurrentBar;
}
Then when you exit set barNumberSinceEntry to 0.
Let me know if I can be of further assistance.BrettNinjaTrader Product Management
Comment
-
This wasn't quite what I ment ....
Example:
1) My conditions are met to enter a Long Limit and I enter Long Limit
2) I mark the barNumberSinceEntry == CurrentBar;
3) But It was not filled at the limit price I asked for
3) How do I know it was not filled ??? Lets say barNumberSinceEntry gets eventually incremented to 5 and I am still not long I want to close the position.
Comment
-
Hello,
In this case you would need to get into some more complex order methods.
Called iOrder.
Then you would use the following sample to monitor the status of the order and then cancel it if its does not reach a status.Filled status.
You want to be sure to only call EnterLongLimit() once however. So you would need to possibly change your entry logic to only call ths once and then set live until cancelled set to true.
Then use the following to cancel it once the barcount has called it to be expired.
Let me know if I can be of further assistance.BrettNinjaTrader Product Management
Comment
-
I have added the code as I understand it but I want to make sure about a few items:
1) I have a number of places where I have code
if (Position.MarketPosition == MarketPosition.Flat)
Even thou I use IOrder the above still works I don't have to make it if (
if ( entryOrder == null )
2)Really everything is the same just I have a IOrder object to get more detail on my order status right?
3)On the following onOrderUpdate is the CancelOrder correct or is it suppose to be the IOrder order passed into method?
protectedoverridevoid OnOrderUpdate(IOrder order)
{
if (entryOrder != null && entryOrder == order)
{
Print(order.ToString());
if (order.OrderState == OrderState.Cancelled)
{
entryOrder = null;
barNumberOfOrder=0;
}
// If more than 5 bars has elapsed, cancel the entry order
if (order.OrderState != OrderState.Filled)
{
if (CurrentBar > barNumberOfOrder+5 )
{
CancelOrder(entryOrder);
}
}
}
}
Comment
-
The reason I ask if this is correct CancelOrder(entryOrder);
Rather than the IOrder param passed in is it seems I read that the value may change during the time you are in the position. So just to double check I use the entryOrder value I received when I placed position?
Comment
-
Yes, this is correct usage of CancelOrder(). There is a sample available that can help with this:
When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-untilRyan M.NinjaTrader Customer Service
Comment
-
1) If I have code that does a decision based off
if (Position.MarketPosition == MarketPosition.Flat
I MUST still have && entryOrder == null added to statement if I am doing a Limit order with LiveTill cancelled. Reason is I could get a IOrder but the Position is still flat Correct?
2)Also if I am looking for how many bars since I received a IOrder I need to make this check in
OnBarUpdate() with following code:
elseif (Position.MarketPosition == MarketPosition.Flat && entryOrder != null && CurrentBar > barNumberOfOrder + numberOfBars)
{
CancelOrder(entryOrder);
}
I can't do it in
OnOrderUpdate because until position status changes this method not called so I have to do bar check where I have it CORRECT?
Comment
-
Originally posted by bbucha_stk View Post1) If I have code that does a decision based off
if (Position.MarketPosition == MarketPosition.Flat
I MUST still have && entryOrder == null added to statement if I am doing a Limit order with LiveTill cancelled. Reason is I could get a IOrder but the Position is still flat Correct?
Hello, This would only be correct on the first OnBarUpdate(). There is a condition that can occur that causes Position.MarketPosition to not yet have updated during the OnBarUpdate() cycle if the order filles during the OnBarUpdate() call and would not be reflected until the next OnBarUpdate(). You can get around this by querying the orders directly, if you are tracking them manually with an iOrder object.
Originally posted by bbucha_stk View Post
2)Also if I am looking for how many bars since I received a IOrder I need to make this check in
OnBarUpdate() with following code:
elseif (Position.MarketPosition == MarketPosition.Flat && entryOrder != null && CurrentBar > barNumberOfOrder + numberOfBars)
{
CancelOrder(entryOrder);
}
I can't do it in
OnOrderUpdate because until position status changes this method not called so I have to do bar check where I have it CORRECT?
This is correct. This would need to be check inside of OnBarUpdate() for example as OnOrderUpdate() is only called wihen the order is updated by some action on your part.
Let me know if I can be of further assistance.BrettNinjaTrader Product Management
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
648 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
369 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
108 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
572 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
573 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|
Comment