Condition-Based Background Color

johndough

New member
I'm looking for a code to be able to change the chart background color based on studies... Im specifically looking to have my chart background color change based on the Supertrend Study on the 5 min (or other) aggregation period, for example when its Supertrend up the background color goes green or when its Supertrend Down the background is Red), all while no matter what timeframe im looking at my chart such as when im looking at the 1 minute chart...

Ive seen recently that Chris Brecher from Simpler trading has TOS and has some study that changes his chart background color based on the atr trailing stop study... so then i was like wow how can i do that with other studies...

that changing color for the atr trailing stop is also a code im interested in...

how do i do it? is there any thread for it here idk...

Im glad i just found this community looks like something i want to get into...
 
Solution
I'm looking for a code to be able to change the chart background color based on studies... Im specifically looking to have my chart background color change based on the Supertrend Study on the 5 min (or other) aggregation period, for example when its Supertrend up the background color goes green or when its Supertrend Down the background is Red), all while no matter what timeframe im looking at my chart such as when im looking at the 1 minute chart...

Ive seen recently that Chris Brecher from Simpler trading has TOS and has some study that changes his chart background color based on the atr trailing stop study... so then i was like wow how can i do that with other studies...

that changing color for the atr trailing stop is also a...
I'm looking for a code to be able to change the chart background color based on studies... Im specifically looking to have my chart background color change based on the Supertrend Study on the 5 min (or other) aggregation period, for example when its Supertrend up the background color goes green or when its Supertrend Down the background is Red), all while no matter what timeframe im looking at my chart such as when im looking at the 1 minute chart...

Ive seen recently that Chris Brecher from Simpler trading has TOS and has some study that changes his chart background color based on the atr trailing stop study... so then i was like wow how can i do that with other studies...

that changing color for the atr trailing stop is also a code im interested in...

how do i do it? is there any thread for it here idk...

Im glad i just found this community looks like something i want to get into...

The image shows a Supertrend code on the upper chart and ATR Trailing Stop in the lower. Both have optional chart background coloring. You can change these colors at the input screen to your liking.

Screenshot 2023-12-08 043708.png
Here is Supertrend code
Code:
# Mobius
# SuperTrend
# Chat Room Request
input AtrMult = 1.0;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
plot SuperTrend = ST;
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

                 then Color.RED

                 else if PaintBars and close > ST

                      then Color.GREEN

                      else Color.CURRENT);

AddChartBubble(close crosses below ST, low[1], low[1], color.Dark_Gray);
AddChartBubble(close crosses above ST, high[1], high[1], color.Dark_Gray, no);

input color_background = yes;
defineglobalColor("UP", color.dark_green);
defineglobalColor("DN", color.dark_red);
assignBackgroundColor(if !color_background then color.current
else if close > ST then globalColor("UP") else globalcolor("DN"));

# End Code SuperTrend
Here is ATR Trailing Stop code
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2009-2023
#

input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);

def trueRange;
switch (trailType) {
case modified:
    trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
    trueRange = TrueRange(high, close, low);
}
def loss = ATRFactor * MovingAverage(averageType, trueRange, ATRPeriod);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail =  close - loss;
        case short:
            state = state.short;
            trail = close + loss;
        }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - loss);
    } else {
        state = state.short;
        trail = close + loss;
    }
case short:
    if (close < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close + loss);
    } else {
        state = state.long;
        trail =  close - loss;
    }
}

def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);

plot TrailingStop = trail;

TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
    then TrailingStop.Color("Sell")
    else TrailingStop.Color("Buy"));

input color_background = yes;
defineglobalColor("UP", color.dark_green);
defineglobalColor("DN", color.dark_red);
assignBackgroundColor(if !color_background then color.current
else if state==state.long then globalColor("UP") else globalcolor("DN"));
 
Solution

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

Wow Thanks! I apologize, Im a newbie and i didnt see any notifications anyone saw my post or replied to it until now 10 days later...

Today I actually came back to write on my post that i was so excited because i figured out an easy way to do it, i just changed in the code from "assignpricecolor" to "assignbackgroundcolor"... and that worked... and now i look at your code and you did the same thing... LOL...

now im trying to find out how i can have a supertrend with a different aggregation period on a chart with a different time frame so i can look at the chart on a 1 or 5 min with the background color changing on the 30 min supertend...

and also change that color of the background from such a bright green and red to a fainter color...

and then of course see if it improves my trading or off to the scrap heap with this idea and on to the next...

thanks for your help...
 
Wow Thanks! I apologize, Im a newbie and i didnt see any notifications anyone saw my post or replied to it until now 10 days later...

Today I actually came back to write on my post that i was so excited because i figured out an easy way to do it, i just changed in the code from "assignpricecolor" to "assignbackgroundcolor"... and that worked... and now i look at your code and you did the same thing... LOL...

now im trying to find out how i can have a supertrend with a different aggregation period on a chart with a different time frame so i can look at the chart on a 1 or 5 min with the background color changing on the 30 min supertend...

and also change that color of the background from such a bright green and red to a fainter color...

and then of course see if it improves my trading or off to the scrap heap with this idea and on to the next...

thanks for your help...

You can use the following on a 1m chart, not a watchlist, nor the 5m or 30m charts. You cannot have lower aggs on a higher agg chart.

Screenshot 2024-01-01 092717.png
Code:
script SuperTrend {
    input AtrMult = 1.0;
    input nATR = 4;
    input AvgType = AverageType.HULL;
    input price = FundamentalType.HL2;
    input agg = AggregationPeriod.MIN;
    def ATR = MovingAverage(AvgType, TrueRange(high(period = agg), close(period = agg), low(period = agg)), nATR);
    def UP = Fundamental(price, period = agg) + (AtrMult * ATR);
    def DN = Fundamental(price, period = agg) + (-AtrMult * ATR);
    def ST = if close(period = agg) < ST[1] then UP else DN;
    plot SuperT = ST;
}

input agg1 = AggregationPeriod.MIN;
input agg2 = AggregationPeriod.FIVE_MIN;
input agg3 = AggregationPeriod.THIRTY_MIN;

def SuperT1 = close > SuperTrend(agg = agg1);
def SuperT2 = close > SuperTrend(agg = agg2);
def SuperT3 = close > SuperTrend(agg = agg3);
def green = SuperT1 + SuperT2 + SuperT3;

assignBackgroundColor(if green == 3 then color.light_green else if green == 0 then color.pink else color.white);
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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