Page 1 of 1

Color Change above and below a value

Posted: Sat Sep 12, 2020 6:45 pm
by Kevin F
This seems so simple, but I cannot figure it out.

I want a Chaikin Money flow the uses columns and has user-defined colors above and below the hline, or <0 or >0. Is there a tutorial on how to do this? I would like to do that on a moving average as well that would change color when price crosses the moving average.

Re: Color Change above and below a value

Posted: Sun Sep 13, 2020 2:47 pm
by Deep-Wave
Hi Kevin

you can implement a color change to your MA with the following code:

Code: Select all

//@version=4
study("Colored EMA", overlay=true)
ema=ema(close, 50) //variable ema assigned to the inbuilt ema function
plot(ema, color = close > ema ? color.green : color.red) //plot green ema when the close price is above the ema and red otherwise
Hope this helps.

Mark

Re: Color Change above and below a value

Posted: Sun Sep 13, 2020 4:45 pm
by wrsmallcap
Here is an example change the color of parabolicSAR

Code: Select all

 // Adding parabolicSAR

SAR = sar(0.02, 0.02, 0.2)
sarcolor = if SAR < close[0]
    sarcolor=color.purple
else
    sarcolor=color.red
plot(SAR, style=plot.style_circles, color=sarcolor)

Re: Color Change above and below a value

Posted: Sun Sep 13, 2020 6:38 pm
by Kevin F
Thank you so much. This is where I ender up.

Code: Select all

//@version=4
study(title="Chaikin Money Flow", shorttitle="CMF", format=format.price, precision=2, resolution="")
length = input(20, minval=1)
ad = close==high and close==low or high==low ? 0 : ((2*close-low-high)/(high-low))*volume
mf = sum(ad, length) / sum(volume, length)
hline=(0)

plot(mf, color = mf > hline ? color.blue : color.fuchsia, style=plot.style_columns, title="KF-MF")

hline(0, color=#606060, title="Zero", linestyle=hline.style_dashed)