View Full Version : Simple Limit Strategy with CancelOrder
stefy
11-07-2008, 10:17 AM
I'm developing a simple strategy based on entering a limit order and then canceling it if not filled in 3 bars. I get no trades at all. That's what I did:
In the Variables region
private IOrder myEntryOrder = null;
OnBarUpdate():
// Entry Long Condition
if (myEntryOrder == null)
{
myEntryOrder = EnterLongLimit(1, true, DefaultQuantity, MaxRange, "LongLimit");
}
// Cancel LimitOrders condition
if (BarsSinceEntry() >= 3)
CancelOrder(myEntryOrder);
// Exit Condition Long
if (conditions
&& Position.MarketPosition == MarketPosition.Long)
{
ExitLong("Take Profit Long", "LongStopLimit");
myEntryOrder = null;
}
// Stop Loss Long
if (conditions
&& Position.MarketPosition == MarketPosition.Long)
{
ExitLong("LongStopLoss", "LongStopLimit");
myEntryOrder = null;
}
Thank you
NinjaTrader_Dierk
11-07-2008, 11:04 AM
You need to debug your NinjaScript as per here: http://www.ninjatrader-support2.com/vb/showthread.php?t=3418
and here
http://www.ninjatrader-support2.com/vb/showthread.php?t=3627
stefy
11-07-2008, 12:06 PM
I adjusted the Bar Index from 1 to 0.
It takes the first trade now, but then it doesn't trade anymore.
I printed my IOrder and after the first LongLimitOrder and Cancelled Order, it keeps on repeating on each following bar:
Order='NT-00002/Back101' Name='LongLimit' State=Cancelled Instrument='$GBPCHF' Action=Buy Limit price=2,195 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='bfdd4edd5a814f6487eddd04d841af37' Gtd='01/12/2099 0.00.00'
Always the same Cancelled state of the same order, but it doesn't take new Limit orders.
Yet, I refresh the variable to myEntryOrder = null when:
1) Stop Loss kicks in
2) Take Profit kicks in
3) The order gets canceled
Thank you
NinjaTrader_Dierk
11-07-2008, 12:15 PM
Unfortunately we are unable to provide support down to the level of plain C# programming or actually coding your strategy.
However here is a hint: as you call CancelOrder you need to set myEntryOrder to null in order to allow your if condition "if (myEntryOrder == null)" to trigger for another entry.
stefy
11-07-2008, 12:36 PM
That's what I did, but I'm not sure I formulated it correctly.
// Cancel LimitOrders condition
if (BarsSinceEntry() >= 3)
{
CancelOrder(myEntryOrder);
myEntryOrder = null;
}
Please another hint, I spent a lot of time on this code. :)
Thank you very much
NinjaTrader_Dierk
11-07-2008, 12:42 PM
You need to trace if EnterLongLimit would be called again and if so what would happen to your signal (see TraceOrders).
stefy
11-10-2008, 02:46 AM
So I did.
I got the following output:
03/01/2008 9.00.00 Entered internal PlaceOrder() method at 03/01/2008 9.00.00: Action=Buy OrderType=Limit Quantity=25.000 LimitPrice=0,7430 StopPrice=0 SignalName='LongLimit' FromEntrySignal=''
0 Order='NT-00000/Back101' Name='LongLimit' State=Working Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
0 Order='NT-00000/Back101' Name='LongLimit' State=Filled Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=25000 Fill price=0,743 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
1 Order='NT-00000/Back101' Name='LongLimit' State=Filled Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=25000 Fill price=0,743 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
2 Order='NT-00000/Back101' Name='LongLimit' State=Filled Instrument='$EURGBP' Action=Buy Limit price=0,743 Stop price=0 Quantity=25.000 Strategy='BreakoutATRRangeLongLimit' Type=Limit Tif=Day Oco='' Filled=25000 Fill price=0,743 Token='800c386990fc48468313fe550b23b7e8' Gtd='01/12/2099 0.00.00'
3
03/01/2008 11.30.00 Entered internal PlaceOrder() method at 03/01/2008 11.30.00: Action=Buy OrderType=Limit Quantity=25.000 LimitPrice=0,7458 StopPrice=0 SignalName='LongLimit' FromEntrySignal=''
03/01/2008 11.30.00 Ignored PlaceOrder() method at 03/01/2008 11.30.00: Action=Buy OrderType=Limit Quantity=25.000 LimitPrice=0,7458 StopPrice=0 SignalName='LongLimit' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
Why do I get "Exceeded entry signals limit"? I have one Entry kicking off at 9AM, I'm working on 30min bars and I have the condition
if (BarsSinceEntry() >= 3)
{
CancelOrder(myEntryOrder);
myEntryOrder = null;
}
so, by 10:30AM, my first LongLimit order is supposed to be canceled and the new LongLimit Order should kick in. Why it doesn't happen?
Thank you
NinjaTrader_Josh
11-10-2008, 07:44 AM
stefy,
If you are getting that message it means you still have an order working. You need to ensure you have actually received a complete cancelled state on your order before you submit your new entry order. To do this means you cannot reset myEntryOrder = null right after you call CancelOrder(). Instead you want to do it inside OnOrderUpdate() when you receive the cancel state.
Untested code.
if (myEntryOrder != null && myEntryOrder.Token == order.Token)
{
if (order.OrderState == OrderState.Cancelled)
myEntryOrder = null;
}