This walkthrough is intended to demonstrate the very basics of scripting.
A chart script is composed of one or more script "statements". A statement can be either a mathematical function that produces a value, an assignment of a function to a variable to use later in the script, or a command instructing the scripting engine what or how to plot a value on chart.
Before we get started, you should have a chart open in front you. It does not matter contract you use, but the chart should be free of any other studies an uncluttered so that you can easily see the results of your script.
Example 1: A line-plot of the closing price.CLOSE
This statement is evaluated one time for every bar on the chart. Because this script contains no commands to the contrary, the script engine will go ahead and plot the output of this script as a simple line chart.
Example 2: A line-plot of the "typical" price.(H + L + C) / 3
The “typical” price of a bar chart is average of the high, low and close. H, L, and C are aliases for the HIGH, LOW, and CLOSE bar prices. Aliases can be used in place of, or interchanged with the full names of the data fields for great conciseness within you scripts.
Example 3: A line-plot of the 10-period Simple Moving Average of the typical price.SUM((H + L + C) / 3, 10) / 10
This example utilizes the built-in
SUM
function. The
SUM
function takes 2 comma-separated parameters as follows:
SUM(<number to be added>, <number of periods to sum over>)
In this example, we would like to add up the typical price
(H+L+C)/3
over 10 periods. Lastly, we divide this sum by 10 to get the average value over the last 10 periods.