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 space_trader, Today, 12:52 AM
      1 response
      19 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by ybhx0315, Yesterday, 09:06 PM
      0 responses
      15 views
      0 likes
      Last Post ybhx0315  
      Started by parkinsonbr, Yesterday, 08:23 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by fxRichard, Today, 04:35 AM
      2 responses
      14 views
      0 likes
      Last Post fxRichard  
      Started by fernlicht, Today, 01:59 AM
      12 responses
      61 views
      0 likes
      Last Post NinjaTrader_Jason  
      Working...
      X