Paint line color based on trend

sclistella13

New member
I am new to this forum and not a coder. I am trying to paint line color based on the trend. I have it semi working now, however the issue I'm having is when the line ticks up it turns green, but if the next move is sideways, then it paints red; and vice versa, when price ticks down it is red, but if the next move is sideways it is green. See attached screenshot and code. Notice how when the price stair steps down it prints red down and green sideways.

Code:
declare lower;
script macd {
    input agg        = AggregationPeriod.TWO_MIN;
    input fastLength = 3;
    input slowLength = 10;
    input MACDLength = 1000;

    input averageType = AverageType.EXPONENTIAL;

    plot Value = MovingAverage(averageType, close(period = agg), fastLength) - MovingAverage(averageType, close(period = agg), slowLength);
    plot Avg = MovingAverage(averageType, Value, MACDLength);

    plot Diff = Value - Avg;
}

input agg1 = AggregationPeriod.FIVE_MIN;
input agg2 = AggregationPeriod.TEN_MIN;
input agg3 = AggregationPeriod.FIFTEEN_MIN;
input agg4 = AggregationPeriod.TWENTY_MIN;
input agg5 = AggregationPeriod.THIRTY_MIN;
input agg6 = AggregationPeriod.HOUR;
plot diff1 = MACD(agg1).Diff;
plot diff2 = MACD(agg2).Diff;
plot diff3 = MACD(agg3).Diff;
plot diff4 = MACD(agg4).Diff;
plot diff5 = MACD(agg5).Diff;
plot diff6 = MACD(agg6).Diff;
plot ZeroLine = 0;

def xdiff1 = diff1 >= 0;
def xdiff2 = diff2 >= 0;
def xdiff3 = diff3 >= 0;
def xdiff4 = diff4 >= 0;
def xdiff5 = diff5 >= 0;
def xdiff6 = diff6 >= 0;

Diff1.SetPaintingStrategy(PaintingStrategy.LINE);
Diff1.SetLineWeight(1);
Diff1.DefineColor("Positive and Up", Color.GREEN);
Diff1.DefineColor("Positive and Down", Color.RED);
Diff1.DefineColor("Negative and Down", Color.RED);
Diff1.DefineColor("Negative and Up", Color.GREEN);
Diff1.AssignValueColor(if Diff1 >= 0 then if Diff1 > Diff1[1] then Diff1.Color("Positive and Up") else Diff1.Color("Positive and Down") else if Diff1 < Diff1[1] then Diff1.Color("Negative and Down") else Diff1.Color("Negative and Up"));

Diff2.SetPaintingStrategy(PaintingStrategy.LINE);
Diff2.SetLineWeight(1);
Diff2.DefineColor("Positive and Up", Color.GREEN);
Diff2.DefineColor("Positive and Down", Color.RED);
Diff2.DefineColor("Negative and Down", Color.RED);
Diff2.DefineColor("Negative and Up", Color.GREEN);
Diff2.AssignValueColor(if Diff2 >= 0 then if Diff2 > Diff2[1] then Diff2.Color("Positive and Up") else Diff2.Color("Positive and Down") else if Diff2 < Diff2[1] then Diff2.Color("Negative and Down") else Diff2.Color("Negative and Up"));

Diff3.SetPaintingStrategy(PaintingStrategy.LINE);
Diff3.SetLineWeight(1);
Diff3.DefineColor("Positive and Up", Color.GREEN);
Diff3.DefineColor("Positive and Down", Color.RED);
Diff3.DefineColor("Negative and Down", Color.RED);
Diff3.DefineColor("Negative and Up", Color.GREEN);
Diff3.AssignValueColor(if Diff3 >= 0 then if Diff3 > Diff3[1] then Diff3.Color("Positive and Up") else Diff3.Color("Positive and Down") else if Diff3 < Diff3[1] then Diff3.Color("Negative and Down") else Diff3.Color("Negative and Up"));

Diff4.SetPaintingStrategy(PaintingStrategy.LINE);
Diff4.SetLineWeight(1);
Diff4.DefineColor("Positive and Up", Color.GREEN);
Diff4.DefineColor("Positive and Down", Color.RED);
Diff4.DefineColor("Negative and Down", Color.RED);
Diff4.DefineColor("Negative and Up", Color.GREEN);
Diff4.AssignValueColor(if Diff4 >= 0 then if Diff4 > Diff4[1] then Diff4.Color("Positive and Up") else Diff4.Color("Positive and Down") else if Diff4 < Diff4[1] then Diff4.Color("Negative and Down") else Diff4.Color("Negative and Up"));

Diff5.SetPaintingStrategy(PaintingStrategy.LINE);
Diff5.SetLineWeight(1);
Diff5.DefineColor("Positive and Up", Color.GREEN);
Diff5.DefineColor("Positive and Down", Color.RED);
Diff5.DefineColor("Negative and Down", Color.RED);
Diff5.DefineColor("Negative and Up", Color.GREEN);
Diff5.AssignValueColor(if Diff5 >= 0 then if Diff5 > Diff5[1] then Diff5.Color("Positive and Up") else Diff5.Color("Positive and Down") else if Diff5 < Diff5[1] then Diff5.Color("Negative and Down") else Diff5.Color("Negative and Up"));

Diff6.SetPaintingStrategy(PaintingStrategy.LINE);
Diff6.SetLineWeight(1);
Diff6.DefineColor("Positive and Up", Color.GREEN);
Diff6.DefineColor("Positive and Down", Color.RED);
Diff6.DefineColor("Negative and Down", Color.RED);
Diff6.DefineColor("Negative and Up", Color.GREEN);

Diff6.AssignValueColor(if Diff6 >= 0 then if Diff6 > Diff6[1] then Diff6.Color("Positive and Up") else Diff6.Color("Positive and Down") else if Diff6 < Diff6[1] then Diff6.Color("Negative and Down") else Diff6.Color("Negative and Up"));
 

Attachments

  • Main@thinkorswim [build 1979] 2023-09-24 12-25-06.png
    Main@thinkorswim [build 1979] 2023-09-24 12-25-06.png
    65.8 KB · Views: 104

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