Announcement

Collapse
No announcement yet.

getPreviousTradingDay?

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

  • getPreviousTradingDay?

    What exactly does getPreviousTradingDay return? I'm trying to retrieve the index of the first bar of the previous trading day, and I'm using:

    var dtPrevTime = getPreviousTradingDay(dtCurrentTime)

    which is ok. However, when I call getFirstBarIndexOfDay(dtPrevTime, Symbol), it throws an error saying that the first argument is invalid.

  • #2
    Hello mazachan,

    The getPreviousTradingDay() function returns a date object for the day that is prior to the date passed to the function.

    The reason you're getting that error with getFirstBarIndex() is because on the first day in your chart, getPreviousTradingDay() returns null. You just need to add a null check for dtPrevTime before you pass this variable to getFirstBarIndexOfDay().

    PHP Code:
    function main() {
        var 
    dtCurrentTime getValue("Time");
        var 
    dtPrevTime getPreviousTradingDay(dtCurrentTime);
        if (
    dtPrevTime == null) return;  // add null check
        
        
    var nIndex getFirstBarIndexOfDay(dtPrevTime);
        
        
    debugPrintln("At bar " getCurrentBarIndex() + "  nIndex = " nIndex);
        
        return;

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Originally posted by JasonK
      Hello mazachan,

      The getPreviousTradingDay() function returns a date object for the day that is prior to the date passed to the function.

      The reason you're getting that error with getFirstBarIndex() is because on the first day in your chart, getPreviousTradingDay() returns null. You just need to add a null check for dtPrevTime before you pass this variable to getFirstBarIndexOfDay().

      PHP Code:
      function main() {
          var 
      dtCurrentTime getValue("Time");
          var 
      dtPrevTime getPreviousTradingDay(dtCurrentTime);
          if (
      dtPrevTime == null) return;  // add null check
          
          
      var nIndex getFirstBarIndexOfDay(dtPrevTime);
          
          
      debugPrintln("At bar " getCurrentBarIndex() + "  nIndex = " nIndex);
          
          return;

      Under what circumstances would getPreviousTradingDay(someDate) return null?

      I'm trying to get today's date with
      currentDate = getValue("Time", symbol);

      and then I'm trying to call prevDate = getPreviousTradingDay(currentDate);

      It appears when I call getFirstBarIndexOfDay(prevDate) is returning null, even though it should not be.

      Comment


      • #4
        Hello mazachan,

        Originally posted by mazachan
        Under what circumstances would getPreviousTradingDay(someDate) return null?
        Because you are passing the current bar's date to the function while your formula is loading and is processing the first day's data in your chart. This would be the oldest day in your chart data.

        I'm trying to get today's date with
        currentDate = getValue("Time", symbol);

        and then I'm trying to call prevDate = getPreviousTradingDay(currentDate);

        It appears when I call getFirstBarIndexOfDay(prevDate) is returning null, even though it should not be.
        The getValue("Time") call refers to the date of the bar that is being processed by the EFS. If you want to pass the current date based on your PC clock assign a date object to the currentDate variable as follows.

        currentDate = new Date();

        Please let me know if this resolves the issue.
        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          Thank you Jason. I do have one last question though. I'm trying to retrieve all the highs from the previous trading day and I'm calling

          getValue("High", -firstBarToday, -firstBarYesterday, symbol);

          For some reason, this seems to work intermittently. When it doesn't work, it appears that the entire EFS just seems to die on that one line and nothing after that gets executed. Those bars are valid bars and esignal should be finding them. Can you give more insight as to why this happens? Other times it will execute perfectly and everything works well.

          Thanks.

          Comment


          • #6
            Hello mazachan,

            You're most welcome.

            It appears to me that the parameters you're passing to getValue() are not appropriate. The second parameter is the starting index you want to retrieve the array for, which is a relative value to the bar that is being processed. For example, when a formula is loading and processing bar -100, if you use a start index value of 0, the first high in the array comes from bar -100. If you use -1 for the start index, the first high for the array will be from bar -101. The getFirstBarIndexOfDay() function returns a negative bar index that corresponds to the actual bar index in the chart, not a relative position compared to the current bar index that is being processed. By simply passing the bar index result of getFirstBarIndexOfDay() for the second parameter, you are telling getValue() to start the array that number of bars offset from the current bar that is being processed. Also, by adding the negative sign to the front of the variable you are changing this value to a positive value, which tells getValue() to start the array that number of bars in front (to the right) of the current bar that is being processed. For this barIndex parameter you need to perform some math to get the correct starting value for the array. Try the following and pass the result of nStartIndex for the second parameter of getValue().

            var nStartIndex = firstBarToday - getCurrentBarIndex();

            The third parameter tells getValue() how many bars to retrieve for the array. If the start index for the array is offset into the history (to the left of current bar), then the number of bars parameter (3rd parameter, numBars) needs to be a positive number. Again, you need to perform some math to determine how many bars you want to retrieve. You can't just pass a bar index value for this parameter as this will not retrieve the array you are expecting. Try the following and pass the result of nNumBars to the third parameter of getValue().

            var nNumBars = firstBarToday - firstBarYesterday;

            nNumBars will be a positive value, so again you do not need to add the negative sign to the variable when passed to the getValue() function.
            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              Ok, as of 4pm, I had counted the start of Wednesday 4/4/2007 at -410 bars ago. The first bar that happened yesterday was at 1604 bars ago and I calculated a total of 1193 bars.

              I called the function getValue("High", -410, 1193, symbol); and it's returning a value of null.

              However, if I do something like getValue("High", -410, 10);
              I get values. Is there a cap as to how much data can be returned?
              Last edited by mazachan; 04-04-2007, 03:14 PM.

              Comment


              • #8
                Hello mazachan,

                You won't be able to pass hard coded constants to the function because the call will refer to a different set of bars for each bar that gets processed by the formula as it loads. You need to try performing the math operation I outlined in my previous reply.

                Taking your hard coded example, let's assume that the formula is processing bar 0. The array that you are requesting will return null because the array is starting at bar -410, which is 410 bars prior to bar 0. The number of bars you can retrieve from that starting point going forward is only 410 bars. You are asking for 1193, which goes into the future where there is no bar data.

                The limit of how many bars of data can be requested is based on how many bars are loaded in the chart. As long as the request is not asking for data in the future or data that is not loaded in the chart, it will properly return an array.
                Jason K.
                Project Manager
                eSignal - an Interactive Data company

                EFS KnowledgeBase
                JavaScript for EFS Video Series
                EFS Beginner Tutorial Series
                EFS Glossary
                Custom EFS Development Policy

                New User Orientation

                Comment


                • #9
                  Ohhhhhh.. I think I understand now. So the second param is the starting point and then the third param is to go forward however many bars. I think I got it. I thought the third param was to go backwards. I will give that a shot.

                  On another note, I just started getting this error:

                  Invalid Date Argument in getFirstBarIndexOfDay();

                  where I have:

                  currentDate = new Date();
                  dtPrevDate = getPreviousTradingDay(currentDate);

                  It should always be able to get the previous trading day from a new Date() object shouldn't it? It seems like these errors are very erratic.
                  Last edited by mazachan; 04-05-2007, 02:43 PM.

                  Comment

                  Working...
                  X