Announcement

Collapse
No announcement yet.

Determining how many bars back on a tick chart is x number of minutes ago

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

  • Determining how many bars back on a tick chart is x number of minutes ago

    Hi. I vaguely recall but can not locate a posting that included a few lines of code for determining how many bars back on a tick chart is x number of minutes ago. That is, to get a good visual/temporal relative bearing on two tick charts of different intervals it would be nice to have a few lines of code to repeatedly draw a line x number of minutes ago on each of the two charts. Can anyone recall that post or recommend an approach? Thanks.

    Mike

  • #2
    Re: Determining how many bars back on a tick chart is x number of minutes ago

    Mike
    The enclosed script should do what you are trying to accomplish
    Alex

    PHP Code:
    function preMain(){
        
    setPriceStudy(true)
    }
    var 
    barIndex 0;
    function 
    main(){
        var 
    0;
        var 
    minutes 10;//set to draw a line 10 minutes back from last bar
        
    if(isLastBarOnChart()){
            if(
    getBarState()==BARSTATE_NEWBAR){
                while(
    true){
                    if(
    rawtime(-i)>=(rawtime(0)-(60*minutes))){
                        
    barIndex i;
                        
    i++;
                    }else{
                        break;
                    }
                }
            }
        }
        
    drawLineRelative(-barIndex,0,-barIndex,Infinity*1,PS_SOLID,2,Color.red,"line");


    Originally posted by mikejhelms
    Hi. I vaguely recall but can not locate a posting that included a few lines of code for determining how many bars back on a tick chart is x number of minutes ago. That is, to get a good visual/temporal relative bearing on two tick charts of different intervals it would be nice to have a few lines of code to repeatedly draw a line x number of minutes ago on each of the two charts. Can anyone recall that post or recommend an approach? Thanks.

    Mike

    Comment


    • #3
      Try this...

      PHP Code:
      var nBarCounter 0;


      function 
      main() {

         if (
      getBarState() == BARSTATE_NEWBAR) {
           
      nBarCounter++;
         }

       
         
      debugPrintlnfLookupBarsInMinutes(120) );
        return;
      }


      function 
      fLookupBarsInMinutes(look4minutes) {
        
      //  enter look4minutes in total seconds - 60, 120, ?
        
      var 0totalmin 0;

        
      vTime getValue("Time"x);
        
      vHour vTime.getHours();
        
      vMin vTime.getMinutes();
        
      vSec vTime.getSeconds();
        var 
      CurrentBarTotalSeconds = (vHour 3600) + (vMin 60) + vSec;

        while ((
      totalmin look4minutes) || (x+nBarCounter 5)) {
           
      x--;
        
      vTime getValue("Time"x);
        
      vHour vTime.getHours();
        
      vMin vTime.getMinutes();
        
      vSec vTime.getSeconds();
        var 
      xBarTotalSeconds = (vHour 3600) + (vMin 60) + vSec;

           
      totalmin Math.abs(CurrentBarTotalSeconds xBarTotalSeconds);


         }
        
         return 
      x;  //  x represents the previous bar/bartime >= look4minutes.  representing a negative number.

      Brad Matheny
      eSignal Solution Provider since 2000

      Comment


      • #4
        oops. The "while" loop may need to be changed to "&&" instead of "||". Don't want any infinite loops.
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          Thanks

          Thanks guys. I like it when I get two or more responses to a post suggesting different approaches. I then usually mix, match or cannibalize the suggestions to come up with a final version. Thanks again.

          Mike

          Comment

          Working...
          X