Change chart's background based on thinkscript condition?

evanevans

Active member
Can the background color of a chart be changed under conditions of a ThinkScript? Or perhaps a full window cloud? It would be great to be able to do this, so I could know which of 100s of charts I monitor simultaneously all day, need my attention.

Something like this:

SJhgVT4.png


I want to create a ThinkScript that can color the background of the whole chart under a particular current condition. I already got this far:

AFyqxt3.png
 
Last edited by a moderator:

Pensar

Well-known member
VIP
Lifetime
@evanevans According to the pic, it looks like you want the background color to change based on the relationship between current price and the 20 day SMA. Check if this is right -

Code:
input length = 20;
input avg_type = averagetype.simple;
input agg = aggregationperiod.day;

def c = close(period = agg);
def sma = movingaverage(avg_type,c,length);

DefineGlobalColor("above",GetColor(1));
DefineGlobalColor("below",Color.BLACK);

AssignBackgroundColor(if close > sma
                      then GlobalColor("above")
                      else GlobalColor("below"));


When I started learning thinkscript, I went down many dead-end roads, and still do so frequently. Usually I'm overthinking the code.
 

evanevans

Active member
Awesome guys, thanks for all your help. I'm sure happy with this code, which I have completed:

Code:
#ChartAlert_SMA
#EvanEvans
#V1.0

input aggregationPeriod = AggregationPeriod.DAY;
input SMAPeriod = 20;
input SuperSMAPeriod = 50;
input averageType = AverageType.SIMPLE;
input AlertWhenPercentAboveSMA = 0.050;
input SuperAlertWhenPercentAboveSMA = 0.100;

def c = close(period = aggregationPeriod);
def sma = movingaverage(averageType,c,SMAperiod);
def SuperSMA = movingaverage(averageType,c,SuperSMAperiod);

DefineGlobalColor("SuperAbove",Color.Green);
DefineGlobalColor("Above",Color.Dark_Green);
DefineGlobalColor("Below",Color.Dark_Red);
DefineGlobalColor("SuperBelow",Color.Red);
DefineGlobalColor("default",Color.Current);

AssignBackgroundColor(if close > ((SuperAlertWhenPercentAboveSMA+1) * sma)
                      then GlobalColor("SuperAbove") else if close > ((AlertWhenPercentAboveSMA+1) * sma) then GlobalColor("Above")
                      else if close < SuperSMA then GlobalColor("SuperBelow") else if close < SMA then GlobalColor("Below") else GlobalColor("default"));

If price is 5% above the 20 sma chart changes to dark green
If price is 10% above the 20 sma chart changes to bright green
If price is below the 20 sma chart changes to dark red
If price is below the 50 sma chart changes to bright red

Example in action:
uT1aOE0.png
 

MichDopo

New member
Awesome guys, thanks for all your help. I'm sure happy with this code, which I have completed:

Code:
#ChartAlert_SMA
#EvanEvans
#V1.0

input aggregationPeriod = AggregationPeriod.DAY;
input SMAPeriod = 20;
input SuperSMAPeriod = 50;
input averageType = AverageType.SIMPLE;
input AlertWhenPercentAboveSMA = 0.050;
input SuperAlertWhenPercentAboveSMA = 0.100;

def c = close(period = aggregationPeriod);
def sma = movingaverage(averageType,c,SMAperiod);
def SuperSMA = movingaverage(averageType,c,SuperSMAperiod);

DefineGlobalColor("SuperAbove",Color.Green);
DefineGlobalColor("Above",Color.Dark_Green);
DefineGlobalColor("Below",Color.Dark_Red);
DefineGlobalColor("SuperBelow",Color.Red);
DefineGlobalColor("default",Color.Current);

AssignBackgroundColor(if close > ((SuperAlertWhenPercentAboveSMA+1) * sma)
                      then GlobalColor("SuperAbove") else if close > ((AlertWhenPercentAboveSMA+1) * sma) then GlobalColor("Above")
                      else if close < SuperSMA then GlobalColor("SuperBelow") else if close < SMA then GlobalColor("Below") else GlobalColor("default"));

If price is 5% above the 20 sma chart changes to dark green
If price is 10% above the 20 sma chart changes to bright green
If price is below the 20 sma chart changes to dark red
If price is below the 50 sma chart changes to bright red

Example in action:
uT1aOE0.png
To change this from SMA to EMA, what would be the change required?
 

Nari2007

Member
Can the background color of a chart be changed under conditions of a ThinkScript? Or perhaps a full window cloud? It would be great to be able to do this, so I could know which of 100s of charts I monitor simultaneously all day, need my attention.

Something like this:

SJhgVT4.png


I want to create a ThinkScript that can color the background of the whole chart under a particular current condition. I already got this far:

AFyqxt3.png
how do i use this, i really want to give it a try. can i have the colors change just at the cross overs not at percentages as that ,makes it harder for me to understand,,,i daytrade, 15mins is the higher time frame maybe go up to a hour, so what should i set this aggregation period be? i use 3 min as entry and 15 mins as my ht
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
how do i use this, i really want to give it a try. can i have the colors change just at the cross overs not at percentages as that ,makes it harder for me to understand,,,i daytrade, 15mins is the higher time frame maybe go up to a hour, so what should i set this aggregation period be? i use 3 min as entry and 15 mins as my ht
If you want to trigger for entry then the aggregation period would be 3min.
 

bosi

New member
@evanevans According to the pic, it looks like you want the background color to change based on the relationship between current price and the 20 day SMA. Check if this is right -

Code:
input length = 20;
input avg_type = averagetype.simple;
input agg = aggregationperiod.day;

def c = close(period = agg);
def sma = movingaverage(avg_type,c,length);

DefineGlobalColor("above",GetColor(1));
DefineGlobalColor("below",Color.BLACK);

AssignBackgroundColor(if close > sma
                      then GlobalColor("above")
                      else GlobalColor("below"));


When I started learning thinkscript, I went down many dead-end roads, and still do so frequently. Usually I'm overthinking the code.

@Pensar
I am using simple logic if close > 100sma to assign background green else red. But I am only getting one color for the entire chart regardless whether close > 100sma or not. What am I doing wrong?

input MA = 100;
plot Avg = average(close, MA);
AssignBackgroundColor(if close < Avg then Color.Red else Color.Green);

Is there a way to set the background color for a section of the chart when a condition is met?
 
Last edited by a moderator:

halcyonguy

Well-known member
VIP
Lifetime
@Pensar
I am using simple logic if close > 100sma to assign background green else red. But I am only getting one color for the entire chart regardless whether close > 100sma or not. What am I doing wrong?

input MA = 100;
plot Avg = average(close, MA);
AssignBackgroundColor(if close < Avg then Color.Red else Color.Green);

Is there a way to set the background color for a section of the chart when a condition is met?

edit
i guess i should have looked at this page,
post #1 has a formula and a cloud , to draw vertical bands of color.
-------------------

if you want to have vertical bands of different colors, you need to use addcloud().

AssignBackgroundColor() Changes the entire background based on the current bar's conditions

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddCloud
 
Last edited:

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.
Top