Kevin F
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: September 12th, 2020
Contact: TradingView Profile

Color Change above and below a value

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.

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: Color Change above and below a value

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
Last edited by Deep-Wave on Sun Sep 13, 2020 6:42 pm, edited 1 time in total.

wrsmallcap
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 13th, 2020
Contact: TradingView Profile

Re: Color Change above and below a value

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)

Kevin F
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: September 12th, 2020
Contact: TradingView Profile

Re: Color Change above and below a value

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)

Return to “Pine Script Q&A”