Announcement

Collapse
No announcement yet.

Seconds per bar

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

  • Seconds per bar

    Hi,

    I'm trying to to have the elapsed time (seconds) per bar shown in real time.
    (On range bars)

    I have used this thread as base:


    The code I use:

    --------------------------------------------------
    function preMain(){


    setStudyTitle("Time3");

    setCursorLabelName("Time3", 0);

    setPlotType(PLOTTYPE_HISTOGRAM, 0);

    setDefaultBarFgColor(Color.white, 0);

    }



    function main(){

    var myDate = new Date();

    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;


    //setBar(Bar.Value, -1, 0,myDifferenceInSecs);


    return(myDifferenceInSecs);

    ------------------------------------------------------------------------------------------------------

    This works fine to show the time in the history bars but the current bar does not update in real time.

    I want to be able to see the time "ticking" since the open of the current bar, preferably updated every tick.

    If I use "return" instead of "setbar" it still doesn't update in real time.

    But if I plot only the "Math.round(myDate.getTime()/1000) - rawtime(0);" the time is updated in real time.

    I've been struggling with this for a couple of days now. Please help...

    Regards, Lars

  • #2
    larssa,

    For the LAST bar, if T is the current time, B0 is the time of the current bar, and B1 is the previous bar, then your calculation is:

    X = (T - B1) - (T - B0)

    which is:

    X = T - B1 - T + B0

    The Ts cancel out leaving:

    X = -B1 + B0

    which will pretty much always be the same value, like you're seeing.

    Since rawtime is the START of each bar, all you need is:

    X = T - B0

    or,
    PHP Code:
    function main(){

        var 
    myDate = new Date();

        var 
    myElapsedTimeInSecs = (myDate.getTime()/1000) - rawtime(0);

        return 
    myElapsedTimeInSecs;

    During market hours this gives the correct value for the last bar.

    However, for past historical bars, you simply want the difference between the bars, which is:

    X = B0 - B1

    What's missing is how to distiguish realtime and historical bars:
    PHP Code:
    function main(){
        var 
    myElapsedTimeInSecs;

        if (
    getBarState() == BARSTATE_CURRENTBAR) {
            var 
    myDate = new Date();
            
    myElapsedTimeInSecs = (myDate.getTime()/1000) - rawtime(0);
        } else {
            
    myElapsedTimeInSecs rawtime(0) - rawtime(-1);
        }

        return 
    myElapsedTimeInSecs;

    Note Date() is the time of your computer's clock and rawtime, I believe, is the time either on eSignal's computer or the exchange's computer where the trade occurred. So, the difference may be negative sometimes if the clocks are not synched up.
    Last edited by shortandlong; 02-05-2010, 03:59 PM.

    Comment


    • #3
      Thanks for the help.

      However, I still have the same problem.

      The time bars are plotted 1 bar wrong to the right.
      So the current bar is actually showing the time it took for the previous bar to form.

      But using:
      myElapsedTimeInSecs = rawtime(0) - rawtime(-1);

      with

      setBar(Bar.Value, -1, 0,myElapsedTimeInSecs);
      will show the historical bars correctly.

      But if I do, the current bar value is "NONE"
      Remark: As the markets are closed now I really can't be sure how it will be shown in realtime.

      Regards, Lars

      Comment


      • #4
        I tried this code now:

        function main(){

        var myElapsedTimeInSecs;



        if (getBarState() == BARSTATE_CURRENTBAR) {

        var myDate = new Date();

        myElapsedTimeInSecs = (myDate.getTime()/1000) - rawtime(0);


        } else {

        myElapsedTimeInSecs = rawtime(0) - rawtime(-1);
        setBar(Bar.Value, -1, 0,myElapsedTimeInSecs);
        }



        return myElapsedTimeInSecs;



        This will show the historical bars correctly but the current bar will show the same value as the -1 bar.


        /Lars

        Comment


        • #5
          Lars,

          Sorry about my earlier post, I didn't get a chance to test it - it was from memory.

          Try this:
          PHP Code:
          // clip times larger than:
          var peakAllowedSeconds 60 60;


          function 
          main(){
              if (
          getBarState() == BARSTATE_CURRENTBAR) {
                  
          // market open and trade occured during already existing bar
                  
          return elapsedSecondsOnLastBar();
              } else {    
          // BARSTATE_NEWBAR or BARSTATE_ALLBARS (first bar on chart)
                  
          var myElapsedTimeInSecs rawtime(0) - rawtime(-1);

                  
          // reduce huge spikes showing time market was closed in seconds.
                  
          myElapsedTimeInSecs clipTimeSpikes (myElapsedTimeInSecs);

                  
          // set previous bar
                  
          setBar(Bar.Value, -10myElapsedTimeInSecs);

                  
          // if new bar is the last one on chart, get elapsed time since time on bar
                  
          if (getCurrentBarIndex() == 0) {    
                      return 
          elapsedSecondsOnLastBar();
                  }

                  
          // no return, since no value for current bar as it is "historical"
              
          }
          }


          function 
          elapsedSecondsOnLastBar() {
              var 
          myDate = new Date();
              var 
          myElapsedTimeInSecs = (myDate.getTime()/1000) - rawtime(0);

              
          // reduce huge spikes showing time market was closed in seconds.
              
          myElapsedTimeInSecs clipTimeSpikes (myElapsedTimeInSecs);

              return 
          myElapsedTimeInSecs;
          }


          function 
          clipTimeSpikes (t) {
              if (
          peakAllowedSecondspeakAllowedSeconds;
              return 
          t;

          I put some simple code to clip the large number of seconds shown when the markets are closed. Change/set the variable peakAllowedSeconds as needed.

          Can't fully test it since the markets are closed...

          Comment


          • #6
            Thanks a lot.
            Tried it and it works perfect.

            Last question, if you got the time...

            Would it be possible to divide a bars volume with the time in seconds that your code provides?

            I was able to do it with the first simpler code but not with this one.

            Before I just put in the code below:
            var VolTime = (volume() / myElapsedTimeInSecs);
            return VolTime;

            However, with this new code I'm not able to get it to work correctly.

            Thanks!

            Comment


            • #7
              Try this:
              PHP Code:
              // clip times larger than:
              var peakAllowedSeconds 60 60;


              function 
              main(){
                  if (
              getBarState() == BARSTATE_NEWBAR) {
                      
              // set previous "historical" bar
                      
              setBar(Bar.Value, -10myTimeFuncForBar (-1));
                  }

                  if (
              getCurrentBarIndex() == 0) {    
                      
              // bar is the last one on chart - get elapsed time since time on bar
                      
              return myTimeFuncForBar (0);
                  }
                  
              // else ... no return, since no elapsed time value for bar as it is "historical"
              }


              function 
              myTimeFuncForBar (nBar) {
                  
              // uncomment line to just display # of seconds per bar
              //    return barTimeInSeconds (nBar);

                  
              var barTimeInSeconds (nBar);
                  if (
              == 0) return 0;
                  return (
              volume(nBar) / t);
              }


              function 
              barTimeInSeconds (nBar) {
                  if (
              nBar == 0) {
                      return 
              elapsedSecondsOnLastBar();
                  } else {
                      var 
              myElapsedTimeInSecs rawtime(nBar 1) - rawtime(nBar);

                      
              // reduce huge spikes showing time market was closed in seconds.
                      
              myElapsedTimeInSecs clipTimeSpikes (myElapsedTimeInSecs);
                      
                      return 
              myElapsedTimeInSecs;
                  }
              }


              function 
              elapsedSecondsOnLastBar() {
                  var 
              myDate = new Date();
                  var 
              myElapsedTimeInSecs = (myDate.getTime()/1000) - rawtime(0);

                  
              // reduce huge spikes showing time market was closed in seconds.
                  
              myElapsedTimeInSecs clipTimeSpikes (myElapsedTimeInSecs);

                  return 
              myElapsedTimeInSecs;
              }


              function 
              clipTimeSpikes (t) {
                  if (
              peakAllowedSecondspeakAllowedSeconds;
                  return 
              t;

              Last edited by shortandlong; 02-10-2010, 06:59 PM.

              Comment


              • #8
                Thank you very much.

                Worked like a charm...

                Larssa

                Comment

                Working...
                X