Announcement

Collapse
No announcement yet.

Time passed to Total Trading Hour

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

  • Time passed to Total Trading Hour

    Is there a way to have in real time efs that calculate:

    total Time passed in trading Hour vs. Total Trading Hours

    Ex) if it's 10AM EST. for NYSE US Equity

    30 min has pass since US equities open and NYSE has total of 6 and 1/2 hours or 390 min

    30 min / 390min = 0.0769 or 7.69% of total Trading hour has passed.

    Any suggestion?

    Thanks,

    Buzybill

  • #2
    buzybill
    It is possible to do what you are trying to accomplish.
    As a first step you need to determine the time of the first bar of the day. Assuming you are plotting only regular trading hours (ie 9:30-16:00) you can easily do that as follows



    Once you have that you need to create a date object to retrieve the system time (ie your computer's time) which you also need to express as rawtime so that you can then use this to calculate the time elapsed from the first bar of the day.
    To do this you can use the .getTime() method of the date object. Note that this will retrieve the time expressed in milliseconds from January 1st 1970 (whereas rawtime is expressed in seconds) so you need to convert it to seconds by dividing it by 1000. Also the getTime() method retrieves the time expressed in universal time (whereas rawtime is expressed in local time) so you need to adjust for the time zone offset and for daylight saving time



    Once you have the elapsed time you can use it in your equation to determine the percentage.
    For information on the Date Object and the available methods see this article in the EFS KnowledgeBase
    Alex

    Comment


    • #3
      I did as you said, but it doesn't work

      I think I have done as you told me, but for some reason I didn't get the results as I want it.

      Here is my code so far:

      function preMain() {
      setPriceStudy(false);
      setStudyTitle("% Trading Hour");
      setCursorLabelName("%TH");
      setComputeOnClose();
      }

      function main() {

      if(getBarState()==BARSTATE_NEWBAR && day(0)!=day(-1)) {//on the first bar of a new day
      StartTime == rawtime(0);//retrieve the rawtime
      }

      var myDateObj = new Date();//create date object
      var mySysTime = parseInt(myDateObj.getTime()/1000);//retrieve current system time and convert to seconds
      var myTZOffset = myDateObj.getTimezoneOffset()*60;//time zone offset expressed in seconds
      var myDSTOffset = 60*60;//daylight saving time offset expressed in seconds
      var CurrentTime = mySysTime-myTZOffset-myDSTOffset;//adjusted current system time
      var ElapsedTime = CurrentTime-StartTime;//difference in seconds between first bar time and current time

      if (ElapsedTime == null) { return;}// null test

      var tradingHour = (ElapsedTime * 60) / 390;
      return tradingHour;
      }

      what am I doing wrong? Thank you for last help

      Thanks,

      Billy

      Comment


      • #4
        Billy
        There are a couple of reasons why you are not getting the results you are expecting.
        The first one is due to an error in this line
        var tradingHour = (ElapsedTime * 60) / 390;
        ElapsedTime is expressed in seconds while 390 is the total trading time in minutes so in your equation you should either divide ElapsedTime by 60 to convert the elapsed time to minutes or [preferably] multiply 390 by 60 to convert that value to seconds.
        More importantly though you need to consider that ElapsedTime represents the time between the most recent bar that is time stamped 9:30 [assuming you are plotting only regular trading hours] and the current time of your computer at the time you are running the script.
        So if for example you ran the script at exactly 9:30 on Sunday the elapsed time would have been 48 hours expressed in seconds.
        Modify the script as I suggested above and try running it while the market is open and you should see that the result will match what you are expecting.
        Alex

        Comment


        • #5
          Billy
          An alternate solution is to calculate the elapsed time using the most recent bar time instead of the system time.
          The difference is that in this case the elapsed time will be computed only once per bar (instead of on every tick) and it will be as up to date as the lowest interval used to retrieve the bar time.
          To implement this alternative solution replace this whole section of code that I suggested in my previous reply



          with the following one.



          In this case to determine the current time I use the rawtime() function based on 1 minute data which means that the elasped time will be up to date to the starting time of the current 1 minute interval (regardless of the interval used in the chart).
          Alex

          Comment


          • #6
            ReferenceError: StartTime is not defined

            I enter as you told me but for some reason... I'm getting "ReferenceError: StartTime is not defined."
            Where am I getting it wrong?

            Thanks,

            Billy


            function preMain() {
            setPriceStudy(false);
            setStudyTitle("% Trading Hour");
            setCursorLabelName("%TH");
            setComputeOnClose();
            }

            function main() {

            if(getBarState()==BARSTATE_NEWBAR && day(0)!=day(-1)) {//on the first bar of a new day
            StartTime == rawtime(0);//retrieve the rawtime
            }

            var CurrentTime = rawtime(0,inv(1));//most recent bar time based on 1 minute data
            var ElapsedTime = CurrentTime-StartTime;//difference in seconds between first bar time and current time

            if (ElapsedTime == null) { return;}// null test

            var tradingHour = (ElapsedTime / 60) / 390;
            return tradingHour;

            Comment


            • #7
              Billy
              You need to declare StartTime as a global variable [ie outside of main() or preMain()] and set it initially to 0
              Alex

              Comment


              • #8
                Wrong Time?

                Dear Alex,
                It seems like my function should start with 0 from the beginning of the day and end with 390 since there is 390 minutes in trading day. However, It seems like it start with number 50,453.01 and it end with 50,453.99. I have no idea what that number represents. Could you point me to right direct? Where am I getting it wrong?

                Thanks,

                Billy

                function preMain() {
                setPriceStudy(false);
                setStudyTitle("% Trading Hour");
                setCursorLabelName("%TH");
                setComputeOnClose();
                }

                var StartTime = 0;

                function main() {

                if(getBarState()==BARSTATE_NEWBAR && day(0)!=day(-1)) {//on the first bar of a new day
                StartTime == rawtime(0);//retrieve the rawtime
                }

                var CurrentTime = rawtime(0,inv(1));//most recent bar time based on 1 minute data
                var ElapsedTime = CurrentTime-StartTime;//difference in seconds between first bar time and current time

                if (ElapsedTime == null) { return;}// null test

                var tradingHour = (ElapsedTime / 60) / 390;
                return tradingHour;
                }

                Comment


                • #9
                  Billy
                  You have an incorrect operator sign in this line of code
                  StartTime == rawtime(0);//retrieve the rawtime
                  where you are using a comparison operator (ie ==) instead of an assignement operator (ie =). The line of code should be
                  StartTime = rawtime(0);//retrieve the rawtime
                  Alex

                  Comment

                  Working...
                  X