NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 07-13-2008, 02:02 PM   #1
auspiv
Member
 
Join Date: Jul 2007
Location: steamboat, co
Posts: 52
Thanks: 0
Thanked 0 times in 0 posts
Default 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?
auspiv is offline  
Reply With Quote
Old 07-13-2008, 02:30 PM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

Declare your 'startTime' variable outside of the OnBarUpdate() method. You can put it in the "Variables" region for convenience.
NinjaTrader_Ray is offline  
Reply With Quote
Old 07-13-2008, 02:47 PM   #3
auspiv
Member
 
Join Date: Jul 2007
Location: steamboat, co
Posts: 52
Thanks: 0
Thanked 0 times in 0 posts
Default

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?
auspiv is offline  
Reply With Quote
Old 07-13-2008, 03:12 PM   #4
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Default

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();
NinjaTrader_Ray is offline  
Reply With Quote
Old 07-13-2008, 04:45 PM   #5
auspiv
Member
 
Join Date: Jul 2007
Location: steamboat, co
Posts: 52
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
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
auspiv is offline  
Reply With Quote
Old 07-13-2008, 05:15 PM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-13-2008, 07:52 PM   #7
auspiv
Member
 
Join Date: Jul 2007
Location: steamboat, co
Posts: 52
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
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
auspiv is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable Stop latkinso General Programming 1 07-09-2008 02:53 PM
Best variable to access to find last order entry price? scriabinop23 General Programming 3 02-14-2008 03:04 PM
T&S filter(block) zq655 General Programming 4 01-22-2008 10:47 AM
variable days simone General Programming 1 08-27-2007 09:22 AM
Variable rounded Richard Von Indicator Development 3 08-03-2007 06:55 AM


All times are GMT -6. The time now is 02:46 AM.