Announcement

Collapse
No announcement yet.

back testing code - Moving average of On Balance volume

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

  • back testing code - Moving average of On Balance volume

    I have plotted the '20 day moving average' of the 'On Balance Volume' on my chart and am trying to write a go-long or go-short strategy for backtesting when the moving average (of the obv) line crosses over the 'On Balance Volume'.

    In other words

    IF the (20 day Moving average of OBV) is greater than the OBV then go LONG and
    IF the (20 Day Moving average of OBV) is less than the OBV then go SHORT.

    How do I write this code? Any help will be much appreciated. Thanks.

  • #2
    EFS Code for moving average of 'On Balance Volume'.

    To help confirm a price trend reversal with the OBV a 20-period moving average of the OBV is often added. When the OBV crosses the 20-period moving average the divergence signal of a trend reversal is confirmed.

    In other words

    IF the (20 day Moving average of OBV) is greater than the OBV then go LONG and
    IF the (20 Day Moving average of OBV) is less than the OBV then go SHORT.

    e.g A Moving Average (sma,ema) is an indicator that shows the average value of a security's PRICE over a period of time, but how do I code if I want to get the 'Moving average' of the On Balance Volume?

    Any help will be much appreciated. Thanks.
    Last edited by SNR2012; 11-20-2012, 04:06 AM.

    Comment


    • #3
      How to code " 20 day moving average o the On Balance Volume" in EFS ?

      I am updating my question below as I feel I may have not asked it correctly earlier.

      -------Question------------------------------------------------------------------------------------
      To help confirm a price trend reversal with the OBV a 20-period moving average of the OBV is often added. When the OBV crosses the 20-period moving average the divergence signal of a trend reversal is confirmed.

      In other words

      IF the (20 day Moving average of OBV) is greater than the OBV then go LONG and
      IF the (20 Day Moving average of OBV) is less than the OBV then go SHORT.

      e.g A Moving Average (sma,ema) is an indicator that shows the average value of a security's PRICE over a period of time, but how do I code if I want to get the 'Moving average' of the On Balance Volume?

      Any help will be much appreciated. Thanks.

      Comment


      • #4
        I'm new too EFS, this may get you started in the right direction

        /************************************************** *******
        Alexis C. Montenegro © May 2003
        Use and/or modify this code freely. If you redistribute it
        please include this and/or any other comment blocks and a
        description of any changes you make.
        ************************************************** ********/

        var vOBVStudy = null;
        var vMAofOBV = null;

        function preMain() {
        setStudyTitle("MAofOBV");
        setCursorLabelName("OBV", 0);
        setColorPriceBars(true);
        setDefaultPriceBarColor(Color.black);
        setDefaultBarFgColor(Color.blue, 0);
        setCursorLabelName("MAofOBV", 1);
        setDefaultBarFgColor(Color.red, 1);
        checkVersion(2,"http://share.esignal.com/ContentRoot/ACM%20Test/Formulas/MAofOBV.efs"); //Comment this line out if modifying the code

        var fp1 = new FunctionParameter("MALength", FunctionParameter.NUMBER);
        fp1.setLowerLimit(1);
        fp1.setDefault(10); //Edit this value to set a new default

        var fp4 = new FunctionParameter("MAType", FunctionParameter.STRING);
        fp4.setName("MAType");
        fp4.addOption("MAStudy.SIMPLE");
        fp4.addOption("MAStudy.EXPONENTIAL");
        fp4.addOption("MAStudy.WEIGHTED");
        fp4.addOption("MAStudy.VOLUMEWEIGHTED");
        fp4.setDefault("MAStudy.SIMPLE"); //Edit this value to set a new default

        }

        function main(MALength, MAType) {

        if (vOBVStudy == null) vOBVStudy = new OBVStudy(OBVStudy.OBV);
        if (vMAofOBV == null) vMAofOBV = new MAStudy(MALength, 0, vOBVStudy, MAStudy.MA, eval(MAType));

        /******************************************
        Insert your code following this text block
        Use vOBVStudy.getValue(OBVStudy.OBV) and
        vMAofOBV.getValue(MAStudy.MA) for your code
        *******************************************/
        nOBVStudy = vOBVStudy.getValue(OBVStudy.OBV,-1);
        nMAofOBV = vMAofOBV.getValue(MAStudy.MA,-1);

        if(nOBVStudy == null || nMAofOBV == null) return;
        if (getCurrentBarIndex() == 0) return;

        if(nOBVStudy >= nMAofOBV && !Strategy.isLong())
        Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.NEXTBAR);
        if(nOBVStudy < nMAofOBV && !Strategy.isShort())
        Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.NEXTBAR);
        if(Strategy.isLong())
        setPriceBarColor(Color.lime);
        else if(Strategy.isShort())
        setPriceBarColor(Color.red);

        return new Array ( vOBVStudy.getValue(OBVStudy.OBV),
        vMAofOBV.getValue(MAStudy.MA));
        }

        Comment


        • #5
          Thanks Dosnoris,
          This was helpful.

          Comment


          • #6
            Alexis...Thank you for developing the OBV moving average...
            My primary income is from trading equities, so my focus is almost exclusively on developing an effective trading methodology.
            I made a concious choice between expertise in trading OR programming.
            I've been working on this OBV moving average for some time.
            now I have my "missing link"
            Thanks Alexis.
            Peter.

            Comment


            • #7
              Peter
              Thank you for your comments even though I do not recall having written the strategy except for the underlying study that calculates the OBV and its moving average
              That said it seems to me that the strategy has a logic error whereby it skips a bar before entering a trade. This is because it checks for the conditions on the bar prior to the one being processed and then enters the trade on the Open of the bar following the one being processed. To correct this you should change the strategy fillbar from NEXTBAR to THISBAR
              Also that formula is somewhat outdated as it was written using the legacy efs functions. You can find a version of the OBV study and its average written using the efs2 functions here
              Alex


              Originally posted by peterjerome View Post
              Alexis...Thank you for developing the OBV moving average...
              My primary income is from trading equities, so my focus is almost exclusively on developing an effective trading methodology.
              I made a concious choice between expertise in trading OR programming.
              I've been working on this OBV moving average for some time.
              now I have my "missing link"
              Thanks Alexis.
              Peter.

              Comment


              • #8
                [QUOTE=Alexis C. Montenegro;145525]Peter
                Thank you for your comments even though I do not recall having written the strategy except for the underlying study that calculates the OBV and its moving average
                That said it seems to me that the strategy has a logic error whereby it skips a bar before entering a trade. This is because it checks for the conditions on the bar prior to the one being processed and then enters the trade on the Open of the bar following the one being processed. To correct this you should change the strategy fillbar from NEXTBAR to THISBAR
                Also that formula is somewhat outdated as it was written using the legacy efs functions. You can find a version of the OBV study and its average written using the efs2 functions here

                Thanks Alex...installed updated OBV Study works good. FWIW the new OBV study gave error on Line 66 missing... )
                one of the ")" was replaced with numeric 0. may have happened during download? no problem...works fine now.
                Thanks Alex
                Peter

                Comment

                Working...
                X