Announcement

Collapse
No announcement yet.

Seconds in a range bar

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

  • Seconds in a range bar

    Hi,

    Is there a way to count the number of seconds in a Range Bar as it's forming. I would like to be able to use that info in a conditional statement. In other words, If this current Range bar is taking too long to form, then it negates my particular trading signal. This could be helpful for Tick Bars and Volume Bars too. I've tried figuring out how to use Date Object, but it's very confusing. Perhaps someone could point me to a similar efs.

    Thanks,

    Steve

  • #2
    Here is some code to help you out.. Anything that does not have a "var" in front of it needs to be declared outside main.

    var vTime = new Date();
    //---------------------------------------------------
    // Get Time Variables
    vTime = new Date();
    vRTHour = vTime.getHours();
    vRTMin = vTime.getMinutes();
    vRTSec = vTime.getSeconds();
    vRTDOW = vTime.getDay();
    var RTBarTime = (vRTHour*100)+vRTMin;

    vTime = getValue("Time", 0);
    vHour = vTime.getHours();
    vMin = vTime.getMinutes();
    vSec = vTime.getSeconds();
    vDay = vTime.getDate();
    vMonth = vTime.getMonth() +1;
    BarTime = (vHour*100)+vMin;
    MonthDay = ""+vMonth+"_"+vDay;
    var vYear = vTime.getYear();
    nBarSecTime = (vHour*3600)+(vMin*60)+vSec;
    var nSecondsLeftInBar = (nBarSecTime+(getInterval()*60))-nRTTime;

    Notice that I'm already calculating the seconds left in the bar (before completion). This is based on MINUTE charts.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Thanks Doji3333,

      Looks like this is beyond me, but I really appreciate the info!

      Steve

      Comment


      • #4
        Steve
        It actually can be done in a far simpler way by using the getTime() method of the Date Object [which returns the number of milliseconds elapsed from January 1st 1970] and subtracting from it rawtime(0) [ie the start time of the bar expressed in seconds elapsed from January 1st 1970]
        To do this you first create the Date Object and then subtract the value of rawtime(0) from the current value returned by the getTime() method [which needs to be divided by 1000 to convert it to seconds]

        PHP Code:
        var myDate = new Date();//create the Date Object
        var myElapsedTimeInSecs Math.round(myDate.getTime()/1000) - rawtime(0
        (note that for simplicity's sake I rounded the value returned by getTime() when dividing it by 1000)
        This will return the seconds elapsed [on each tick] from the beginning of the bar
        Alex

        Comment


        • #5
          Thanks much Alex, I will try that.

          Steve

          Comment


          • #6
            Steve
            You are most welcome
            Alex


            Originally posted by opstock
            Thanks much Alex, I will try that.

            Steve

            Comment


            • #7
              Hi Alex

              Is there a way to get the rawtime values in milliseceonds instead of seconds. e.g., I would like to get the time elapsed for a 100V NQ chart. In some special cases, the time between two bars will be less then one second and returns a zero value. If I would like to invert the scale (1/value), then I have a division with 0 that results in an infinite value if I don't set this values to 1.

              PHP Code:
              var myDate                     = new Date();//create the Date Object
              var myElapsedTimeInSecs        Math.round(myDate.getTime()/1000) - rawtime(0);
              var 
              myElapsedTimeInSecsPrevBar Math.round(myDate.getTime()/1000) - rawtime(-1);
              var 
              myDifferenceInSecs         = (myElapsedTimeInSecsPrevBar myElapsedTimeInSecs);

               
              //   if(myDifferenceInSecs == 0) myDifferenceInSecs = 1;
                  
              var myInvertDifferenceInSecs 10 myDifferenceInSecs

              //  return(myInvertDifferenceInSecs);
              return(myDifferenceInSecs); 
              Peter
              Last edited by habi; 09-21-2009, 12:59 PM.

              Comment


              • #8
                Peter
                To my knowledge you would not be able to do that using rawtime() since the lowest resolution available in the bar timestamp is seconds.
                In order to do what you want you would need to use the getTime() method of the Date Object [which returns the milliseconds elapsed from January 1st 1970] to retrieve and store the time when the bar is created [ie at BARSTATE_NEWBAR]. At that point you can calculate the difference in milliseconds between the time the bar was created and the current time. Keep in mind though that this would only work in real time
                Alex


                Originally posted by habi
                Hi Alex

                Is there a way to get the rawtime values in milliseceonds instead of seconds. e.g., I would like to get the time elapsed for a 100V NQ chart. In some special cases, the time between two bars will be less then one second and returns a zero value. If I would like to invert the scale (1/value), then I have a division with 0 that results in an infinite value if I don't set this values to 1.

                PHP Code:
                var myDate                     = new Date();//create the Date Object
                var myElapsedTimeInSecs        Math.round(myDate.getTime()/1000) - rawtime(0);
                var 
                myElapsedTimeInSecsPrevBar Math.round(myDate.getTime()/1000) - rawtime(-1);
                var 
                myDifferenceInSecs         = (myElapsedTimeInSecsPrevBar myElapsedTimeInSecs);

                 
                //   if(myDifferenceInSecs == 0) myDifferenceInSecs = 1;
                    
                var myInvertDifferenceInSecs 10 myDifferenceInSecs

                //  return(myInvertDifferenceInSecs);
                return(myDifferenceInSecs); 
                Peter

                Comment

                Working...
                X