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 11-16-2010, 03:45 AM   #1
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default null bars

I am programming (for NT 6.5) a 1-min bar NinjaScript strategy for an instrument that is quite likely to have less than 1 tick per minute during the time period of interest.

Say that in a 10-minute period there a three 1-min periods in which no tick occurs.

In relation to the above situation, if I reference Close[10] in my code will this refer to the close of the bar approx 10 minutes ago, or to the close of the bar approx 13 minutes ago (as there were three 1-min bars that had no OHLC, since no ticks occurred during the minute)?
AnotherTrader is offline  
Reply With Quote
Old 11-16-2010, 04:34 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

AnotherTrader, NinjaTrader is event not time based, as such if there's no bar there's no Close price for you to 'grab' at that point - it would give you the close price of the bar 13 mins ago then.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 11-17-2010, 11:38 AM   #3
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

OK, thanks.
So, when needing to know how many bars ago correspond with a particular point in time (e.g. say it’s now 13h00 EST, and I want know which bar corresponds to the bar which closed at 12h00 EST), it is not enough to make use of say Close[59] for the closing price of that bar; I guess I’ll need to do something like start a counter at 12h00 EST that updates on each OnBarUpdate, and then use Close[counter] for the bar that closed at 12h00 EST.
Is that what you would recommend?
AnotherTrader is offline  
Reply With Quote
Old 11-17-2010, 11:43 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

You can use the GetBar() method for this task -

http://www.ninjatrader-support.com/H...V6/GetBar.html
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 11-17-2010, 11:48 AM   #5
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

OK, thanks.

If the timestamp specifies a time after the first bar and before the last bar, but refers to a time at which no bar exists (for the reason previous, i.e. there were no ticks during that timeframe) what does GetBar() return?
AnotherTrader is offline  
Reply With Quote
Old 11-17-2010, 12:00 PM   #6
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

It would then return the next bar satisfying it - for example I try getting todays 9:02 bar which would not exist in cash session on ES running on EST - so GetBar() returns then the 9:35 bar (on 5 min chart).
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-10-2011, 11:13 AM   #7
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
You can use the GetBar() method for this task -

http://www.ninjatrader-support.com/H...V6/GetBar.html

The above link references the following example:
Code:
 
// Check that its past 9:45 AM 
if (ToTime(Time[0]) >= ToTime(9, 45, 00)) 
{ 
// Calculate the bars ago value for the 9 AM bar for the current day 
int barsAgo = GetBar(new DateTime(2006, 12, 18, 9, 0, 0)); 

// Print out the 9 AM bar closing price 
Print("The close price on the 9 AM bar was: " + Close[barsAgo].ToString());
I need to modify the above to calculate the bars ago value of the prior day's 9AM bar, and to do this on a rolling basis. How would I modify the above?

Thanks.
AnotherTrader is offline  
Reply With Quote
Old 05-10-2011, 11:31 AM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

You would for example use DateTime.Today.AddDays(-1) for yesterdays date, but you would need to introduce custom logic to cover weekends here.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-10-2011, 11:35 AM   #9
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
You would for example use DateTime.Today.AddDays(-1) for yesterdays date, but you would need to introduce custom logic to cover weekends here.
... and I guess market holidays, too.

Many thanks!
AnotherTrader is offline  
Reply With Quote
Old 05-10-2011, 12:03 PM   #10
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

How is DateTime.Today.AddDays(-1) inserted into the following in the simplest case (ie ignoring weekends and holidays)?

Code:
 
// Check that its past 9:45 AM 
if (ToTime(Time[0]) >= ToTime(9, 45, 00)) 
{ 
// Calculate the bars ago value for the 9 AM bar for the current day 
int barsAgo = GetBar(new DateTime(2006, 12, 18, 9, 0, 0)); 
 
// Print out the 9 AM bar closing price 
Print("The close price on the 9 AM bar was: " + Close[barsAgo].ToString());
AnotherTrader is offline  
Reply With Quote
Old 05-10-2011, 04:57 PM   #11
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hello AnotherTrader,


If you want that for yesterday using your PC clock as reference:

int barsAgo = GetBar(DateTime.Today.AddDays(-1));

DateTime.Today is your computer clock date/time so this works for the most current session. If you want it to work historically, then you would use instead Time[0] which is the bar's time stamp.


int barsAgo = GetBar(Time[0].AddDays(-1));
NinjaTrader_RyanM is offline  
Reply With Quote
Old 05-11-2011, 04:48 AM   #12
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

Quote:
Originally Posted by NinjaTrader_RyanM View Post
.... If you want it to work historically, then you would use instead Time[0] which is the bar's time stamp.

int barsAgo = GetBar(Time[0].AddDays(-1));
But how would I specify 09h00 am EST on the previous day?

The following doesn't seem to work...

Code:
int barsAgo = GetBar(Time[0].AddDays(-1), 9, 0, 0);
Thanks for the help.
Last edited by AnotherTrader; 05-11-2011 at 04:59 AM.
AnotherTrader is offline  
Reply With Quote
Old 05-11-2011, 05:31 AM   #13
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

AnotherTrader, I would give something like this a try :

Code:
int myYear  = DateTime.Today.AddDays(-1).Year;
int myMonth = DateTime.Today.AddDays(-1).Month;
int myDay   = DateTime.Today.AddDays(-1).Day;
				
int barsAgo = GetBar(new DateTime(myYear, myMonth, myDay, 9, 0, 0));
Last edited by NinjaTrader_Bertrand; 05-11-2011 at 05:46 AM.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-11-2011, 05:36 AM   #14
AnotherTrader
Senior Member
 
Join Date: Aug 2009
Posts: 269
Thanks: 41
Thanked 8 times in 8 posts
Send a message via ICQ to AnotherTrader
Default

Thanks.

And to be able to backtest, would this be modified to something as follows?

Code:
int myYear  = Time[0].AddDays(1).Year;
int myMonth = Time[0].AddDays(1).Month;
int myDay   = Time[0].AddDays(1).Day;
 
int barsAgo = GetBar(new DateTime(myYear, myMonth, myDay, 9, 0, 0));
AnotherTrader is offline  
Reply With Quote
Old 05-11-2011, 05:47 AM   #15
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,018 times in 999 posts
Default

Correct, you would need to work with the historical bar timestamp property then for backtesting.

Note: I just changed to AddDays(-1) in my posted previous snippet, as you would want to subtract a day, not really add.
NinjaTrader_Bertrand 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
Bars.GetDayBar(1).Time returning null drolles General Programming 9 01-18-2011 02:42 AM
Plots and Null Value TAJTrades General Programming 2 05-12-2009 05:28 AM
Bars == NULL oferr Strategy Analyzer 4 01-31-2008 07:57 AM
Testing for null zoltran General Programming 7 11-25-2007 05:09 PM
Determining if an indicator value is null newguy Indicator Development 4 10-16-2007 04:16 PM


All times are GMT -6. The time now is 07:53 PM.