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:

rad14733

Well-known member
VIP
Try something like the conditional logic below...

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

rad14733

Well-known member
VIP
sorry, I got error messages?
I didn't try the actual code, I merely gave an example of how to achieve color changes... Let me try the code...

Ok... Aside from a typo of LinSig instead of LinReg, you may need to use InertiaAll() instead of Inertia() if you want to be able to change line color based off of bar comparison...
 
Last edited by a moderator:

tradegeek

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

Miket

Member
@tradegeek Thanks for trying. For some reason it works 50% correct. I was hoping that the other logic would give us the same 100% result. There are times in the slope where it is clearly sloping up but is red. Conversely, green when sloping down and sometimes both colors. I displace it 3 with a length of 6.
 

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
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 in message #9. 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
 

Similar threads

Top