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

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 ghoul, Today, 06:02 PM
                    1 response
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    44 views
                    0 likes
                    Last Post jeronymite  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    7 responses
                    20 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by AttiM, 02-14-2024, 05:20 PM
                    10 responses
                    179 views
                    0 likes
                    Last Post jeronymite  
                    Started by DanielSanMartin, Yesterday, 02:37 PM
                    2 responses
                    13 views
                    0 likes
                    Last Post DanielSanMartin  
                    Working...
                    X