Announcement

Collapse
No announcement yet.

Moving average of custom indicator

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

  • Moving average of custom indicator

    Hi,
    I’m trying to make my own indicator on EFS. I want to get the difference between 2 moving averages and plot that line, and I also want to plot the moving average of that difference. I can plot the difference ok but I can’t plot the moving average of the difference. Basically I’m trying to make something like a MACD but I don’t want to use exponential averages. Can I do this in the wizard or do I need to use code? Any help will be greatly appreciated
    Thanks

    //{{EFSWizard_Description
    //
    // This formula was generated by the Alert Wizard
    //
    //}}EFSWizard_Description


    //{{EFSWizard_Declarations
    var vSMA750 = new MAStudy(750, 0, "Close", MAStudy.SIMPLE);
    var vEMA300 = new MAStudy(300, 0, "Close", MAStudy.EXPONENTIAL);
    var vSMA100_of_vSMA750 = new MAStudy(100, 0, vSMA750, MAStudy.MA, MAStudy.SIMPLE);
    var vLastAlert = -1;
    //}}EFSWizard_Declarations


    function preMain() {
    /**
    * This function is called only once, before any of the bars are loaded.
    * Place any study or EFS configuration commands here.
    */
    //{{EFSWizard_PreMain
    setPriceStudy(false);
    setStudyTitle("MAxDiff");
    setCursorLabelName("Diff", 0);
    setCursorLabelName("Ave", 1);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);
    //}}EFSWizard_PreMain

    }

    function main() {
    /**
    * The main() function is called once per bar on all previous bars, once per
    * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
    * in your preMain(), it is also called on every tick.
    */

    //{{EFSWizard_Expressions
    //{{EFSWizard_Expression_1
    //}}EFSWizard_Expression_1

    //}}EFSWizard_Expressions



    //{{EFSWizard_Return
    return new Array(
    vEMA300.getValue(MAStudy.MA) - vSMA750.getValue(MAStudy.MA),
    vSMA100_of_vSMA750.getValue(MAStudy.MA)
    );
    //}}EFSWizard_Return

    }

    function postMain() {
    /**
    * The postMain() function is called only once, when the EFS is no longer used for
    * the current symbol (ie, symbol change, chart closing, or application shutdown).
    */
    }

    //{{EFSWizard_Actions
    //{{EFSWizard_Action_1
    function onAction1() {
    vLastAlert = 1;
    }
    //}}EFSWizard_Action_1

    //}}EFSWizard_Actions
    Last edited by shanebrannick; 09-17-2009, 04:32 AM.

  • #2
    Re: Moving average of custom indicator

    shanebrannick
    You cannot do what you are trying to accomplish using the Formula Wizard. This is because the Formula Wizard uses efs1 functions that cannot use custom variables as the source. Those functions can only use a price series (ie "High", "Low", "Close", etc) and in some cases another efs1 built-in study.
    To do what you want you will need to code your own script using efs2 functions which can use a price series, another built-in study or a custom series as a source. You can find the complete list of the efs2 built-in study functions in the EFS KnowledgeBase in the Function Reference-> Built-in Study Functions folder
    To create a custom series you need to perform your calculations in a separate function or external efs and then retrieve the results of those calculations using the efsInternal() or efsExternal() functions This series can then be used as a source for the built-in studies.
    For the description of the efsInternal() and efsExternal() functions and the required syntax see the links above which will take you to the corresponding articles in the EFS KnowledgeBase.
    You can also find several detailed examples on how to use efsInternal() and efsExternal() in this and this thread.
    Also try searching the forum as there are many more examples of averages based on custom variables as this topic has been discussed at length many times before
    Alex


    Originally posted by shanebrannick
    Hi,
    I’m trying to make my own indicator on EFS. I want to get the difference between 2 moving averages and plot that line, and I also want to plot the moving average of that difference. I can plot the difference ok but I can’t plot the moving average of the difference. Basically I’m trying to make something like a MACD but I don’t want to use exponential averages. Can I do this in the wizard or do I need to use code? Any help will be greatly appreciated
    Thanks

    //{{EFSWizard_Description
    //
    // This formula was generated by the Alert Wizard
    //
    //}}EFSWizard_Description


    //{{EFSWizard_Declarations
    var vSMA750 = new MAStudy(750, 0, "Close", MAStudy.SIMPLE);
    var vEMA300 = new MAStudy(300, 0, "Close", MAStudy.EXPONENTIAL);
    var vSMA100_of_vSMA750 = new MAStudy(100, 0, vSMA750, MAStudy.MA, MAStudy.SIMPLE);
    var vLastAlert = -1;
    //}}EFSWizard_Declarations


    function preMain() {
    /**
    * This function is called only once, before any of the bars are loaded.
    * Place any study or EFS configuration commands here.
    */
    //{{EFSWizard_PreMain
    setPriceStudy(false);
    setStudyTitle("MAxDiff");
    setCursorLabelName("Diff", 0);
    setCursorLabelName("Ave", 1);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarThickness(1, 0);
    setDefaultBarThickness(1, 1);
    setPlotType(PLOTTYPE_LINE, 0);
    setPlotType(PLOTTYPE_LINE, 1);
    //}}EFSWizard_PreMain

    }

    function main() {
    /**
    * The main() function is called once per bar on all previous bars, once per
    * each incoming completed bar, and if you don't have 'setComputeOnClose(true)'
    * in your preMain(), it is also called on every tick.
    */

    //{{EFSWizard_Expressions
    //{{EFSWizard_Expression_1
    //}}EFSWizard_Expression_1

    //}}EFSWizard_Expressions



    //{{EFSWizard_Return
    return new Array(
    vEMA300.getValue(MAStudy.MA) - vSMA750.getValue(MAStudy.MA),
    vSMA100_of_vSMA750.getValue(MAStudy.MA)
    );
    //}}EFSWizard_Return

    }

    function postMain() {
    /**
    * The postMain() function is called only once, when the EFS is no longer used for
    * the current symbol (ie, symbol change, chart closing, or application shutdown).
    */
    }

    //{{EFSWizard_Actions
    //{{EFSWizard_Action_1
    function onAction1() {
    vLastAlert = 1;
    }
    //}}EFSWizard_Action_1

    //}}EFSWizard_Actions

    Comment


    • #3
      Hi Alexis,

      Thanks for your help. I got it done after I did a bit of reading. Here it is in case anyone else on the forum wants it. Also do you know where I could get a list of the EFS functions and what they do, and where I can get the formulas for bar values (high low, etc.)

      var vLastAlert = -1;



      function preMain() {

      setPriceStudy(false);
      setStudyTitle("MAxDiff");
      setCursorLabelName("Diff", 0);
      setCursorLabelName("Ave", 1);
      setDefaultBarStyle(PS_SOLID, 0);
      setDefaultBarStyle(PS_SOLID, 1);
      setDefaultBarFgColor(Color.red, 0);
      setDefaultBarFgColor(Color.blue, 1);
      setDefaultBarThickness(1, 0);
      setDefaultBarThickness(1, 1);
      setPlotType(PLOTTYPE_LINE, 0);
      setPlotType(PLOTTYPE_LINE, 1);

      addBand(0, PS_SOLID, 2, Color.black)

      }



      function main(vEMA, vSMA, vAve) {

      if (vEMA == null) vEMA = 250 ;
      if (vSMA == null) vSMA = 625;
      if (vAve == null) vAve = 100;


      var vEMA = new MAStudy(vEMA, 0, "Close", MAStudy.EXPONENTIAL);
      var vSMA = new MAStudy(vSMA, 0, "Close", MAStudy.SIMPLE);


      var vIntEfs = efsInternal("Diff",vEMA, vSMA);
      var vDiff = getSeries(vIntEfs,0);
      var vDiffAve = sma(vAve,getSeries(vDiff),0);




      return new Array(
      vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA),
      vDiffAve
      );

      }



      function Diff(veMA, vsMA){
      var vEMA = new MAStudy(veMA, 0, "Close", MAStudy.EXPONENTIAL);
      var vSMA = new MAStudy(vsMA, 0, "Close", MAStudy.SIMPLE);
      return new Array (vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA), 0);
      }





      function postMain() {

      }



      function onAction1() {
      vLastAlert = 1;
      }

      Comment


      • #4
        shanebrannick
        You are most welcome.
        FYI there are a few issues in the formula you posted. These are described in the comments enclosed with the script together with the suggested corrections
        Alex


        PHP Code:
        var vLastAlert = -1;

        function 
        preMain() {

            
        setPriceStudy(false);
            
        setStudyTitle("MAxDiff");
            
        setCursorLabelName("Diff"0);
            
        setCursorLabelName("Ave"1);
            
        setDefaultBarStyle(PS_SOLID0);
            
        setDefaultBarStyle(PS_SOLID1);
            
        setDefaultBarFgColor(Color.red0);
            
        setDefaultBarFgColor(Color.blue1);
            
        setDefaultBarThickness(10);
            
        setDefaultBarThickness(11);
            
        setPlotType(PLOTTYPE_LINE0);
            
        setPlotType(PLOTTYPE_LINE1);
            
            
        addBand(0PS_SOLID2Color.black)
        }

        function 
        main(vEMAvSMAvAve) {

            if (
        vEMA == nullvEMA 250 ;
            if (
        vSMA == nullvSMA 625;
            if (
        vAve == nullvAve 100;

            
        //in the following two lines you are declaring the moving average studies using
            //the same variable names of the parameters
            //furthermore these two lines of code are unnecessary because you are already
            //computing the difference between these two averages in the Diff() function
            //remove these two lines of code
            
        var vEMA = new MAStudy(vEMA0"Close"MAStudy.EXPONENTIAL);
            var 
        vSMA = new MAStudy(vSMA0"Close"MAStudy.SIMPLE);

            var 
        vIntEfs efsInternal("Diff",vEMAvSMA); 
            
        //the following line of code is unecessary and can be removed
            
        var vDiff getSeries(vIntEfs,0);
            
        //due to the construct used in this efs it is not necessary to call
            //the series in the following line of code
            
        var vDiffAve sma(vAve,getSeries(vDiff),0);
            
        //replace it with
            //var vDiffAve = sma(vAve,vDiff,0);

            //in the following line of code you do not need to calculate the difference
            //between the averages as that is already done in the Diff() function called
            //using efsInternal
            
        return new Array(vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA), vDiffAve);
            
        //replace it with the following
            //return new Array(vIntEfs.getValue(0), vDiffAve);
        }

        function 
        Diff(veMAvsMA){
           var 
        vEMA = new MAStudy(veMA0"Close"MAStudy.EXPONENTIAL);
           var 
        vSMA = new MAStudy(vsMA0"Close"MAStudy.SIMPLE);
           
        //in the following line you do not need to return an arry as it is not necessary to return the constant 0
           //if you need to plot the 0 line then return that value in the main function
           //if you do not need to plot it then just use the addBand() function to draw the line at 0
           
        return new Array (vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA), 0); 
           
        //change the line of code to 
           //return vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA)
        }

        //you can remove the following function as it is not used
        function postMain() {
        }

        //you can remove the following function as it is not used
        function onAction1() {
            
        vLastAlert 1;


        Originally posted by shanebrannick
        Hi Alexis,

        Thanks for your help. I got it done after I did a bit of reading. Here it is in case anyone else on the forum wants it. Also do you know where I could get a list of the EFS functions and what they do, and where I can get the formulas for bar values (high low, etc.)

        var vLastAlert = -1;



        function preMain() {

        setPriceStudy(false);
        setStudyTitle("MAxDiff");
        setCursorLabelName("Diff", 0);
        setCursorLabelName("Ave", 1);
        setDefaultBarStyle(PS_SOLID, 0);
        setDefaultBarStyle(PS_SOLID, 1);
        setDefaultBarFgColor(Color.red, 0);
        setDefaultBarFgColor(Color.blue, 1);
        setDefaultBarThickness(1, 0);
        setDefaultBarThickness(1, 1);
        setPlotType(PLOTTYPE_LINE, 0);
        setPlotType(PLOTTYPE_LINE, 1);

        addBand(0, PS_SOLID, 2, Color.black)

        }



        function main(vEMA, vSMA, vAve) {

        if (vEMA == null) vEMA = 250 ;
        if (vSMA == null) vSMA = 625;
        if (vAve == null) vAve = 100;


        var vEMA = new MAStudy(vEMA, 0, "Close", MAStudy.EXPONENTIAL);
        var vSMA = new MAStudy(vSMA, 0, "Close", MAStudy.SIMPLE);


        var vIntEfs = efsInternal("Diff",vEMA, vSMA);
        var vDiff = getSeries(vIntEfs,0);
        var vDiffAve = sma(vAve,getSeries(vDiff),0);




        return new Array(
        vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA),
        vDiffAve
        );

        }



        function Diff(veMA, vsMA){
        var vEMA = new MAStudy(veMA, 0, "Close", MAStudy.EXPONENTIAL);
        var vSMA = new MAStudy(vsMA, 0, "Close", MAStudy.SIMPLE);
        return new Array (vEMA.getValue(MAStudy.MA) - vSMA.getValue(MAStudy.MA), 0);
        }





        function postMain() {

        }



        function onAction1() {
        vLastAlert = 1;
        }

        Comment


        • #5
          In order to obtain the bar values, you will have to use the BarIndex function, and define this in the Premain.
          Very helpful in referencing the high, low, open and close of every bar.
          Haven't used it that much, but responds well to user interaction.
          You can find more details on this function here:

          http://share.esignal.com/download.js...older=Tutorial PDFs&file=tutorial4_StateAndIndexing.pdf

          If you don't mind my asking, what is your strategy behind plotting the difference of 2 moving averages?
          Meaning, what are you looking for that the macd doesn't address?
          Last edited by kevinbaker; 10-16-2009, 01:34 AM.

          Comment


          • #6
            The MACD uses the difference between 2 exponential moving averages. So you can't use different types of averages (simple, weighted, smooth, etc.) and there's no real reason to say that the optimal way to model the behaviour of a product is the difference between 2 exponential moving averages. So I basically wanted more options.

            Comment

            Working...
            X