If this is the case, then in OnBarUpdate I want to detect that the order was not filled, and resubmit it at the high of the new bar that just closed. How do I do that? How can I tell if the order was not filled?
In my class I am using the managed approach to submit orders, and I have a private method like this:
private void enterBuyStop(int quantity, double entryPrice, int stopTicks, int takeProfitTicks) {
SetStopLoss(LONG_ENTRY_NAME, CalculationMode.Ticks, (double)stopTicks, false);
SetProfitTarget(LONG_ENTRY_NAME, CalculationMode.Ticks, (double)takeProfitTicks);
EnterLongStopMarket(quantity, entryPrice, LONG_ENTRY_NAME);
}
namespace NinjaTrader.NinjaScript.Strategies
{
public class MyStrategy: Strategy
{
private const string LONG_ENTRY_NAME = "Long Entry";
private Order longEntry = null;
private void enterBuyStop(int quantity, double entryPrice, int stopTicks, int takeProfitTicks) {
SetStopLoss(LONG_ENTRY_NAME, CalculationMode.Ticks, (double)stopTicks, false);
SetProfitTarget(LONG_ENTRY_NAME, CalculationMode.Ticks, (double)takeProfitTicks);
EnterLongStopMarket(quantity, entryPrice, LONG_ENTRY_NAME);
}
protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
int quantity, int filled, double averageFillPrice,
Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
{
// Assign Order objects here
// This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
if (order.Name == LONG_ENTRY_NAME)
longEntry = order;
if (longEntry != null && longEntry == order) {
// Reset the longEntry object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
longEntry = null;
}
}
}
}
}
Please tell me how to implement this.
NOTE: I don't think using a test for MarketPosition.Flat would quite work - what happens if the order gets filled and stopped out in the same bar?
Comment