Chapter 6. Human-Readable Syntax

While watching price action and indicators reach certain values and conform to certain conditions can provide valuable results, it is often more interesting to know when exactly values reached a notable threshold or trend began its development. A popular technique called double crossover method uses two moving averages with different lengths based on a premise that a Buy signal is produced when the shorter moving average crosses above the longer, Sell signal in the opposite situation. Many chartists use length of 5 for the short SMA and 20 for the long one. You are already able to compose a script checking whether the short SMA is above or below the long one:

declare lower;
plot isAbove = Average(close, 5) > Average (close, 20);

Just like you expect, this will plot a line on the lower subgraph with value of 1 when the shorter is above and 0 otherwise. But this is not quite what the double crossover method implies: we need to know when exactly the shorter average crossed the longer one from below, i.e., it had been below for some time but started rising and finally went above. Human-readable syntax is here to help you:

declare lower;
plot isAbove = Average(close, 5) crosses above Average (close, 20);

In this script, we used two reserved words: crosses and above. These are two human-readable commands which tell thinkScript® to check whether shorter average crossed above the long one. Just like it can be expected, the script producing Sell signals for the double crossover method will look like this:

  declare lower;
plot isBelow = Average(close, 5) crosses below Average (close, 20);

"Above" and "below" are additional statements to the main command "crosses" – they define the direction of crossovers we are interested in. If it doesn't matter to us if a line crosses another line from above or below and we only need to know the fact of the crossover itself, either of the two additional commands could be omitted:

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

Now the lower subgraph will display a line with value of 1 for timestamps where crossovers happen and zero otherwise. Moving just a little bit forward: this kind of representation might seem not very convenient and, although output formatting will be discussed in chapters 9 and 10, consider using the following script to display the crossovers as dots at the bars where they take place:

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

Human-readable syntax is not restricted to crossover identification. Full vocabulary of commands can be found here. A number of sentences can be constructed using this vocabulary in order to define logical expressions, e.g.,

def condition1 = open is equal to close;
def condition2 = close is greater than Average(close,9);
def condition3 = condition1 is false and condition2 is false;

This looks more like a text than a script, doesn't it? However, it will work: condition1 will check if bars have equal Open and Close price, condition2 will define bars whose Close price is greater than its 9 period SMA, and condition3 will identify bars for which neither of the two is true.

Human-readable syntax can also be helpful to beginners when they need to use a past value of data (this is what we call 'referencing historical data' which is going to be fully described in chapter 11). For example, if you need to identify bars whose Close price is greater than its previous value, use this script:

declare lower;
plot Raise = close is greater than close from 1 bar ago;

Here, we used expression "from 1 bar ago" (which could be easily transformed to "from 2 bars ago", "from 20 bars ago", "from 77 bars ago" – whichever you prefer), and the result will be displayed as the line with values of 1 and 0. Again, for those who don't like the line representation, the Boolean points can be used: feel free to experiment with the SetPaintingStrategy function we revealed before.

Another interesting command is "within": it is used to check whether some condition was true at least once for any of the last several bars. So, we could expand a bit the previous script:

declare lower;
plot Raise = close is greater than close from 1 bar ago within 3 bars;

Now this script is going to work differently: the line will have value of 1 if the condition is true in at least one of the last three bars.

Now that we have learned many ways of specifying conditions which will possibly yield some valuable trading signals, we could test them by constructing a strategy to put these signals on chart and estimate the Profit/Loss value. Creating strategies will be thoroughly described in the next chapter.