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

Setting an internal Array as variable

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

    #16
    How to address this issue ?

    Within a loop, I must use the variable Close[0] and at the same time, assigning a certain value to a List array variable. Despite all is well declared previously, and it compiles flawlessly, when it comes to act, a runtime error appears:

    Error on calling 'OnBarUpdate' method for strategy 'QWS/d86de07fec8c46148c329c5d2a13fd08': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    Obviously is cause I'm trying to use Close[0] object with a List array variable at the same time. How to declare properly variables, procedures...etc in order to avoid "interference" and being able to run the script?

    Here is the structure of the script:

    Code:
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Q
        /// </summary>
    	using System.Collections.Generic;
    	
        [Description("Q")]
        public class QWS : Strategy
        {
            #region Variables
    		// all variables here
    	#endregion		
    		
    
    	List<int> cable = new List<int>();
    		
            protected override void Initialize()
            { 
    	        SetStopLoss("", CalculationMode.Ticks, pnllimit, false);
                    CalculateOnBarClose = true;	
    		BarsRequired=31;
    		DisconnectDelaySeconds = 130; 
        		MaxRestartMinutes = 30;
            	MaxRestartAttempts = 120; 
        		ConnectionLossHandling = ConnectionLossHandling.KeepRunning ; 
        		RestartDelaySeconds = 2;
            }
    
    		protected override void OnPositionUpdate(IPosition position)
    		{
    			// do my stuff
    		}
            
            protected override void OnBarUpdate()						
            {		
    			if ( Bars.BarsSinceSession<= BarsRequired )				
    			{				
    				return;			
    			}
    						
    			for ( int d = 1; d <= t; d++ )				
    			{				
    				// do calculations using Close[0] and assign values to List Array
    			}
    			
    			if( A)			
    			{			
    				EnterLong(qty);		
    			}
    			if( B)			
    			{				
    				EnterShort(qty);
    			}				
            }							
    			
    		protected override void OnTermination()						
    		{						
    			// Do final
    		}
    Any help here?
    Last edited by pstrusi; 04-16-2015, 03:18 PM.

    Comment


      #17
      Originally posted by pstrusi View Post
      How to address this issue ?

      Within a loop, I must use the variable Close[0] and at the same time, assigning a certain value to a List array variable. Despite all is well declared previously, and it compiles flawlessly, when it comes to act, a runtime error appears:

      Error on calling 'OnBarUpdate' method for strategy 'QWS/d86de07fec8c46148c329c5d2a13fd08': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      Obviously is cause I'm trying to use Close[0] object with a List array variable at the same time. How to declare properly variables, procedures...etc in order to avoid "interference" and being able to run the script?

      Here is the structure of the script:

      Code:
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// Q
          /// </summary>
      	using System.Collections.Generic;
      	
          [Description("Q")]
          public class QWS : Strategy
          {
              #region Variables
      		// all variables here
      	#endregion		
      		
      
      	List<int> cable = new List<int>();
      		
              protected override void Initialize()
              { 
      	        SetStopLoss("", CalculationMode.Ticks, pnllimit, false);
                      CalculateOnBarClose = true;	
      		BarsRequired=31;
      		DisconnectDelaySeconds = 130; 
          		MaxRestartMinutes = 30;
              	MaxRestartAttempts = 120; 
          		ConnectionLossHandling = ConnectionLossHandling.KeepRunning ; 
          		RestartDelaySeconds = 2;
              }
      
      		protected override void OnPositionUpdate(IPosition position)
      		{
      			// do my stuff
      		}
              
              protected override void OnBarUpdate()						
              {		
      			if ( Bars.BarsSinceSession<= BarsRequired )				
      			{				
      				return;			
      			}
      						
      			for ( int d = 1; d <= t; d++ )				
      			{				
      				// do calculations using Close[0] and assign values to List Array
      			}
      			
      			if( A)			
      			{			
      				EnterLong(qty);		
      			}
      			if( B)			
      			{				
      				EnterShort(qty);
      			}				
              }							
      			
      		protected override void OnTermination()						
      		{						
      			// Do final
      		}
      Any help here?
      What exactly are you doing in your For Loop?

      Also, remember that this error means that any items you are indexing, AKA using [value], the value is too big. Meaning that index value is larger than the actual collection size you are trying to access

      Comment


        #18
        Originally posted by Calonious View Post
        What exactly are you doing in your For Loop?

        Also, remember that this error means that any items you are indexing, AKA using [value], the value is too big. Meaning that index value is larger than the actual collection size you are trying to access
        Trying to calculate some values using Close[0] and assigning some results to an internal List array variable, that actually is being indexed orderly within that loop. It sound simple but it's not. There must be some obvious error but I still don't realize it

        Comment


          #19
          I meant like an example or share your code.

          Comment


            #20
            Originally posted by Calonious View Post
            I meant like an example or share your code.

            Something like this dumb example:

            Code:
            for ( int d = 1; d <= t; d++ )				
            {				
            	if ( Close[0]-Close[d] > 0 )
                    {
                          Listb[d]=1;
                    }			
            }
            Then other calculations
            Last edited by pstrusi; 04-16-2015, 04:13 PM.

            Comment


              #21
              Pstrusi,

              This is the exact opposite how DataSeries will work. Essentially, if you don't set a value for that bar update, the DataSeries will replace it with 0, or Close[0] price( I don't remember which off the top of my head) for that bar.

              Since you have now created this out of sync series, or async series if you will, you need to insert your own indexes.

              The great thing about .Insert() is that it handles the shifting of the other indexes appropriately like with the DataSeries.Set()

              https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

              Exampe -
              Instead of this
              Listb[d]=1;

              try this
              Listb.Insert(0, 1)

              Also, ensure that you do have enough data for your loop call for the Close[d] prices.

              Comment


                #22
                Thank you very much Calonious for your help, very useful. Yes, I'll have to think better how to work at the same time with sync and out-of-sync series for certain calculations in an efficient way, it's obvious that I got this wrong.

                Regards

                Comment


                  #23
                  The problem is the Bars object index of NT that interferes with that way to assign values to a List array variable, thus maybe the solution is to create manually vertical dimensions through others procedures, without altering the original index.

                  Comment


                    #24
                    Originally posted by pstrusi View Post
                    Something like this dumb example:

                    Code:
                    for ( int d = 1; d <= t; d++ )				
                    {				
                    	if ( Close[0]-Close[d] > 0 )
                            {
                                  Listb[d]=1;
                            }			
                    }
                    Then other calculations
                    You are referencing Close[d]. Make sure you arrange for CurrentBar to always be at least as big as the referenced index "d". That means that you need to control the value of "t".

                    Comment


                      #25
                      Thanks Koganam, yes, I'm aware of that minimum necessary condition but it's not the cause of the runtime error. It seems instead that the NT internal Dataseries of prices doesn't make possible to assign values to out-of-sync List variable. The way I'm working this is with some kind adding dimensional variable set of data.

                      Comment


                        #26
                        Originally posted by pstrusi View Post
                        Thanks Koganam, yes, I'm aware of that minimum necessary condition but it's not the cause of the runtime error. It seems instead that the NT internal Dataseries of prices doesn't make possible to assign values to out-of-sync List variable. The way I'm working this is with some kind adding dimensional variable set of data.
                        The point of OOP is that objects are independent entities that contain their data and the methods to manipulate said data. The Bars object cannot interfere with assignations to a different object. Resolve your indexing issue, based on the object that is being indexed.

                        Your issue is assigning data to an index that does not exist. That is the only issue that you need to resolve in this case. Unfortunately, the error message that you are getting is very generic, and could relate to anything that is indexed and to which you are referring. Your desire for secrecy if understandable, but if you really want a resolution, you are probably going to have to reveal everything that relates to this part of your code.
                        1. What is "t", for example?
                        2. How big is it?
                        3. Is it a fixed size?
                        4. What is the capacity of your List? If you have not defined its capacity, direct assignation can be problematic: Use List.Add() or List.Insert(), which are the standard way of adding elements to a List.

                        And you are still going to have to ensure that the Close index to which you refer is valid.

                        Comment


                          #27
                          Originally posted by koganam View Post
                          The point of OOP is that objects are independent entities that contain their data and the methods to manipulate said data. The Bars object cannot interfere with assignations to a different object. Resolve your indexing issue, based on the object that is being indexed.

                          Your issue is assigning data to an index that does not exist. That is the only issue that you need to resolve in this case. Unfortunately, the error message that you are getting is very generic, and could relate to anything that is indexed and to which you are referring. Your desire for secrecy if understandable, but if you really want a resolution, you are probably going to have to reveal everything that relates to this part of your code.
                          1. What is "t", for example?
                          2. How big is it?
                          3. Is it a fixed size?
                          4. What is the capacity of your List? If you have not defined its capacity, direct assignation can be problematic: Use List.Add() or List.Insert(), which are the standard way of adding elements to a List.

                          And you are still going to have to ensure that the Close index to which you refer is valid.
                          I appreciate a lot your advanced knowledge and insight Koganam. Yes you're right, using LIST was not the smart way to do what I wanted, thus I had to figure out others solutions, what I did fortunately.

                          Once again, thanks

                          Best of luck Pal

                          Comment


                            #28
                            The simplest, quickest and final solution to my requirement is using a traditional ARRAY.
                            Here, all the basic steps done in order to be able to work properly as needed

                            1. Declaring ARRAY

                            #region Variables
                            double[] acu = new double[initial];
                            #endregion

                            2. Updating the Array dimension that actually will be used

                            protected override void OnStartUp()
                            {
                            acu = new double[oscillation+1];
                            }

                            3. After use if "reseting or clearing" the Array is needed, then proceed with:

                            Array.Clear(acu, 0, wpoint.Length);

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Mestor, 03-10-2023, 01:50 AM
                            16 responses
                            388 views
                            0 likes
                            Last Post z.franck  
                            Started by rtwave, 04-12-2024, 09:30 AM
                            4 responses
                            31 views
                            0 likes
                            Last Post rtwave
                            by rtwave
                             
                            Started by yertle, Yesterday, 08:38 AM
                            7 responses
                            29 views
                            0 likes
                            Last Post yertle
                            by yertle
                             
                            Started by bmartz, 03-12-2024, 06:12 AM
                            2 responses
                            22 views
                            0 likes
                            Last Post bmartz
                            by bmartz
                             
                            Started by funk10101, Today, 12:02 AM
                            0 responses
                            7 views
                            0 likes
                            Last Post funk10101  
                            Working...
                            X