Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Loop through a list of variables

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

    Loop through a list of variables

    short question.

    I have 10 variables the user sets up in the strategy definitions. TradeSize1, TradeSize2, etc..

    I want to be able to loop through the variables.
    something like bellow

    for ( int i = 1; i<10 ; i++)
    {
    .....
    tradesize = ("TradeSize" + i.ToString());
    ....
    }

    I searched in the forums and apparently is seems it can not be done. PLS confirm ?

    so I though I will create a LIST and populate it with the values during the
    State == State.Configure stage.

    whats the best programing practice for such a case ? It looks like I need to loop through the same variables, or just have 10 separate lines of :

    TradeSizeList[1] = TradeSize1;
    ....

    #2
    ? anyone please

    Comment


      #3
      Hello dadarara,

      Thank you for your patience, sorry for the delay.

      Correct, You will need to store the variables in an array and loop through them. Since these are public properties input by the user, it is not possible to load them into an array right away. You will need to implement a way to load them in 'by hand'. I have made an example of this below:

      I am assuming that TradeSize is of type int.

      Code:
      public class SampleLoop : Indicator {
      
      
      
      protected override void OnStateChange()
      {
      
       private int[] myArr;
      
       if (State == State.SetDefaults)
       {
        ...
        TradeSize0 = 1;
        TradeSize1 = 2;
        TradeSize2 = 3;
        TradeSize3 = 4;
        TradeSize4 = 5;
        TradeSize5 = 6;
        TradeSize6 = 7;
        TradeSize7 = 8;
        TradeSize8 = 9;
        TradeSize9 = 10;
      
        myArr = new int[10];
      
      
       }
       else if (State == State.Configure)
       {
        ...
      
        myArr[0] = TradeSize0;
      
        myArr[1] = TradeSize1;
      
        myArr[2] = TradeSize2;
      
        myArr[3] = TradeSize3;
      
        myArr[4] = TradeSize4;
      
        myArr[5] = TradeSize5;
      
        myArr[6] = TradeSize6;
      
        myArr[7] = TradeSize7;
      
        myArr[8] = TradeSize8;
      
        myArr[9] = TradeSize9;
        
      }
      
      protected void OnBarUpdate(){
       //now you can loop through the data. 
       for(int i = 0; i < 10; ++i){
        Print("TradeSize"+ i + " equals " + myArr[i])
       }
      }
      Please let us know if we may be of any further assistance.
      Chris L.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by RookieKiwi, Today, 06:53 PM
      0 responses
      2 views
      0 likes
      Last Post RookieKiwi  
      Started by kazisl, Today, 05:52 PM
      0 responses
      4 views
      0 likes
      Last Post kazisl
      by kazisl
       
      Started by andrewjoseph939, Today, 02:39 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by trader252, Today, 02:09 PM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by mballagan, Today, 01:19 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Working...
      X