if

The if function allows you to see if various conditions (sometimes called expressions) are true or false and then make decisions based on the validity of the conditions. Each if statement will have a then and else function.


if (expression)

    then

        (statement 1)

    else

        (statement 2)


These if statements are then executed as shown by the following two examples:


if (expression is true)

    then

        (do statement 1)

    else

        (statement 2)


if (expression is false)

    then

        (statement 1)

    else

        (do statement 2)


The "expression" part of the if statement will almost always contain one of the following symbols to help you decide whether you want to execute the then or else statement: <, <=, =, !=, >, >=


Please note that the if function can optionally have more if functions after a then or else function. (These are called nested if statements.)


An example explains it best. On Balance Volume (OBV) is a running total of volume. You start with zero. If the close is up for the bar, you add the volume to the running total. If the close is down for the bar, you subtract the volume from the running total. If the close is equal to the previous close, you do nothing. In more logical terms, OBV can be expressed as follows:


if today's close is > yesterday's close

    then

        add the volume

    else if today's close is < yesterday's close

        then

            subtract the volume

        else

            add or subtract nothing


You can see that in the above sentences, we have the word "if" two times, followed by an else. Each "if" has a statement after it that can be true or false. If the first "if" statement is true, we do something - add the volume; otherwise, we continue on to the next "if" (or in this case, Else if) statement to see if it's true. We keep continuing through the "if" statements until we find one that's true. If none of the “if” statements are true, we do whatever the “else” statement directs.


To summarize:


Investigator provides you with the following branch control functions to construct if statements: if(), then(), else()


"if" functions can be constructed as follows:


If (value 1) comparison function (value 2)

    then

        the result if above comparison is true

    else

        the result if above comparison is false



Another example:



if (value 1) comparison function (value 2)

    then

        the result if (value 1 and value 2) comparison is true

    else

        if (value 3) comparison function (value 4)

            then

                the result if (value 3 and value 4) comparison is true

            else

                the result if (value 3 and value 4) comparison is false



Yet another example:


if (value 1) comparison function (value 2)

    then if (value 3) comparison function (value 4)

        then

            the result if (value 3 and value 4) comparison is true

        else

            the result if (value 3 and value 4) comparison is false

    else

        the result if (value 1 and value 2) comparison is false.



A final example:


if (value 1) comparison function (value 2)

    then

        the result if (value 1 and value 2) comparison is true

    else

        if (value 3) comparison function (value 4)

            then

                the result if (value 3 and value 4) comparison is true

            else

                if (value 5) comparison function (value 6)

                    then

                        the result if (value 5 and value 6) comparison is true

                    else

                        the result if (value 5 and value 6) comparison is false


As you can see, you can have multiple if functions but you must have at least one then statement and one else statement for each if statement that you have.