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

Working with Dates

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

    Working with Dates

    I'm having a heck of a time working with dates.

    I have a DateTimeSeries and I can pull dates out at will, no problem there. I also have a DateTime 'variable' that I have set to the Expiration of a futures contract. I would have thought I could use "Instruments[0].Expiry, but that seem to only give me the first day of the expiration month as opposed to the actual expiration day, but I guess it doesn't. That's OK though since I have got that part fixed too. So, I've got two dates........

    ....all I want to do it to find out how many days there are remaining, at any given day from say, myDateTimeSeries[0] to myContractExpiry (in the future)

    Both dates are in the proper format as opposed to me trying to use a hack-up like ToDay() method which I first tried but found out why later that we just shouldn't go there :-)

    Why can't I just use math operators?!!! Is it to0 tough to just make Date1-Date2 'in days'? I'm sure that would be a hell of a lot of help and make it 99% easier for everyone!

    I feel I'm close but so far, nada.
    Last edited by Steve R; 08-15-2012, 07:01 PM.

    #2
    Try the DateTime.Subtract method:

    TimeSpan ts = datetime2.Subtract(datetime1);

    etc

    Comment


      #3
      Originally posted by Radical View Post
      Try the DateTime.Subtract method:

      TimeSpan ts = datetime2.Subtract(datetime1);

      etc
      OK I've got the second part - datetime2.Subtract(datetime1);

      But I'm not sure what the first part is and I've got a compile error since I don't understand it like I should

      TimeSpan ts ????

      BTW where in the NT help files did you find this gem? With DateTimeSeries it's seriously slim pickings and next to useless for info like this.

      Comment


        #4
        Originally posted by Radical View Post
        Try the DateTime.Subtract method:

        TimeSpan ts = datetime2.Subtract(datetime1);

        etc
        What I am saying is that I can Print the difference, so you know you've got that part correct...


        Print("The difference between now and Expiration is " + dtContract_Exp.Subtract(myDateTimeSeries[0]).ToString());

        Where I'm getting hung up is I want to store that difference in a variable to do some more math with it... I can't figure out how to do that part. I'm assuming that's your TimeSpan ts, but I can't figure it out.

        Comment


          #5
          Steve,

          TimeSpan is not a supported NinjaScript method, so we do not have any documentation on it. However it is a valid .NET method so you do have the ability to use this function in your program.

          Please see the MSDN article on this method for more information:



          If you can provide me with some of your code to determine how you're coming up with the expiry date, etc, we can help you do the calculations you're looking for.
          MatthewNinjaTrader Product Management

          Comment


            #6
            Originally posted by NinjaTrader_Matthew View Post
            Steve,

            TimeSpan is not a supported NinjaScript method, so we do not have any documentation on it. However it is a valid .NET method so you do have the ability to use this function in your program.

            Please see the MSDN article on this method for more information:



            If you can provide me with some of your code to determine how you're coming up with the expiry date, etc, we can help you do the calculations you're looking for.
            OK I think this is what you're looking to see.

            DateTime dtContract_Exp = new DateTime(iYear, iMonth, iDay,0,0,0);
            Where: iYear, iMonth and iDay are user input integers. to allow me to set the contract expiration date.

            When I print
            dtContract_Exp
            the format looks identical to when I print
            myDateTimeSeries[0]
            so I assume they're in the same format and I haven't buggered it up by doing it this way.

            I've seen the MSDN article but ya gotta understand dude, if we could read that crap and understand it, we wouldn't be having this conversation. I only tell you that because 75% of the people tell you the same thing because they have the same issues lol.
            Last edited by Steve R; 08-15-2012, 08:34 PM.

            Comment


              #7
              Originally posted by NinjaTrader_Matthew View Post
              Steve,

              TimeSpan is not a supported NinjaScript method, so we do not have any documentation on it. However it is a valid .NET method so you do have the ability to use this function in your program.

              Please see the MSDN article on this method for more information:



              If you can provide me with some of your code to determine how you're coming up with the expiry date, etc, we can help you do the calculations you're looking for.
              Oh yeah, and when I use this statement..

              Print("The difference between now and Expiration is " + dtContract_Exp.Subtract(myDateTimeSeries[0]).ToString());
              The output for today is
              32.00:00:00
              since the expiration date is set to Sept 17, 2012

              Comment


                #8
                Originally posted by Steve R View Post
                But I'm not sure what the first part is and I've got a compile error since I don't understand it like I should

                TimeSpan ts ????
                What is the error you're getting on this line?

                Comment


                  #9
                  Originally posted by Radical View Post
                  What is the error you're getting on this line?
                  lol what the heck!

                  I just re-wrote it, and my answer is nothing is wrong!

                  I can't even recall now what the error was as I've been screwing with it too much to remember since then.

                  Sooo, how do I now convert the 32.00:00:00 to a nice round integer of 32? Since all I wanted was the number of days (as an integer).
                  Last edited by NinjaTrader_James; 08-16-2012, 06:54 AM.

                  Comment


                    #10
                    TimeSpan.Days should give you just the days as an integer. So something like

                    int days = ts.Days;

                    Comment


                      #11
                      Originally posted by Radical View Post
                      TimeSpan.Days should give you just the days as an integer. So something like

                      int days = ts.Days;
                      Yeah my man, I just found it myself as it turns out.

                      int iDaysBetween = ts.Days;
                      Even a blind dog finds a bone once in a while.

                      Thx guys

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Kaledus, Today, 01:29 PM
                      0 responses
                      0 views
                      0 likes
                      Last Post Kaledus
                      by Kaledus
                       
                      Started by PaulMohn, Today, 12:36 PM
                      1 response
                      14 views
                      0 likes
                      Last Post NinjaTrader_Gaby  
                      Started by yertle, Yesterday, 08:38 AM
                      8 responses
                      36 views
                      0 likes
                      Last Post ryjoga
                      by ryjoga
                       
                      Started by rdtdale, Today, 01:02 PM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_LuisH  
                      Started by alifarahani, Today, 09:40 AM
                      3 responses
                      17 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X