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 cmtjoancolmenero, Yesterday, 03:58 PM
      3 responses
      23 views
      0 likes
      Last Post cmtjoancolmenero  
      Started by Brevo, Today, 01:45 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by rjbtrade1, 11-30-2023, 04:38 PM
      2 responses
      72 views
      0 likes
      Last Post DavidHP
      by DavidHP
       
      Started by suroot, 04-10-2017, 02:18 AM
      5 responses
      3,021 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by Stanfillirenfro, Today, 07:23 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Working...
      X