Announcement

Collapse
No announcement yet.

Newby difficulty with variables and returns

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

  • Newby difficulty with variables and returns

    I am trying to teach myself EFS and would appreciate it if someone could point out my error in this code as I am having difficulty with variable declaration and returns.

    I want to return the gap % size on a daily chart to the cursor window.

    What I have come up with is the code below.

    If someone could have a look and explain the problem I would really appreciate it.

    Thanks in anticipation

    P



    function preMain() {

    setStudyTitle("Gap Size");
    setCursorLabelName("Gap", 0);

    }
    //Declare variable percentage size

    var perc;


    function main(perc) {


    {
    // Calculate gap size by - Today's Open divided by yesterday's Close subtract 1 multiply answer by 100

    perc = (((Open(0)/Close(-1))-1)*100);

    }

    // If gap percent is zero, return nothing

    {
    if Open() != Close(-1) onAction1();

    // otherwise return result of calculation as a variable


    return;

    perc;


    }
    }

  • #2
    Patch227
    Enclosed below is your script with my comments explaining the errors
    Alex

    PHP Code:
    function preMain() {
        
    setStudyTitle("Gap Size");
        
    setCursorLabelName("Gap"0);
    }

    var 
    perc;

    function 
    main(perc) {//ACM - perc is a variable not a parameter. It should not be in
                         //included in the arguments of the function

    {//ACM - this open curly bracket is not required

        
    perc = (((Open(0)/Close(-1))-1)*100);//ACM - incorrect syntax for open() and close() functions
          
          
    }//ACM - not required
          
    // If gap percent is zero, return nothing
    //ACM - with regards to your comment above you have added no code that evaluates 
    //your condition and stops the execution of the script you may want to add 
    //something like the following
    //if(perc==0) return;

    {//ACM - not required 
        
    if Open() != Close(-1onAction1();//ACM - several errors in this line ie missing parenthesis and
                                            //incorrect syntax for the functions open() and close()
                                            // should be as follows
                                            //if(open(0) != close(-1)) onAction1();
                                            //what does the onAction1() function do? It is is not 
                                            //included in the script

        
    return;//ACM - by adding a semicolon after the return you are ending the return 
               //statement and not returning anything to the chart
               //If you want to plot perc then the return statement should be
               //return perc;
                
                
    perc;//ACM - this will do nothing because it is located after the return
                     //see note regarding the return statement if you want to return this
                     //to the chart
                     
    }//ACM - not required


    Originally posted by Patch227
    I am trying to teach myself EFS and would appreciate it if someone could point out my error in this code as I am having difficulty with variable declaration and returns.

    I want to return the gap % size on a daily chart to the cursor window.

    What I have come up with is the code below.

    If someone could have a look and explain the problem I would really appreciate it.

    Thanks in anticipation

    P


    function preMain() {

    setStudyTitle("Gap Size");
    setCursorLabelName("Gap", 0);

    }
    //Declare variable percentage size

    var perc;


    function main(perc) {


    {
    // Calculate gap size by - Today's Open divided by yesterday's Close subtract 1 multiply answer by 100

    perc = (((Open(0)/Close(-1))-1)*100);

    }

    // If gap percent is zero, return nothing

    {
    if Open() != Close(-1) onAction1();

    // otherwise return result of calculation as a variable


    return;

    perc;


    }
    }

    Comment


    • #3
      Alex

      Thank you very much for your comprehensive help. I will no doubt have some other questions on Monday

      Have a good weekend

      Comment


      • #4
        Patch227
        You are most welcome.
        You too have a great weekend
        Alex

        Comment

        Working...
        X