Announcement

Collapse
No announcement yet.

Using a 15 minute MA in a 5 minute backtest

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

  • Using a 15 minute MA in a 5 minute backtest

    How would you do it? Ive used daily MA values in a backtest on 5 minute charts before using a piece of code that was posted earlier. It was a function called dailyVMA. I tried to alter it to use a 15 minute interval instead of daily, but had no luck. Can anyone give me any ideas on how to backtest looking at a 15 minute MA on a 5 minute chart? Thanks!

  • #2
    Build it and they will come...

    You have to build the MA for 15 minutes out of the 5 minute data.

    It is not super hard to do, but in this case, the best solution if to build the indicator you need from the existing data.

    For example...

    var v15MALength = 10
    var vMAArray;

    if (getInterval() == "5") {
    vMAArray = new Array();
    var x = 0;
    var MACount = -1;
    // Build the array of 15 minute closing price data.
    while (MACount <=v15MALength ) {
    if (x>0) && ((x/3) == Math.abs(x/3)) { // time to record a 15 minute close
    MACount += 1;
    vMAArray[MACount] = close((0-x)+3);
    }
    x++;
    }

    // Calc the 15 Min MA
    var MASum = 0;
    for (x=0; x<=MACount ; x++) {
    MASum += vMAArray[x];
    }
    // Record the finished 15 Min MA Value
    MASum = MASum/MACount;

    }

    This code has not been tested - it came from my head. The logic should be correct. The only thing that might need corrections is the functioning of the array and the price variables.

    Other than that, this is a good example of how I accomplish this task.

    Brad
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Thanks Doji, that worked well with a few modifications. There is a small issue of this function always going 15 minutes behind the current bar.. as opposed to what you would get on a 15 minute chart with an MA. But I cant think of an easy way to use the proper 15 minute bars. In case anyone wants to use this I have made it into a function that is working for me.


      function VMA15(v15MALength) {
      if (v15MALength == null) {
      v15MALength = 8;
      }
      var vMAArray;

      if (getInterval() == "5") {
      vMAArray = new Array();
      var x = 0;
      var MACount = -1;
      // Build the array of 15 minute closing price data.
      while (MACount < v15MALength ) {
      if ((x>0) && ((x/3) == Math.abs(x/3))) { // time to record a 15 minute close
      MACount += 1;
      vMAArray[MACount] = close((0-x)+3);
      }
      x++;
      }
      // Calc the 15 Min MA
      var MASum = 0;
      for (x=0; x<MACount ; x++) {
      MASum += vMAArray[x];
      }
      // Record the finished 15 Min MA Value
      MASum = MASum / v15MALength;
      }
      return MASum;
      }

      Comment


      • #4
        Super...

        I wrote the function to only operate on "finished bars".

        The basics of the function are such that you can modify it to include the current bar (closing price) if necessary. Building and populating an array like this is a good way to handle lots of different things.

        Glad I could help and glad you were able to share your "interpretative needs" of the function with others.

        let me know if you need more help..

        Brad
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          I have added some code that allows the MA to be shifted right or left using drawShapeRelative

          function preMain(){
          setPriceStudy(true)
          }

          function main(v15MALength) {
          if (v15MALength == null) {
          v15MALength = 8;
          }
          var vMAArray;

          if (getInterval() == "5") {
          vMAArray = new Array();
          var x = 0;
          var MACount = -1;
          // Build the array of 15 minute closing price data.
          while (MACount < v15MALength ) {
          if ((x>0) && ((x/3) == Math.abs(x/3))) { // time to record a 15 minute close
          MACount += 1;
          vMAArray[MACount] = close((0-x)+3);
          }
          x++;
          }
          // Calc the 15 Min MA
          var MASum = 0;
          for (x=0; x<MACount ; x++) {
          MASum += vMAArray[x];
          }
          // Record the finished 15 Min MA Value
          MASum = MASum / v15MALength;
          }


          drawShapeRelative(1, MASum, Shape.CIRCLE, "", Color.RGB(155,0,0), Shape.LEFT);//adjust the first term to move the MA left or right

          return MASum; //use return null; if you just want the shapes.
          }

          Comment


          • #6
            What I had meant was that this formula takes the end of the 15 minute bar from the end of the current bar. For instance, normally the 15 minute bars are from 10:00 - 10:15, 10:15 - 10:30, etc. However this formula, at 10:25 would create the 15 minute bars as follows: 9:55 - 10:10, 10:10 - 10:25. Perhaps you can setup an offset for the last bar by dividing getMinute() by 5 and then moding it by 3 (can you do that with EFS?). and then look at 9:45 - 10:00, 10:00 - 10:15 and 10:15 - 10:25.

            Comment


            • #7
              I understand...

              Here is the solution...

              PHP Code:

              function VMA15(v15MALength) {
              if (
              v15MALength == null) {
              v15MALength 8;
              }
              var 
              vMAArray;

              if ((
              getInterval() == "5") && (Math.abs( ( (getHour()*100)+getMinute())/15) == ((getHour()*100)+getMinute())) {
                 
              vMAArray = new Array();
                 var 
              0;
                 var 
              MACount = -1;
              // Build the array of 15 minute closing price data.
                 
              while (MACount v15MALength ) {
                    if ((
              x>0) && ((x/3) == Math.abs(x/3))) { // time to record a 15    minute close
                    
              MACount += 1;
                    
              vMAArray[MACount] = close((0-x)+3);
                    }
                    
              x++;
                 }
                 
              // Calc the 15 Min MA
                 
              var MASum 0;
                 for (
              x=0x<MACount x++) {
                    
              MASum += vMAArray[x];
                 }
                 
              // Record the finished 15 Min MA Value
                 
              MASum MASum v15MALength;
                 }

                 return 
              MASum;

              The addition of...

              (Math.abs( ( (getHour()*100)+getMinute())/15) == ((getHour()*100)+getMinute())

              Verifies the time interval is ON the 15 minute interval. Otherwise, the function will not work. This is one way to resolve the issue you are having.

              Brad
              Brad Matheny
              eSignal Solution Provider since 2000

              Comment


              • #8
                Ignore that last post..

                My mind was not working properly...

                PHP Code:
                function VMA15(v15MALength) {
                if (
                v15MALength == null) {
                v15MALength 8;
                }
                var 
                vMAArray;

                if ((
                getInterval() == "5") && (Math.abs( ( (getHour()*100)+getMinute())/15) == (((getHour()*100)+getMinute())/15)) {
                   
                vMAArray = new Array();
                   var 
                0;
                   var 
                MACount = -1;
                // Build the array of 15 minute closing price data.
                   
                while (MACount v15MALength ) {
                      if ((
                x>0) && ((x/3) == Math.abs(x/3))) { // time to record a 15    minute close
                      
                MACount += 1;
                      
                vMAArray[MACount] = close((0-x)+3);
                      }
                      
                x++;
                   }
                   
                // Calc the 15 Min MA
                   
                var MASum 0;
                   for (
                x=0x<MACount x++) {
                      
                MASum += vMAArray[x];
                   }
                   
                // Record the finished 15 Min MA Value
                   
                MASum MASum v15MALength;
                   }

                   return 
                MASum;

                Here is the proper solution. Working too hard I guess..

                Brad
                Brad Matheny
                eSignal Solution Provider since 2000

                Comment

                Working...
                X