Announcement

Collapse
No announcement yet.

Volume Rate on Tick Bar

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

  • Volume Rate on Tick Bar

    I am trying to calculate the volume rate (volume per minute) (BTW, is the volume number for ES U6 a tick volume or real volume?) for each bar on a tick chart.

    I started with my own code:

    var lastTime = ((getHour(0) - getHour(-1))*60) + (getMinute(0) - getMinute(-1)) + ((getSecond(0) - getSecond(-1))/60);
    var volumeDiff = volume(0);

    return volumeDiff / lastTime;

    But realized I might be off by a bar so I redid it thus:

    var lastTime = ((getHour(+1) - getHour(0))*60) + (getMinute(+1) - getMinute(0)) + ((getSecond(+1) - getSecond(0))/60);
    var volumeDiff = volume(0);

    return volumeDiff / lastTime;

    First, is that correct? I believe I am getting the time elapsed for the current bar and dividing that in minutes into the current bar volume.

    Now if that is correct. There is still the problem of the last bar since there is no future bar. So in KB I found this, which I believe may give me the time elapsed for the last bar:

    if (getCurrentBarIndex() < -1) return;
    var nState = getBarState();
    var vClockTime = new Date()*1;

    if (nState == BARSTATE_NEWBAR) {
    vTimeStamp = getValue("Time")*1 ;
    }

    var vTimeElapse = (vClockTime-vTimeStamp);
    if (vTimeElapse < 0) return;

    If I combine these two pieces together, will I be able to get what I want? Or better yet has someone already done this (I couldn't find anything)?

    Thanks.

  • #2
    Re: Volume Rate on Tick Bar

    Hello buhrmaster,

    Originally posted by buhrmaster
    I am trying to calculate the volume rate (volume per minute) (BTW, is the volume number for ES U6 a tick volume or real volume?) for each bar on a tick chart.

    I started with my own code:

    var lastTime = ((getHour(0) - getHour(-1))*60) + (getMinute(0) - getMinute(-1)) + ((getSecond(0) - getSecond(-1))/60);
    var volumeDiff = volume(0);

    return volumeDiff / lastTime;

    But realized I might be off by a bar so I redid it thus:

    var lastTime = ((getHour(+1) - getHour(0))*60) + (getMinute(+1) - getMinute(0)) + ((getSecond(+1) - getSecond(0))/60);
    var volumeDiff = volume(0);

    return volumeDiff / lastTime;

    First, is that correct? I believe I am getting the time elapsed for the current bar and dividing that in minutes into the current bar volume.
    This is not correct. The +1 bar does not exist yet as you know, but even if it did it would be the start time of that bar, which would not give you the time elasped during the current interval.

    Now if that is correct. There is still the problem of the last bar since there is no future bar. So in KB I found this, which I believe may give me the time elapsed for the last bar:

    if (getCurrentBarIndex() < -1) return;
    var nState = getBarState();
    var vClockTime = new Date()*1;

    if (nState == BARSTATE_NEWBAR) {
    vTimeStamp = getValue("Time")*1 ;
    }

    var vTimeElapse = (vClockTime-vTimeStamp);
    if (vTimeElapse < 0) return;

    If I combine these two pieces together, will I be able to get what I want? Or better yet has someone already done this (I couldn't find anything)?

    Thanks.
    You're on the right track here. To get the time elapsed during the interval you need to compare the start time of the bar using hour(), minute() and second() to real time that can be retrieved from the date object. Use the .getHours(), .getMinutes() and .getSeconds() methods of the date object. It would look something like this.

    PHP Code:
    function main() {
        if (
    getCurrentBarIndex() < -1) return;
        
        var 
    vClockTime = new Date();
        
    vClockTime vClockTime.getHours()*3600 vClockTime.getMinutes()*60 vClockTime.getSeconds();
        
        var 
    vTimeStamp hour(0)*3600 minute(0)*60 second(0);
        
        var 
    vTimeElapse = (vClockTime-vTimeStamp);
        
        return 
    vTimeElapse+"";

    vTimeElapse would represent number of seconds in this case.
    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
      Jason -

      Thanks for your response. After I sent my first post I had combined the code with some variations and came up with this:

      function main() {

      var barIndex = getCurrentBarIndex();
      var volumeDiff = volume(0);

      if (barIndex < 0){
      var lastTime = (rawtime(+1) - rawtime(0)) / 60
      } else if (barIndex == 0){
      var vClockTime = new Date()*1;
      var vTimeStamp = getValue("Time")*1 ;
      lastTime = (vClockTime - vTimeStamp)/60000
      }

      return Math.floor(volumeDiff / lastTime);
      }

      It seems to work or at least returns results that seem plausible.

      Three questions:

      1. Is the use of rawtime correct in that I am trying to get the time elapsed in minutes in the bar being evaluated?

      2. In the else-if block I am using Date and Time to get the current time (in milliseconds?) and the starting time for the most current bar. Is what I did correct?

      3. Will the results I get for the current bar using Date and Time be the same as the results I get using rawtime if I were to later force a refresh when the current bar is no longer the current one? (Actually there should be a slight difference as the end of the current bar is slightly earlier than the beginning of the new current bar. I guess a question then would be is there a way to recalculate bar(-1) given the opening time of the new current bar(0). In other words, if I get a barstate that is a new bar can I recalculate bar(-1)?)


      I am slightly confused by the use of indexes. Only the most right bar on a chart is the 0 bar but when loading an efs I may be evaluating at bar -222, for example, and to get the volume for that bar I refer to it as Volume(0), not volume(-222). To do so would return the volume for bar(-444). Is that correct?

      Thanks for your assistance.

      Comment


      • #4
        Hello buhrmaster,

        Originally posted by buhrmaster
        Jason -

        Thanks for your response. After I sent my first post I had combined the code with some variations and came up with this:

        function main() {

        var barIndex = getCurrentBarIndex();
        var volumeDiff = volume(0);

        if (barIndex < 0){
        var lastTime = (rawtime(+1) - rawtime(0)) / 60
        } else if (barIndex == 0){
        var vClockTime = new Date()*1;
        var vTimeStamp = getValue("Time")*1 ;
        lastTime = (vClockTime - vTimeStamp)/60000
        }

        return Math.floor(volumeDiff / lastTime);
        }

        It seems to work or at least returns results that seem plausible.

        Three questions:

        1. Is the use of rawtime correct in that I am trying to get the time elapsed in minutes in the bar being evaluated?

        2. In the else-if block I am using Date and Time to get the current time (in milliseconds?) and the starting time for the most current bar. Is what I did correct?
        Nope, but you are close. The rawtime() function returns the number of seconds since Jan 1 1970 00:00:00. When you multiply the date object by 1 it gives you the milliseconds since Jan 1 1970. To compare apples to apples here, divide vClockTime by 1000 to convert it to seconds. Then you could divide lastTime by 60 if you want the result in minutes.

        3. Will the results I get for the current bar using Date and Time be the same as the results I get using rawtime if I were to later force a refresh when the current bar is no longer the current one? (Actually there should be a slight difference as the end of the current bar is slightly earlier than the beginning of the new current bar. I guess a question then would be is there a way to recalculate bar(-1) given the opening time of the new current bar(0). In other words, if I get a barstate that is a new bar can I recalculate bar(-1)?)
        Yes, you can. Inside a condition that detects BARSTATE_NEWBAR, you could recalculate the prior bar's number using rawtime(0) and rawtime(-1). The rawtime(0) returns the start time of the current bar and rawtime(-1) would be the start time of the previous bar. Then to re-plot the value on the previous bar, pass the new result to the setBar() function.

        setBar(Bar.Value, -1, nNewValue);


        I am slightly confused by the use of indexes. Only the most right bar on a chart is the 0 bar but when loading an efs I may be evaluating at bar -222, for example, and to get the volume for that bar I refer to it as Volume(0), not volume(-222). To do so would return the volume for bar(-444). Is that correct?

        Thanks for your assistance.
        Yes, that is correct. The bar index used within a series like volume() refers to the bar that is relative to the bar being processed. When an EFS is processing bar 0, volume(-222) returns the value from bar index -222. When the EFS is processing bar -222 during initialization, volume(-222) refers to the bar that was 222 bars prior or -444 as you've already described.
        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
          Thanks, Jason

          Comment

          Working...
          X