Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Variables in a loop

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

    Variables in a loop

    Hi,

    I have defined 10 variables: "v1" to "v10". I would like to use them in a loop.

    My x for a loop is between 1 and 10. Every loop is next data series (BarsArray). Each variable is for each data series: v1 for BarsArray[1], v2 for BarArray[2], ... etc.

    Is it possible to define such variables in a loop (vx)? First loop (BarrArray[1] would use v1, second loop v2, ... etc.

    Please support.

    #2
    Hello kucharek,
    You can, but its difficult to say for sure unless I know the exact reason for doing so.

    If you are trying to access data from the different bar series, then we would suggest to filter your code using the BarsInProgress property.




    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Since you only have 10 variables, you could combine a 'for' loop and 'switch' statement..

      The `if` and `switch` statements provide branching logic in C#. You use `if, `else` and `switch` to choose the path your program follows.

      Comment


        #4
        Hi,

        Thank you both for your support.

        The only reason I would like to do so is to make code smaller. I am using BarsInProgress now but code for every BarsInProgress is 2 000 lines long which gives 20 000 lines of code in total.

        'Switch' statement is new for me so I will try this out.

        If you have any other suggestions regarding the above reason please let me know. Thanks again.

        Comment


          #5
          Hello kucharek,
          You can create a private method. A sample code will be like

          Code:
          private void DoStuff(int barInProgress, double variable)
          {
          	if (BarsArray[barsInProgress] == *****)  //compare the BarsArray for example with some value etc
          	{
          		//do stuff
          	}
          }
          
          protected override void OnBarUpdate()
          {
          	
          	if (BarsInProgress == 1)
          	{
          		DoStuff(1, variable1)
          	}
          	else if (BarsInProgress == 2)
          	{
          		DoStuff(2, variable2);
          	}
          }
          DoStuff is the private method and variable1, variable2 is the variable you declare.

          JoydeepNinjaTrader Customer Service

          Comment


            #6
            Thanks. I have made first steps with that.

            I do not know how to remember variable1 and variable 2 after DoStuff. After bar update I have 0 value again. Can you help?

            Comment


              #7
              Hello kucharek,
              You can use a function or you can use reference in the methods to get the values. Please refer here to know more http://msdn.microsoft.com/en-us/library/14akc2c7.aspx

              Unfortunately methods functions are more of a C# query and we can only provide limited support on this.
              JoydeepNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              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
              1 view
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              6 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              10 views
              0 likes
              Last Post bltdavid  
              Started by alifarahani, Today, 09:40 AM
              6 responses
              41 views
              0 likes
              Last Post alifarahani  
              Working...
              X