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

Place 2 opposite StopLimit order at once.

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

    Place 2 opposite StopLimit order at once.

    Hello.
    I'm trying to place 2 opposite StopLimit orders at the same time:
    My code:
    Code:
    protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < 1)
    			return;
    
    			EnterShortStopMarket(Convert.ToInt32(DefaultQuantity), (Close[1] + (-10 * TickSize)) , ""); 
    			EnterLongStopMarket(Convert.ToInt32(DefaultQuantity), (Close[1] + (10 * TickSize)) , "");
    		}
    Unfortunately strategy places one order only.
    How to fix this?

    #2
    Hello Czarek,

    Thank you for your note.

    You would not be able to submit both enter long limit and enter short limit orders at the same time due to Internal Order Handling Rules. You can view the Internal Order Handling Rules at the following link:



    If you wish to work with Unmanaged Orders you may do so and the orders would not be ignored in this case: https://ninjatrader.com/support/help...d_approach.htm

    Please let me know if I may be of further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      I do not get it clearly.
      Do you mean that I can place 2 opposite StopLimit orders using Unmanaged Approach only but cannot with Managed Approach?

      Comment


        #4
        I'm trying to use the Unmanaged Orders but strategy do not place orders.
        Can you advice me what is wrong with my code?
        Code:
        private Order entryOrder = null;
        		protected override void OnBarUpdate()
        		    {
        		if (Open[0] < Close[0] && entryOrder == null)
        		return;                   
        		    	
        		SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopLimit, 1, 0, Close[0] - 10 * TickSize, "", "First Short");
        		SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopLimit, 1, 0, Close[0] + 10 * TickSize, "", "First Long");
        		   }

        Comment


          #5
          Hello Czarek,

          Correct, you would have to use the unmanaged approach to submit a 2 opposite stop limit orders.

          Have you added teh following under State == State.SetDefaults?

          Code:
          IsUnmanaged = true;
          If so, please upload a full copy of the strategy so we may test on our end.

          To export a NinjaScript from NinjaTrader 8 do the following:
          From the Control Center window select Tools -> Export -> NinjaScript...
          Click Add>Select the indicator>OK>Export.
          Then attach that file you saved; under My Docs>NT8>Bin>Custom>Select the downloaded .zip file.

          I look forward to your reply.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            I attached my strategy.
            Attached Files

            Comment


              #7
              Hello Czarek,

              The strategy you attached is a compiled assembly.

              Please export the strategy not as a compiled assembly, ie do not check "Export as Compiled Assembly".

              I look forward to your reply.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Is now OK?
                Attached Files

                Comment


                  #9
                  I update strategy and now orders are placed. There is another problem.
                  How to prevent strategy to place orders on each incoming bar.
                  I tried to make bool variable but in OnBarUpdate() it changes state before orders are placed.
                  In OnOrderUpdate() does not work at all.
                  This method works in Ninja 7.
                  Can you tell how to do it in Ninja 8?
                  My code below and strategy in the attachment.
                  Code:
                   private Order myEntryOrderLong = null;
                  		private Order myEntryOrderShort = null;
                  		protected override void OnBarUpdate()
                  		
                  		{
                  			Print(Time[0] + "lock1entries =" + lock1entries.ToString());
                  			
                  			if (Open[0] < Close[0] && myEntryOrderLong == null && myEntryOrderShort == null && lock1entries == false)
                  //			return;
                         
                  			{SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.StopMarket, 1, 0, (Close[0] - 10 * TickSize), "", "First Short");
                  			SubmitOrderUnmanaged(0, OrderAction.Buy, OrderType.StopMarket, 1, 0, (Close[0] + 10 * TickSize), "", "First Long");
                   	    	lock1entries = true;
                  			}
                  			
                  		}
                             
                   
                          protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                         {
                                  if (myEntryOrderLong != null && order.Name == "First Long" && myEntryOrderLong != null && order.Name == "First Short")
                              {
                                  // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
                                  // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
                                  //myentryOrderlong = order;
                  				myEntryOrderShort = order;
                  				myEntryOrderLong = order;
                  				
                  				if (order.Name == "First Short")
                  				{lock1entries = true;}
                              }
                          }
                  Attached Files

                  Comment


                    #10
                    Hello Czarek,

                    From the code you provided it would be expected your actions within OnOrderUpdate would not run.

                    Within OnOrderUpdate, I would suggest adding print statements to check whether your conditions are becoming true. I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
                    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


                    Please see the following example of OnOrderUpdate and take note of the assignment of order objects.


                    Regarding preventing strategy placing orders on each incoming bar, could you please clairify what it is you are trying to do?

                    I look forward to your reply.
                    Alan P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ScottWalsh, Today, 06:52 PM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by ScottW, Today, 06:09 PM
                    1 response
                    4 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by ftsc2022, 10-25-2022, 12:03 PM
                    5 responses
                    256 views
                    0 likes
                    Last Post KeyonMatthews  
                    Started by Board game geek, 10-29-2023, 12:00 PM
                    14 responses
                    244 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Waxavi, 04-19-2024, 02:10 AM
                    4 responses
                    56 views
                    0 likes
                    Last Post sonia0101  
                    Working...
                    X