Moving average color change when sloping up and down?

Miket

Member
Come someone make this moving average turn green when its sloping up and red when its sloping down please?

Code:
input price = close;
input length = 9;
input displace = 0;
input showBreakoutSignals = no;

plot SMA = Average(price[-displace], length);
plot UpSignal = price crosses above SMA;
plot DownSignal = price crosses below SMA;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

SMA.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
Last edited by a moderator:
Code:
input displace = 0;
input length = 8;
input price = low;
input movingAverageType = {default Simple, Exponential, Weighted, Hull, Variable};

rec data;

switch (movingAverageType) {
case Simple:
    data = compoundValue(1, Average(price[-displace], length), price);
case Exponential:
    data = compoundValue(1, ExpAverage(price[-displace], length), price);
case Weighted:
    data = compoundValue(1, wma(price[-displace], length), price);
Case Hull:
    data = compoundValue(1, hullMovingAvg(price[-displace], length), price);
case variable:
    data = VariableMA(price=price, length=length);
}

plot ave = data;

ave.SetLineWeight(2);
ave.AssignValueColor(if ave > ave[1] then color.YELLOW else color.dark_red);
ave.HideBubble();
def lastbar = IsNaN(close[-1]) and !IsNaN(close);
#AddChartBubble(lastbar, data, "MA" + Length, Color.DARK_ORANGE);
 

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

@Ramesh16 Can someone make this Linear Reg Curve turn green when its sloping up and red when its sloping down please?

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

input displace = 0;
input length = 9;
input price = close;

plot LinReg = Inertia(price[-displace], length);
LinReg.setDefaultColor(GetColor(1));
 
Last edited:
@Miket
Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

input displace = 0;
input length = 9;
input price = close;

plot LinReg = Inertia(price[-displace], length);
LinReg.setDefaultColor(GetColor(1));
LinReg.AssignValueColor(if LinReg > LinReg[1] then Color.GREEN else if LinReg < LinReg[1] then Color.RED else Color.GRAY);

Use same logic for the slope of the SMA. If it changes too often then you may want to compare the current bar to an average of a few previous bars not just the previous one. Keep it simple.
 
Last edited:
Hi. Just joined. This is my first post.

Trying to get a 3 colored simple moving average, one color for upslope, another color for downslope and another color for flat slope.

I did some copying and pasting because I don't know how to write thinkscript.

The part where I got a syntax error was at the following:

Code:
SMA.AssignValueColor(if SMA > SMA[1] then SMA.Color("Up") else if SMA < SMA[1] then SMA.Color("Down") else if SMA == SMA[1] then SMA.Color("Flat"));

The error read: "Syntax error: An 'else' block expected at..." with a red highlight box on the "if" in the third condition

Is it even possible to have a 3 color moving average line on TOS?

Can someone please help me out?
 
Whenever you have an if-then you must have an else:
Code:
#Here is yours:
SMA.AssignValueColor(if SMA > SMA[1] then SMA.Color("Up") else
                     if SMA < SMA[1] then SMA.Color("Down") else
                     if SMA == SMA[1] then SMA.Color("Flat"));

#Here is mine:
SMA.AssignValueColor(if SMA > SMA[1] then SMA.Color("Up")    else
                     if SMA < SMA[1] then SMA.Color("Down")  else
                     if SMA == SMA[1] then SMA.Color("Flat") else color.light_gray);
#Do you see the difference?
 
@Ldmart Take a look at what @tradegeek posted. Pay attention to the following:

Code:
LinReg.AssignValueColor(if LinReg > LinReg[1] then Color.GREEN else if LinReg < LinReg[1] then Color.RED else Color.GRAY);

Now, since you want to use the SMA indicator, we are going to use the SMA code provided by ToS.

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2017-2020
#

input price = close;
input length = 9;
input displace = 0;
input showBreakoutSignals = no;

plot SMA = Average(price[-displace], length);

Add the following to your SMA code above will change the color of the SMA line.

Code:
SMA.AssignValueColor(if close > SMA then color.green else color.red);

  • close above 9 SMA = green
  • close below = SMA = red
 
Last edited by a moderator:
Code:
##############################################
#
#     Calculate and Plot Moving Average With Colors
#     by Martin Begley
#
##############################################

input length = 30;
input price = close;
input AverageType = {Default Simple, Exponential, Weighted, Wilders, Hull};

def average;
switch (AverageType) {
case Simple:
    average = Average(price, length);
case Exponential:
    average = ExpAverage(price, length);
case Weighted:
    average = wma(price, length);
case Wilders:
    average = WildersAverage(price, length);
case Hull:
    average = HullMovingAvg(price, length);
}

plot MA = average;
MA.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
MA.SetLineWeight(2);

MA.DefineColor("Up", Color.DARK_GREEN);
MA.DefineColor("Down", Color.RED);
MA.DefineColor("Flat", Color.GRAY);

MA.AssignValueColor(if MA[0] > MA[1] then MA.Color("Up") else if MA[0] <
MA[1] then MA.Color("Down") else MA.Color("Flat"));

Can someone add code to add audio alert on change line color if possible?

Many thanks
 
Is there a way to use this framework to plot (EMA) and color code it for the actual slope? Ex. green when 9 ema slope is 45 degrees or higher and red when slope of 9 ema is -45 degrees or higher. Thanks in advance for any help.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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