Simple bar color script using AddChart() not working or am I overthinking it?

jngy2k

Member
It's been AWHILE but glad the community is still as popular as ever.
Firstly my code runs.
Lastly it doesn't work. lol. The display shows it as purple for some reason so I'm thinking there is a settings problem:unsure:?
price code.PNG


Here's my code to change the Current bar color according to high and low of the Previous Bar:
Code:
declare upper;
def h = high;
def l = low;
def c = close;
def o = open;
def isGreen = c > high[1];
def isRed = c < low[1];    
def isYellow = !isGreen and !isRed;
AddChart(high = h,low = l,open = o,close = c,type = ChartType.Bar);
AssignPriceColor(if isGreen then Color.GREEN else if isRed then Color.RED else Color.YELLOW);

What am I doing wrong?

edit: I just started working on this code an hour ago from posting so I might figure it out but thought I'd get some input from here in the meanwhile.
 
Solution
I'm an *****.


Code:
declare upper;
AssignPriceColor(if close > high[1] then Color.GREEN else if close < low[1] then Color.RED else Color.YELLOW);
I'm an *****.


Code:
declare upper;
AssignPriceColor(if close > high[1] then Color.GREEN else if close < low[1] then Color.RED else Color.YELLOW);
 
Solution
@jngy2k Here is documentation for the AddChart() function, including bar color parameters... This example assumes that global colors have already been defined using DefineGlobalColor(), however, you can use standard Color.<color> or any color definition...

AddChart(high = haHigh, low = haLow, open = o, close = haclose, type = ChartType.CANDLE, growColor = GlobalColor("RisingMA"), fallColor = GlobalColor("FallingMA"), neutralColor = color.gray);
 
It's been AWHILE but glad the community is still as popular as ever.
Firstly my code runs.
Lastly it doesn't work. lol. The display shows it as purple for some reason so I'm thinking there is a settings problem:unsure:?


Here's my code to change the Current bar color according to high and low of the Previous Bar:
Code:
declare upper;
def h = high;
def l = low;
def c = close;
def o = open;
def isGreen = c > high[1];
def isRed = c < low[1];   
def isYellow = !isGreen and !isRed;
AddChart(high = h,low = l,open = o,close = c,type = ChartType.Bar);
AssignPriceColor(if isGreen then Color.GREEN else if isRed then Color.RED else Color.YELLOW);

What am I doing wrong?

edit: I just started working on this code an hour ago from posting so I might figure it out but thought I'd get some input from here in the meanwhile.

things wrong,

you didn't post the whole code.
i assume you have something like this in your code?
Code:
# turn off price bar
input HidePrice = YES;
HidePricePlot(HidePrice);

AssignPriceColor( ) is used to control the default price bar colors. but you have them turned off, so it isn't going to do anything.

you are using addchart( ) , but didn't include a color, so it assigned a previously used color (or a random one, i'm not sure) AssignPriceColor() is not going to affect addchart().


if you want to draw bars in 3 different colors, you will need 3 groups of code, for each color. each group with its own addchart(). i know it is mentioned that addchart() has several color parameters, but i never had more than 1 color work as intended.


learn how to experiment and try things, to see what really happens. don't just read something and believe it.

experiment with these lines.
enable the first line. it will draw all the bars in cyan.
disable the 1st and enable the second line. all the bars are magenta.
disable the 2nd and enable the 3rd line, one with 2 colors. the 2nd color is ignored and all bars are cyan.
in these examples, addchart() is applying 1 color to all the bars. it doesn't seem to matter which color parameter is used.
Code:
input type = ChartType.Bar;
AddChart(high = h,low = l,open = o,close = c, growColor = Color.cyan, type = type);
#AddChart(high = h,low = l,open = o,close = c, fallColor = color.magenta, type = type);
#AddChart(high = h,low = l,open = o,close = c, growColor = Color.cyan, fallColor = color.magenta, type = type);
[\code]


here are some links,
draws colored bars, with 2 addchart() commands
https://usethinkscript.com/threads/addcloud-hide-remove.12419/#post-106691

draws candles with 2 different stacked colors, 
https://usethinkscript.com/threads/candle-fill-color.6691/#post-70469
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
415 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top