Formula 201 – #11 Writing Quote Formula Indicator

I am going to cover a few common techniques related to writing indicators that are updated by tick. Specifically, indicators in quote formulas are updated by tick all the time. Thus it is important that we know what to do so that the indicators we have written are updated properly in the quote windows, dynamic tables, etc.

Reduce Memory Usage by Using Local Variables

Indicator instances initiated in quote formulas (Quote window, Dynamic Table, Dynamic Grid in the Time Chart, etc.) do use memory in your computer. If you are using the quote formulas in a quote window, you could be creating hundreds, if not thousands, of indicator instances depending on the number of symbols in the quote window and how complex the quote formulas are.

Thus, it is important that we save memory when we write indicators that are expected to be used in the quote windows.

How to save memory? To understand that, lets take a look at the indicator example below,

myMA1 := average (data1, param1);
myMA2 := average (data1, param2);
plot1 := myMA1 - myMA2

In the formula statements above, the 2 series variables are not necessary because,

1. we are not refering to the previous values of those variables
2. those variables are not carrying out memory effect duties

Thus, we can rewrite the indicator in the following way to save more than one third of the memory usage,

$myMA1 := average (data1, param1);
$myMA2 := average (data1, param2);
plot1 := $myMA1 - $myMA2

Use Local Variables to Store Temporary Information Only

As illustrated in the previous tutorial, it is a bad idea to use local variables to store information that you will refer to in subsequent bar updates. That is, of course, if you are working on a normal indicator, not a Tick Precise indicator.

In this article, we will focus on normal indicators only.

For example, you are trying to create an indicator they will tell you it is time to trade or not. For simplicity purpose, we will use the condition of skipping the first 5 bars from day start.

bcount := if (day <> day (1), 1, bcount (1) + 1);
plot1 := bcount > 5

Notice that we are not using the if function in the plot assignment statement because the boolean expression returns 1 or 0 anyway.

Now, the following code will not produce the correct results we want, due to the fact that the indicator is updated by tick in quote formula, even though it looks very similar to the one above.

$bcount := if (day <> day (1), 1, $bcount + 1);
plot1 := $bcount > 5

Depending on the time of day you open the quote window, the above formula will produce inconsistent results.

Identify Bar Change Using the Barsnum Function

Here is a very useful technique to include into your own indicator,

$newbar := barsnum <> $lastbnum;
$lastbnum := barsnum;

Knowing that our indicator will be updated by tick, the 2 assignment statements above will first check if we just started updating on a new bar and save the condition to the local variable $newbar.

Then, we update the local variable $lastbnum to the current barsnum. Since local variables carry tick level memory effect, by the next update, the value we saved will be used in the comparison on the first line.

In the rest of the indicator, whenever we want to do something that depends on completion of a bar, we can use $newbar as part of the condition.

Now if we are interested in showing that a white hammer is just formed then we are going to check the previous bar, not the current bar because it is still being updated in real-time.

plot1 := c (1) > o (1) and c (1) >= h (1) and (c (1) - o (1)) < (h (1) - l (1)) / 3

Finally, if you want to play a sound when this happens, you can add the following line,

playsound (islastbar > 0 and $newbar > 0 and plot1 > 0, "mysound.wav")

We have enforced 3 conditions in the boolean expression above,

1. must happen on the last bar
2. the moment a bar is just completed (Or, a new bar is just created)
3. our customized condition is met

In effect, we are emulating the update on bar completion behaviour programmatically.

Organization Tips

For indicators written specifically for quote formula usage, you can add them to a customized indicator list so that you can locate them easier when using the Indicator Wizard.

If you have never tried creating your own Indicator List, then look under the Manager menu in the main program window and open the Indicator List Manager. It is a very useful tool to manage large number of indicators.

You can also name your indicators with a prefix so that you can tell the difference immediately when you are editing your quote formulas.


I have presented a few useful tricks in this tutorial on writing indicators for quote formulas (and in general for update by tick situation). Get yourself familiar with these tricks so that you utilize them when the situations arise.


No exercise for this tutorial.

Discuss this article

Leave a Comment

Blog Developed
By ContentRobot