Hi everyone - I'm back after a couple years hiatus. I am stumped as to why an unmanaged limit order, when filled in backtesting, generates an error "Object reference not set to an instance of an object" on the last bar.
Try the source below. All it does it place an unmanaged limit order on a specific date on a weekly chart, using an internally-added series of daily bars for executing orders. The order gets filled a few days after placing it, and it does nothing else.
To set it up:
Load a WEEKLY chart for stock BA, using 1850 days up to 11/1/2014.
The strategy adds its own daily series internally.
Limit order is placed on 6/5/2009 against the daily series, and filled a few days later
The error "Object reference not set to an instance of an object" happens on last bar.
The entire strategy is just 4 lines in OnBarUpdate(). That's it. Can't get any simpler!
As far as I can tell, the error isn't on my end, unless I'm missing something incredibly basic. I mean, I've built my own backtestable ATM order management framework that does many things that LOM does, and it works fine. I was trying to distill a strategy down to the bare minimum to demonstrate a NinjaTrader bug to the NT support staff, and I encountered this. Any thoughts?
namespace NinjaTrader.Strategy {
[Description("Unmanaged limit order test")]
public class UnmanagedLimit : Strategy {
#region Variables
private IOrder entryorder = null;
#endregion
protected override void Initialize() {
Add(PeriodType.Day, 1); // add daily bar series to weekly chart, to trade on
Unmanaged = true;
TimeInForce = Cbi.TimeInForce.Gtc;
ExitOnClose = false;
BarsRequired = 20;
CalculateOnBarClose = true;
}
protected override void OnExecution(IExecution ex) {
Print("Executed order "+ex.Order.Name);
}
protected override void OnBarUpdate() {
if (BarsInProgress != 0) return;
if (ToDay(Time[0]) == 20090605) {
entryorder = SubmitOrder(1, OrderAction.Buy, OrderType.Limit, 100, 40.68, 0, "", "MyEntry");
Print("Submitted order "+entryorder.ToString());
}
}
#region Properties
#endregion
}
}
The bug I found, that I want to demonstrate, isn't this one. It's an observation that optimizing an unmanaged order strategy on EOD data can result in remant orders from previous iterations polluting the current iteration, in an optimization. But before I can demonstrate that, I need to understand what's going on with this simple strategy first.