Chapter 8. Formatting Output: Part I

You might have noticed how good all the built-in studies look when applied to chart. But so far we only learned how to display plots as plain lines, except for chapter 6 where we gave you a hint on how to display a plot as Boolean points:

plot Crossover = Average(close, 5) crosses Average (close, 20);

This notation perfectly shows the common syntax for some of the Look and Feel functions. These functions will surely add the finished look to your plots. But first things first, let us study the syntax using the SetPaintingStrategy function from the script above as an example. Calling this function will command thinkScript® to change representation of the values from line to points or arrows or histogram or many other shapes. In the script above, we declared the “Crossover” plot – so, to change the way it is displayed, its name is going to be the first thing we type. After that, we put a dot and call the function by typing SetPaintingStrategy. And finally, we specify arguments for this function in parentheses; luckily, there is only one argument which happens to be a PaintingStrategy constant – and don’t forget the semicolon after the closing parenthesis. In the example above, we chose the Boolean points (with PaintingStrategy.BOOLEAN_POINTS) constant – it will put a dot at the Close price of those bars at which the crossover condition is true (i.e., where the 5 period SMA crosses the 20 period SMA).

There are several other ways of representing Boolean values. Consider using BOOLEAN_ARROW_DOWN and BOOLEAN_ARROW_UP painting strategies. These can be especially useful in a script like this one:

plot isAbove = Average(close, 5) crosses above Average (close, 20);
plot isBelow = Average(close, 5) crosses below Average (close, 20);

Now you will see an up arrow below those bars at which the shorter moving average crosses above the longer one and down arrow above those bars when it crosses below.

There are also numerous ways to display the numerical plots. First of all, the line can be complemented with points, squares, or triangles which would definitely focus your attention on the values the line has at each bar (for this purpose, use LINE_VS_POINTS, LINE_VS_SQUARES, or LINE_VS_TRIANGLES). You can even discard the line and use only these basic shapes: POINTS, SQUARES, or TRIANGLES constants should be used then. Some values need to be represented as a histogram, use the HISTOGRAM constants for those. There are several other interesting painting strategies; the full list can be found here.

If you, however, prefer to keep the line, there are many options to make it look differently. For example, the SetStyle function. Consider the following script:

plot avg = ExpAverage(close, 15);

Now the EMA line will be displayed as a short-dashed curve. The SetStyle function accepts Curve constants, the full list and descriptions can be found here.

SetLineWeight function is very simple to use: specify a number from 1 to 5 as an argument and it will define the width of the plot line in pixels:

plot avg = ExpAverage(close, 15);

Sometimes there are situations where you need to hide a plot; there are two functions to help you with that: Hide and SetHiding. First function will hide the plot by default while the second will only hide it dynamically, i.e., upon some condition:

plot SMA5 = Average(close, 5);
plot SMA10 = Average(close, 10);
plot SMA15 = Average(close, 15);
SMA15.setHiding(getAggregationPeriod() < AggregationPeriod.DAY);

In this example, plot SMA10 plot is hidden by default and the SMA15 is hidden on intraday charts only.

In previous chapter we promised to tell you more about colors that can be used when drawing plots. You can color plots using any of the following functions: SetDefaultColor, AssignValueColor, or AssignNormGradientColor. SetDefaultColor is used to add a specific solid color to the whole plot. AssignValueColor is used to draw the plot with different colors depending on specified conditions. AssignNormGradientColor is used to color the plot in the gradient manner depending on values.

Before we move on to examples showing how to use these functions, let’s see how to specify which colors we will need. First way is to use a predefined Color constant, e.g, Color.RED (this is what we actually did in the previous chapter when assigning colors to strategy signals). Second way is to use functions Color and DefineColor: the former draws the plot with colors named by the latter function. Another way is to use the GetColor function which accepts a number as an argument and returns the respective color from the palette; this palette can be found here. For those who find our palettes insufficient, we have the CreateColor function which accepts three numerical arguments used as RGB values in order to create whichever color you might think of. And finally, function TakeValueColor will color the plot the same way as another plot; this is especially useful when colors of the plot change dynamically.

Now that you are provided you with a whole lot of functions, let’s see how they work together:

plot UpperBand = close * 1.1;
plot LowerBand = close *0.9;
plot Middle = close;
Middle.DefineColor("Highest", Color.RED);
Middle.DefineColor("Lowest", CreateColor(250, 150, 25));
Middle.AssignNormGradientColor(14, Middle.color("Highest"), Middle.color("Lowest"));

So, we started with drawing two bands of the Close price-based envelope: two bands shifted up and down by 10 percent from the Middle band which is the Close price plot. After that, we used the DefineColor function to define two named colors for the Middle plot: Highest and Lowest. The Highest color is defined by this function using the Color constant: Color.RED. In order to specify the value of Lowest color, we had the CreateColor function translate RGB value (250, 150, 25) into argument of the DefineColor function. After that we used the SetDefaultColor function to paint the LowerBand in a solid color taken from the dynamic palette by the GetColor function – and made sure that UpperBand would be colored the same (by using functions AssignValueColor and TakeValueColor). And still the result was not colorful enough to us – so, we decided to add gradient coloring to the Middle plot with Highest color for highest values and Lowest color for lowest ones.

Of course, we do not expect you to use all these functions in a single script, however, you may try. Also, if you feel tired of the plots, let’s move on to next chapter as there are several other ways to output values for you study.