Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Order canceled after entry bar

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Order canceled after entry bar

    Hi all,

    I'm struggling with a problem in my strategy.
    I have my strategy set to place a stoploss when my entry order is hit and filled. Problem is, as soon as the bar that hit my entry order closes, the stoploss order gets canceled. I can't figure out why.

    Can anybody help me with my coding?

    Code:
    #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
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class MyCustomStrategy : Strategy
        {
            private Order LongEntryOrder = null;
            private Order LongTarget1 = null;
            private Order StoppedOutLong = null;
            private Order ShortEntryOrder = null;
            private Order ShortTarget1 = null;
            private Order StoppedOutShort = null;
            
            private int QuantityInput = 1;
            private int LongTarget1Points = 78;
            private int MACDFast = 25;
            private int MACDSlow = 15;
            private int MACDSmooth = 3;
            private int KeltnerPeriod = 100;
            private double KeltnerOffset = 7;
            
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                            = @"TestTest";
                    Name                                = "MyCustomStrategy";
                    Calculate                            = Calculate.OnBarClose;
                    EntriesPerDirection                    = 1;
                    EntryHandling                        = EntryHandling.UniqueEntries;
                    IsExitOnSessionCloseStrategy        = false;
                    ExitOnSessionCloseSeconds            = 0;
                    IsFillLimitOnTouch                    = false;
                    MaximumBarsLookBack                    = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                    = OrderFillResolution.Standard;
                    Slippage                            = 0;
                    StartBehavior                        = StartBehavior.AdoptAccountPosition;
                    TimeInForce                            = TimeInForce.Gtc;
                    TraceOrders                            = false;
                    RealtimeErrorHandling                = RealtimeErrorHandling.IgnoreAllErrors;
                    StopTargetHandling                    = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                    = 0;
                    ConnectionLossHandling                 = ConnectionLossHandling.KeepRunning;
    
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, 
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
                if (LongEntryOrder != null && LongEntryOrder == execution.Order)
                {
                    StoppedOutLong = ExitLongStopMarket(QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Lower[0] - (4 * TickSize));
                    LongTarget1 = ExitLongLimit(QuantityInput / 2, Position.AveragePrice + (4 * LongTarget1Points * TickSize));
                }
                
                if (ShortEntryOrder != null && ShortEntryOrder == execution.Order)
                {
                    StoppedOutShort = ExitShortStopMarket(QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Upper [0] + (4 * TickSize));
                    ShortTarget1 = ExitShortLimit(QuantityInput / 2, Position.AveragePrice - (4 * LongTarget1Points * TickSize));
                }
            }
    
            protected override void OnBarUpdate()
            {
                if(Position.MarketPosition == MarketPosition.Flat && MACD(MACDFast, MACDSlow, MACDSmooth)[0] >= 0 && MACD(MACDFast, MACDSlow, MACDSmooth).Diff[0] >= 0)
                {
                    LongEntryOrder = EnterLongLimit(QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Midline[0] + (1 * TickSize));
                }
                
                if (Position.MarketPosition == MarketPosition.Long && StoppedOutLong.LimitPrice < (KeltnerChannel(KeltnerOffset, KeltnerPeriod).Lower[0] - (4 * TickSize)) )
                {
                    StoppedOutLong = ExitLongStopMarket(QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Lower[0] - (4 * TickSize));
                }
                
                if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == 2)
                {
                    LongTarget1 = ExitLongLimit(QuantityInput / 2, Position.AveragePrice + (4 * LongTarget1Points * TickSize));    
                }
                
                if (Position.MarketPosition == MarketPosition.Long && MACD(MACDFast, MACDSlow, MACDSmooth).Diff[0] < 0)
                {
                    StoppedOutLong = ExitLong();
                }
                
                
                
                
                
                
                if(Position.MarketPosition == MarketPosition.Flat && MACD(MACDFast, MACDSlow, MACDSmooth)[0] < 0 && MACD(MACDFast, MACDSlow, MACDSmooth).Diff[0] < 0)
                {
                    ShortEntryOrder = EnterShortLimit(QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Midline[0] - (1 * TickSize));
                }
                
                if (Position.MarketPosition == MarketPosition.Short && StoppedOutShort.LimitPrice > (KeltnerChannel(KeltnerOffset, KeltnerPeriod).Upper[0] + (4 * TickSize)) )
                {
                    StoppedOutShort = ExitShortStopMarket(QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Upper[0] + (4 * TickSize));
                }
                
                if (Position.MarketPosition == MarketPosition.Short && Position.Quantity == 2)
                {
                    ShortTarget1 = ExitShortLimit(QuantityInput / 2, Position.AveragePrice - (4 * LongTarget1Points * TickSize));    
                }
                
                if (Position.MarketPosition == MarketPosition.Short && MACD(MACDFast, MACDSlow, MACDSmooth).Diff[0] > 0)
                {
                    StoppedOutShort = ExitShort();
                }
            }
            
        }
    }
    Many thanks in advance.

    Grtz, Dennis

    #2
    What you see happening is expected behavior.

    The problem is this: you're using the wrong method.

    Look here:


    Or here:


    The point is: Pay special attention to the method signature that has the 'liveUntilCancelled' or 'isLiveUntilCancelled' argument. It is this feature that will solve your problem.

    You will want to use this style of signature for all your Stop and Limit orders.

    Comment


      #3
      Thanks for your reply!

      So if I understand corectly, you are saying I should change my order coding to:

      Code:
      StoppedOutLong = ExitLongStopMarket(0, true, QuantityInput, KeltnerChannel(KeltnerOffset, KeltnerPeriod).Lower[0] - (4 * TickSize), "", "");
      ?

      Comment


        #4
        Exactly.

        The 1st two arguments are new for that method signature, so adding those two new args is the only real change, if I recall.

        And yes, typically '0' is used for BarsInProgress and 'true' is used for IsLiveUntilCancelled.

        Comment


          #5
          Hello Aqua-Life,

          bltdavid is correct.

          If an order is submitted without liveUntilCancelled this will default to false, meaning the order will cancel after the bar it is submitted on closes.

          This is also something that will occur with entry orders if the entry order is submitted without liveUntilCancelled or if this is set to false.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by DJ888, Today, 10:57 PM
          0 responses
          6 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          158 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Today, 09:29 PM
          0 responses
          7 views
          0 likes
          Last Post Belfortbucks  
          Started by zstheorist, Today, 07:52 PM
          0 responses
          7 views
          0 likes
          Last Post zstheorist  
          Started by pmachiraju, 11-01-2023, 04:46 AM
          8 responses
          151 views
          0 likes
          Last Post rehmans
          by rehmans
           
          Working...
          X