Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Adding and subtracting time using time as Integer
Collapse
X
-
Adding and subtracting time using time as Integer
I have a time parameter where time is set to integer (TimeSpan gives me problems). Does anyone have code that adds/subtracts time when time is set to integer? For instance, add 30min to an input of 93000? Since I discovered from a General Charting question that NT timestamps it's bars at close (meaning, getting the open price of a bar referencing the same time on the same instrument but on, say, a 5min vs 30min chart will be different) - that means if I have a start-time input parameter set as integer, I either have to remember to enter an offset time that is the time-interval of the bar, or have the code add/subtract the bar interval to the same input start-time on both charts.Tags: None
-
marketmasher,
You would need to do the math by hand. Remember the starting bar on a 30 minute chart would be timestamped 10AM. On 5min, it would be timestamped 9:35AM. You can use Bars.Period.Value to get the actual time frame of your chart and then you can run your addition/subtraction from based off of that.
input = 93000
Bars.Period.Value = 30
input + 100 * Bars.Period.Value = your timeJosh P.NinjaTrader Customer Service
-
Thanks for the response! The example you give would make 93000 + 3000 = 96000 though, which is an invalid time.Originally posted by NinjaTrader_Josh View Postmarketmasher,
You would need to do the math by hand. Remember the starting bar on a 30 minute chart would be timestamped 10AM. On 5min, it would be timestamped 9:35AM. You can use Bars.Period.Value to get the actual time frame of your chart and then you can run your addition/subtraction from based off of that.
input = 93000
Bars.Period.Value = 30
input + 100 * Bars.Period.Value = your time
Comment
-
Thanks again - I am working on it. I have not been able to make TimeSpan save my input parameters and have to re-input them every time the chart is loaded. Is there a Serialize method to save TimeSpan inputs?Originally posted by NinjaTrader_Josh View PostIndeed. In that case, additional math would be needed. It may almost be easier if you just used a DateTime/TimeSpan variable. You can just use the C# functions to add 30 minutes and forget the math.
Comment
-
-
Thanks a lot for the help. This is for the Date Stamp on the last bar on the chart? For adding time to an integer-based startTime input value I worked out this: AddTostartTime = startTime + (Bars.Period.Value*100); if (AddTostartTime % 10000 > 5959) AddTostartTime = 10000*Math.Round(((AddTostartTime)/10000),0);Originally posted by Ralph View PostTo simulate the start-date of a bar, I use tho following piece of code:
RegardsCode:[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] lastDate = Bars.Get(lastBar).Time.AddMinutes(-Bars.Period.Value); [/SIZE][/FONT][/SIZE][/FONT]
Ralph
Comment
-
Hi marketmasher,
Yes the result is a DateTime structure. The index variable lastBar is some bar number, but in this particular case it is the last bar processed by the indicator so far. Generally I use DateTime structs for my time variables because sometime I compare time stamps of different days, or even substract the DateTime values of different days. That way, it is simple calculation. If, for some reason, I need a real time value, I do access i.e. lastDate.TimeOfDay().
Regards
Ralph
Comment
-
Anyone there can help me on this:
startTime = new DateTime(0001, 1, 1, 9, 30, 00); // ignore the date portion. It is unused
endTime = new DateTime(0001, 1, 1, 16, 00, 00);
if (ToTime(Time[0]) >= ToTime(startTime) && ToTime(Time[0]) <= ToTime(endTime))
Why does it will continue to trade when it is equal to my endTime? What's wrong with my code? Please help.
Comment
-
Thanks Ralph.
Does the ToTime(Time[0]) will work in a market replay? I have a problem with my strategy with the startTime and endTime.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
567 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
330 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
548 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
548 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment