Announcement

Collapse
No announcement yet.

using amstudies in EFS script

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

  • using amstudies in EFS script

    Hi, I am trying to calculate standard deviation in EFS script. As EFS does not provide a method for this I tried amStudies but get this error msg:
    ReferenceError: amStdDev is not defined

    Below is the efs script.

    debugClear();

    function preMain()

    var myLib = addLibrary('amStudies.efsLib');
    var study1 = null;
    var bInit = false;


    function main(){
    var study1 = amStdDev(50,roc(1));

    return study1;
    }

  • #2
    Re: using amstudies in EFS script

    haseko
    The amStdDev() function is not in the amStudies library but in the amFunctions library.
    For examples on how to use these functions see the documentation provided here
    Alex


    Originally posted by haseko
    Hi, I am trying to calculate standard deviation in EFS script. As EFS does not provide a method for this I tried amStudies but get this error msg:
    ReferenceError: amStdDev is not defined

    Below is the efs script.

    debugClear();

    function preMain()

    var myLib = addLibrary('amStudies.efsLib');
    var study1 = null;
    var bInit = false;


    function main(){
    var study1 = amStdDev(50,roc(1));

    return study1;
    }

    Comment


    • #3
      Thanks Alex, got the amstdDev function working but seems to produce so erratic results when using ES #F as a symbol, this problem does not appear in $SPX.
      Here is my script:

      function preMain()

      setStudyTitle("SQN");
      setCursorLabelName("SQNFast");
      setDefaultBarFgColor(Color.red);
      addBand(0.75, PS_SOLID, 1, Color.RGB(0,0,0));
      addBand(-0.75, PS_SOLID, 1, Color.RGB(0,0,0));
      var myLibrary = addLibrary('amFunctions.efsLib');

      function main(){
      var study2 = myLibrary.amStdDev(50,roc(1));

      return study2;
      }
      Attached Files

      Comment


      • #4
        haseko
        The issue may be with how the function is working within the library. I will review it at some point and update it if necessary.
        In the mean time you can resolve it by coding your script as shown below [which as an aside is more efficient]
        Alex

        PHP Code:
        function preMain(){
            
        setStudyTitle("SQN");
            
        setCursorLabelName("SQNFast");
            
        setDefaultBarFgColor(Color.red);
            
        addBand(0.75PS_SOLID1Color.RGB(0,0,0));
            
        addBand(-0.75PS_SOLID1Color.RGB(0,0,0));
        }

        var 
        myLibrary addLibrary('amFunctions.efsLib');
        var 
        study2 null;//declare as a global variable

        function main(){
            if(
        getBarState()==BARSTATE_ALLBARS) {//once all the bars are loaded in the chart
                
        study2 myLibrary.amStdDev(50,roc(1));//initialize the study
            
        }
            return 
        study2.getValue(0);//return the value using the getValue() method of the series object

        Originally posted by haseko
        Thanks Alex, got the amstdDev function working but seems to produce so erratic results when using ES #F as a symbol, this problem does not appear in $SPX.
        Here is my script:

        function preMain()

        setStudyTitle("SQN");
        setCursorLabelName("SQNFast");
        setDefaultBarFgColor(Color.red);
        addBand(0.75, PS_SOLID, 1, Color.RGB(0,0,0));
        addBand(-0.75, PS_SOLID, 1, Color.RGB(0,0,0));
        var myLibrary = addLibrary('amFunctions.efsLib');

        function main(){
        var study2 = myLibrary.amStdDev(50,roc(1));

        return study2;
        }

        Comment


        • #5
          Thanks very much Alex, working perfectly.
          Can I get further input on your coding so that I can expand my efs/javascript understanding please.

          1) why did you use return study2.getValue(0);
          instead of return study2;

          2) if(getBarState()==BARSTATE_ALLBARS)
          Is this for smoother/faster calculation, as opposed to letting the study calculate every time a new bar is updated?

          Comment


          • #6
            haseko
            1) The study2 series object is cached when it is initialized at BARSTATE_ALLBARS (or within any other initialization routine you may be using that runs once only) so to return it you would need to call it using the getSeries() function which can be done at BARSTATE_ALLBARS (or other initialization routine) or at any time outside of that [although the latter is less efficient].
            Given that in your script you are not calling external symbols and/or intervals [hence you do not require the efs engine to maintain the plots synchronized] then it is slightly more efficient to return its values using the getValue() method.
            2) The study is being calculated on every tick [and once on every historical bar as the formula iterates through them while loading in the chart]. It is only being initialized at BARSTATE_ALLBARS. More importantly even if initialized outside a routine that runs once only the study is still created once only and not on every tick.
            Alex


            Originally posted by haseko
            Thanks very much Alex, working perfectly.
            Can I get further input on your coding so that I can expand my efs/javascript understanding please.

            1) why did you use return study2.getValue(0);
            instead of return study2;

            2) if(getBarState()==BARSTATE_ALLBARS)
            Is this for smoother/faster calculation, as opposed to letting the study calculate every time a new bar is updated?

            Comment

            Working...
            X