![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
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)? |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,419
Thanks: 252
Thanked 981 times in 963 posts
|
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.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Senior Member
|
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? |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,419
Thanks: 252
Thanked 981 times in 963 posts
|
You can use the GetBar() method for this task -
http://www.ninjatrader-support.com/H...V6/GetBar.html
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Senior Member
|
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? |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,419
Thanks: 252
Thanked 981 times in 963 posts
|
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).
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#7 | |
|
Senior Member
|
Quote:
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());
Thanks. |
|
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,419
Thanks: 252
Thanked 981 times in 963 posts
|
You would for example use DateTime.Today.AddDays(-1) for yesterdays date, but you would need to introduce custom logic to cover weekends here.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
Senior Member
|
|
|
|
|
|
|
#10 |
|
Senior Member
|
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());
|
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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));
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#12 | |
|
Senior Member
|
Quote:
The following doesn't seem to work... Code:
int barsAgo = GetBar(Time[0].AddDays(-1), 9, 0, 0);
Last edited by AnotherTrader; 05-11-2011 at 04:59 AM.
|
|
|
|
|
|
|
#13 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,419
Thanks: 252
Thanked 981 times in 963 posts
|
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));
Bertrand
NinjaTrader Customer Service
Last edited by NinjaTrader_Bertrand; 05-11-2011 at 05:46 AM.
|
|
|
|
|
|
#14 |
|
Senior Member
|
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)); |
|
|
|
|
|
#15 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,419
Thanks: 252
Thanked 981 times in 963 posts
|
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.
Bertrand
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |