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

access variable outside of if block?

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

    access variable outside of if block?

    Hello all, today I started writing a strategy (not that it matters now) to see how long a bar takes to complete. Here is the code inside OnBarUpdate:
    Code:
    if (FirstTickOfBar)
                {
                    DateTime startTime = DateTime.Now;
                    Print("first tick of bar: " + Close[0] + " at: " + startTime);
                }
                
         if (Bars.TickCount == 17)
                {
                    DateTime stopTime = DateTime.Now;
                    Print("bar ends: " + stopTime);
                    TimeSpan duration = stopTime - startTime;
                    Print("seconds:" + duration.TotalSeconds);
                    //Console.WriteLine("milliseconds:" + duration.TotalMilliseconds);
    
                }
    the problem is the compiler gives me an error "the name 'startTime' does not exist in the current context". now i dug around a little, and came to learn that variables declared in an if statement are inaccessible from outside that if statement. then i started fooling around with declaring a variable before the if statement, but didn't come up with any good solutions, because of the format of the whole DateTime thing.

    so my question is: does anyone have any suggestions for allowing me to access the variable 'startTime' from the 1st if block while in the 2nd if block?

    #2
    Declare your 'startTime' variable outside of the OnBarUpdate() method. You can put it in the "Variables" region for convenience.
    RayNinjaTrader Customer Service

    Comment


      #3
      yes, i realize thats what i needed to do, but i was wondering if anyone has any phenomenal ideas to store DateTime.Now in a (single?) variable.

      it can be stored as a string, but then i'll need to parse it to use it again. it could also be stored as many integers, but again, the need to parse it arises.

      so now i guess the question changed to: how should i declare a variable only to store DateTime.Now in it, to be retrieved later?

      Comment


        #4
        Curious, why would you want to store a string to only parse it again later instead of storing the actual DateTime structure itself?

        If you want a string...

        string starStime = DateTime.Now.ToString();
        RayNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Ray View Post
          Curious, why would you want to store a string to only parse it again later instead of storing the actual DateTime structure itself?

          If you want a string...

          string starStime = DateTime.Now.ToString();
          hmm... at this point you've probably noticed i'm not very familiar with programming. i would like to store the actual DateTime structure, but i can't figure out how to declare the variable so i can use 'startTime' in two if blocks. if there is some declaration for storing the actual DateTime structure, i'd like to know it.

          thanks for your patience, auspiv

          Comment


            #6
            I'm not sure what you are trying to do exactly. Please describe the context in which you will use the DateTime. You could always just pass along the DateTime object itself. Create a variable private DateTime currentTime = DateTime.Now or something like that.

            You can also try using the ToTime() or ToDay() methods. They will provide you with int values representing either the time or the date.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by Josh View Post
              Create a variable private DateTime currentTime = DateTime.Now or something like that.
              thats what i figured out. just a simple
              Code:
              DateTime startTime;
              worked in the vars section

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by traderqz, Today, 12:06 AM
              10 responses
              18 views
              0 likes
              Last Post traderqz  
              Started by algospoke, 04-17-2024, 06:40 PM
              5 responses
              46 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by arvidvanstaey, Today, 02:19 PM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Started by mmckinnm, Today, 01:34 PM
              3 responses
              5 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by f.saeidi, Today, 01:32 PM
              2 responses
              9 views
              0 likes
              Last Post f.saeidi  
              Working...
              X