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!
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
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
-
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
-
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
-
-
Originally posted by Ralph View PostTo simulate the start-date of a bar, I use tho following piece of code:
Code:[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 cre8able, 02-11-2023, 05:43 PM
|
3 responses
236 views
0 likes
|
Last Post
by rhubear
Today, 10:46 AM
|
||
Started by frslvr, 04-11-2024, 07:26 AM
|
8 responses
113 views
1 like
|
Last Post Today, 10:30 AM | ||
Started by stafe, 04-15-2024, 08:34 PM
|
10 responses
46 views
0 likes
|
Last Post
by stafe
Today, 10:29 AM
|
||
Started by rocketman7, Today, 09:41 AM
|
3 responses
11 views
0 likes
|
Last Post Today, 10:07 AM | ||
Started by traderqz, Today, 09:44 AM
|
2 responses
10 views
0 likes
|
Last Post Today, 10:00 AM |
Comment