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 wooofad, Today, 05:38 PM
      0 responses
      3 views
      0 likes
      Last Post wooofad
      by wooofad
       
      Started by caacapital, Today, 10:10 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ShawnB  
      Started by DiverG20, Today, 10:23 AM
      0 responses
      21 views
      0 likes
      Last Post DiverG20  
      Started by SuneSorgenfrei, Today, 09:29 AM
      0 responses
      21 views
      0 likes
      Last Post SuneSorgenfrei  
      Started by dodoei, Today, 07:24 AM
      0 responses
      14 views
      0 likes
      Last Post dodoei
      by dodoei
       
      Working...
      X