Colored EMA/SMA at Tradingview.com

3AMBH

Member
2019 Donor
Hi Traders, I am looking for a TOS indicator like the one found at Tradingview.com called Colored EMA/SMA. I have searched without success.

Code:
//Created by Robert Nance on 072315
study(title="Moving Average Colored EMA/SMA", shorttitle="Colored EMA /SMA", overlay=true)
emaplot = input (true, title="Show EMA on chart")
len = input(8, minval=1, title="ema Length")
src = close
out = ema(src, len)
up = out > out[1]
down = out < out[1]
mycolor = up ? green : down ? red : blue
plot(out and emaplot ? out :na, title="EMA", color=mycolor, linewidth=3)


smaplot = input (false, title="Show SMA on chart")
len2 = input(8, minval=1, title="sma Length")
src2 = close
out2 = sma(src2, len2)
up2 = out2 > out2[1]
down2 = out2 < out2[1]
mycolor2 = up2 ? green : down2 ? red : blue
plot(out2 and smaplot ? out2 :na , title="SMA", color=mycolor2, linewidth=1)

https://www.tradingview.com/script/pJ2ndFEf-Moving-Average-Colored-EMA-SMA/ Thank you in advance
 

BenTen

Administrative
Staff member
Staff
VIP
I don't have my ToS open, so here is the code for that, shared by @AlgoTrader77

Code:
plot myindicator=ExpAverage(close,20);
myindicator.SetDefaultColor(Color.White);
myindicator.AssignValueColor(if close>=myindicator then Color.Green else Color.Red);
myindicator.SetStyle(curve.LONG_DASH);
myindicator.SetPaintingStrategy(paintingstrategy.LINE_VS_SQUARES);
myindicator.SetLineWeight(5);
myindicator.setHiding(if close>open(period="DAY") then 0 else 1);
 

3AMBH

Member
2019 Donor
Here is what it looks like after placed on a TOS Chart....The Red & Green dashed line and I have it set to default of 20. If you want to change the default setting go to the code and change the 20 to maybe 15.

Code:
plot myindicator=ExpAverage(close,15);
myindicator.SetDefaultColor(Color.White);
myindicator.AssignValueColor(if close>=myindicator then Color.Green else Color.Red);
myindicator.SetStyle(curve.LONG_DASH);
myindicator.SetPaintingStrategy(paintingstrategy.LINE_VS_SQUARES);
myindicator.SetLineWeight(5);
myindicator.setHiding(if close>open(period="DAY") then 0 else 1);

rBkGcQA.png
 
Last edited:

fat_tony

New member
if EMA > EMA[1] and MF_EMA > MF_EMA[1] and MACDHistogram() < MACDHistogram()[1] {
EMA.SetDefaultColor(GetColor(1));
}else{

}
Basically, I want to color the line green if these condition are met, if they are not met, don't do anything.


I this error
Can not be called within branching: setdefaultcolor at 19:5
 

Slippage

Active member
Can not be called within branching: setdefaultcolor at 19:5

Use EMA.AssignValueColor() instead of EMA.SetDefaultColor()
And for the case where you don't want to change the color you can use EMA.AssignValueColor(Color.CURRENT)

Actually, unless you have more code that will go in the if and else blocks a better way to write it is...

Ruby:
EMA.AssignValueColor(
  if EMA > EMA[1]
    and MF_EMA > MF_EMA[1]
    and MACDHistogram() < MACDHistogram()[1]
  then Color.GREEN 
  else Color.CURRENT
);
 
Last edited:

Similar threads

Top