Announcement

Collapse
No announcement yet.

MA offset

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

  • MA offset

    What is the syntax for returning a moving average in an offset position to the price chart?

    Thanx

  • #2
    Hello wrandall,

    The second parameter of an MAStudy is the offset value you need to specify. A positive number will shift the study forward and a negative will shift the study backwards.

    PHP Code:
    var study = new MAStudy(33"Close"MAStudy.SIMPLE);

    function 
    main() {
         var 
    vDMA study.getValue(MAStudy.MA);
         if (
    vDMA == null) {
              return;
         }
         return 
    vDMA;

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Thanks. I should have asked the preliminary question, how do I create a MAStudy using my own custom weighting? I have already written the code that creates the weights and the output of the filter. I need to return it to the price chart in an offset position. Can you help?

      Comment


      • #4
        Hello wrandall,

        I have two options for you to try.

        Option 1: Create a global array to store your values so that you can return the specific "offset" value of your custom ma. The example below charts a simple MA shifted forward by 3 bars. You can use this method by setting the vMA to your custom MA values.

        PHP Code:
        var study null
        var 
        myArray null;
        var 
        vMA null;

        function 
        main(nLength) {
            if (
        nLength == null)
                
        nLength 10;
            
            if (
        myArray == null || study == null) {
                
        myArray = new Array(nLength);
                
        study = new MAStudy(nLength0"Close"MAStudy.SIMPLE);
            }
            
            
        vMA study.getValue(MAStudy.MA);
            if (
        vMA == null)
                return;
            
            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        myArray.pop()       // removes the last element of the array
                
        myArray.unshift(vMA)   // inserts a new element to the front and shifts the others by 1.
            
        } else {
                
        myArray[0] = vMA;
            }
            
            return 
        myArray[2];

        Option 2: Use the offset parameter in your getValue() statements when performing the calculations of your custom ma.
        The example below will give you the 5 closing values starting at bar index of -10. You could then calculate your custom ma on those 5 values and plot the ma on the current bar. This method will also shift your custom ma forward by 5 bars.

        PHP Code:
        function main() {
            var 
        vPrices getValue("Close", -105);
            if (
        getCurrentBarIndex() == -1) {
                
        debugPrintln(vPrices);  // open your formula output window to view the results.
            
        }

            
        // perform custom ma calculations here and set vMA to your custom ma

            
        return vMA;

        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          Thanks for the time and effort trying to help me. What I'm attempting to do is recreate the "offset" function that is part of the moving average and envelope builtin studies, i.e. centering my moving average/envelope. The solutions you have presented all return the value to the current bar. I wish to return the value to a bar other than the current bar. Since the builtin studies can do it I assumed it was only a matter of syntax to accomplish the same thing for a non-builtin study. Am I mistaken? Please help!

          Thanks

          Comment


          • #6
            Hi,

            I had posted something about this that I thought did a reasonable job of explaining what you need to do, and why. But it seems to have disappeared into the void....

            I assume you are trying to offset into the future. The problem being, that EFS really only can draw for the current bar (this isn't 100% true, there are drawLinexxx() functions that allow you to draw anywhere, but they have limitations). Therefore what you have to do is look backwards in time from the current bar to caculate what the offset MA would be for the current bar.

            You can look back in time by using negative offsets to open(), close(), etc. So close(-1) would look one bar back, etc.

            The means you will never see the nice effect of having the forward offset MA's displayed in front of the current price...only what the offset MA is at the current price.

            You COULD use the drawLinnexxx() routines (I suspect you would want to use drawLineRelative()) and specify each forward point...if you did this to get the nice "forward looking effect", you would specify +ve bars numbers to the drawline function to specify bars that have yet to happen.

            If you did the above, you will have a bit of a headache keeping track of things, but not too bad. The real downside would be that you would get no vlaues in the cursor window...which for me at least is very important. I want to know what those MA values are...

            The best plan might be to use drawLinexxx for lines into the future, and then delete the line segment for the current bar when you get there and use return() to draw it again...in that way you will get all historic bars values in the cursor window (up to and including the current bar), but only the future bars exact values would be a mystery.

            Not sure if all of this is clear or not...
            Garth

            Comment


            • #7
              Thanks! You have certainly and clearly answered my question. This forum is terrific!

              Comment

              Working...
              X