Learning EasyLanguage & PowerLanguage – Lesson 05: Text and Trendlines

Tracking daily extremes with an indicator

The goal for this lesson is to do some more programming and create a study that tracks the daily extremes of a symbol. If you haven’t read the previous lessons, I suggest you start at the beginning with lesson 01, as this lesson will use basics covered in earlier sessions.

As I wrote the goal is a study that is able to track the daily extremes and to display them on the chart. We want to be able to see the current extremes for the day and also show yesterday’s extremes on today’s data. Let’s take a moment to consider what we need for this study and how we want to handle the objectives best:

  • we need to be able to find the highest high and lowest low for each day
  • the study should use trendlines to display yesterday’s extremes
  • we want to be able to change the appearance on the chart via inputs
  • the study should display text on the chart that labels the lines

This already gives you an idea about what we have to look at today. Besides being a useful study or framework for future modifications, drawing text and trendlines and updating them will be the focal point of this study.

Multicharts and Tradestation provide functions that return the daily high and low, but for various reasons this is not what we want to use here. Instead we’ll create two tracking variables that store the current highest high and lowest low. On a new day these variables need to be reset and their value will be stored in a second set of variables. We need to do this to be able to track the new extremes and to draw the trendlines displaying yesterday’s values.

Simple Program Logic

We have outlined what the program should do, let’s create a short list with the steps we need to do for easier reference:

  • track daily high and low with a variable throughout the day
  • store the previous daily extremes on a new day and reset the tracking variables
  • draw text and trendlines for the previous extremes on today’s data and update it with every new bar
  • add inputs to be able to conveniently change the text and trendline looks (color, size etc.)

This doesn’t look like too much work, so let’s start with it by creating a new indicator in PowerLanguage editor. Give it a meaningful name, so you can find it easily later on. I will start with the basic code for drawing text and trendlines and I will make sure that the code will work in both Multicharts and Tradestation. This means I will omit some reserved words for text and trendline manipulation that specifically Multicharts offers, but we can take a look at these later as they are not needed to produce the desired results here. Let’s take a look at the code for creating trendlines and text now.

Trendlines

Every trendline you create by code has a unique trendline-specific ID that you can use to modify the trendline later. You don’t have to worry about creating the IDs as the software will do that for you. The only thing we need to do is store the trendline ID into a variable, so we can retrieve it later in the code. Trendlines are drawn with using the reserved word “TL_New”, which is followed by six trendline parameters in brackets. When you want to draw a trendline in real live or in a programming code you need to have at least two anchor points – a start point and an end point. However you are not confined to keep the trendline within these two points, but can extend them left and right to infinity. Why does “TL_New” require six parameters? The six trendline paramaters are needed to specify those two anchor points on a chart. When you look on a chart the location of every value can be described using three parameters- the date, the time and the price of this value. As you need two anchor points, the “TL_New” reserved word therefore has six parameters.

trendline code image

“TLID” is a variable that holds the trendline-specific ID, the other six variables are properly named to show you which parameter represents what. Let’s create a simple code drawing a trendline and look at the outcome on the chart. I put the trendline code within a once statement to make sure that the code only creates one trendline.

basic trendline code image

This is what the above code draws on my chart:

trendline on a chart image

You need to go to the beginning of the chart and you should find the trendline there. The color will probably be orange as this is the default color the program will use, but don’t worry if you should see a different color on your end.
With a couple of additional reserved words we are going to change the color, size and style of the trendline. The first parameter for any of the reserved words that change the trendline is always the trendline-specific ID – otherwise the program wouldn’t know which trendline you want to change. “TL_SetColor” will change the trendline color, “TL_SetSize” is used to change the trendline width from 0 to 6 and “TL_SetStyle” sets the style according to the following list. While it doesn’t matter if you use the numbers or the reserved words like “Tool_Solid”, “Tool_Dashed” and so on for changing the style, I will use the numbers during this lesson.

trendline styles image

Adding the three reserved words and their parameters to the basic trendline code will change it to this:

trendline related reserved words image