Announcement

Collapse
No announcement yet.

Study On A Study

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

  • Study On A Study

    so I came up with this simple little formula to plot $ADD as a histogram, any way to plot a 20ema of the $ADD on the same?

    function preMain()
    {
    setStudyTitle("ADVDEC");
    setCursorLabelName("ADD");
    setDefaultBarFgColor(Color.red);
    setPlotType(PLOTTYPE_HISTOGRAM);

    }
    function main()
    {
    var vA;



    vA = getValue("Close",0,1,"$ADD");


    return (vA);
    }

  • #2
    Hello paulford,

    Try the following. I'm using a global array to store the previous 19 values of vA to calculate the moving average, which includes the current bar's vA. At the top of main when our bar state is NEWBAR, update the array before calculating your new value for vA.

    PHP Code:
    addBand(0PS_SOLID1Color.red"zero");

    function 
    preMain() {
        
    setStudyTitle("ADVDEC");
        
    setCursorLabelName("ADD"0);
        
    setCursorLabelName("ADD MA"1);
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarFgColor(Color.blue1);
        
    setPlotType(PLOTTYPE_HISTOGRAM0);
    }

    var 
    myArray = new Array(19);
    var 
    vA null;

    function 
    main() {
        if (
    getBarState() == BARSTATE_NEWBAR && vA != null) {
            
    myArray.pop();
            
    myArray.unshift(vA);
        }
        
        
    vA getValue("Close",0,1,"$ADD") * 1;
        if (
    vA == null)
            return;
        
        var 
    vAma null;
        if (
    myArray[18] != null) {
            var 
    dSum 0;
            for (
    019; ++i) {
                
    dSum += myArray[i];
            }
            
    vAma = (dSum vA)/20;
        }
        return new Array(
    vAvAma);

    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
      wow that's works great, thank you very much!

      is there anyway i can tweak that moving average into an exponential?

      Comment


      • #4
        Hello paulford,

        I thought you might ask for that. Here you go. I set it up so that you can use the "Edit Studies" option to toggle this formula from simple to exponential. For the input parameter, enter EMA or MA.

        PHP Code:
        addBand(0PS_SOLID1Color.red"zero");

        function 
        preMain() {
            
        setStudyTitle("ADVDEC");
            
        setCursorLabelName("ADD"0);
            
        setCursorLabelName("ADD MA"1);
            
        setDefaultBarFgColor(Color.red0);
            
        setDefaultBarFgColor(Color.blue1);
            
        setPlotType(PLOTTYPE_HISTOGRAM0);
        }

        var 
        dLastMA2 null;
        var 
        dThisMA2 null;
        var 
        bPrimed2 false;
        var 
        dPercent2 = (2.0 / (20 1.0));


        function 
        ema() {
            var 
        nBarState getBarState();
            var 
        dValue2;
            var 
        dSum 0.0;

            if(
        nBarState == BARSTATE_ALLBARS) {
                
        // reset!
                
        dLastMA2 null;
                
        dThisMA2 null;
            }

            if(
        nBarState == BARSTATE_NEWBAR) {
                
        dLastMA2 dThisMA2;
            }
            
        dThisMA2 dLastMA2;
            
            if (
        myArray[18] != null) {
                if (
        bPrimed2 == false) {
                    
        dValue2 = new Array(19);
                    for (
        019; ++i) {
                        
        dValue2[i] = myArray[i];
                    }
                    
        dSum 0;
                    for(
        019i++) {
                        
        dSum += dValue2[i];
                    }
                    
        dSum += vA;
                    
        dLastMA2 dSum 20;
                    
        dThisMA2 dLastMA2;
                    
        bPrimed2 true;
                } else {
                    
        dThisMA2 = (vA dLastMA2) * dPercent2 dLastMA2;
                }
            }
            return 
        dThisMA2;
        }

        var 
        myArray = new Array(19);
        var 
        vA null;

        function 
        main(nType) {
            if (
        nType == null) {
                
        nType "EMA";
            } else {
                
        nType "MA";
            }
            
            if (
        getBarState() == BARSTATE_NEWBAR && vA != null) {
                
        myArray.pop();
                
        myArray.unshift(vA);
            }
            
            
        vA getValue("Close",0,1,"$ADD") * 1;
            if (
        vA == null)
                return;
            
        /*** simple moving average *********/
            
        if (nType == "MA") {
                var 
        vAma null;
                if (
        myArray[18] != null) {
                    var 
        dSum 0;
                    for (
        019; ++i) {
                        
        dSum += myArray[i];
                    }
                    
        vAma = (dSum vA)/20;
                }
                return new Array(
        vAvAma);
            }
        /***********************************/


        /*** expontential moving average ***/
            
        if (nType == "EMA") {
                var 
        vAema null;
                
        vAema ema();
                return new Array(
        vAvAema);
            }
        /***********************************/

        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
          aces again!

          thanks alot

          Comment


          • #6
            Jason

            Do you have an editor to line up all of the { and {'s so nice and neat, or are you just a neat and tidy person by nature?

            What is the editor called, btw?

            Comment


            • #7
              Hi David,

              I'm just neat and tidy by nature. I use tabs instead of spaces to begin each line to keep everything lined up nicely.
              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

              Working...
              X