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

Variables region - "for" loop

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

    Variables region - "for" loop

    All,

    Is it possible to use a "for" loop in the variables region to define multple variables. For example:

    Code:
    for (int i=0; i<3; i++)
    {
      private int ("CurrentBar" + i.ToString());
      private int ("CurrentPeak" + i.ToString());
    }
    To produce six variables:
    CurrentBar0
    CurrentPeak0
    CurrentBar1
    CurrentPeak1
    CurrentBar2
    CurrentPeak2

    Regards
    Shannon

    #2
    Shansen, from what I recall, creating variables in the Initialize() section programmatically isn't supported.

    I'm not sure it even works... does your code correctly initialize the variables? Can you access the variables later on?
    AustinNinjaTrader Customer Service

    Comment


      #3
      Austin,

      Thank you for the quick reply.

      The loop structure is intended to rest in the variables region ( not the initialize section ).
      I think the issue could revolve around my joining of two strings as the variable name.
      Any assistance would be appreciated.

      Thanks again
      Shannon

      Comment


        #4
        Hi Shannon,

        what you are trying to do is not possible with a compiler language like c# (perhaps with a interpreter language like tcl/tk?). A for-loop is a construct to be executed during runtime. But a variable has to be recognised by the compiler during compile time. To group variables you could use arrays:

        int CurrentBar[3];
        int CurrentPeak[3];
        ...
        CurrentBar[0] = 42;
        CurrentPeak[0] = 84;

        Regards
        Ralph

        Comment


          #5
          Ralph,

          Thanks for the reply. Bummer it can't be done.

          Elsewhere in in the NT Strategy code I do use ArrayLists.

          This request was aimed at reducing some of repetitive nature of defining and parsing values to the strategy from one indicator that is run on four different time-frames. Once the parsed values are assigned to variable in the strategy, they are evaluated.

          Gauging from your reply, would it be possible to separately define variables:
          Code:
          #region Variables
            private int CurrentBar0;
            private int CurrentPeak0;
            private int CurrentBar1;
            private int CurrentPeak1;
            private int CurrentBar2;
            private int CurrentPeak2;
            etc...
          #endregion
          And then work with these variables and a "looped" fashion:

          Code:
          protected override void OnBarUpdate()
          {
            for (int i=0; i<3; 1++)
            {  
              if (BarsInProgress == i && FirstTickOfBar)
              {
                ("CurrentBar" + i.ToString()) = indicator value;
                ("CurrentPeak" + i.ToString()) = indicator value;
              }
            }
            ...
            ...
            ...
          }
          If this is possible what syntax should be used to join the strings to form the variable name?

          As always, thanks a bunch
          Shannon
          Last edited by Shansen; 11-29-2009, 04:29 AM.

          Comment


            #6
            You should swap the lines for(int...) and if(BarsInProgress...), otherwhise you waste cpu resources just for nothing with every invocation of OnBarUpdate().

            How do you fill variable "indicator value"? Is it a fixed value by source code, or set by a public property, or ...?

            Regards
            Ralph

            Comment


              #7
              Ralph,

              Apologies, I have amended the code in the prior post to reflect my intentions. Within the loop structure the test should be BarsInProgress == i.

              The indicator value are either public DataSeries or public IntSeries. Does this impact your response? And importantly, will the syntax ("CurrentBar" + i.ToString()) have the expected results?

              Thanks again
              Shannon

              Comment


                #8
                Hi Shannon,

                For some reason I missed your concept of BarsInProgres. Suspect I was still sleeping while typing this morning.

                You can't neither construct a variable during runtime nor can't you construct a reference to a variable during runtime. That may be possible with an interpreter language but not with a compiler language. But that does not make sense anyway, c# provides you with different containers for efficient data storage instead of dealing with expensive string operations during runtime. But I think for your application a simple array is the best choice, the dimension has to be the number of time frames used. Here is a second attempt:
                Code:
                private int CurrentBar[4];
                private int CurrentPeak[4];
                 
                protected override void OnBarUpdate()
                {
                  if (FirstTickOfBar)
                  {
                    if (BarsInProgress >= 0 && BarsInProgress <=3)
                    {
                      CurrentBar[BarsInProgress] = indicator value;
                      CurrentPeak[BarsInProgress] = indicator value;
                    }
                    else
                    {
                      Print("Unexpected value for BarsInProgress: " + BarsInProgress);
                      Log("Unexpected value for BarsInProgress: " + BarsInProgress);
                    }
                  }
                  ...
                  ...
                  ...
                }
                I was asking for the "indicator value" because it is possible to fill an array during variable initialisation. But that's not your intention obviously.

                Regards
                Ralf

                Comment


                  #9
                  Ralph,

                  Thanks for help. I'm pretty sure that would have never crossed my mind.

                  In implementing:
                  Code:
                  #region Variables
                    private int[] CurrentBars;
                    private int[] CurrentPeak;
                  #endregion
                  
                  protected override void Initialize()
                  {
                    CurrentBars = new int[4];
                    CurrentPeak = new int[4];
                  
                    for (int x = 0; x < 4; x++)
                    {
                      CurrentBars[x] = 0;
                      CurrentPeak[x] = 0;
                    }
                  }
                  
                  protected override void OnBarUpdate()
                  {
                    if (FirstTickOfBar)
                    {
                      if (BarsInProgress >= 0 && BarsInProgress <= 3) 
                      {
                        Print (BarsInProgress + " - " + Times[BarsInProgress][0].ToString());
                        CurrentBars[BarsInProgress] = CurrentBar;
                        CurrentPeak[BarsInProgress] = Indicator.Property[1];
                      }
                      else
                      {
                        Print("Unexpected value for BarsInProgress: " + BarsInProgress);
                      }
                    }
                  }
                  The Log shows an error-> Error on calling 'OnBarUpdate' ...: Index was out of range. Must be non-negative and less than the size of the collection Parameter name: index

                  The four time frames are in order of BarArray:
                  0 - 15min futures
                  1 - 5min cash
                  2 - 5min futures
                  3 - 1min futures

                  I could be off-track trying to debug this issue, but I'm pretty sure these BarArrays should not present a problem in their current descending order.

                  As always any assistance would be appreciated.

                  Regards
                  Shannon

                  Comment


                    #10
                    Hi Shannon,

                    you do not need to initialise your arrays with zero. C# promises that this is done during construction.
                    Cannot see an issue either. Do your print statements work? Guess that would allow a conclusion.

                    Regards
                    Ralph

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by helpwanted, Today, 03:06 AM
                    1 response
                    7 views
                    0 likes
                    Last Post sarafuenonly123  
                    Started by Brevo, Today, 01:45 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post Brevo
                    by Brevo
                     
                    Started by aussugardefender, Today, 01:07 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post aussugardefender  
                    Started by pvincent, 06-23-2022, 12:53 PM
                    14 responses
                    242 views
                    0 likes
                    Last Post Nyman
                    by Nyman
                     
                    Started by TraderG23, 12-08-2023, 07:56 AM
                    9 responses
                    384 views
                    1 like
                    Last Post Gavini
                    by Gavini
                     
                    Working...
                    X