Chapter 9. Formatting Output: Part II

Some time ago, plots were mandatory for thinkScript® studies. If someone tried adding a study without any plots, they saw an error message: "At least one plot should be declared". Well, now it is not an error message any more: it is just a friendly reminder as, for most of the studies, plots are still expected. For most, but not for all: let's see how to replace plots with some of the Look and Feel functions.

Let's start with function AddLabel. This function adds a nice little label to the upper left corner of chart. This label indicates whichever data you prefer:

AddLabel(yes, if close > Average(close, 20) then "Uptrend" else "Downtrend");

This label will display word "Uptrend" if Close price is greater than its 20 period SMA and "Downtrend" otherwise. First argument of this function is a condition upon which the label is displayed; here, we used word yes for the label to be always visible. We also specified a condition (close > Average(close, 20)) which will select either word "Uptrend" or "Downtrend" to be shown, however, you might want to display some calculated value without any conditions:

AddLabel(yes, Average(close, 20));

Now the label will display value of the SMA itself. You can also add a short explanation of what is being displayed by using the following notation:

AddLabel(yes, "Average: " + Average(close, 20));

Here we specified the text to be displayed and concatenated it with the value of the average using the sign "+". There is also another way to concatenate some values in a string, which is the Concat function, but we advise that you use the plus sign as it is much more convenient.

And, of course, we could not leave you without an option to choose the color of the label. By default, it is displayed red, but let's make it dark orange:

AddLabel(yes, Average(close, 20), Color.DARK_ORANGE);

We used the simplest way, the Color constant, but you are free to color the label using techniques described in previous chapter. For example, let's make it chartreuse green (RGB value: 127,255,0 ):

AddLabel(yes, Average(close, 20),CreateColor(127,255,0));

Now you are displaying this label with a color which is the most visible to human eye. Feel free to experiment with other colors. Also, you can add some dynamic coloring:

AddLabel(yes, Average(close, 20), if close > Average(close, 20) then Color.GREEN else Color.RED);

This script will display 20 period SMA of Close price in a green label if Close price is greater than this average, or in a red label when the Close price is lower.

To sum it up, three arguments can be used for AddLabel function. These arguments are: condition upon which the label is visible (state yes to make it always visible), text and values to be displayed (specify text in quotes and use the "+" sign to concatenate it with a value), and color which is red by default.

Another way to output values is showing bubbles at price bars. This is done by using AddChartBubble function:

AddChartBubble(close crosses above Average(close, 20), close, "Close price " + close + " is greater");

This script will display chart bubbles at bars where Close price crosses above its 20 period SMA. Bubbles will be displayed at Close price of these bars (this is what the second argument, close, stands for) with default color (red). By default, bubbles are located above the price plot; if you would like to display them below, transform this script into:

 AddChartBubble(close crosses above Average(close, 20), close, "Close price " + close + " is greater", up = no);

Here, we explicitly set the value of the last argument up to no. So far, we customized each function in such a way where all the arguments change their default values. This is a perfect example of what should be done if you are fine with some of the arguments' default values but would like to change those of others: you specify the name of the argument and assign a value to it with the 'equals' sign. Name of each argument of each function can be found in thinkScript® Reference. For AddChartBubble function, the following arguments can be used: time condition (condition upon which the bubble is displayed), price location (price at which the bubble is shown), text (information to be displayed in the bubble), color (use a color constant or a color-related function), and up (defines whether the bubble should be displayed above the price plot).

When a bubble is not enough to draw attention to some price action, you can add a vertical line with some text. Function AddVerticalLine is used for this purpose:

AddVerticalLine(close crosses above Average(close, 20),  "Close price " + close + " is greater");

This script will mark the same bars as the previous did, but the vertical lines will be displayed instead of bubbles. Arguments for the AddVerticalLine function are: visible (identical to time condition argument of AddChartBubble), text (has the same application as the two previously described functions), color (all the same, again), and stroke (the style of the line to be displayed, set with Curve constants).

The last but not the least, AddCloud function is a nice way to avoid several plots when you only need difference in their values. This function visualizes the difference between two calculated values by filling area between them with a translucent color:

def avg1 = Average(close, 5);
def avg2 = Average(close, 20);
AddCloud(avg1, avg2, Color.PINK, Color.BLUE);

This script is another visualization of the previously mentioned double crossover method. We have two SMAs of the Close price: 5 period and 20 period. AddCloud function visualizes difference between them in the following way: those sections where the shorter average is greater are colored pink and those where the longer is greater, blue. Remember not to swap the arguments as this function compares the first argument to the second and colors the mentioned sections in respective colors.

We hope that you had fun using the Look&Feel functions described in this and previous chapters as we are moving on to some advanced material concerning logics of referencing data in thinkScript®.