1) I'm using unmanaged orders.
2) Market Replay (using E-mini ES)
3) Logging whether the order "_buyOrder" is null or not as it progresses through order completion.
4) Always seems to be null when passing through OnOrderUpdate and OnExecutionUpdate
4) Mysteriously goes back to being != null when logging from OnBarUpdate
Sample output
NinjaScript strategy 'testNull/69020941' submitting order
12/18/2015 6:31:00 AM :: OnOrderUpdate _buyOrder = null
12/18/2015 6:31:00 AM :: OnOrderUpdate _buyOrder = null
12/18/2015 6:31:00 AM :: OnOrderUpdate _buyOrder = null
12/18/2015 6:31:00 AM :: OnOrderUpdate _buyOrder = null
12/18/2015 6:31:00 AM :: OnExecutionUpdate. Price: 2020.75 Quantity: 1 execution.Order.Name: buyOrder
12/18/2015 6:31:00 AM :: buyOrder has executed @ 2020.75
12/18/2015 6:31:00 AM :: Order must of been nulled in OnOrderUpdate
12/18/2015 6:31:00 AM :: CURRENTLY != NULL... Every min, we check to see if _buyOrder is null.
12/18/2015 6:32:00 AM :: CURRENTLY != NULL... Every min, we check to see if _buyOrder is null.
#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class testNull : Strategy { // RUN THIS STRATEGY ON ES WITH MARKET REPLAY. Set to start @ 6:30am (may have to adjust to your timezone) // private Order _buyOrder = null; double tickSize = .25; double currentBid; double currentOffer; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"RUN THIS STRATEGY ON ES WITH MARKET REPLAY."; Name = "testNull"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1000; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; IsUnmanaged = true; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 0; } else if (State == State.Configure) { } } protected override void OnBarUpdate() { if (State == State.Realtime) { if (Position.MarketPosition == MarketPosition.Flat && ToTime(Time[0]) > 63000) _buyOrder = SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.Limit, 1, currentOffer + tickSize, 0, "", "buyOrder"); if (_buyOrder == null) P(1,"CURRENTLY == NULL... Every min, we check to see if _buyOrder is null."); else P(1,"CURRENTLY != NULL... Every min, we check to see if _buyOrder is null."); } } protected override void OnMarketData(MarketDataEventArgs e) { if (e.MarketDataType == MarketDataType.Bid) { currentBid = e.Price; } if (e.MarketDataType == MarketDataType.Ask) { currentOffer = e.Price; } } private void P(int outputWindow, string message) { if (outputWindow == 1) PrintTo = PrintTo.OutputTab1; if (outputWindow == 2) PrintTo = PrintTo.OutputTab2; Print(Time[0] + " :: " + message); } 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) { if (_buyOrder == null) { P(1, "OnOrderUpdate _buyOrder = null"); } else { P(1, "OnOrderUpdate _buyOrder != null"); if (_buyOrder == order) { P(1, "try nulling here"); if (order.OrderState == OrderState.Filled) { P(1, "Null happening"); _buyOrder = null; } } } } protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time) { P(1, "OnExecutionUpdate. Price: " + price + " Quantity: " + quantity + " execution.Order.Name: " + execution.Order.Name); if (execution.Order.OrderState == OrderState.Filled) { if (execution.Order.Name == "buyOrder") { P(1, "buyOrder has executed @ " + execution.Order.LimitPrice); if (_buyOrder == null) P(1, "Order must of been nulled in OnOrderUpdate"); else { P(1, "Order wasn't nulled in OnOrderUpdate. Let's null it here"); _buyOrder = null; } } } } protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition) { } protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value) { } } }
Comment