• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 RockRiver, Today, 10:38 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post RockRiver  
                            Started by 7robert, Today, 10:07 AM
                            1 response
                            4 views
                            0 likes
                            Last Post NinjaTrader_ChrisL  
                            Started by Irukandji, 06-10-2019, 06:13 AM
                            4 responses
                            35 views
                            0 likes
                            Last Post Irukandji  
                            Started by Uwe1981, Today, 03:41 AM
                            1 response
                            18 views
                            0 likes
                            Last Post NinjaTrader_Kate  
                            Started by emuns, Today, 09:36 AM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_PaulH  
                            Working...
                            X