The plot() function in thinkScript is the most repeatedly used function to render and display data on your chart.
In fact, the template that you start with when creating a new custom ThinkorSwim indicator begins with plot Data = close;
That shows how important the plot() function is in thinkScript.
In today’s guide, I will explain how the plot function works and share a few coding examples with you.
When you want to output data from your indicator or backtesting strategy, you employ the plot() function.
Exampl #1: Plot a simple moving average
input price = close; input length = 9; plot SMA = Average(price, length);
This script simply plots a 9-period simple moving average on your chart.
Example #2: Plot implied volatility
declare lower; plot ImpVol = IMP_VOLATILITY();
If you want to show the implied volatility of the stock, you can start with
plot and insert the
Example #3: Plot highest high and lowest low
plot highestHigh = highest(high, 22); plot lowestLow = lowest(low, 22);
The code above plots the highest highs and lowest lows of the last 22 candles.
It’s really that easy! You only need two main components.
Start by writing the required
series argument and then use the
plot() function to render the specified data on the chart.
Customizing your plot
Sometimes, you may not like how thinkScript presents the data on your chart. Certain values require a different set of styles.
By default, the plot function is always displayed as a regular line.
In order to change the style of your plot, you will need to use the SetPaintingStrategy constant.
Indicators like volume, which shows the distribution of values, might be better suited to be shown as a histogram rather than a line.
Without changing anything from your plot function, you can utilize the SetPaintingStrategy constant to change the style from a line to a histogram.
declare lower; plot Vol = volume; Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
In some indicators, you will need to use a particular plotting style.
In the following script, I plot arrows on my chart to quickly identify when a 3-consecutive green bars pattern occurs.
def IsUp = close > open; plot greenBars = if IsUp and IsUp and IsUp then low else double.nan; greenBars.SetPaintingStrategy(paintingStrategy.ARROW_UP);
The plot() function is thinkScript is very flexible.
You can declare the plot first and then define its value afterward or vice versa.
To top it off, you can use different colors and styling options to make the plot more visible on your chart.