Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 DJ888, 04-16-2024, 06:09 PM
      4 responses
      12 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by terofs, Today, 04:18 PM
      0 responses
      11 views
      0 likes
      Last Post terofs
      by terofs
       
      Started by nandhumca, Today, 03:41 PM
      0 responses
      7 views
      0 likes
      Last Post nandhumca  
      Started by The_Sec, Today, 03:37 PM
      0 responses
      3 views
      0 likes
      Last Post The_Sec
      by The_Sec
       
      Started by GwFutures1988, Today, 02:48 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Working...
      X