Announcement

Collapse
No announcement yet.

coding question

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

  • coding question

    I am making an EFS that has 3 different conditions, similar to the following

    1) if (high(0) > sma(5) & high(-1) > sma(5, -1) & high(-2) > sma(5, -2) etc)
    condtion1 = true;

    2) sma(3) > sma(5)
    condition2= true

    3) sma(5) > (sma(5 -10) + 4)
    condtion3 = true

    i wrote the code so that if a condtion is true, a bool variable = true

    My questions are:

    is there a better way to code

    if (condtion1 =="true" & condtion2=="true" & condtion 3=="true")
    { make alert}

    I know that with one bool you can just code : if(condition1), and it will only evaluate if the bool is true. can you do the same for more than 1 bool? when i test it doesnt seem to work

    2) do I need to reset the bools after each bar? I think i do, or maybe I need to add an "else" statment to all my bools and make those false

    3) is this way of coding the most efficient, and if not (probably) does anyone have any suggestions

    Thanks in advance

  • #2
    Re: coding question

    eurostoxx
    Not necessarily a "better way" but a simpler one would be to just write your statements and assign them to variables eg
    PHP Code:
    var Condition1 close(0)>sma(5,0) && close(-1)>sma(5,-1) && close(-2)>sma(5,-2);//will return boolean
    var Condition2 sma(3,0)>sma(5,0);//as above will return boolean
    var Condition3 etc
    then check for those variables to be true in your conditional statement eg
    PHP Code:
    if(Condition1 && Condition2 && Condition3){
        
    //execute your command(s)

    As to the reason why this line of your code
    if (condtion1 =="true" & condtion2=="true" & condtion 3=="true")
    is not working this is because
    a) you are checking for the string "true" instead of the boolean true which you are instead assigning to those variables and
    b) you are using the & operator incorrectly [see this article in the Core JavaScript Guide for a detailed description of the available operators].
    That line of code should be as follows
    if (condtion1 ==true && condtion2==true && condtion 3==true)
    or in the simpler form I show earlier
    As to efficiency keep in mind that calling directly the values of series objects [eg close(0), sma(5,0)] is quite an inefficient way of using those functions. You can find a brief explanation as to why in this thread. Additionally search the forums and you will find many other threads on this topic as it has been discussed at length before
    You should instead first declare the variable to which you will assign a series as a global variable. Then initialize the series at BARSTATE_ALLBARS or using some initialization routine that will execute once only when the formula is loaded [or reloaded]. At that point you call the value of that series using the getValue() method of the Series Object
    Alex


    Originally posted by eurostoxx
    I am making an EFS that has 3 different conditions, similar to the following

    1) if (high(0) > sma(5) & high(-1) > sma(5, -1) & high(-2) > sma(5, -2) etc)
    condtion1 = true;

    2) sma(3) > sma(5)
    condition2= true

    3) sma(5) > (sma(5 -10) + 4)
    condtion3 = true

    i wrote the code so that if a condtion is true, a bool variable = true

    My questions are:

    is there a better way to code

    if (condtion1 =="true" & condtion2=="true" & condtion 3=="true")
    { make alert}

    I know that with one bool you can just code : if(condition1), and it will only evaluate if the bool is true. can you do the same for more than 1 bool? when i test it doesnt seem to work

    2) do I need to reset the bools after each bar? I think i do, or maybe I need to add an "else" statment to all my bools and make those false

    3) is this way of coding the most efficient, and if not (probably) does anyone have any suggestions

    Thanks in advance

    Comment


    • #3
      thanks you kindly alexis.. I will incorporate those suggestions into my code. I actaully did invoke the getValue() function instead of calling the sma() each time, but know I understand better why its efficient to do so

      you are a gentleman and a scholar !

      Comment


      • #4
        eurostoxx
        You are welcome
        Alex


        Originally posted by eurostoxx
        thanks you kindly alexis.. I will incorporate those suggestions into my code. I actaully did invoke the getValue() function instead of calling the sma() each time, but know I understand better why its efficient to do so

        you are a gentleman and a scholar !

        Comment

        Working...
        X