I am writing a code for an strategy. I have not been able to create a command for placing an order that expires/cancels if it is not filled after 12 bars. Any advise is greatly appreciated
Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Cancel order if not filled after 12 bars
Collapse
X
-
Cancel order if not filled after 12 bars
Hello,
I am writing a code for an strategy. I have not been able to create a command for placing an order that expires/cancels if it is not filled after 12 bars. Any advise is greatly appreciated -
Hello Jorge.andres.o,
Thanks for your post and welcome to the Ninjatrader forums!
As far as logic, you can use an int counter that you set to 0 when you place the order and as long as your market position is still flat to increment the counter once per bar, once the counter meets 12 then issue the cancel order: https://ninjatrader.com/support/help...ancelorder.htm
Here is a link to an educational example of using CancelOrder(): https://ninjatrader.com/support/help...thod_to_ca.htmPaul H.NinjaTrader Customer Service
-
Thanks Paul,
I have not been able to make it work. I want to cancel the order if not filled after 5 bars. However, the algorithm is cancelling the order after 1 bar. Could you help me find the error on this example?
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 1)
return;
// Set 1
if (CrossAbove(EMA1, EMA2, 1))
{
EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (23 * TickSize)) , @"11111111");
EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (15 * TickSize)) , @"222222");
EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (10 * TickSize)) , @"3333333");
EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (8 * TickSize)) , @"444444444");
barNumberOfOrder = CurrentBar;
Draw.VerticalLine(this, "tag1"+CurrentBar, 0, Brushes.Red, DashStyleHelper.Solid, 10);
Print("orders submmitted");
}
//if more than 5 bar has elapsed, cancel entry order
if (myEntryOrder == null && CurrentBar > barNumberOfOrder + 5)
CancelOrder(myEntryOrder);
Print("order cancelled");
}
Comment
-
Hello Jorge.andres.o,
Thanks for your reply.
It looks like you are using a managed approach strategy which means that in the case of a limit order, the limit order will be automatically canceled if not filled in the bar it is submitted.
I would suggest reviewing the requirements of the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm
As is, you would need to continue resubmitting the orders on the next five bars unless filled. This may be the easier way for you rather than canceling with the extra code needed.
Alternately you can use the Advanced order method which allows you to specify GTC (Good till cancel) which means once placed you orders would remain until filled or cancelled. You can read about that here: https://ninjatrader.com/support/help...r_handling.htm
NOTE Based on your code example and assuming you used th advanced Order Handling, in order to be able to cancel the multiple orders, you would have to use signal names for the orders and then assign (In OnOrderUpdate), each entry order to its own order object. Please review the example in the help guide (which only shows 1 order) https://ninjatrader.com/support/help...ancelorder.htmPaul H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by cupir2, Yesterday, 07:44 PM
|
3 responses
21 views
0 likes
|
Last Post Today, 08:33 AM | ||
Started by reynoldsn, Yesterday, 07:26 PM
|
2 responses
14 views
0 likes
|
Last Post
by reynoldsn
Today, 08:32 AM
|
||
Started by MartinT, 05-17-2023, 06:00 AM
|
18 responses
173 views
0 likes
|
Last Post
by flybuzz
Today, 08:20 AM
|
||
Started by sgordet, Today, 05:24 AM
|
2 responses
20 views
0 likes
|
Last Post
by sgordet
Today, 08:09 AM
|
||
Started by Board game geek, Today, 02:20 AM
|
1 response
15 views
0 likes
|
Last Post Today, 07:50 AM |
Comment