I have simplified the code the maximum so maybe someone can troubleshoot it.
I am still getting the error Object reference not set to an instance of an object. I understand this is has to do with the init of the order and more precisely the Name of the order.
any comment will help.
namespace NinjaTrader.NinjaScript.Strategies
{
public class testordermanagement : Strategy
{
private Order entryOrder = null; // This variable holds an object representing our entry order
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter the description for your new custom Strategy here.";
Name = "testordermanagement";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
}
else if (State == State.Configure)
{
if (entryOrder != null)
entryOrder = GetRealtimeOrder(entryOrder);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
if ( Close[0] > Open[0])
{
EnterLongLimit(0, true, 1, Close[0] , "LongEntry");
}
if (entryOrder.Name == "LongEntry" && Close[0] < Open[0])
{
CancelOrder(entryOrder);
}
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if (order.Name == "LongEntry")
{
entryOrder = order;
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrder = null;
}
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (entryOrder != null && entryOrder == execution.Order)
{
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
}
}
}
}

Comment