14

## Formula 101 – #8 Local Variables, Assignments and Multiple Statements

All the previous tutorials focus on a *single statement* formula at works. For some users, that limits what they wanted to do. In order to be able to utilize multiple formulas, say, within a single quote column, or, within one cell in the dynamic grid, the usage of *assignment* and *multiple statements* is necessary.

**Multiple statements**

All *expressions* we have written in previous tutorials are just *single statement* formulas. A statement within the formula language is very similar to a statement within our everyday language, it contains a complete description and expresses a complete idea. The issue with that is in case we need to express more than just one complete idea, a single statement is not enough.

In our formula language, statements are separated by the semicolon character. Thus we can write multiple statements within a formula like this,

`statement; statement; statement; statement`

Here is an example,

`average (M5, 10); llv (M15.Low, 10); hhv (M15.high, 20)`

NeoTicker will *always* return the results of the *last statement* within a quote formula having multiple statements. For the example above, only the very last statement `hhv (M15.high, 20)`

will have its value returned.

Having just multiple statements within a formula does not really help, does it?

If the evaluation of the statements preceeding the last one are discarded, then what is the point of having them there in the first place?

That we have to explain two (2) very important concepts making multiple statements more useful.

**Assignment statement and local variables**

The first concept I am going to talk about is obvious with its usage.

Since we know that results from statements preceeding the last one is discarded in general, if we can find a way to save the result of a statement and reuse that in the statements after, then we will be able to do what our everyday language can do – delivering more information by maintaining the context from one statement to next.

This technique is called assignment.

The general form of assignment looks like this,

`localvariable := expression`

The *localvariable* is a name you suggested to NeoTicker for saving the evaluation results from the *expression*, which is a complete formula that we have talked about all along in the tutorials.

A *localvariable* must be named with the dollar sign (`$`

) as the first character. It is a way to tell NeoTicker you are talking about a *local variable*, something that you can store value within. Here is an example of an assignment statement,

`$myaverage := (high + low) / 2`

In the formula above, *High* and *Low* are quote fields. If the above statement is our complete formula, the value returned by `(high + low) / 2`

will be the returned value of the complete formula.

Now, lets add more statements to this formula,

`$myaverage := (high + low) / 2; $myaverage2 := (high + low + close) / 3;`

choose ($myaverage > $myaverage2, 1, $myaverage < $myaverage2, -1, 0)

The above formula *defined* two local variables and that we utilized these two local variables in the last statement for our decision making. Notice that if we do not have assignment in the formula language, we will need to express our idea in a much more messy way, like the following formula,

`choose (`

(high + low) / 2 > (high + low + close) / 3, 1,

(high + low) / 2 < (high + low + close) / 3, -1,
0)

It is less *readable* and harder to *maintain*.

Thus assignment statements promote clarity within our formulas.

A side benefit of assignment statement is that it reduces unnecessary repeated computation. Take a look at the formula we just talked about. We are effectively calculating exactly the same thing twice. With local variables, the calculation is done only once and can be reused multiple times. In fact, as many times as required.

**Side-effect functions**

The second concept that makes multiple statements useful is the usage of *side-effect functions*.

Side-effect functions are functions that do more than just returning values for our formula need. During the evaluation of a function with side-effects, it can signal NeoTicker to do something outside of its normal duty to return a value. For example, the *generic* function *playsound* is one of the functions having special side-effect.

The general form of playsound is,

`PlaySound (Condition, SoundFileName)`

When the parameter *Condition* is evaluated to true (non-zero value, see Tutorial 4), NeoTicker will play the sound file specified by *SoundFileName*. That does not affect the normal evaluation of the complete formula at all, just some extra work is done based on the playsound function. Here is an example that is very common among daytraders, used as a quote window column formula,

`$mypreviousdhigh := prevDHigh (M5);`

playsound ($mypreviousdhigh > close (1, M1) and $mypeviousdhigh < close(M1), "boing.wav");
$mypreviousdhigh

This formula will cause NeoTicker to play the sound file *boing.wav* again and again when the quote formula is evaluated to true within the quote window if any one of the symbols within the quote window meet the criteria of just breaking out of the previous day high within the 1-minute series time frame. Many users choose to use a lower time frame like 15-second to reduce the duration of sound playing. Some users also have their own customized sound file that only play a very short "tick" sound so that it is less annoying.

Due to the fact that side-effect functions does not really affect the calculated result of the final value of a complete formula, it is common that we embed them into our formulas to do various useful things - just like the playsound function we talked about.

**Example - New High Pullback **

One of the very important daytrading set up is the violation of the previous day high. When that happen, two camps of traders will watch very carefully on the price movement. The bullish ones and the bearish ones both will monitor the price movement of the symbols making the price penerateion.

Pro-traders in general do not have the time to monitor a symbol when it is not doing one of these setups. Thus sound alert is necessary to alert the user to check the quote window to see which symbol is producing a potential setup in real-time.

The formula we have here will check for new high being made, and then play a sound for 1/2 minute, during which the returning value of the formula properly return the short term pullback states of the symbols of interest.

Here is the formula setup,

The color rules,

Here is how the quote window looks like,

**Summary**

We have completed all the basic concepts related to quote formulas. The concepts we have delivered in this tutorial will prove very useful in the upcoming tutorials focused on actual usage case studies. Stay tune.

**Exercise**

1. What is the formula for the classification of price range based on previous day average (the pivot point) vs the midpoint of the current trading day?

2. What is the formula for classifying the range of slowk indicator applied onto 5-minute series into 5 regions?

3. What is the formula that play a sound when slowk just crossed below 70 within the last 15 seconds, using 5-minute data series?

**Answers for Previous Exercise**

1. Since we cannot use range indicator, we have to use the hhv and llv indicators,

`hhv (M5.H, 10) - llv (M5.L, 10)`

2. This one is straight forward,

`Low (1, M5) < = llv (1, M5.L, 10)`

3. You need to utilize the time related functions, and the tricky part is that we are interested in the first bar *after* the hour just switched,

`hour (Datetime (1, M5)) <> hour (DateTime (2, M5))`

**Interact:**Add a Comment | Trackback Link | Permalink

**Share:**digg | del.icio.us | Technorati | StumbleUpon