Announcement

Collapse
No announcement yet.

Subtracting two Dates

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

  • Subtracting two Dates

    Now here's one I haven't been able to solve.

    I wish to input a user defined date through the Edit Studies (this will be an options expiry date).

    Then grab the date of the current bar.

    Then subtract the two and plot the difference, giving the number of days to expiry.

    It only has to work on a daily chart.

    I found the following approach in the archives....

    date1 = yyyymmdd ==>> create a number in this format

    then perform the following multiplication in two steps

    step1=(4 digit year)*100 + month;
    step2=step1*100+day;


    Now this works as long as the two dates are in the same month.

    When the dates are in different months, the resulting difference is way off, as the date numbers are not really correct. When you change to a new month, the number is off by approximately 70.

    Is there a way to do what I described above?

    Below is a test efs that shows the problem.
    Attached Files

  • #2
    Re: Subtracting two Dates

    bigtee
    One way to do it is to create two Date Objects one for the future date eg



    and one for the system date eg



    Then by using the getTime() method of the date object which returns the number of milliseconds from January 1st 1970 you can determine the difference in milliseconds between those two dates eg



    Divide that difference by 86400000 which is the number of milliseconds in a day and you will get the number of days in between those two dates
    For information on the Date Object see this article in the EFS KnowledgeBase. If I remember correctly I think this topic is also covered in the JavaScript for EFS video series that you may want to review as it will provide you with a thorough introduction to JavaScript which is at the foundation of EFS
    Alex


    Originally posted by bigtee
    Now here's one I haven't been able to solve.

    I wish to input a user defined date through the Edit Studies (this will be an options expiry date).

    Then grab the date of the current bar.

    Then subtract the two and plot the difference, giving the number of days to expiry.

    It only has to work on a daily chart.

    I found the following approach in the archives....

    date1 = yyyymmdd ==>> create a number in this format

    then perform the following multiplication in two steps

    step1=(4 digit year)*100 + month;
    step2=step1*100+day;


    Now this works as long as the two dates are in the same month.

    When the dates are in different months, the resulting difference is way off, as the date numbers are not really correct. When you change to a new month, the number is off by approximately 70.

    Is there a way to do what I described above?

    Below is a test efs that shows the problem.

    Comment


    • #3
      Thanks Alex...

      I have gotten the efs to work. I had to change the approach slightly. Your suggestion of
      currentDate = newDate();
      always returns today's date. What I needed was a bar by bar date. So I changed that to

      var currentBarDate = getValue("rawtime", 0);

      I was also able to make your "forwardDate" a user input by making it a string "nExpiryString" and then converting to a date using the parse function as below:

      nExpiry = Date.parse(nExpiryString);

      However, I had to add a fudge factor.

      It seems that using 6/20/2008 for the user input the formula
      nExpiry = Date.parse(nExpiryString);
      returns the number1213938000000, 13 digits.

      The formula for Friday's bar 0 (which is also 6/20/08)
      var currentBarDate = getValue("rawtime", 0);
      returns the number 1213916460, only 10 digits.

      So I had to put in a fudge factor of 1000 and multiply currentBarDate by 1000 before doing the subtraction, etc.

      Do you have any ideas why the "parse" returns 13 digits while the "getValue" only returns 10 digits for the same date?

      efs attached if you wish to check it out.

      thanks

      bigtee
      Attached Files

      Comment


      • #4
        bigtee

        Your suggestion of
        currentDate = newDate();
        always returns today's date.
        That is correct. In fact in my code example I indicated that currentDate = new Date() returns the system date (ie the computer's date)
        If you instead want to retrieve the bar date you can use either the solution you implemented ie getValue("rawtime",0) or better yet just rawtime(0) or you can assign getValue("time") to the variable currentDate which you can then use with all the methods of the date object.

        Do you have any ideas why the "parse" returns 13 digits while the "getValue" only returns 10 digits for the same date?
        That is because the parse() method returns the number of milliseconds from January 1st 1970 while rawtime returns the number of seconds from January 1st 1970. This is why you need to multiply your currentBarDate by 1000 as that converts it to milliseconds
        Alex


        Originally posted by bigtee
        Thanks Alex...

        I have gotten the efs to work. I had to change the approach slightly. Your suggestion of
        currentDate = newDate();
        always returns today's date. What I needed was a bar by bar date. So I changed that to

        var currentBarDate = getValue("rawtime", 0);

        I was also able to make your "forwardDate" a user input by making it a string "nExpiryString" and then converting to a date using the parse function as below:

        nExpiry = Date.parse(nExpiryString);

        However, I had to add a fudge factor.

        It seems that using 6/20/2008 for the user input the formula
        nExpiry = Date.parse(nExpiryString);
        returns the number1213938000000, 13 digits.

        The formula for Friday's bar 0 (which is also 6/20/08)
        var currentBarDate = getValue("rawtime", 0);
        returns the number 1213916460, only 10 digits.

        So I had to put in a fudge factor of 1000 and multiply currentBarDate by 1000 before doing the subtraction, etc.

        Do you have any ideas why the "parse" returns 13 digits while the "getValue" only returns 10 digits for the same date?

        efs attached if you wish to check it out.

        thanks

        bigtee

        Comment


        • #5
          Thanks again, Alex.

          I'm good to go now. Everything working great.

          bigtee

          Comment


          • #6
            bigtee
            You are most welcome
            Alex


            Originally posted by bigtee
            Thanks again, Alex.

            I'm good to go now. Everything working great.

            bigtee

            Comment

            Working...
            X