This example shows an easy modification of the simple moving average that draws two additional lines on the chart.
The Moving Average Simple calculates the sum of a number of values over a selected period and divides it by their number. It then draws the result on the screen as a line.
You want the study to show two more lines:
▫ One that is drawn 10 percent above the central line
▫ One that is drawn 10 percent below the central line
▪ Save the Moving Average Simple under the name movs3. See Basing Your Study on an Existing Study.
▪ Now, take a look at the source code of the study:
Numeric Field source;
Result = Average(source, period) * (1 + shift /
▪ You can see that:
- The result of the study is a number
- You have three input parameters: source, which is used for the base security, period, which controls the number of bars (time period) that is used for calculating the average, and shift, which can be used to move the moving average line up or down by a certain percentage
- You have no variables to store intermediate results
- In the execution section, you have one line that calculates the result for the moving average
- With the next code line, a line called "MovS" is drawn on the chart with the end result
▪ The only part that concerns you is the part where the line is drawn. You want to draw two additional lines, so the first step would be to just duplicate this line two times:
▪ As you know, every element in a program has to have a unique name. Now, however, you have three lines with the same name. The next step would be to give each line its unique name. Change the name of the first line to "MovS_above" and that of the last line to "MovS_below":
▪ You now have three different lines with unique names. However, they all work with the same result data, the simple moving average, that is calculated with the first line of the execution block. You want one line to be drawn 10 percent above the moving average, and the other 10 percent below. This means that you have to adjust the value that is used to draw these lines. The easiest way to do this is by just multiplying the result with 1.1 (10 percent more) and 0.9 (10 percent less):
DrawLine("MovS_above", 1.1 *
DrawLine("MovS_below", 0.9 * Result);
▪ And this is already your final result! Check the syntax of your adapted code by clicking on Check Syntax, save it and test your new study on a chart. You should see three lines now.
Note You might wonder if you couldn’t use more flexible percentage values when drawing the additional lines: Say you want to be able to also draw lines that are 20 percent above and below the center line. Do you have to write a new study for every percentage value that you need? Of course not – that’s what parameters are here for. Learn how to further customize your study here: Adding Parameters to a Study (Moving Average Simple Cont’d).
What if you don’t want additional lines, but areas or markers? To add other study objects is not very different to the example shown above: You only have to be aware that each of them needs different input values to be able to draw something on the screen. For a line, as you have seen, you only need a name (for example "MovS_above") and a value that it should draw.
For other study objects, you need more input:
Areas need two values: Where the upper line should be positioned and where the lower line should be positioned. You could for example rewrite the example above to use an area instead of two lines:
DrawArea("MovS_area", 0.9 * Result, 1.1 * Result);
Markers need even more values – you have to define their name, the level where the price arrow will be positioned, if an up arrow or a down arrow should be used, and what marker text is inserted:
DrawMarker("BuySellMarker", Source.High, False, "Sell");
To learn more, see Drawing Functions.