Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

backtest error

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

    backtest error

    If i make backtest, it will show in puput window this message, could somebody help me, please.

    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 ArankaTicks : Strategy
        {
             #region Variables
    		private IOrder Order 	= null; // This variable holds an object representing our entry order
    		private IOrder stopOrder 	= null; // This variable holds an object representing our stop loss order
    		private IOrder targetOrder 	= null; // This variable holds an object representing our profit target order
    		private double diff = 1; // Default setting for Diff
           
            private int profitTarget = 1; // Default setting for ProfitTarget
    		
    	    private IOrder entryOrder1 	= null; // This variable holds an object representing our stop loss order
            // User defined variables (add any user defined variables below)
            #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()
            {
                CalculateOnBarClose = true;
    			 MaxProcessedEvents =10000;
    			
    		}
    		
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
    			
            {
    			if (Low[0] == CurrentDayOHL().CurrentLow[0]&& (Order != null) && Close[0] > Open[0] && High[0] < Variable0 - Diff * TickSize)
    			{
    				/* The entryOrder object will take on a unique ID from our EnterLong()
    				that we can use later for order identification purposes in the OnOrderUpdate() method. */
    				Order = EnterLongStop(DefaultQuantity, High[0] + 1 * TickSize, "Long");
    			}
    			
    			// Submit an entry limit order if we currently don't have an entry order open
    			if (Low[0] == CurrentDayOHL().CurrentLow[0] && Order == null && Close[0] > Open[0])
    			{
    				/* The entryOrder object will take on a unique ID from our EnterLong()
    				that we can use later for order identification purposes in the OnOrderUpdate() method. */
    				Order = EnterLongStop(DefaultQuantity, High[0] + 1 * TickSize, "Long");
    				
    				
    			}
    			
    			
    			
    			
            }
    
            /// <summary>
            /// Called on each incoming order event
            /// </summary>
            protected override void OnOrderUpdate(IOrder order)
            {
    			
    			
    			if (Order != null && Order.OrderState == OrderState.Filled) 
    			{
    //				ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize);
    				ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize, "Flat", "Long" );
    //				ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize, "", "");
    //				if (Close[0] > Position.AvgPrice + ProfitTarget* TickSize)
    //				{
    //					ExitLong();
    //
    //				}	
    //				
    				
    				
    			}
    			
    			
    			
    				
    				
    			
    
    			if (Order != null && Order == order) 
    			{	
    				
    				if (Order.OrderState == OrderState.Cancelled && order.Filled == 0)
    				{
    					
    				
    				Order = null;
    				}
    			}
            }
    			
    		
    		
    		
    		/// <summary>
            /// Called on each incoming execution
            /// </summary>
            protected override void OnExecution(IExecution execution)
    			
            {
    			if (Order != null && Order == execution.Order ) 
    			Variable0 = execution.Price;
    		    Print(Variable0);
    			
    		}
    
            /// <summary>
            /// 
            /// </summary>
            protected override void OnPositionUpdate(IPosition position)
            {
    			
    			if 	(Position.MarketPosition == MarketPosition.Flat)
    		{
    				
    				Order = null;
    		}
    			
    			DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
            }
    
            #region Properties
    		[Description("")]
            [GridCategory("Parameters")]
            public double Diff
            {
                get { return diff; }
                set { diff = Math.Max(0, value); }
            }
    		
            
    
            [Description("")]
            [GridCategory("Parameters")]
            public int ProfitTarget
            {
                get { return profitTarget; }
                set { profitTarget = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Attached Files

    #2
    Hello tranta,

    I am unable to reproduce this error. (I did have MaxProcessedEvents =10000; set but I didn't get an error with this, so I removed it and still did not get an error).

    Attached is an export of the code that I am using.

    I have tested this on the ES 09-14 1 Minute data.

    May I have a screenshot of the parameters you are using with this strategy?

    To send a screenshot press Alt + PRINT SCREEN to take a screen shot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.

    For detailed instructions please visit the following link
    http://take-a-screenshot.org/

    Are you running this through the Market Replay, or the Strategy Analyzer?
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I used Strategy Analyzer.
      Attached Files

      Comment


        #4
        Image

        here is image
        Attached Files

        Comment


          #5
          Hi tranta,

          There is an endless loop in your code within OnOrderUpdate().

          if (Order != null && Order.OrderState == OrderState.Filled)
          {
          ExitLongLimit(Position.AvgPrice + ProfitTarget* TickSize, "Flat", "Long" );
          }

          This does not check to see what order is triggering OnOrderUpdate. So when the entry order (Order) Order.OrderState becomes OrderState.Filled the exit order is submitted. This triggers OnOrderUpdate again because the exit order has been submitted. The OrderState of Order (the entry order) is still OrderState.Filled so this again submits the exit which again triggers OnOrderUpdate again in an endless loop until the entry order is no longer set to filled.

          Try instead:
          if (Order != null && order == Order && Order.OrderState == OrderState.Filled)
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            I made change, but now it doesnt take profittarget.

            Comment


              #7
              Hi tranta,

              This is because the Exit order is not being set repeatly now and is being cancelled when it isn't filled on the next bar.

              To prevent this use the liveUntilCancelled bool as true.
              ExitLongLimit(0, true, 1, Position.AvgPrice + ProfitTarget* TickSize, "Flat", "Long");

              http://www.ninjatrader.com/support/h...tlonglimit.htm
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Still doeasnt work

                I made change, but it still doesnt work.
                #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 ArankaTicksSMADaily : Strategy
                {
                #region Variables
                private IOrder Order = null; // This variable holds an object representing our entry order
                private IOrder stopOrder = null; // This variable holds an object representing our stop loss order
                private IOrder targetOrder = null; // This variable holds an object representing our profit target order
                private double diff = 1; // Default setting for Diff

                private int profitTarget = 1; // Default setting for ProfitTarget
                private int smaPerioda = 1;

                private IOrder entryOrder1 = null; // This variable holds an object representing our stop loss order
                // User defined variables (add any user defined variables below)
                #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()
                {
                CalculateOnBarClose = true;
                MaxProcessedEvents =10000;
                Add(PeriodType.Day, 1);

                }

                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()

                {
                if (BarsInProgress == 1)
                return;

                if (Low[0] == CurrentDayOHL().CurrentLow[0]&& (Order != null) && Close[0] > Open[0] && High[0] < Variable0 - Diff * TickSize)
                {
                /* The entryOrder object will take on a unique ID from our EnterLong()
                that we can use later for order identification purposes in the OnOrderUpdate() method. */
                Order = EnterLongStop(DefaultQuantity, High[0] + 1 * TickSize, "Long");
                }

                // Submit an entry limit order if we currently don't have an entry order open
                if (Low[0] == CurrentDayOHL().CurrentLow[0] && Order == null && Close[0] > Open[0] && Close[0] >= SMA(BarsArray[1], SmaPerioda)[0])
                {
                /* The entryOrder object will take on a unique ID from our EnterLong()
                that we can use later for order identification purposes in the OnOrderUpdate() method. */
                Order = EnterLongStop(DefaultQuantity, High[0] + 1 * TickSize, "Long");


                }




                }

                /// <summary>
                /// Called on each incoming order event
                /// </summary>
                protected override void OnOrderUpdate(IOrder order)
                {


                // if (Order != null && Order.OrderState == OrderState.Filled)
                if (Order != null && order == Order && Order.OrderState == OrderState.Filled)
                {

                ExitLongLimit(0, true, 1, Position.AvgPrice + ProfitTarget* TickSize, "Flat", "Long");
                }

                if (Order != null && Order == order)
                {

                if (Order.OrderState == OrderState.Cancelled && order.Filled == 0)
                {


                Order = null;
                }
                }
                }




                /// <summary>
                /// Called on each incoming execution
                /// </summary>
                protected override void OnExecution(IExecution execution)

                {
                if (Order != null && Order == execution.Order )
                Variable0 = execution.Price;
                Print(Variable0);

                }

                /// <summary>
                ///
                /// </summary>
                protected override void OnPositionUpdate(IPosition position)
                {

                if (Position.MarketPosition == MarketPosition.Flat)
                {

                Order = null;
                }

                DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
                }

                #region Properties
                [Description("")]
                [GridCategory("Parameters")]
                public double Diff
                {
                get { return diff; }
                set { diff = Math.Max(0, value); }
                }



                [Description("")]
                [GridCategory("Parameters")]
                public int ProfitTarget
                {
                get { return profitTarget; }
                set { profitTarget = Math.Max(1, value); }
                }

                [Description("")]
                [GridCategory("Parameters")]
                public int SmaPerioda
                {
                get { return smaPerioda; }
                set { smaPerioda = Math.Max(1, value); }
                }
                #endregion
                }
                }
                Attached Files

                Comment


                  #9
                  Hi tranta,

                  The exit order is named Flat. In the screenshot there are many many exits with the name Flat. How is this not working?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Last enter to position is in 2008, and backtest is to 2014. Pt is 110USD and last 5 trades have PT 46k to 55k USD

                    Comment


                      #11
                      Hi tranta,

                      I am not able to reproduce this.

                      Attached is the code I am using from your post.

                      I'm testing on the GC 12-14 with Diff set to 190 and ProfitTarget set to 11 and on a 5 minute series.

                      The SmaPeriods I do not have as this is not in the code of your first post.

                      Are you able to reproduce the issue using the attached file?
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        I am sorry, Im donkye, here is 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 ArankaTicksSMADaily : Strategy
                        {
                        #region Variables
                        private IOrder Order = null; // This variable holds an object representing our entry order
                        private IOrder stopOrder = null; // This variable holds an object representing our stop loss order
                        private IOrder targetOrder = null; // This variable holds an object representing our profit target order
                        private double diff = 1; // Default setting for Diff

                        private int profitTarget = 1; // Default setting for ProfitTarget
                        private int smaPerioda = 1;

                        private IOrder entryOrder1 = null; // This variable holds an object representing our stop loss order
                        // User defined variables (add any user defined variables below)
                        #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()
                        {
                        CalculateOnBarClose = true;
                        MaxProcessedEvents =10000;
                        Add(PeriodType.Day, 1);

                        }

                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()

                        {
                        if (BarsInProgress == 1)
                        return;

                        if (Low[0] == CurrentDayOHL().CurrentLow[0]&& (Order != null) && Close[0] > Open[0] && High[0] < Variable0 - Diff * TickSize)
                        {
                        /* The entryOrder object will take on a unique ID from our EnterLong()
                        that we can use later for order identification purposes in the OnOrderUpdate() method. */
                        Order = EnterLongStop(DefaultQuantity, High[0] + 1 * TickSize, "Long");
                        }

                        // Submit an entry limit order if we currently don't have an entry order open
                        if (Low[0] == CurrentDayOHL().CurrentLow[0] && Order == null && Close[0] > Open[0] && Close[0] >= SMA(BarsArray[1], SmaPerioda)[0])
                        {
                        /* The entryOrder object will take on a unique ID from our EnterLong()
                        that we can use later for order identification purposes in the OnOrderUpdate() method. */
                        Order = EnterLongStop(DefaultQuantity, High[0] + 1 * TickSize, "Long");


                        }




                        }

                        /// <summary>
                        /// Called on each incoming order event
                        /// </summary>
                        protected override void OnOrderUpdate(IOrder order)
                        {


                        // if (Order != null && Order.OrderState == OrderState.Filled)
                        if (Order != null && order == Order && Order.OrderState == OrderState.Filled)
                        {

                        ExitLongLimit(0, true, 1, Position.AvgPrice + ProfitTarget* TickSize, "Flat", "Long");
                        }

                        if (Order != null && Order == order)
                        {

                        if (Order.OrderState == OrderState.Cancelled && order.Filled == 0)
                        {


                        Order = null;
                        }
                        }
                        }




                        /// <summary>
                        /// Called on each incoming execution
                        /// </summary>
                        protected override void OnExecution(IExecution execution)

                        {
                        if (Order != null && Order == execution.Order )
                        Variable0 = execution.Price;
                        Print(Variable0);

                        }

                        /// <summary>
                        ///
                        /// </summary>
                        protected override void OnPositionUpdate(IPosition position)
                        {

                        if (Position.MarketPosition == MarketPosition.Flat)
                        {

                        Order = null;
                        }

                        DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
                        }

                        #region Properties
                        [Description("")]
                        [GridCategory("Parameters")]
                        public double Diff
                        {
                        get { return diff; }
                        set { diff = Math.Max(0, value); }
                        }



                        [Description("")]
                        [GridCategory("Parameters")]
                        public int ProfitTarget
                        {
                        get { return profitTarget; }
                        set { profitTarget = Math.Max(1, value); }
                        }

                        [Description("")]
                        [GridCategory("Parameters")]
                        public int SmaPerioda
                        {
                        get { return smaPerioda; }
                        set { smaPerioda = Math.Max(1, value); }
                        }
                        #endregion
                        }
                        }


                        Attached Files
                        Last edited by tranta; 08-20-2014, 05:25 AM.

                        Comment


                          #13
                          Hi tranta,

                          Have you tested the script I have posted?

                          Did you find this to be working incorrectly?
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Yes, i did, but ssame ressult. Please look at my post 4:22am
                            Attached Files

                            Comment


                              #15
                              Hi tranta,

                              You are getting different results than I am (and far fewer trades).

                              Attached is a screenshot of how this looks on my end.

                              I would like you to try clearing your historical data and redownloading it for this test.

                              Please use the steps I have provided below to delete your historical data and cache:
                              • Close NinjaTrader
                              • Open (My) Documents/NinjaTrader 7/db/cache/
                              • Delete all files within this folder
                              • Go up one directory to the /db/ folder
                              • Repeat this procedure for the minute folder as well to delete your downloaded historical data
                              • Restart NinjaTrader
                              • Connect
                              • Rerun this test
                              Attached Files
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              649 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              576 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X