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

SetStopLoss or ExitLongLimit

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

    SetStopLoss or ExitLongLimit

    Dear,

    I have been developing a new strategy.

    Everything is ok, until I put SetStopLoss in my strategy.

    I have two situation that I want exit from market.

    1 - ExitLongLimit or SetStopLoss;

    When I use SetStopLoss in my strategy, every orders are executed by it (SetStopLoss); When I remove it from my strategy every orders are executed exit by ExitLongLimit;

    Is SetStopLoss priority?

    How Can I do to fix it?

    Thank you!!!!

    #2
    Hello marcyomatheus,

    Thank you for your post.

    Methods that generate orders to exit a position (such as ExitLongLimit()) will be ignored if a position is open and an order submitted by a set method (SetStopLoss() for example) is active.

    Set methods are not designed to be used with Exit methods. For more information on please visit the following link: http://www.ninjatrader.com/support/h...d_approach.htm

    Comment


      #3
      Hello PatrickH.

      This a simple strategy using SMA.

      When the close price is above SMA and the next candle exceed the prices of candle before = buy;

      Stop below the low prices in this candle;

      Exit When the close price below SMA.

      Could you help me make this script ?

      Thank you!

      Comment


        #4
        Hello marcyomatheus,

        Thank you for your response.

        Can you attach what you have already created to your response?

        Comment


          #5
          SetStopLoss or ExitLongLimit Reply to Thread

          Ok.

          See attachments.

          Thank you!
          Attached Files
          Last edited by marcyomatheus; 01-28-2015, 06:13 PM.

          Comment


            #6
            Adding IOrder objects to the order methods will allow for further management of the orders. Using OnExecution will allow you to make changes when orders execute. The following incorporates IOrder objects for the entry and exits.
            Code:
                    #region Variables
                    private int Period = 20;
            		private IOrder entry = null;
            		private IOrder exit = null;
            		private double lowAtCross = 0;
                    #endregion
            
                    /// <summary>
                    /// This method is used to configure the strategy and is called once before any strategy method is called.
                    /// </summary>
                    protected override void Initialize()
                    {
            			Add(SMA(Period));
            			ExitOnClose = false;
                        CalculateOnBarClose = true;
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {	
                        // Condition set 1
                        if (CrossAbove(Close, SMA(Period), 1)
                            && High[0] < High[-1])
                        {
                            entry = EnterLongLimit(DefaultQuantity, High[0], "entry");
            				lowAtCross = Low[0];
                        }
            
                        // Condition set 2
                        if (CrossBelow(Close, SMA(Period), 1)
            				&& Position.MarketPosition == MarketPosition.Long)
                        {
                            exit = ExitLongLimit(Close[0], "exit", "entry");
                        }
                    }
            		
            		protected override void OnExecution(IExecution execution)
            		{
            			if(execution.Order != null
            				&& execution.Order.Name == "entry"
            				&& execution.Order.OrderState == OrderState.Filled)
            			{
            				entry = null;
            				exit = exit = ExitLongStop(lowAtCross, "exit", "entry");
            			}
            			if(execution.Order != null
            				&& execution.Order.Name == "exit"
            				&& execution.Order.OrderState == OrderState.Filled)
            			{
            				exit = null;
            			}
            		}

            Comment


              #7
              Originally posted by NinjaTrader_PatrickH View Post
              Adding IOrder objects to the order methods will allow for further management of the orders. Using OnExecution will allow you to make changes when orders execute. The following incorporates IOrder objects for the entry and exits.
              Code:
                      #region Variables
                      private int Period = 20;
              		private IOrder entry = null;
              		private IOrder exit = null;
              		private double lowAtCross = 0;
                      #endregion
              
                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize()
                      {
              			Add(SMA(Period));
              			ExitOnClose = false;
                          CalculateOnBarClose = true;
                      }
              
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {	
                          // Condition set 1
                          if (CrossAbove(Close, SMA(Period), 1)
                              && High[0] < High[-1])
                          {
                              entry = EnterLongLimit(DefaultQuantity, High[0], "entry");
              				lowAtCross = Low[0];
                          }
              
                          // Condition set 2
                          if (CrossBelow(Close, SMA(Period), 1)
              				&& Position.MarketPosition == MarketPosition.Long)
                          {
                              exit = ExitLongLimit(Close[0], "exit", "entry");
                          }
                      }
              		
              		protected override void OnExecution(IExecution execution)
              		{
              			if(execution.Order != null
              				&& execution.Order.Name == "entry"
              				&& execution.Order.OrderState == OrderState.Filled)
              			{
              				entry = null;
              				exit = exit = ExitLongStop(lowAtCross, "exit", "entry");
              			}
              			if(execution.Order != null
              				&& execution.Order.Name == "exit"
              				&& execution.Order.OrderState == OrderState.Filled)
              			{
              				exit = null;
              			}
              		}
              So far, in my experience, NinjaTrader has been inconsistent between versions, on how it treats null IOrders. For that reason, I always check for nullity of IOrders before I assign anything to them. YMMV.

              Comment


                #8
                SetStopLoss or ExitLongLimit Reply to Thread

                Hello PatrickH.

                See attachment.

                The Execute exit still isn't working.



                Thank you
                Attached Files

                Comment


                  #9
                  Hello marcyomatheus,

                  Thank you for your response.

                  Can you attach the code you are using for this example?

                  Comment


                    #10
                    Hi PatrickH.

                    This is your code, that you sent me.

                    Thank you.

                    Comment


                      #11
                      marcyomatheus,

                      If this is NinjaTrader_PatrickHs' code then it won't exit at that price. It was designed to exit out when the bar crosses below the SMA, Submit the order with the current bars' close price, which was 14.02 and filled at that price on the next bar. This was the expected result of his code.
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Cal.

                        So, is there a problem or fail with code ?

                        You did some test with code?

                        Do you want the code that patrickH sent me?

                        Thank you!

                        Comment


                          #13
                          marcyomatheus,

                          Yes, please send me the code that you are using in this instance.
                          Cal H.NinjaTrader Customer Service

                          Comment


                            #14
                            SetStopLoss or ExitLongLimit Reply to Thread

                            Ok. Cal

                            See attachments.

                            I think that problem is in region variable, but I don't know.

                            Thank you for your help!
                            Attached Files

                            Comment


                              #15
                              marcyomatheus,,

                              What is going on here is that your orders are being cancelled due to expired orders.

                              This is part of the managed approach logic, in which working orders will be cancelled at the end of the next bar if the order is not resubmitted.

                              In your scenario the StopMarket was expired and when the Limit order was placed from the cross condition it was filled.

                              You will want to have logic in place that will help keep these orders alive for however long you need them -
                              http://www.ninjatrader.com/support/f...ad.php?t=19169
                              Cal H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by love2code2trade, 04-17-2024, 01:45 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post love2code2trade  
                              Started by alifarahani, Today, 09:40 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post alifarahani  
                              Started by junkone, Today, 11:37 AM
                              3 responses
                              15 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by pickmyonlineclass, Today, 12:23 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post pickmyonlineclass  
                              Started by frankthearm, Yesterday, 09:08 AM
                              12 responses
                              44 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Working...
                              X