Help coloring line of a study applied to lower time frame

ChocolateVol

New member
VIP
I'm tracking the classic MACD using a 20min time frame but displayed in the 5min chart.

Can you help me fix the below code so that the bars are colored green if up/sideways and red once they turn lower/sideways? The idea is that they should be all green or all red until the line flips direction and or remain of the same "trending" color otherwise.
Code:
declare lower;

input time1 = AggregationPeriod.TWENTY_MIN;

input fastLength = 3;
input slowLength = 10;
input averageType = AverageType.EXPONENTIAL;

plot Value1 = MovingAverage(averageType, close(period = time1), fastLength) - MovingAverage(averageType, close(period = time1), slowLength);

plot ZeroLine = 0;

Value1.SetPaintingStrategy(PaintingStrategy.LINE);
Value1.SetLineWeight(1);
Value1.DefineColor("Positive and Up", Color.GREEN);
Value1.DefineColor("Positive and Down", Color.RED);
Value1.DefineColor("Negative and Down", Color.RED);
Value1.DefineColor("Negative and Up", Color.GREEN);
Value1.AssignValueColor(if Value1 >= 0 then if Value1 >= Value1[1] then Value1.color("Positive and Up") else Value1.color("Positive and Down") else if Value1 < Value1[1] then Value1.color("Negative and Down") else Value1.color("Negative and Up"));
 
Last edited by a moderator:
Solution
I think this does it. I'm not sure if there was also a requirement to do something when it was above/below the zeroline, so that isn't included. Additionally, I made a few changes to setup your code for sustainability and best practice.

1. Removed "DefineColor" since you're using standard colors and only 2. Should just use Color.RED / Color.GREEN instead.
2. Added labels to your sections.
3. Added Value1Data to allow for other references / calculations prior to being put into a plot.
4. Broke your if/then/else chunks up into indented lines for readability.
5. Added AssignPriceColor() to color the bars as requested.


1695420660356.png




Ruby:
# DECLARATIONS
declare lower;

# INPUTS
input time1 = AggregationPeriod.TWENTY_MIN;
input...
I think this does it. I'm not sure if there was also a requirement to do something when it was above/below the zeroline, so that isn't included. Additionally, I made a few changes to setup your code for sustainability and best practice.

1. Removed "DefineColor" since you're using standard colors and only 2. Should just use Color.RED / Color.GREEN instead.
2. Added labels to your sections.
3. Added Value1Data to allow for other references / calculations prior to being put into a plot.
4. Broke your if/then/else chunks up into indented lines for readability.
5. Added AssignPriceColor() to color the bars as requested.


1695420660356.png




Ruby:
# DECLARATIONS
declare lower;

# INPUTS
input time1 = AggregationPeriod.TWENTY_MIN;
input fastLength = 3;
input slowLength = 10;
input averageType = AverageType.EXPONENTIAL;

# CALCULATIONS
def Value1Data = MovingAverage(averageType, close(period = time1), fastLength) - MovingAverage(averageType, close(period = time1), slowLength);
def Value1Direction = if Value1Data >= Value1Data[1] then 1 else if Value1Data < Value1Data[1] then -1 else Value1Direction[1];

# PLOTS
plot Value1 = Value1Data;
plot ZeroLine = 0;

# FORMATTING
Value1.SetPaintingStrategy(PaintingStrategy.LINE);
Value1.AssignValueColor(
    if Value1 >= 0 then
        if Value1 >= Value1[1] then
            Color.GREEN
        else
            Color.RED
    else if Value1 < Value1[1] then
        Color.RED
    else
        Color.GREEN);

# BAR COLORING
AssignPriceColor(
    if Value1Direction == 1 then
        Color.GREEN
    else if Value1Direction == -1 then
        Color.RED
    else
        Color.GREEN);
 
Solution

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
419 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