Change chart color based on indicator

tinfox

Member
I'm curious if its possible to change the chart color based on a couple of indicators.

Particularity a long and short RSI, say 14 and 7 (or 10 minute and 5 minute)

Turn the chart red if there is a large gap between the short and long (with short being under) and vice versa.
 
Solution
You can have only ONE AssignBackgroundColor statement in a study. Your post contains at least THREE

Code:
# Turn the chart color
AssignBackgroundColor(
if RSI2 < RSI3 then color.green else
if RSI2 > RSI3 then color.pink else color.current);
I'm curious if its possible to change the chart color based on a couple of indicators.

Particularity a long and short RSI, say 14 and 7 (or 10 minute and 5 minute)

Turn the chart red if there is a large gap between the short and long (with short being under) and vice versa.

The variables that you provided definitions for:
Ruby:
# long and short RSI, say 14 and 7
def RSI7 = RSI("length" = 7) ;
def RSI14 = RSI("length" = 14) ;

# Turn the chart red
AssignBackgroundColor(
if RSI7 < RSI14 then color.red else color.green);


The variable missing a definition:
large gap

ToS platform requires all variables be defined before a study can be scripted.
 
I'm using 2hr for RSI2 and 4hr for RSI3 on the (on a 5 minute chart) MTF RSI script and trying to add this to the bottom. Only one of them works at a time.

# Turn the chart color
AssignBackgroundColor(
if RSI2 < RSI3 then color.green else color.current);

AssignBackgroundColor(
if RSI2 > RSI3 then color.pink else color.current);

------------------------------------------------------------

This is the script I'm adding it to
------------------------------------------------------------
#MTF RSI Three standard ToS RSI studies with a choice of time frame for second and third RSI. Cloud between chart and second RSI.
# RSI with agg periods by Horserider. 5/12/2019

declare lower;
#RSI
input length = 14;
input over_Bought = 80;
input over_Sold = 20;
input price = close;
input averageType = AverageType.WILDERS;



def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot lline = 50;


RSI.DefineColor("Positive and Up", Color.GREEN);
RSI.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI.DefineColor("Negative and Down", Color.RED);
RSI.DefineColor("Negative and Up", Color.DARK_RED);
RSI.AssignValueColor(if RSI >= 50 then if RSI > RSI[1] then RSI.Color("Positive and Up") else RSI.Color("Positive and Down") else if RSI < RSI[1] then RSI.Color("Negative and Down") else RSI.Color("Negative and Up"));

OverSold.SetDefaultColor(GetColor(7));
OverBought.SetDefaultColor(GetColor(7));

# RSI 2
input length2 = 14;
input price2 = close;
input averageType2 = AverageType.WILDERS;
input agg = AggregationPeriod.DAY;


def c = close(period = agg);

def NetChgAvg2 = MovingAverage(averageType2, c - c[1], length2);
def TotChgAvg2 = MovingAverage(averageType2, AbsValue(c - c[1]),length2);
def ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;

plot RSI2 = 50 * (ChgRatio2 + 1);


RSI2.DefineColor("Positive and Up", Color.GREEN);
RSI2.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI2.DefineColor("Negative and Down", Color.RED);
RSI2.DefineColor("Negative and Up", Color.DARK_RED);
RSI2.AssignValueColor(if RSI2 >= 50 then if RSI2 > RSI2[1] then RSI2.Color("Positive and Up") else RSI2.Color("Positive and Down") else if RSI2 < RSI2[1] then RSI2.Color("Negative and Down") else RSI2.Color("Negative and Up"));
RSI2.setLineWeight(3);

# RSI 3
input length3 = 14;
input price3 = close;
input averageType3 = AverageType.WILDERS;
input agg3 = AggregationPeriod.WEEK;


def c3 = close(period = agg3);

def NetChgAvg3 = MovingAverage(averageType3, c3 - c3[1], length3);
def TotChgAvg3 = MovingAverage(averageType3, AbsValue(c3 - c3[1]),length3);
def ChgRatio3 = if TotChgAvg3 != 0 then NetChgAvg3 / TotChgAvg3 else 0;

plot RSI3 = 50 * (ChgRatio3 + 1);


RSI3.DefineColor("Positive and Up", Color.GREEN);
RSI3.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI3.DefineColor("Negative and Down", Color.RED);
RSI3.DefineColor("Negative and Up", Color.DARK_RED);
RSI3.AssignValueColor(if RSI3 >= 50 then if RSI3 > RSI3[1] then RSI3.Color("Positive and Up") else RSI3.Color("Positive and Down") else if RSI3 < RSI3[1] then RSI3.Color("Negative and Down") else RSI3.Color("Negative and Up"));
RSI3.setLineWeight(5);

#addCloud(RSI, RSI2, color.green, color.red);

def rsilong=RSI3;
def rsishort=RSI2;


# Turn the chart red
AssignBackgroundColor(
if RSI2 < RSI3 then color.red else color.green);
 
You can have only ONE AssignBackgroundColor statement in a study. Your post contains at least THREE

Code:
# Turn the chart color
AssignBackgroundColor(
if RSI2 < RSI3 then color.green else
if RSI2 > RSI3 then color.pink else color.current);
 
Solution
Is there a way to change the chart background color based on what I have circled in yellow. For both oversold and overbought conditions?

Like a light red for OB and light green for OS? These particular RSI's on this 5 minute chart are the 1hr and 4hr..
https://1drv.ms/u/s!AgXNDtw32l9YgpcqAzI9bgiqRvmVOw?e=IyIVRy
The ToS platform requires definitions that would trigger the colors.
Please use words to describe the triggers of the background colors.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
500 Online
Create Post

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