Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unexpected error in State.Transition in Strategy

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

    Unexpected error in State.Transition in Strategy

    Hi,

    I am coding a Strategy that runs a loop over part of the loaded historical data in State.Transition. I put this code here since it only needs to be run once prior to going to live data.

    I have successfully tested this code in an Indicator, but I am getting an index out of range error when I try this in a Strategy (chart). The code checks to make sure there are sufficient bars before attempting the loop.

    It works OK if I stick to the default value (as set in State.Defaults) for the loopback period, but if I then edit the Strategy settings and change this value to any other value and then try to enable the Strategy again, I get the standard index out of range error message.

    EDIT: I just tried to put the loop code in OnBarUpdate() - same behaviour as above.
    Last edited by ours_solaire; 07-19-2016, 07:05 PM. Reason: updated information

    #2
    Hello,

    Thank you for the post.

    What you are describing doesn't sound like anything specific I could assist with based solely on a description.

    Do you perhaps have a sample you could provide of the logic being used to see what may be causing the error?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      sample code

      OK, here you go. This code is simply a copy of the MA crossover strategy provided by NT, with a few mods to demonstrate the problem.

      Code:
      	public class SampleMAC2 : Strategy
      	{
      		private SMA smaFast;
      		private SMA smaSlow;
      
      		protected override void OnStateChange()
      		{
      			if (State == State.SetDefaults)
      			{
      				Description	= NinjaTrader.Custom.Resource.NinjaScriptStrategyDescriptionSampleMACrossOver;
       				Name		= "SampleMAC2";
      				Fast		= 10;
      				Slow		= 25;
                      // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
                      // See the Help Guide for additional information
                      IsInstantiatedOnEachOptimizationIteration = false;
                  }
      			else if (State == State.Configure)
      			{
      				smaFast = SMA(Fast);
      				smaSlow = SMA(Slow);
      
      				smaFast.Plots[0].Brush = Brushes.Orange;
      				smaSlow.Plots[0].Brush = Brushes.Green;
      
      				AddChartIndicator(smaFast);
      				AddChartIndicator(smaSlow);
      				
      				AddDataSeries("SPY", Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
      			}
      			else if (State == State.Transition)
      			{
      
      			}
      			
      			
      		}
      
      		protected override void OnBarUpdate()
      		{
      			if (CurrentBar < Slow)
      				return;
      			double sum = 0;
      			for (int i = Slow; i > -1; i--) //Loop starts at bar = Period -> forwards
      			{
      				sum += Math.Pow(Closes[0][i] - Closes[1][i],2);
      				
      			}
      			sum = Math.Sqrt(sum);
      			Print(sum.ToString("f0"));
      		}
      Let me save you some work - the problem lies with the second instrument - the index error occurs when we try to access, for example, Closes[1][0].

      This code will work fine on initial defaults remember - you need to go and change a parameter such as the Slow period to a different value, then re-enable for the error to occur.

      Comment


        #4
        Hello,

        Thank you for the sample, this helps.

        I do see that you are adding an additional series, and also that you are only checking if the Primary series has enough bars.

        Please try replacing the CurrentBar check you are using with the following:

        Code:
        if(CurrentBars[0] < Slow || CurrentBars[1] < Slow) return;
        This ensures both series have enough bars to accommodate the Slow, if so the logic can continue. I see this has resolved the error on my end but you would need to test using the settings you are as well.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          see the output file.

          Hi,

          I agree that your modified check is good practice, but that is not the issue here. Something is getting corrupted when I edit this strategy and change the Slow parameter, causing the second bars object to not load properly.

          I have added the following line right at the start of OnBarUpdate() :

          Code:
          Print(CurrentBars[0]+"   "+CurrentBars[1]);
          I then loaded a new copy of the strategy to a chart and enabled it with the default settings. Then I disabled it, edited it and changed the Slow parameter to a different number. Then I tried to re-enable it. Your modified line prevents the error, but results in no output of the looped sum value since the CurrentBars[1] values are -1.

          I have saved the output to a file, the first section is the initial run, where both CurrentBars increment in sequence - normal behaviour. The second section is after I change the Slow parameter and re-enable. Notice the CurrentBars[1] value stuck at -1, hence the Print() method is never reached.

          Please can you confirm this unexpected behaviour at your end, thanks.
          Attached Files

          Comment


            #6
            Hello,

            Thank you for the reply.

            Unfortunately I am not seeing the same with what has been provided so far. I believe I have followed the same steps you have outlined but I am seeing only what is expected.

            I have attached the output of my test, I did remove the print from the loop and appended the BarsInProgress to the print to make it more readable.

            The only changes are the following two items:
            Code:
            Print(CurrentBars[0]+"   "+CurrentBars[1] [B]+ "   " + BarsInProgress[/B]);
            if(CurrentBars[0] < Slow || CurrentBars[1] < Slow) return;
            	
            double sum = 0;
            for (int i = Slow; i > -1; i--) //Loop starts at bar = Period -> forwards
            {
            	sum += Math.Pow(Closes[0][i] - Closes[1][i],2);
            				
            }
            sum = Math.Sqrt(sum);
            [B]//Print(sum.ToString("f0"));[/B]
            Could you attach the .cs file for the test you are conducting currently, along with the exact setup steps like primary instrument settings, time frames, menus used to create the chart and apply strategy etc.

            Also what connection provider are you currently using for this test? I would like to test using the exact same connection and identical setup to ensure this is not specific to a certain connection or if I had missed a crucial step.

            I look forward to being of further assistance.
            Attached Files
            JesseNinjaTrader Customer Service

            Comment


              #7
              Seems there is a problem with my installation then I guess. I installed B12 over B11, so I guess I will have to do a fresh install again...

              Strangely, I ran the test on a different machine also running B12, with the same result.

              I will attach the .cs file for you to look at in any case. I was running it with QQQ as primary chart instrument, using Kinetick free end of day connection, 500 days of daily bars.
              Attached Files

              Comment


                #8
                Hello,

                Thank you for the reply.

                Running this script seems to still produce a correct result in between switching the properties.

                Before trying an un install, this could potentially be a cache or data problem. To check for this, you could clear the historcial data for the instrument and then start up the platform and re download the data.

                You could do that by doing the following:

                Exit the platform.
                Open the folder: Documents\NinjaTrader 8\db
                Remove the contents of the cache folder.
                Remove the contents of the day folder
                Now start the platform and re connect and retest.

                If you see a different result after this, it was likely a caching or historical data problem. If you start the platform in between removing the contents of each folder it would be easier to see which if this is the problem.

                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  OK, so I have tried both your database clearing ideas, un-installed B12 and re-installed a fresh copy.

                  I am still getting the same unexpected results. I then tried connecting to Yahoo data to see if that made any different, but no, same result.

                  I have no more ideas to try - what next?

                  Comment


                    #10
                    different strategy

                    Hi,

                    I have tried to modify a different Strategy to see if it makes a difference to the outcome - I took the MultiInstrument sample strategy and added a Period parameter to the properties, then your Print() line that prints the currentbars and barsinprogress.

                    See the text file for the output. Notice that the initial run cycles through both bars objects as expected, but after modifying the Period variable, we see that BarsInProgress == 1 is never run!

                    Please can you verify that you are testing using the same version as me - 8.0.0.12 64-bit Standard. If you are, and you are still not seeing this problem, perhaps you need to remotely access my machine to test for yourself...
                    Attached Files

                    Comment


                      #11
                      would appreciate a reply...

                      Could someone please reply to my last post from last week? I can't move forward with my strategy until this issue is resolved. If this turns out to be a bug, which I suspect it is, it needs to be put on the list ASAP.

                      Am happy to schedule a time for you to check this out directly on my system.

                      Thanks in anticipation.

                      Comment


                        #12
                        Hello,

                        Thank you for the reply.

                        I noted that you had first connected to the Kinetick end of day free connection but are now testing with yahoo and have similar results. To ensure we are indeed doing the same test, please send an email to platformsupport[at]ninjatrader[dot]com.

                        Please include both a link to this thread and also the steps you are currently using to produce the prints along with the test script that produced the result you are seeing.

                        I will first test the exact steps on my end to see if I can reproduce this, if not we can schedule a time to remote in to collect data.

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by bortz, 11-06-2023, 08:04 AM
                        47 responses
                        1,602 views
                        0 likes
                        Last Post aligator  
                        Started by jaybedreamin, Today, 05:56 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post jaybedreamin  
                        Started by DJ888, 04-16-2024, 06:09 PM
                        6 responses
                        18 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by Jon17, Today, 04:33 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post Jon17
                        by Jon17
                         
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        12 views
                        0 likes
                        Last Post Javierw.ok  
                        Working...
                        X