Indicator distance between MA 200 and Price

Hello, I try to program an indicator that tells me the distance visually, between MA 200 and the price closing. I created this but I am not sure that it marks well. Can you check it out?
Thank you

ma200 = MA(Close, 200);

cero = ParamColor("Color", ColorRGB(0,0,0));
distancia = IIf(Close>=ma200, (Close-ma200)/Close * 100,
IIf(Close<ma200, (ma200-Close)/Close * 100, 0));

Plot(distancia,"Distance MA 200",colorBlack,styleLine);

you could just use abs() function, like below:

ma200 = MA(Close, 200);

cero = ParamColor("Color", ColorRGB(0,0,0));

distancia = abs(Close-ma200)/Close * 100;

Plot(distancia,"Distance MA 200",colorBlack,styleLine);
1 Like

@lol I guess it depends on what is visually most pleasing to you. I like @rottor idea but here is an alternative that will give you a different way of looking at your indicator.

ma200 = MA(Close, 200);

distancia = (Close-ma200)/Close * 100;

cero = IIf(Close >= ma200, colorGreen, colorRed);

Plot( distancia, "distancia", cero, styleHistogram | styleThick, Null, Null, 0, 1, 5 );

image

1 Like

Rotor can you explain me why it´s better use "abs"?
Thank you

Thank you both for your solutions. I will study them to see differences.
Thank you.
Luis

the code is shorter and more readable, and it produces the same output as yours using the function created just for this purpose (getting absolute value)

it may even work faster - although I haven't checked it myself