assignBackgroundColor Not working

OG Bubba Knox

New member
Hi All,

I have this code where assignBackgroundColor is not working as expected. Here is the entire script:


Code:
input price = CLOSE;
input length = 14;

def Earnings = AbsValue(GetEventOffset(Events.Earnings, 0));
def BBWidth = BollingerBandwidth(AverageType.Exponential, price, 0, length, -2.2, 2.2); #needs to bee BB width
def AvgVol = MovingAverage(AverageType.Simple,BBWidth,20);
plot NextEarnings = if isNaN(Earnings)then 0 else Earnings;
assignBackgroundColor(if BBWidth > AvgVol then color.black else color.green);

It is code for a watchlist column. It should color the background of the cell green if the volatility is relatively low and black if volatility is relatively high... this part is not working, the colors are not applied and I have no clue what is wrong with the syntax.

Thanks!!
 
Solution
Hi All,

I have this code where assignBackgroundColor is not working as expected. Here is the entire script:


Code:
input price = CLOSE;
input length = 14;

def Earnings = AbsValue(GetEventOffset(Events.Earnings, 0));
def BBWidth = BollingerBandwidth(AverageType.Exponential, price, 0, length, -2.2, 2.2); #needs to bee BB width
def AvgVol = MovingAverage(AverageType.Simple,BBWidth,20);
plot NextEarnings = if isNaN(Earnings)then 0 else Earnings;
assignBackgroundColor(if BBWidth > AvgVol then color.black else color.green);

It is code for a watchlist column. It should color the background of the cell green if the volatility is relatively low and black if volatility is relatively high... this part is not working, the colors are not...
Hi All,

I have this code where assignBackgroundColor is not working as expected. Here is the entire script:


Code:
input price = CLOSE;
input length = 14;

def Earnings = AbsValue(GetEventOffset(Events.Earnings, 0));
def BBWidth = BollingerBandwidth(AverageType.Exponential, price, 0, length, -2.2, 2.2); #needs to bee BB width
def AvgVol = MovingAverage(AverageType.Simple,BBWidth,20);
plot NextEarnings = if isNaN(Earnings)then 0 else Earnings;
assignBackgroundColor(if BBWidth > AvgVol then color.black else color.green);

It is code for a watchlist column. It should color the background of the cell green if the volatility is relatively low and black if volatility is relatively high... this part is not working, the colors are not applied and I have no clue what is wrong with the syntax.

Thanks!!

This seems to work on the watchlist. Your study worked on a chart.

Screenshot-2022-10-31-063407.png
Ruby:
input price = CLOSE;
input length = 14;

def Earnings = AbsValue(GetEventOffset(Events.Earnings, 0));
def BBWidth = BollingerBandwidth(AverageType.Exponential, price, 0, length, -2.2, 2.2); #needs to bee BB width
def AvgVol = MovingAverage(AverageType.Simple,BBWidth,20);
def NextEarnings = if isNaN(Earnings)then 0 else Earnings;
addlabel(1,"", if BBWidth > AvgVol then color.black else color.green);
assignBackgroundColor(if BBWidth > AvgVol then color.black else color.green);
 
Solution

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

This seems to work on the watchlist. Your study worked on a chart.

@SleepyZ Thank you so much... Ok that seems to get the colors changing on the background how I want... I am trying to get the background colors to change green and black based off volatility, that part works now, thank you! I need that part to work while the cell displays the number of days till earnings. I currently can either only have the numbers show up with no green and black background or the background colors work with no number of days till earnings.

I know there is a way to have both, I have it on other watchlists... This one chunk of code isn't working for this one though....

Here is an example of some code that does both, exactly how I want it (green + Black + numbers)... Although, this one has different numbers being displayed. (this one displays RSI not days till next ER)

Code:
input price = CLOSE;
input length = 14;

def RSIDaily = RSI( 14, 80, 20, price, AverageType.Wilders);

def BBWidth = BollingerBandwidth(AverageType.Exponential, price, 0, length, -2.2, 2.2);
def AvgVol = MovingAverage(AverageType.Simple,BBWidth,20);
plot threshold = if BBWidth == 0 then 1 else RSIDaily;
assignBackgroundColor(if BBWidth > AvgVol then color.black else color.green);
threshold.AssignValueColor(if RSIDaily < 30 then color.blue else
                           if RSIDaily > 70 then color.red else
                           if BBWidth < AvgVol then color.black else color.white);

Thanks again for helping me out here, you are a legend!
 
Last edited:
@SleepyZ Thank you so much... Ok that seems to get the colors changing on the background how I want... I am trying to get the background colors to change green and black based off volatility, that part works now, thank you! I need that part to work while the cell displays the number of days till earnings. I currently can either only have the numbers show up with no green and black background or the background colors work with no number of days till earnings.

I know there is a way to have both, I have it on other watchlists... This one chunk of code isn't working for this one though....

Here is an example of some code that does both, exactly how I want it (green + Black + numbers)... Although, this one has different numbers being displayed. (this one displays RSI not days till next ER)

Code:
input price = CLOSE;
input length = 14;

def RSIDaily = RSI( 14, 80, 20, price, AverageType.Wilders);

def BBWidth = BollingerBandwidth(AverageType.Exponential, price, 0, length, -2.2, 2.2);
def AvgVol = MovingAverage(AverageType.Simple,BBWidth,20);
plot threshold = if BBWidth == 0 then 1 else RSIDaily;
assignBackgroundColor(if BBWidth > AvgVol then color.black else color.green);
threshold.AssignValueColor(if RSIDaily < 30 then color.blue else
                           if RSIDaily > 70 then color.red else
                           if BBWidth < AvgVol then color.black else color.white);

Thanks again for helping me out here, you are a legend!

Sorry, but the assignbackgroundcolor does not work for everything I have tried with earnings to either plot or appear in a label.

So, the best I can see working is to displaying the earnings value in a label as green per your color criteria or black, appearing as blank unless you click on the cell.

 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
513 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