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!
See more
See less

Partner 728x90

Collapse

Adding and subtracting time using time as Integer

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    #2
    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 time
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      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 time
      Thanks for the response! The example you give would make 93000 + 3000 = 96000 though, which is an invalid time.

      Comment


        #4
        Indeed. 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.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          Indeed. 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.
          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?

          Comment


            #6
            Sorry I would not know. That would be more advanced C# than what I can offer support on.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              Sorry I would not know. That would be more advanced C# than what I can offer support on.
              Thanks anyway - I was hoping Ninja had a method like the way you can Serialize Color to save them as inputs.

              Comment


                #8
                To 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]
                Regards
                Ralph

                Comment


                  #9
                  Originally posted by Ralph View Post
                  To 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]
                  Regards
                  Ralph
                  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);

                  Comment


                    #10
                    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


                      #11
                      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


                        #12
                        You could try to use "<" instead of "<=".

                        Regards
                        Ralph

                        Comment


                          #13
                          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


                            #14
                            You can test that with a print statement in OnBarUpdate:

                            Print("ToTime(Time[0]): " + ToTime(Time[0]);

                            Regards
                            Ralph

                            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 rhubear
                            by rhubear
                             
                            Started by frslvr, 04-11-2024, 07:26 AM
                            8 responses
                            113 views
                            1 like
                            Last Post NinjaTrader_BrandonH  
                            Started by stafe, 04-15-2024, 08:34 PM
                            10 responses
                            46 views
                            0 likes
                            Last Post stafe
                            by stafe
                             
                            Started by rocketman7, Today, 09:41 AM
                            3 responses
                            11 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by traderqz, Today, 09:44 AM
                            2 responses
                            10 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Working...
                            X