Chapter 13. Referencing Other Data

In this chapter we are going to discuss how to reference data which is not defined by current chart settings. We actually started doing this in chapter 11 where we used secondary aggregation period which overrides timeframe specified in chart settings.

Let's start with referencing fundamental data with a different price type. We hope that you've carefully read our reference dealing with fundamental functions, e.g., high, low, close, etc. In those articles you might have seen that the last argument for each fundamental function is priceType. This argument specifies which value of the fundamental function is needed: LAST, ASK, BID, or MARK. By default, this price type coincides with the one specified by chart settings. Here is how we could change that:


plot ask = close(priceType = "ASK");
plot bid = close(priceType = "BID");

On the "MARK" price type chart for a Forex symbol, this code will plot ask and bid plots. Just like it was explained in chapter 9, we only change the default value of argument priceType while keeping the other two (symbol and period) so that we put the 'equals' sign and state that we need ASK and BID price types for the first and second Close price plots, respectively. Note also that for non-Forex symbols, the last three are only supported on intraday charts with time interval not greater than 15 days.

As it was implied before, you can also change the symbol for some fundamental function in your script:

plot spread = close - close("GOOG");

This will plot the difference between the Close price of your current symbol and that of Google. Note how we did not use the full notation for the reference (symbol = "GOOG") as we did for priceType. This is because symbol is the first argument for close (and any other fundamental functions) so that thinkScript® automatically decides to use the string "GOOG" as the value for it.

The last thing we are going to discuss here is referencing pre-defined studies. This is a convenient way to use values of more than 200 built-in studies in your script without reconstructing them.

In order to use default values of the study, type command reference:

plot MyMACD = reference MACDHistogram;

This script will add the plot of MACD Histogram which means that you are able incorporate this plot in your own study, e.g.,

plot MyMACD = reference MACDHistogram;
plot diff = Average(close, 5) – Average(close, 20);

Now your script displays MACD Histogram plot and difference between two averages.

You can also reference a study with parameters different from default ones:

plot SMA = SimpleMovingAvg(volume, 20);

This code will plot the 20 period SMA of volume. Note that in this case, you need to specify all the parameters that the study has; these should be separated with comma.

Currently users are only able to reference the built-in studies, however, there is a workaround for integrating their own scripts into new ones. This can be done using command script:

script avg {
input length = 10;
plot SMA = Average(close, length);
plot EMA = ExpAverage(close, length);
declare lower;
plot SMAOsc = avg(10) - avg(20);
plot EMAOsc = avg(10).EMA - avg(20).EMA;

The script will plot two moving average oscillators on the lower subgraph. Here is what we did: we had the script command introduce a study called avg which has two plots, SMA and EMA which happen to be simple and exponential moving averages of the Close price with variable length (we had the length input for this purpose). After that, we declared that we needed the lower subgraph and created two plots: SMAOsc and EMAOsc. For the latter, we called the newly created study avg, specified 10 and 20 in brackets in order to use these as lengths for EMA plot which was also specified after the dot in the notation:

plot EMAOsc = avg(10).EMA - avg(20).EMA;

For the SMAOsc plot, we did not state that we needed SMA from avg as it was the first plot in that study, thus it is referenced by default and there is no need to specifically call it like we did for EMA.

In the next chapter we will learn more about string concatenation as sometimes output demands compound string values.