* The exit condition was fullfilled
if (ToTime(this.Time[0]) >= this.ExitTime) { if (this.Position.MarketPosition == MarketPosition.Long) this.ExitLong(this.Contracts); else if (this.Position.MarketPosition == MarketPosition.Short) this.ExitShort(this.Contracts); }
* Empty positions tab, as expected
* But my strategy is still shown as holding 1L
* Orders tab for reference
Once it happens, my strategy will not enter again, since I have a MarketPosition == Flat check before entering a new trade.
Considerations:
* I'm using NT 8.0.14.2 (64 bits).
* I'm just using managed order methods.
* It just happens with my live connection (Rithmic). I don't have this problem with backtesting or market replay.
* It doesn't happen all time, and (to date) just after a Exit method (market order) or a stop loss fill (stop order).
Attached below all the strategy code that I can share with you:
namespace NinjaTrader.NinjaScript.Strategies { public class MyStrategy : Strategy { #region Fields private Order entryOrderLong; private Order entryOrderShort; #endregion #region Properties [Range(1, 3), NinjaScriptProperty] public int Contracts { get; set; } [Range(110000, 160000), NinjaScriptProperty] public double ExitTime { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] public int StopTicks { get; set; } [Range(1, int.MaxValue), NinjaScriptProperty] public int TargetTicks { get; set; } #endregion protected override void OnBarUpdate () { int volMultiplier = <some multiplier>; if (<some entry long condition> && this.Position.MarketPosition == MarketPosition.Flat) { this.SetProfitTarget(CalculationMode.Ticks, this.TargetTicks * volMultiplier); this.SetStopLoss(CalculationMode.Ticks, this.StopTicks * volMultiplier); this.entryOrderLong = this.EnterLongLimit(this.Contracts, <some limit price>); } if (<some entry short condition> && this.Position.MarketPosition == MarketPosition.Flat) { this.SetProfitTarget(CalculationMode.Ticks, this.TargetTicks * volMultiplier); this.SetStopLoss(CalculationMode.Ticks, this.StopTicks * volMultiplier); this.entryOrderShort = this.EnterShortLimit(this.Contracts, <some limit price>); } if (ToTime(this.Time[0]) >= this.ExitTime) { if (this.Position.MarketPosition == MarketPosition.Long) this.ExitLong(this.Contracts); else if (this.Position.MarketPosition == MarketPosition.Short) this.ExitShort(this.Contracts); } } protected override void OnOrderUpdate (Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) { if (order.OrderState == OrderState.Filled) { if (order == this.entryOrderLong) { if (this.entryOrderShort != null) this.CancelOrder(this.entryOrderShort); return; } if (order == this.entryOrderShort) { if (this.entryOrderLong != null) this.CancelOrder(this.entryOrderLong); return; } } } protected override void OnStateChange () { if (this.State == State.SetDefaults) { this.Calculate = Calculate.OnBarClose; <initialization of other variables> } } } }
Comment