Paintbars Come to Think or Swim

The latest Think Desktop software release has finally given us paintbars! Inside of your thinkscript code, you just use the function “AssignPriceColor(;”. You can also use conditional statements inside the function call, like this:

assignpricecolor(if close>ema then else;

That code will paint the bar green if its close is above the value of “ema”, or red if it is below. Here’s the result on a chart:


27 Responses to “Paintbars Come to Think or Swim”

  1. guest Says:

    that is cool. how would i set this up? do i open the EMA file and input the code. can you tell i am not much of a coder! 🙂

  2. Prospectus Says:

    In this example, It was assumed that there was already a variable named “ema” available in your script. Here’s a more basic example. Say you wanted to apply a paintbar if the bar close was above or below the 20 period exponential moving average. You would put this into a new study:

    input period = 20; def ema = expaverage(close,period); assignpricecolor(if close>ema then else;

    Save that study, then add it to your chart and you’re good to go.

  3. James T. Says:

    can’t wait to see what magic u can do with it =)

  4. richardwtodd Says:

    Isn’t the butterworth filter nice? I actually haven’t used them any in actual trading, but I play with them a lot because they are so pretty.

  5. Prospectus Says:

    Yes, it’s awesome! I’m looking for other ways to filter out noise than standard EMA’s, inspired by your articles. I found a bunch of Ehler’s indicators for Tradestation that I’m converting to Think or Swim. I’m also playing with an idea to make the butterworth filter adaptive, where the input period is reduced in trending markets, and is increased in choppy markets. Cool stuff–hopefully I can translate something from “cool” to profitable.

  6. Richard Says:

    yeah, I have the 2 and 3 pole “super smoothers” tied to the cycle function, which actually makes it *longer* durning trends and *shorter* during stationary periods, BTW. Opposite of what you indicated.

    I believe I am going to start a free indicator of the month program, or something, and I’ll probably release those as part of it, since I would never sell something that’s just a modification of publicly available work.

  7. Prospectus Says:

    Now that’s interesting–why would you want the filter to get more sloppy during chop? I would have thought that you would want more inertia in chop to keep from getting whipsawed…

  8. Richard Says:

    I’m just saying what a dominant cycle kind of adaptive does… you can make your filter adaptive the opposite way. As I said on twitter, one reason to go the other way is to have quicker turns during tighter action, whereas in a trend you don’t need much except the nerve to hop on board.

  9. Prospectus Says:

    This is an interesting topic. I’ll have to put it on the list and do some testing to see which way makes for a better trading system. I imagine it might be somewhat timeframe dependent. I was looking to put my inertial filter on a very fast chart, an idea you mentioned once–speed up your chart, slow down your indicators.

  10. SUresh Says:

    this is good as I was looking to use Paint bars within bettervolume study..
    how can I use this function within multiple IF statements..
    If condition==1 then{
    }else {
    if cond==2 then
    if cond==3 …..

    I can’t get this work.. appreciate your inputs.

  11. Prospectus Says:

    You’re on the right track. Just use () instead of {} in thinkscript. You also have to place the if-then-else statements inside of the assignpricecolor function itself. Something like this:

    Assignpricecolor(If condition then (color.whatever) else if (condition2 then color.another) else (color.stillanother));

  12. Webby Says:


    I tried to use this method to have PaintBars for the candle formations such as Doji etc. but my code keeps erroring out.

    def isDoji = if (AbsValue(open – close) open,,;

    I’m simply trying to paint a complete candle WHITE if it is a Doji otherwise simply paint it Red or Green. Pretty simple I thought but the validation routine throws it out.

    Can you please take a look and suggest what may be wrong ? Thanks.

  13. Prospectus Says:

    You can’t use it in a variable definition. It has to be inside of it’s own line of code where you call the assignpricecolor function. This is how you could do it:

    Def isDoji = if (***doji test logic, 1,0);


  14. Webby Says:

    looks like my copy-paste had a snag. the actual line in TOS reads:

    def isDoji = if (AbsValue(open – close) open,,;

    But it still gives me errors. Apologies about the earlier note.

  15. Webby Says:


    As suggested I finally managed to get that working, in spite of my inability to get the copy-paste to work properly when posting the note.


  16. Prospectus Says:

    It’s not copy paste, it’s wordpress taking your > and < and thinking they are HTML tags. If you want to paste them, use (without spaces): & gt; for > and & lt; for <

    Glad you got it working!

  17. Eric Says:


    I would like to change the size of price bar. May I know what thinkscript code should I use?

  18. HR Says:

    Paint Bars is a nice addition to TOS. However, it is amazing to me how much effort people are willing to put into custom coding for a TOS platform that is SO user unfriendly and the “new” Paint Bar features highlights this. For example, even TDAmeritrade’s Strategy Desk has a very user friendly means to select multiple conditions from which to test and then generate COLOR BARS easilty. It also lets you easily do the same with Strategies AND…you can view backtest results in a user configurable table right below the chart. TOS backtesting is woefully behind the times. Amazing. I admire you all for continuing to unlock the code required to do the things other brokerage’s tools already do (Scottrade, Fidelity, etc.). It’s more like a passion, a labor of love for the challenge of programming, reinventing the wheel for a lot of basic features. I’ve gotten caught up in the challenge as well! Again, I admire your expertise and dedication to the challenge TOS presents! : )

  19. Prospectus Says:

    I agree that other brokers have more advanced platforms in general. Think or Swim does have one of the most powerful platform for complex options trading and analysis, and options were initially their focus. Also, at present I can’t trade futures from an account with one of the other brokers you mentioned. Plus, you are paying for those tools with higher commissions. Scottrade hasn’t updated their software in years, and I find TD’s strategy desk to be unusable. TOS updates their software and adds new (at least to them) features almost monthly. But to each his own.

  20. HR Says:

    Understood, Prospectus. I appreciate your helping us make TOS work better. I had not heard about the addition of TOS’ Paint Bars functionality except from you. I quickly integrated it into my charts and will continue to improve my use of it. Sweet. That one find alone is worth the donation I’ll be sending. Thanks for all you do!

  21. Max Says:

    Can you help me to create my code. I very appreciate.
    Thank in advance.

    IF I want

    Price over SMA 20,50,200 = Green
    Price below SMA 20,50 200 = Red
    Price over SMA 20,50, 200 and over top Bollinger Band = Blue
    Priver below SMA 20,50,200 and below lower Bollinger Band = Purple
    Price below SMA 20 = yellow
    Price below SMA 50 = orange

  22. Max Says:

    plot Data = close;

    input price = close;

    input length = 20;

    input price2 = close;

    input displace = 0;

    input lenght2 = 50;

    input price3 = close;

    input lenght3 = 200;

    def AVG1 = Average(price[-displace], length);

    def AVG2 = Average(price2[-displace], lenght2);

    def AVG3 = Average(price3[-displace], lenght3);

    Data.DefineColor(“strong Uptrend”, GetColor(6));

    Data.DefineColor(“consolidate”, GetColor(8));

    Data.DefineColor(“strong Downtrend”, GetColor(5));

    Data.DefineColor(“consolidate low”, GetColor(1));

    Data.DefineColor(“Downtrend low”, GetColor(4));

    AssignPriceColor(if Data > AVG1 and Data > AVG2 and Data > AVG3 then Data.color(“strong Uptrend”) else if (Data < AVG3 and Data < Avg2 and data < avg1) then Data.color("strong Downtrend") else if Data < AVG3 and data avg1 then Data.color (“Downtrend low”) else if Data avg2 and data <avg3 then Data.color ("consolidate low") else Data.color("consolidate"));

  23. TR Says:

    Thank you for posting this code. I’m just getting started with TOS and this is a useful resource to have. One question – if I only want the test to be performed on the opening bar, how would I specify that ?

  24. SK Chua (From Singapore) Says:

    Hi, I am new to ToS and would like to achieve the following and hope if you can kindly help me.

    I want to paint my pricebar GREEN if it meet 2 conditions (1) MACD(12,26,9) above MACD signal line and (2) MACD above zero line, and to paint RED if it meets 2 conditions (1) MACD below MACD signal line and (2) MACD below zero line. And if any of the above conditions are not met, paint the price as Grey or White.

    I would also like to place a green triangle below the pricebar when RSI is above 70, and place a red triangle above the pricebar when RSI is below 30. My RSI parameter is 7.


  25. Declan Says:

    Appreciating the time and effort you put into your site and detailed information you offer.

    It’s awesome to come across a blog every once
    in a while that isn’t the same out of date rehashed information. Wonderful read!
    I’ve saved your site and I’m adding your RSS feeds to my Google account.

  26. Zac Says:


    Anyone knows how to get the following on ThinkScript?

    assignpricecolor ( if LBR_SmartADX Level 1 is less than 20 where length is 6, then color.yellow)
    assignpricecolor ( if LBR_SmartADX Level 2 is greater than 60 where length is 6, then color.cyan)

    much appreciated


  27. Joe Gonzalvez Says:

    Can anyone help me with a thinkscript code that will change the color of a chart bar given the following conditions:

    Color the chart bar green when the Slow Stochastics (14,3) %D is equal to or greater than 50
    AND the RSI(7) is equal to or greater than 50
    AND the MACD (8,18,6) fast line crosses above the slow line

    Also color the chart bar red when the Slow Stichastics (14,3)% D is equal to or less than 50
    AND the rsi(7) is equal to or less than 50
    AND the MACD fast line crosses below the slow line

    if none of those two conditions are met, then color the bar chart black

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: