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:

Ramesh16

Member
VIP
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);
 

Miket

Member
@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:

tradegeek

Active member
2019 Donor
@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:

kalukoy

New member
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?
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
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?
 

BenTen

Administrative
Staff member
Staff
VIP
@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
 

hockeycoachdoug

Member
2019 Donor
VIP
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

Top