Announcement

Collapse
No announcement yet.

Constant share moving average

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

  • Constant share moving average

    I am looking for a way to produce a volume weighted moving average of price over a given number of prior traded shares (input paramater).

    We have a script for volume weighted average price. This is an average over a given number of prior interval bars.

    I would like a similar calculation but instead of over a constant number of prior interval bars (like 10 or 50) I want the weighted average computed over the last 1 million shares for example.

    The shares could be aggregated into small intervals such as 5 minutes and the volume weighted average price computed for all past bars until the cumulative volume equals an input parameter.

    Does such a script exist or anything close?

    Mike Scott
    Tarzana, CA
    ....Mike

  • #2
    Constant Share Moving Average

    My question was moved to this forum perhaps by mistake.
    I have had zero responses, no doubt. So below is the script I created to try to make a volume weighted moving average over a fixed number of shares and So far I think I have an infinite loop that needs to be shut down abnormally.

    Please help and possibly move this back to another forum.

    Mike Scott


    // This formula written by Mike Scott
    // This formula computes an average volume-weighted average price for stocks over a fixed number of traded shares
    // The user inputs a value of Active Float representing a hypothetical number of shares being traded by active traders
    // Active Float should be much less than the Total Float
    // Current price is then plotted as a percent above or below the volume-weighted average price
    // This price may then represent the current average profit position for the active traders

    function preMain() {

    setStudyTitle("Active Boundaries");
    setPriceStudy(false);
    setPlotType(PLOTTYPE_LINE, 0);
    setDefaultBarFgColor(Color.RGB(0,0,255), 0);
    setStudyMin(-50);
    setStudyMax(50);
    }
    function main(nActiveFloat) {
    if(nActiveFloat == null) {
    nActiveFloat = 500000; //500 thousand shares
    }
    var vCumVol = 0;
    var a5minV = new Array(); // Array for collecting 5 minute volume
    var a5minC = new Array(); //Array for collecting 5 minute close prices

    //Find 5-minute interval when cumulative volume equals Active Float and compute values
    i =0;

    while(vCumVol < nActiveFloat) {
    vVol = volume(-1*i, inv("5"));
    vCumVol += vVol;
    a5minV.push(vVol);
    a5minC.push(close(-1*i, inv("5")));
    i++;
    nLasti = i
    }
    //Compute volume weighted average price

    vVolWtdPrice = 0;
    vVolWtdAvgPrice = 0;
    i = 0;

    while(i < nLasti) {
    vVolWtdPrice += a5minC(-i) * a5minV(-i);
    i++
    }
    vVolWtdAvgPrice = vVolWtdPrice/vCumVol

    //Now compute percent profit

    var nProfit = 100 * (close(0)/vVolWtdAvgPrice -1)

    Return(nProfit);
    }
    ....Mike

    Comment


    • #3
      from what I could see, you had an unending loop setup in the code and were missing a variable declaration.

      Try this..

      PHP Code:
      // This formula written by Mike Scott
      // This formula computes an average volume-weighted average price for stocks over a fixed number of traded shares
      // The user inputs a value of Active Float representing a hypothetical number of shares being traded by active traders
      // Active Float should be much less than the Total Float
      // Current price is then plotted as a percent above or below the volume-weighted average price
      // This price may then represent the current average profit position for the active traders

      function preMain() {

      setStudyTitle("Active Boundaries");
      setPriceStudy(false);
      setPlotType(PLOTTYPE_LINE0);
      setDefaultBarFgColor(Color.RGB(0,0,255), 0);
      setStudyMin(-50);
      setStudyMax(50);
      }

      var 
      nBarCounter 0;
      var 
      nLasti 0;

      function 
      main(nActiveFloat) {
        if(
      nActiveFloat == null) {
          
      nActiveFloat 500000//500 thousand shares
        
      }

        
        var 
      nState getBarState();
        if (
      nState == BARSTATE_NEWBAR) {
          
      nBarCounter++;
        }  
      var 
      vCumVol 0;
      var 
      a5minV = new Array(); // Array for collecting 5 minute volume
      var a5minC = new Array(); //Array for collecting 5 minute close prices

      //Find 5-minute interval when cumulative volume equals Active Float and compute values
      =0;
      nLasti 0;

      while ( ((
      nBarCounter+(-1*i)) > 0) && (vCumVol nActiveFloat)) {
      vVol volume((-1*i), inv("5"));
      vCumVol += vVol;
      a5minV.push(vVol);
      a5minC.push(close((-1*i), inv("5")));
      i++;
      nLasti i;
      }
      //Compute volume weighted average price

      vVolWtdPrice 0;
      vVolWtdAvgPrice 0;
      0;

      while(
      < (nLasti-1)) {
       
      vVolWtdPrice += a5minC[i] * a5minV[i];
       
      i++
      }
      vVolWtdAvgPrice vVolWtdPrice/vCumVol;

      //Now compute percent profit

      var nProfit 100 * (close(0)/vVolWtdAvgPrice -1)

        return(
      nProfit);

      Brad Matheny
      eSignal Solution Provider since 2000

      Comment


      • #4
        Constant share moving average

        Brad, Thank You for your help.

        I made an attempt with your code and I am getting an error that is leading me astray. It is one of those can't find a ";" type error. Script follows. I am stuck until I figure this out. Programming is not my day job

        Mike Scott
        Tarzana, CA

        //This script was written by Mike Scott
        // It is inspired by concepts written by Pascal Willain "Value in Time"
        // This formula computes an average volume-weighted average price for stocks over a fixed number of traded shares
        // The user inputs a value of Active Float representing a hypothetical number of shares being traded by active traders
        // Active Float should be much less than the Total Float
        // Current price is then plotted as a percent above or below the volume-weighted average price
        // This price may then represent the current average profit position for the active traders

        function preMain() {

        setStudyTitle("Active Boundaries");
        setPriceStudy(false);
        setPlotType(PLOTTYPE_LINE, 0);
        setDefaultBarFgColor(Color.RGB(0,0,255), 0);
        setStudyMin(-50); setStudyMax(50);
        }
        var nBarCounter =0;
        var nLasti =0;

        function main(nActiveFloat) {
        if(nActiveFloat == null) {
        nActiveFloat = 500000; //500 thousand shares
        }

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

        var vCumVol = 0;
        var a5minV = new Array(); // Array for collecting 5 minute volume
        var a5minC = new Array(); //Array for collecting 5 minute close prices


        //Find 5-minute interval when cumulative volume equals Active Float and compute values

        i =0;

        while( ((nBarCounter(-1*i)) > 0) && (vCumVol < nActiveFloat)) {
        vVol = volume(-1*i, inv("5"));
        vCumVol += vVol;
        a5minV.push(vVol);
        a5minC.push(close(-1*i, inv("5")));
        i++;
        nLasti = i;
        }
        //Compute volume weighted average price

        vVolWtdPrice = 0;
        vVolWtdAvgPrice = 0;
        i = 0;

        while(i < nLasti) {
        vVolWtdPrice += a5minC(i) * a5minV(i);
        i++;
        }
        vVolWtdAvgPrice = vVolWtdPrice/vCumVol;

        //Now compute percent profit

        nProfit = 100 * (close(0)/vVolWtdAvgPrice -1);


        Return nProfit;
        }
        ....Mike

        Comment


        • #5
          Try this. It loads and runs on my PC.

          B
          Attached Files
          Brad Matheny
          eSignal Solution Provider since 2000

          Comment

          Working...
          X