Announcement

Collapse
No announcement yet.

Problem comparing 2 date variables to see if they are equal

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

  • Problem comparing 2 date variables to see if they are equal

    I have a back testing strategy that takes a start date and end date as inputs. When I run a back test if there is an open trade at the end date it will close it at the close of todays date even if the end date I specified is prior to today.

    So I want to compare barDate to endDate and if they are equal I want to close the open trade at the closing price of barDate.

    I have tried if (barDate == endDate) Strategy.doSell(.......)
    but it does not execute.

    I also tried if (barDate != endDate)
    else Strategy.doSell(....)
    but that doesn't execute either.

    I have put debugPrintln to display the endDate and barDate so I know that they are equal. What is going wrong?

  • #2
    Re: Problem comparing 2 date variables to see if they are equal

    richthomas
    Without seeing the code it is not possible to determine why it is not working as expected
    Post your script or if you do not wish to do that provide a complete example that replicates your code and illustrates the issue you are seeing and someone will try to asist you
    Alex


    Originally posted by richthomas
    I have a back testing strategy that takes a start date and end date as inputs. When I run a back test if there is an open trade at the end date it will close it at the close of todays date even if the end date I specified is prior to today.

    So I want to compare barDate to endDate and if they are equal I want to close the open trade at the closing price of barDate.

    I have tried if (barDate == endDate) Strategy.doSell(.......)
    but it does not execute.

    I also tried if (barDate != endDate)
    else Strategy.doSell(....)
    but that doesn't execute either.

    I have put debugPrintln to display the endDate and barDate so I know that they are equal. What is going wrong?

    Comment


    • #3
      OK I've attached the file with the related code.

      I have been running this against symbol CAF.

      As you can see from the advanced chart it was in a long trade on 9th January. However running the back test closes that trade on todays current price or todays close (depending on status of market). So if I run it tomorrow I will get a different result for the last trade.

      If you look at the debug print lines it displays the end date for 9th January and the bar date for 9th January but the date comparison on line 151 returns false and it prints out the message 'Bar date not equal to end date'.

      Your help is appreciated.
      Attached Files

      Comment


      • #4
        richthomas
        I don't think you can run the comparison using the Date Objects in their native format. You need to either retrieve the actual values (using the available methods described in this article in the EFS KnowledgeBase) or convert them to a number by (for example) multiplying them by 1.
        In the case of your script the two easiest solutions that I can think of are to change the following line
        if(endDate == barDate))
        to either this
        if(endDate.getTime() == barDate.getTime())
        or this
        if((endDate*1) == (barDate*1))
        Once you make this change the script should work as expected
        Hope this helps
        Alex


        Originally posted by richthomas
        OK I've attached the file with the related code.

        I have been running this against symbol CAF.

        As you can see from the advanced chart it was in a long trade on 9th January. However running the back test closes that trade on todays current price or todays close (depending on status of market). So if I run it tomorrow I will get a different result for the last trade.

        If you look at the debug print lines it displays the end date for 9th January and the bar date for 9th January but the date comparison on line 151 returns false and it prints out the message 'Bar date not equal to end date'.

        Your help is appreciated.

        Comment


        • #5
          Thanks Alexis that works fine now - I changed it to (if endDate.getTime() == barDate.getTime())

          Comment


          • #6
            richthomas
            You are most welcome. Glad to hear that is is working fine now.
            Alex


            Originally posted by richthomas
            Thanks Alexis that works fine now - I changed it to (if endDate.getTime() == barDate.getTime())

            Comment

            Working...
            X