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

Issue with SL/TP

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

    #16
    I would like to update my last post because I didn't notice I have to write the variable in a OnOrderUpdate() section, so I tried to put this after OnBarUpdate() part. I got an error : "myOrder" doesn"t exit

    Code:
      protected override void OnBarUpdate()
            {
    	
    	
    
    			//////Order
    			if (Condition == 1 && openTrades==0 ) {
    				entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "one", "Enter Long");
    				stoporder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "one", "Stop");
    				targetorder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "one", "Target");
    			} else if (openTrades ==0) {
    				entryOrder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, Target, StopL, "two", "Enter Sell");
    				stoporder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "two", "Stop2");
    				targetorder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "two", "Target2");
    			}
    
    }
    
    
    
      protected override void OnOrderUpdate(IOrder order)
    			{
       				 if(myOrder.Name == "Enter Long" && myOrder.OrderState == OrderState.Filled)
      				 openTrades++;
    				
    				if(myOrder.Name == "Enter Sell" && myOrder.OrderState == OrderState.Filled)
      				 openTrades++;
    
      		  		 if(myOrder.Name == "Stop" && myOrder.OrderState == OrderState.Filled) 
      				openTrades--;
    				if(myOrder.Name == "Target" && myOrder.OrderState == OrderState.Filled) 
      				openTrades--;
    				if(myOrder.Name == "Stop2" && myOrder.OrderState == OrderState.Filled) 
      				openTrades--;
    				if(myOrder.Name == "Target2" && myOrder.OrderState == OrderState.Filled) 
      				openTrades--;
    			}

    Comment


      #17
      Hello,

      I see what's going on. In your call to OnOrderUpdate(), you are passing in an IOrder object named "order," but you are referencing "myOrder." You will need to change each reference to "myOrder" to just "order," so that it refers to the object passed in to the call to OnBarUpdate().

      Please let me know if I can assist further.
      Dave I.NinjaTrader Product Management

      Comment


        #18
        Thanks Dave ! It's fixed, cool

        Well, it seems my target profit/stop loss orders are still wrong-written. In the strategy analyzer, I've just my first entry order and then it's not closed until the last candle.

        Code:
        entryOrder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "two", "Enter Sell");
        stoporder2 = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0,StopL, "two", "Stop2");
        targetorder2 = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Target, 0, "two", "Target2");
        I guess the "BarInProgressIndex" of my stop order and my target order could be the reason ? Should I write "infinite" or something like this and not 0 ?

        Comment


          #19
          Your BarsInProgressIndex looks good, because the index will always be 0 in a single-bars script. However, your OCO ID may be causing the issue. I recommend using " " as the OCO ID for the entry order, then keeping the OCO ID as it is for the stop loss and profit target. With all three using the same OCO ID, the stop loss and profit target orders are probably being cancelled when the entry order is filled.
          Dave I.NinjaTrader Product Management

          Comment


            #20
            Thanks ! You were right, the OCO was the problem.

            I fixed another thing : the variable which know how many orders are running isn't a good idea because we could have 10 orders running at the same time but it doesn't mean the first order opened will be the first order closed. As a result we still don't really know which "order name" is free or not.

            I decided to track each order name one by one to fix this. Look at my code :

            Code:
            protected override void OnBarUpdate()
                    {
            			
            		
            			
            	
            
            			//////Order
            			if (Condition == 1 && Trade0==0 ) {
            				entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "Enter Long");
            				stoporder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "a", "Stop");
            				targetorder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "a", "Target");
            			} else if (Trade0 ==0) {
            				entryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "", "Enter Sell");
            				stoporder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0,StopL, "b", "Stop");
            				targetorder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Target, 0, "b", "Target");
            			}
            			
            			
            			if (Condition == 1 && Trade1==0 && Trade0==1 ) {
            				entryOrder1 = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "Enter Long1");
            				stoporder1 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "c", "Stop1");
            				targetorder1 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "c", "Target1");
            			} else if (Trade1==0  && Trade0==1 ) {
            				entryOrder1 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "", "Enter Sell1");
            				stoporder1 = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0,StopL, "d", "Stop1");
            				targetorder1 = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Target, 0, "d", "Target1");
            			}
            			
            			
            
                    }
            		
            		
            		
            		   protected override void OnOrderUpdate(IOrder order)
            			{
               				 if(order.Name == "Enter Long" && order.OrderState == OrderState.Filled)
              				 Trade0++;
            				
            				if(order.Name == "Enter Sell" && order.OrderState == OrderState.Filled)
              				 Trade0++;
            				
            				 if(order.Name == "Enter Long1" && order.OrderState == OrderState.Filled)
              				 Trade1++;
            				
            				if(order.Name == "Enter Sell1" && order.OrderState == OrderState.Filled)
              				 Trade1++;
            				
            				
              		  		 if(order.Name == "Stop" && order.OrderState == OrderState.Filled) 
              				Trade0--;
            				
            				if(order.Name == "Target" && order.OrderState == OrderState.Filled) 
              				Trade0--;
            				
            				if(order.Name == "Stop1" && order.OrderState == OrderState.Filled) 
              				Trade1--;
            				
            				if(order.Name == "Target1" && order.OrderState == OrderState.Filled) 
              				Trade1--;
            				
            			
            			}

            However, I got this in the strategy analyzer (picture)

            How is it possible to have a entry order at the middle of a candle bar with this code ? Really strange I don't understand.... Target and stop aren't good too. Do you see a mistake ?
            Attached Files
            Last edited by After; 03-27-2015, 06:06 AM.

            Comment


              #21
              Have you added intra-bar granularity in your script by basing your executions off of a 1-tick data series, by chance, or does your snippet represent all of your order-entry logic?
              Dave I.NinjaTrader Product Management

              Comment


                #22
                No my snippet represent all of my entry-logic order...

                Here you can see the whole code :

                Code:
                #region Using declarations
                using System;
                using System.ComponentModel;
                using System.Diagnostics;
                using System.Drawing;
                using System.Drawing.Drawing2D;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Data;
                using NinjaTrader.Indicator;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Strategy;
                #endregion
                
                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                    /// <summary>
                    /// Enter the description of your strategy here
                    /// </summary>
                    [Description("Enter the description of your strategy here")]
                    public class Trendy : Strategy
                    {
                        #region Variables
                        // Wizard generated variables
                        private int myInput0 = 1; // Default setting for MyInput0
                        // User defined variables (add any user defined variables below)
                        #endregion
                		
                		double Condition, Target, StopL, Trade0, Trade1,Trade2,Trade3,Trade4,Trade5,Trade6,Trade7,Trade8,Trade9;
                		private IOrder entryOrder = null;
                		private IOrder entryOrder1 = null;
                		private IOrder entryOrder2 = null;
                		private IOrder entryOrder3 = null;
                		private IOrder entryOrder4 = null;
                		private IOrder entryOrder5 = null;
                		private IOrder entryOrder6 = null;
                		private IOrder entryOrder7 = null;
                		private IOrder entryOrder8 = null;
                		private IOrder entryOrder9 = null;
                		private IOrder stoporder = null;
                		private IOrder stoporder1 = null;
                		private IOrder stoporder2 = null;
                		private IOrder stoporder3 = null;
                		private IOrder stoporder4 = null;
                		private IOrder stoporder5 = null;
                		private IOrder stoporder6 = null;
                		private IOrder stoporder7 = null;
                		private IOrder stoporder8 = null;
                		private IOrder stoporder9 = null;
                		private IOrder targetorder = null;
                		private IOrder targetorder1 = null;
                		private IOrder targetorder2 = null;
                		private IOrder targetorder3 = null;
                		private IOrder targetorder4 = null;
                		private IOrder targetorder5 = null;
                		private IOrder targetorder6 = null;
                		private IOrder targetorder7 = null;
                		private IOrder targetorder8 = null;
                		private IOrder targetorder9 = null;
                
                		
                
                        /// <summary>
                        /// This method is used to configure the strategy and is called once before any strategy method is called.
                        /// </summary>
                        protected override void Initialize()
                        {
                            CalculateOnBarClose = true;
                			Unmanaged = true;
                			Trade0=0;
                			Trade1=0;
                
                			
                        }
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                			
                			
                			///////Condition
                			if (X) {
                				Condition = 1;
                				StopL = Y;
                				Target = L;
                			} else {
                				Condition = 0;
                				StopL = Y;
                				Target = L;
                			}
                			
                	
                
                			//////Order
                			if (Condition == 1 && Trade0==0 ) {
                				entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "Enter Long");
                				stoporder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "a", "Stop");
                				targetorder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "a", "Target");
                			} else if (Trade0 ==0) {
                				entryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "", "Enter Sell");
                				stoporder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0,StopL, "b", "Stop1");
                				targetorder = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Target, 0, "b", "Target1");
                			}
                			
                			
                			if (Condition == 1 && Trade1==0 && Trade0==1 ) {
                				entryOrder1 = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "", "Enter Long1");
                				stoporder1 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "c", "Stop2");
                				targetorder1 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "c", "Target2");
                			} else if (Trade1 ==0  && Trade0==1 ) {
                				entryOrder1 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "", "Enter Sell1");
                				stoporder1 = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, 1, 0,StopL, "d", "Stop3");
                				targetorder1 = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Target, 0, "d", "Target3");
                			}
                			
                			
                
                        }
                		
                		
                		
                		   protected override void OnOrderUpdate(IOrder order)
                			{
                   				 if(order.Name == "Enter Long" && order.OrderState == OrderState.Filled)
                  				 Trade0++;
                				
                				if(order.Name == "Enter Sell" && order.OrderState == OrderState.Filled)
                  				 Trade0++;
                				
                				 if(order.Name == "Enter Long1" && order.OrderState == OrderState.Filled)
                  				 Trade1++;
                				
                				if(order.Name == "Enter Sell1" && order.OrderState == OrderState.Filled)
                  				 Trade1++;
                				
                				
                  		  		 if(order.Name == "Stop" && order.OrderState == OrderState.Filled) 
                  				Trade0--;
                				
                				if(order.Name == "Target" && order.OrderState == OrderState.Filled) 
                  				Trade0--;
                				
                				 if(order.Name == "Stop1" && order.OrderState == OrderState.Filled) 
                  				Trade0--;
                				
                				if(order.Name == "Target1" && order.OrderState == OrderState.Filled) 
                  				Trade0--;
                				
                				if(order.Name == "Stop2" && order.OrderState == OrderState.Filled) 
                  				Trade1--;
                				
                				if(order.Name == "Target2" && order.OrderState == OrderState.Filled) 
                  				Trade1--;
                				
                				if(order.Name == "Stop3" && order.OrderState == OrderState.Filled) 
                  				Trade1--;
                				
                				if(order.Name == "Target3" && order.OrderState == OrderState.Filled) 
                  				Trade1--;
                				
                			
                			}
                
                        #region Properties
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int MyInput0
                        {
                            get { return myInput0; }
                            set { myInput0 = Math.Max(1, value); }
                        }
                        #endregion
                    }
                }

                You have another example of "intra-bar" entry on the Dax in the strategy analyzer
                Attached Files

                Comment


                  #23
                  Are you trading to your Sim101 account, by chance? If so, it is possible that some kind of database corruption is causing the fills to be off. If so, please try to reset your Sim101 account via Tools > Options > Database > "Reset Simulator," and see if you get any more odd fills like that.
                  Dave I.NinjaTrader Product Management

                  Comment


                    #24
                    Thanks yes I got a Sim101 account !

                    I got "Reset DB" and "Reset Instruments" but not "Reset simulator" anywhere?
                    Attached Files

                    Comment


                      #25
                      Hello After,

                      Thank you for your response.

                      Please go to Tools > Options > Simulator > Reset > select the account and OK.

                      Comment


                        #26
                        Thanks Pat and Dave, unfortunately it's still the same thing, look at the picture.

                        I got the feeling the second buying trade (at the openning of the 3rd february candle, the big green candle where there is an intra-day trade) closes the first buying trade (opened at the openning of the previous candle), and then Target of these two trades messed up together during the intra-candle of the 3rd february.... Gosh it makes no sense
                        Attached Files

                        Comment


                          #27
                          Hello,

                          Would you be willing to provide me a full export of your code, so I can test further on my end? You can email it to platformsupport [at] ninjatrader [dot] com and reference ticket # 1283075, if you would like to keep it private.
                          Dave I.NinjaTrader Product Management

                          Comment


                            #28
                            Thanks Dave ! You got a mail

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by cre8able, Yesterday, 01:16 PM
                            3 responses
                            11 views
                            0 likes
                            Last Post cre8able  
                            Started by ChartTourist, Today, 08:22 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post ChartTourist  
                            Started by LiamTwine, Today, 08:10 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post LiamTwine  
                            Started by Balage0922, Today, 07:38 AM
                            0 responses
                            5 views
                            0 likes
                            Last Post Balage0922  
                            Started by JoMoon2024, Today, 06:56 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post JoMoon2024  
                            Working...
                            X