Announcement

Collapse
No announcement yet.

Help with simple script

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

  • Help with simple script

    I am trying to get my first script to run and am failing, output is blank. I want to graph a non-price study that graphs the % price above the 200-day moving average.

    Simple script follows.

    //{High Jump Description
    // This formula returns a plot of the percent prices close above the 200 period moving average
    // This formula was generated by Mike Scott
    //
    //}

    var vSMA200 = new MAStudy(200, 0, "Close", MAStudy.SIMPLE);

    function preMain() {
    setPriceStudy(false);
    setStudyTitle("High Jump");
    setCursorLabelName("High Jump", 0);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarThickness(3, 0);
    setPlotType(PLOTTYPE_LINE, 0);
    }

    function main() {

    var vClose = close();
    if(vClose == null)
    return;

    return ((vClose - vSMA200) / vSMA200) * 100;
    }
    ....Mike

  • #2
    Hi mike_scott,

    There are several problems here. Try checking out the code in the EFS 2 Custom or EFS 2 Basic folders.

    Your variable vSMA200 is defined using an EFS1 method, albeit incorrectly. Try checking out the tutorials at this link. There are some other links in my signature block below that may help as well.

    Also var vClose = close(); is being set to a series object, the proper technique to assign it to a specific value is var vClose = close(0);

    Comment


    • #3
      mike_scott,

      Your main problem is one that all new efs programers get hit with (including me!). You are confusing a series object with a number value.

      Your return statement should be:
      return ((close(0) - vSMA200.getValue(0)) / vSMA200.getValue(0)) * 100;

      You were trying to divide by the entire series, which won't work, and as you saw it returned null. You should only need the above return line in your main function. You don't need vClose any more.

      Steve

      Comment


      • #4
        Steve
        Actually the correct method to retrieve the value of the efs1 Moving Average function used by Mike should be
        PHP Code:
        vSMA200.getValue(MAStudy.MA0)//replace 0 with required bar index if necessary 
        While vSMA200.getValue(0) will also work when returning the current value it will not do so when retrieving prior bar's values.

        Mike
        In implementing the suggested changes you will also need to perform a null check on the value of vSMA200.getValue(MAStudy.MA,0) prior to using it in the return statement so as to avoid possible "divide by 0" errors
        Just before the return statement add the following line of code
        if(vSMA200.getValue(MAStudy.MA,0)==null) return;
        Alex

        Originally posted by smeyer55
        mike_scott,

        Your main problem is one that all new efs programers get hit with (including me!). You are confusing a series object with a number value.

        Your return statement should be:
        return ((close(0) - vSMA200.getValue(0)) / vSMA200.getValue(0)) * 100;

        You were trying to divide by the entire series, which won't work, and as you saw it returned null. You should only need the above return line in your main function. You don't need vClose any more.

        Steve

        Comment

        Working...
        X