Announcement

Collapse
No announcement yet.

adding high/low of day bands around a moving averge

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

  • adding high/low of day bands around a moving averge

    I am trying to add a moving band around a moving average (in this case a vwap band from the amvwap.efs study - but any moving average will do). What I need to do is find the high/low of the day from the start of the day to the current bar and then just basically do:

    (hi-low)/2 and add/subtract from the vwap band.

    So I first started trying to plot the band by using the high/low values from the getTodayOHLC.efs file:

    ie. todayHi=call(".\\OHLC\\getTodayOHLC.efs", "High");

    but that did not work because it gets the current high/low for the entire day...so my bands didn't work to well.

    Then I decided to use the functions:
    todayHi = highest(vCounter, high() );
    todayLow = lowest (vCounter, low() );

    where vCounter is a count of bars for the current day...thtat seemed to work, but it seems to HOG the CPU a lot and takes forever for me to create a 1 min chart onn my quad cpu machine...

    so then I tried to create my own running high/low indicators for the day by using my own variables. But what I found was the data never seemed to be correct and the high and lows I start with at 9:30am (I am using a 2 day template for testing, start time 9:30, end 4:00pm) don't matche the high/low bars of my esignal chart (I changed the curstor screen to show the bar#). There must be something wrong with esignal, or something wrong with my code...

    For example, I am looking at VMW (vmware) for the open on Mar26. Esignal shows bar -76 as 24.87 and 24.66 as high/low respectively...when I dump the data in the data window, it shows as 25.08 24.93 respectively...the stock didn't even trade at that level yet!

    Is it my code? Here it is:
    PHP Code:
    var fpArray = new Array();

    function 
    preMain() {
        
    setPriceStudy(true);
        
        
    setStudyTitle("LR_VWAP_MPD");

        
    setCursorLabelName("MPD+1",0);
        
    setDefaultBarFgColor(Color.red,0);
        
    //setPlotType(PLOTTYPE_SQUAREWAVE,2);
        
        // VWAP
        
    setCursorLabelName("VWAP",1);
        
    setDefaultBarFgColor(Color.blue,1);
        
    //setPlotType(PLOTTYPE_SQUAREWAVE,1);
        
    setDefaultBarThickness(2,1);
        
        
    setCursorLabelName("MPD-1",2);
        
    setDefaultBarFgColor(Color.red,2);
        
    //setPlotType(PLOTTYPE_SQUAREWAVE,2);
        
        //askForInput();

        
    var x=0;
        
    fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setDefault();
        }
        
    fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setDefault();
        }
        
    fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
        
    with(fpArray[x++]){
            
    setName("Show Parameters");
            
    setDefault(false);
        }
    }

    // rnd function - round to two places
    function format(value) { // Round the price to 2 digits

        // NOTE: can just return value if u think there is a rounding problem
        
    value *= 100 ;
        return 
    Math.round(value)/100 ;
    }

    var 
    amLib addLibrary("amStudies.efsLib");
    var 
    dsLib addLibrary("dsFunctions.efsLib");
    var 
    bInit false
    var vCounter 0
    var xVWAP null;
    var 
    todayHi=0;
    var 
    todayLow=0;

    function 
    main(Symbol,Interval,Params) {

        if(
    bInit==false){
            
    withamLib ) {
            
                if(
    Symbol == null
                    
    Symbol getSymbol();
                if(
    Interval == null
                    
    Interval getInterval();
                var 
    vSymbol Symbol+","+Interval;
                
    xVWAP amVWAP(sym(vSymbol));
            
                
    setShowTitleParameters(eval(Params));
                
    bInit=true;
            }
        }

        var 
    nVWAP getSeries(xVWAP); //retrieve the VWAP series
        
    var nStdDev 0;

        if(
    getDay(0)!=getDay(-1) && getBarState()==BARSTATE_NEWBAR){ //if a new day and new bar

            
    vCounter=0//set vCounter to 0

            // init to 1st bars of the day...
            
    todayHi high(getCurrentBarIndex());
            
    todayLow low(getCurrentBarIndex());
            
            
    debugPrintln ("------------------NEW DAY---------------------------");
            
    debugPrintln ("bar: " bar "-1st day bar: high= " todayHi " LOW= " todayLow);
        }
        
        if(
    getBarState()==BARSTATE_NEWBAR){ //if a new bar
            
    vCounter++; //increment vCounter by 1

            
    bar=getCurrentBarIndex();
            
            if (
    todayHi high(bar))
            {
                
    debugPrintln ("bar: " bar " NewHI: " format(high(bar)) + " oldhi: " format(todayHi));
                
    todayHi=high(bar);
            }        
            
            if (
    todayLow low(bar))
            {
                
    debugPrintln ("bar: " bar " NewLO: " format(low(bar)) + " oldLo: " format(todayLow));
                
    todayLow=low(bar);
            }

            
    ///*
            
    debugPrintln ("bar: " bar 
                            
    " high : " format(todayHi) +
                            
    " low  : " format(low(bar)) + 
                            
    " open : " format(open(bar)) +
                            
    " close: " format(close(bar)));
            
    //*/
        
    }

        var 
    nVWAP xVWAP.getValue(0);
        if(
    nVWAP==null ) return;

        
    // get highest high/low in lookback period...
        // which should 
        //todayHi = highest(vCounter, high() ); 
        //todayLow = lowest (vCounter, low() ); 
        
        //debugPrintln ("bar: " + getCurrentBarIndex() + " high: " + todayHi + " low: " + todayLow);
        //                    //+ " avg: " + (todayHi-todayLow)/2);
        //debugPrintln("counter: " +vCounter);
        
        
    mpd= (todayHi-todayLow)/2;

        return new Array (  
    nVWAP+(mpd), 
                            
    nVWAP
                            
    nVWAP-(mpd));

    If you uncomment the following lines, yoou will see what happens when it works..note, you should use a 5 min period.

    [php]
    //todayHi = highest(vCounter, high() );

    //todayLow = lowest (vCounter, low() );

    [php]

    attached is a snapshot of how it works with the highest(...) and lowest(...) api

    the 2nd attachment is of how it does not work...note the bar # in the cursor screen, and then the bar number in the left output window...they do not match. I don't know why.
    Attached Files
    Last edited by ljr500; 03-26-2009, 02:19 PM.

  • #2
    follow up...can 't seem to attach 2 images...here is the second one

    Here is the second image, of my version of the high/low calculation...it does NOT work...and note that bar-74 does not match the bar in the left output window.
    Attached Files

    Comment


    • #3
      Re: adding high/low of day bands around a moving averge

      ljr500
      To compute the running High and Low of the day [without resorting to loops] first create two global variables called for example nHigh and nLow and set them both initially to 0.
      The in main create a condition that checks for the first tick of each day and when that occurs assign the current value of high(0) to nHigh and of low(0) to nLow else compute the highest between nHigh and the current high(0) and the lowest between nLow and the current low(0) and assign them to nHigh and nLow respectively [see example enclosed below]
      Alex

      PHP Code:
      if(getBarState()==BARSTATE_NEWBAR && day(0)!=day(-1)){
              
      nHigh high(0)
              
      nLow low(0)
          }else{
              
      nHigh Math.max(nHigh,high(0))
              
      nLow Math.min(nLow,low(0))
          } 

      Originally posted by ljr500
      I am trying to add a moving band around a moving average (in this case a vwap band from the amvwap.efs study - but any moving average will do). What I need to do is find the high/low of the day from the start of the day to the current bar and then just basically do:

      (hi-low)/2 and add/subtract from the vwap band.

      So I first started trying to plot the band by using the high/low values from the getTodayOHLC.efs file:

      ie. todayHi=call(".\\OHLC\\getTodayOHLC.efs", "High");

      but that did not work because it gets the current high/low for the entire day...so my bands didn't work to well.

      Then I decided to use the functions:
      todayHi = highest(vCounter, high() );
      todayLow = lowest (vCounter, low() );

      where vCounter is a count of bars for the current day...thtat seemed to work, but it seems to HOG the CPU a lot and takes forever for me to create a 1 min chart onn my quad cpu machine...

      so then I tried to create my own running high/low indicators for the day by using my own variables. But what I found was the data never seemed to be correct and the high and lows I start with at 9:30am (I am using a 2 day template for testing, start time 9:30, end 4:00pm) don't matche the high/low bars of my esignal chart (I changed the curstor screen to show the bar#). There must be something wrong with esignal, or something wrong with my code...

      For example, I am looking at VMW (vmware) for the open on Mar26. Esignal shows bar -76 as 24.87 and 24.66 as high/low respectively...when I dump the data in the data window, it shows as 25.08 24.93 respectively...the stock didn't even trade at that level yet!

      Is it my code? Here it is:
      PHP Code:
      var fpArray = new Array();

      function 
      preMain() {
          
      setPriceStudy(true);
          
          
      setStudyTitle("LR_VWAP_MPD");

          
      setCursorLabelName("MPD+1",0);
          
      setDefaultBarFgColor(Color.red,0);
          
      //setPlotType(PLOTTYPE_SQUAREWAVE,2);
          
          // VWAP
          
      setCursorLabelName("VWAP",1);
          
      setDefaultBarFgColor(Color.blue,1);
          
      //setPlotType(PLOTTYPE_SQUAREWAVE,1);
          
      setDefaultBarThickness(2,1);
          
          
      setCursorLabelName("MPD-1",2);
          
      setDefaultBarFgColor(Color.red,2);
          
      //setPlotType(PLOTTYPE_SQUAREWAVE,2);
          
          //askForInput();

          
      var x=0;
          
      fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
          
      with(fpArray[x++]){
              
      setDefault();
          }
          
      fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
          
      with(fpArray[x++]){
              
      setDefault();
          }
          
      fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
          
      with(fpArray[x++]){
              
      setName("Show Parameters");
              
      setDefault(false);
          }
      }

      // rnd function - round to two places
      function format(value) { // Round the price to 2 digits

          // NOTE: can just return value if u think there is a rounding problem
          
      value *= 100 ;
          return 
      Math.round(value)/100 ;
      }

      var 
      amLib addLibrary("amStudies.efsLib");
      var 
      dsLib addLibrary("dsFunctions.efsLib");
      var 
      bInit false
      var vCounter 0
      var xVWAP null;
      var 
      todayHi=0;
      var 
      todayLow=0;

      function 
      main(Symbol,Interval,Params) {

          if(
      bInit==false){
              
      withamLib ) {
              
                  if(
      Symbol == null
                      
      Symbol getSymbol();
                  if(
      Interval == null
                      
      Interval getInterval();
                  var 
      vSymbol Symbol+","+Interval;
                  
      xVWAP amVWAP(sym(vSymbol));
              
                  
      setShowTitleParameters(eval(Params));
                  
      bInit=true;
              }
          }

          var 
      nVWAP getSeries(xVWAP); //retrieve the VWAP series
          
      var nStdDev 0;

          if(
      getDay(0)!=getDay(-1) && getBarState()==BARSTATE_NEWBAR){ //if a new day and new bar

              
      vCounter=0//set vCounter to 0

              // init to 1st bars of the day...
              
      todayHi high(getCurrentBarIndex());
              
      todayLow low(getCurrentBarIndex());
              
              
      debugPrintln ("------------------NEW DAY---------------------------");
              
      debugPrintln ("bar: " bar "-1st day bar: high= " todayHi " LOW= " todayLow);
          }
          
          if(
      getBarState()==BARSTATE_NEWBAR){ //if a new bar
              
      vCounter++; //increment vCounter by 1

              
      bar=getCurrentBarIndex();
              
              if (
      todayHi high(bar))
              {
                  
      debugPrintln ("bar: " bar " NewHI: " format(high(bar)) + " oldhi: " format(todayHi));
                  
      todayHi=high(bar);
              }        
              
              if (
      todayLow low(bar))
              {
                  
      debugPrintln ("bar: " bar " NewLO: " format(low(bar)) + " oldLo: " format(todayLow));
                  
      todayLow=low(bar);
              }

              
      ///*
              
      debugPrintln ("bar: " bar 
                              
      " high : " format(todayHi) +
                              
      " low  : " format(low(bar)) + 
                              
      " open : " format(open(bar)) +
                              
      " close: " format(close(bar)));
              
      //*/
          
      }

          var 
      nVWAP xVWAP.getValue(0);
          if(
      nVWAP==null ) return;

          
      // get highest high/low in lookback period...
          // which should 
          //todayHi = highest(vCounter, high() ); 
          //todayLow = lowest (vCounter, low() ); 
          
          //debugPrintln ("bar: " + getCurrentBarIndex() + " high: " + todayHi + " low: " + todayLow);
          //                    //+ " avg: " + (todayHi-todayLow)/2);
          //debugPrintln("counter: " +vCounter);
          
          
      mpd= (todayHi-todayLow)/2;

          return new Array (  
      nVWAP+(mpd), 
                              
      nVWAP
                              
      nVWAP-(mpd));

      If you uncomment the following lines, yoou will see what happens when it works..note, you should use a 5 min period.

      [php]
      //todayHi = highest(vCounter, high() );

      //todayLow = lowest (vCounter, low() );

      [php]

      attached is a snapshot of how it works with the highest(...) and lowest(...) api

      the 2nd attachment is of how it does not work...note the bar # in the cursor screen, and then the bar number in the left output window...they do not match. I don't know why.

      Comment


      • #4
        Eureka, this works! and it is 10x faster!

        Thanks very much...but I have to ask, why do you use index 0 rather than
        getCurrentBarIndex()?

        I changed my code that didn't work to use 0 rather than getCurrentBarIndex() and it worked too... I might be missing some basic concept here...

        Don't we have to start at the first bar being displayed then go to the last (which is 0)?
        that was why I had to loop...from the first index, to the last (0)...not sure about using 0 all th e time...

        if not, when do we use getCurrentBarIndex()?

        Thanks again for the QUICK response.

        Larry JR

        Comment


        • #5
          Larry
          You are most welcome.
          With regards to your questions see the explanations I provided in this [and subsequent posts in that thread] in response to similar questions
          Alex


          Originally posted by ljr500
          Eureka, this works! and it is 10x faster!

          Thanks very much...but I have to ask, why do you use index 0 rather than
          getCurrentBarIndex()?

          I changed my code that didn't work to use 0 rather than getCurrentBarIndex() and it worked too... I might be missing some basic concept here...

          Don't we have to start at the first bar being displayed then go to the last (which is 0)?
          that was why I had to loop...from the first index, to the last (0)...not sure about using 0 all th e time...

          if not, when do we use getCurrentBarIndex()?

          Thanks again for the QUICK response.

          Larry JR

          Comment


          • #6
            I'm still a bit confused...

            From:
            http://forum.esignalcentral.com/show...161#post124161

            The function getCurrentBarIndex() returns the index of a bar relative to the most recent bar in the chart which has an index of 0. The bar index parameter used in the close() function is instead relative to the bar being processed. So if you pass Idx [to which you have assigned the value of getCurrentBarIndex()] to the close() function you are in effect requesting the value of the Close at Idx bars prior to the bar being processed at that time [while the formula runs through historical bars] and not the value of the Close at a specific bar index.
            getCurrentBarIndex() does not take any parameters, so what does it actually return? I thought it returned the bar index that we are currently working wit.

            ie, say I have a chart that starts at 9:30 and it is 10:30, 1 minute bar, time template starts at 9:30, 1 day, so first bar is -60. If ensign is drawing/loading bar -50 (the 10th bar in the array from the start), shouldn't getcurrentbarIndex() return bar -50 or will it return -10? [I havn't tried it to see]

            I might be missunderstanding this function...if so/not do I ever really need to use it?

            Thanks again.
            Larry

            Comment


            • #7
              Larry

              getCurrentBarIndex() does not take any parameters, so what does it actually return? I thought it returned the bar index that we are currently working wit.
              getCurrentBarIndex() returns the bar index of a bar relative to the most recent bar on the chart.
              Assuming you have loaded 300 bars in your chart then getCurrentBarIndex() will return -299 on the oldest bar, -298 on the second most oldest bar and so on and will return -1 on the second most recent bar and 0 on the most recent bar
              The bar index parameter in the function high(), low() etc [ie high(0), high(-1) and so on] is instead relative to the bar being processed and not relative to the most recent bar on the chart
              For example while the formula engine runs through historical bars and is processing bar index -200 then high(-1) will return the High of the bar prior to the one being processed at that moment ie the High of bar index -201 because that is bar -1 relative to bar index -200.
              It should be apparent at this point that if the formula is processing bar index -200 and you call high(getCurrentBarIndex()) you are actually retrieving the High 200 bars back relative to bar index -200 in other words you are retrieving the High at getCurrentBarIndex()== -400
              As to its use I show an example of it in the same thread I indicated earlier and if you search the forums you will find hundreds of other examples.
              Alex


              Originally posted by ljr500
              I'm still a bit confused...



              getCurrentBarIndex() does not take any parameters, so what does it actually return? I thought it returned the bar index that we are currently working wit.

              ie, say I have a chart that starts at 9:30 and it is 10:30, 1 minute bar, time template starts at 9:30, 1 day, so first bar is -60. If ensign is drawing/loading bar -50 (the 10th bar in the array from the start), shouldn't getcurrentbarIndex() return bar -50 or will it return -10? [I havn't tried it to see]

              I might be missunderstanding this function...if so/not do I ever really need to use it?

              Thanks again.
              Larry

              Comment


              • #8
                It should be apparent at this point that if the formula is processing bar index -200 and you call high(getCurrentBarIndex()) you are actually retrieving the High 200 bars back relative to bar index -200 in other words you are retrieving the High at getCurrentBarIndex()== -400
                AHA! I got it...

                Thanks for the explanation.

                Comment


                • #9
                  Larry
                  My pleasure
                  Alex


                  Originally posted by ljr500
                  AHA! I got it...

                  Thanks for the explanation.

                  Comment

                  Working...
                  X