Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Converting date

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

    Converting date

    Hi all.

    I've got a bar date variable as integer
    Code:
    ToDay(Time[0])
    and I'd like to convert it as DateTime format to find the number of days between dates.
    I tried something like:
    Code:
    DateTime DayToConvert = new DateTime (ToDay(Time[0].Month),ToDay(Time[0].Day),ToDay(Time[0].Year));
    but doesn't work.
    How can I do?

    Thanks.

    #2
    Hello Max,

    ToDay() is always going to return an integer with has the year, month and day. Therefore, you would have to do quite a bit of manual conversions to be able to get the values individually.

    You may want to just use the (Time[0].Day), (Time[0].Month), and (Time[0].Year) to be able to get the dates that you would like more easily.

    Let us know if we can be of further assistance.
    JCNinjaTrader Customer Service

    Comment


      #3
      Thanks, but your suggestion creates an integer variable as well. I have to convert it in a DateTime format.

      Comment


        #4
        Hello Max,

        Yes, but if you are creating a new Data Time variable using your own format.

        Note that "Time[0]" is a Date Time variable that you may use that will return a Date Time variable of the current bar.

        JCNinjaTrader Customer Service

        Comment


          #5
          I tried something like:

          Code:
          DateTime DataDay = new DateTime(4,29,2013);
          DateTime DataBar = Time[0];
          TimeSpan ts = DataDay - DataBar;
          // Difference in days.
          int differenceInDays = ts.Days;
          				
          Print(Time[0].ToString()+ " "+differenceInDays.ToString());
          but I've got the following error message:
          Error on calling 'OnBarUpdate' method for indicator 'TestExp' on bar 1: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          Comment


            #6
            Originally posted by MAX View Post
            I tried something like:

            [code]
            DateTime DataDay = new DateTime(4,29,2013);
            You might have those reversed. Try
            DateTime DataDay = new DateTime(2013, 4,29);

            Comment


              #7
              Originally posted by MAX View Post
              I tried something like:

              Code:
              DateTime DataDay = new DateTime(4,29,2013);
              DateTime DataBar = Time[0];
              TimeSpan ts = DataDay - DataBar;
              // Difference in days.
              int differenceInDays = ts.Days;
               
              Print(Time[0].ToString()+ " "+differenceInDays.ToString());
              but I've got the following error message:
              Error on calling 'OnBarUpdate' method for indicator 'TestExp' on bar 1: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
              The error that you are quoting is not coming from the code that you posted.

              Comment


                #8
                The correct format for data is:

                DateTime DataDay = new DateTime(2013, 4,29);

                Thanks to both TAJTrades and koganam for the hint and happy fourth of july to all.

                Comment


                  #9
                  Hi all again.

                  I don't understand why the difference in days calculates twice 0 days remaining to the chosen date (ex. 4/24/2013 as date).

                  See attached pict.

                  Is there anything to be done in the code?

                  Thanks.
                  Attached Files

                  Comment


                    #10
                    Solution found.It depends on session time end.
                    Difference in time makes the calculation wrong.

                    To anybody interested in the subject, to compare dates (only when you have the daily bar date and one input date from the user), the date of the daily bar must be coded as below:

                    Code:
                    DateTime DataBar = Time[0].Date;
                    this brings both dates 2013/04/24 in my example as:

                    24/04/2013 00:00:00 ---- 24/04/2013 00:00:00

                    and the difference in days can perfectly be calculated.

                    This is not needed comparing dates between daily bars, because everyone has the same session time end.

                    Hope this helps.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    656 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    371 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    109 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    574 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    579 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X