I have had many requests in the past to change the default formatting of an indicator–color, line style, etc. This is easy to do, but not well documented. So here’s a tutorial on how to format plots in Think or Swim!
All indicators need to have at least one “plot” for them to show up on a chart. Let’s work with this example of a 20-period Exponential Moving Average:
plot myindicator=ExpAverage(close,20);
Once you have defined the plot, you can change many different parameters in this way:
myindicator.FUNCTION(arguments);
You begin with the name of the plot. “FUNCTION” is a certain function you want to use, and “arguments” are any inputs to the function. Here’s some things you can do with your Thinkscript plots:
Set the Color
To set the plot’s default color, you can use the “SetDefaultColor()” function. To set the indicator to the color white, use this:
myindicator.SetDefaultColor(Color.White);
“Color.White” is an argument that tells Thinkscript to use the color white. Very complicated, I know. π Here’s a list of the different colors that are pre-defined in Thinkscript:
(FYI, the list is found under “Color” in the “Constants” section of the Thinkscript editor sidebar.) “Color.Uptick” and “Color.Downtick” match whatever you have set for these colors in the main chart settings panel.
Change the Color According to a Value
To change the color on the fly, you use the “AssignValueColor()” function. If you want to apply conditional color formatting, you can also use an if/then/else statement, like this:
myindicator.AssignValueColor(if close>=myindicator then Color.Green else Color.Red);
In our example, this code will recolor the EMA green if the close of a bar is above the EMA, and red if the close is below the EMA.
Set the Curve Style
You can change the line style of your indicator using the “SetStyle()” function. The possible arguments are:
curve.FIRM # The standard line
curve.INEDITABLE # Standard line, but cannot be changed by user
curve.LONG_DASH # Long dashes
curve.SHORT_DASH # Short dashes
curve.POINTS # Just a dot
So if you wanted your EMA to be long dashed, use this:
myindicator.SetStyle(curve.LONG_DASH);
Set the Plot To a Histogram or a Line With Markers
The “SetStyle” command just applies to the standard curve. To use other styles, use the “SetPaintingStrategy() function. The possible arguments are:
paintingstrategy.HISTOGRAM # Plots with a bar from 0 to the value
paintingstrategy.LINE_VS_POINTS # Plots a line, but with dots at each value
paintingstrategy.LINE_VS_SQUARES # Plots a line, but with squares at each value
paintingstrategy.LINE_VS_TRIANGLES # Plots a line, but with triangles at each value
To make our EMA have a square at each point, use this code:
myindicator.SetPaintingStrategy(paintingstrategy.LINE_VS_SQUARES);
Set the Line Weight
To change the thickness of the plotted line, dot, marker or bar, use the “SetLineWeight()” function. The arguments are 1, 2, 3, 4, or 5. That’s it. The thinnest is 1 and the thickest is 5. This example sets the line to the thickest weight possible:
myindicator.SetLineWeight(5);
Dynamically Hide and Show the Plot
You can hide or show your plot programmatically with the function “SetHiding()”. The argument should be 0 for show, 1 for hide. If you wanted to show our EMA only when price was above today’s open, you could use this code:
myindicator.setHiding(if close>open(period="DAY") then 0 else 1);
Note that this will either hide or show THE ENTIRE plot based on the results of the “if” test in real time. This is what I do in my Formatted Pivot Points indicator to do dynamic hiding.
Now you know the basics of formatting your plots with Thinkscript! Leave a comment if you have any questions. For your convenience, here’s all the example plot code repeated below:
plot myindicator=ExpAverage(close,20);
myindicator.SetDefaultColor(Color.White);
myindicator.AssignValueColor(if close>=myindicator then Color.Green else Color.Red);
myindicator.SetStyle(curve.LONG_DASH);
myindicator.SetPaintingStrategy(paintingstrategy.LINE_VS_SQUARES);
myindicator.SetLineWeight(5);
myindicator.setHiding(if close>open(period="DAY") then 0 else 1);
Tags: plot_formatting, Thinkscript, tutorial
June 4, 2009 at 10:28 pm
Hey Pro,
Your tutorial and breakdown is much appreciated as is your time and the time it took you to draft it up.
π‘ You can actually create a tutorial series on the functions, workings and nuances of good ‘ol thinkScript. I’d definitely tune in for for the series.
Keep up the great work
Peace-
November 12, 2009 at 3:28 pm
Good post. Is there anyway to change the color of the price bar (Chart Bar) other than the two colors choice? I want the price bar to change color is for example Price Bar goes yellow if close is above 20 day ma. Price bar turns blue if close is below 20 day ma. Any suggestion?
Thanks.
November 12, 2009 at 3:56 pm
Yes, try this post:
https://readtheprospectus.wordpress.com/2009/09/12/paintbars-finally-come-to-think-or-swim/
January 3, 2012 at 10:15 pm
Such a great post inspired people who read it..I’m waiting more good quality post from this, so bookmark!
December 6, 2012 at 10:01 pm
How would I set the background conditionally. For example,
if MA1>MA3
assignbackgroundcolor(color.cyan)
else
assignbackgroundcolor(color.pink)
I get an error message saying I can not do this in branch statements. Any ideas appreciated.
Thanks.
December 6, 2012 at 10:55 pm
You have to do it inside of the assignbackgroundcolor statement, like this:
assignbackgroundcolor(if MA1>=MA3 then color.cyan else color.pink);
December 6, 2012 at 11:15 pm
Thanks I thought I tried that, but it was showing arror..
Now, it works … appreciate it.
December 4, 2013 at 4:32 am
I tried to change the background color as follows:
assignbackgroundcolor(if close > Average(close, 15) then color.green else color.red);
I put the line in a custom study. However, the study does not work as I expect – it turns the whole background into one color instead of strips of green and red colors.
I have searched through the interent and read another thread, but I still cannot solve the issue. (http://finance.dir.groups.yahoo.com/neo/groups/TOS_thinkscript/conversations/topics/9327)
Please help. Thanks a lot!
July 5, 2016 at 7:15 am
In your SetHiding() example you mention that the ENTIRE PLOT is hidden/shown.
I’m having a hard time hiding/showing my indicator in a lower pane when there is an expansion area in the upper pane. However, I don’t want the entire plot hidden, just for the bars in the expansion area.
How do I deal with this?
Thanks!