Hi there,
I would like to code a moving average(10 SMA) but change the colour when it points up or down. is there a way to do it in the code so I do not have to do it manually?I did search for it but didn’t find anything.
thanks in advance, 🙂
A colour change is when the current candle is:
- ascending and the previous one was descending;
- descending and the previous one was ascending.
Here is the code:
MySma = average[20,0](close)
Rising = MySma > MySma[1] AND MySma[1] < MySma[2]
Falling = MySma < MySma[1] AND MySma[1] > MySma[2]
Okay, just struggling to make it work in the code. What do I have to return in the code to make the average appear on chart and still have two separate colours? I know it might be a dumb question, just not too great at coding. Usually to make the average a standard colour we return the average and also the colour, but more than that I’m struggling with.
Just a way to do it (many other options to color based on conditions)
MySma = average[20,0](close)
Rising = MySma > MySma[1] AND MySma[1] < MySma[2]
Falling = MySma < MySma[1] AND MySma[1] > MySma[2]
if rising then
r = 0
g = 255
elsif falling then
r = 255
g = 0
endif
return mysma coloured(r,g,0)
Thank you.very simple and I just didn’t think of that. There isn’t maybe a way to do colourzones as well in a code?
You can use the indicator’s properties (settings) to add colour zones.
This is the code that mimicks the properties, but is not as good:
MySma = average[20,0](close)
Rising = MySma > MySma[1] AND MySma[1] < MySma[2]
Falling = MySma < MySma[1] AND MySma[1] > MySma[2]
if rising then
r = 0
g = 255
elsif falling then
r = 255
g = 0
endif
IF close > MySma THEN
OO = close
HH = close
LL = MySma
CC = MySma
DrawCandle(OO,HH,LL,CC) coloured(0,128,0,16)
ELSIF close < MySma THEN
OO = MySma
HH = MySma
LL = close
CC = close
DrawCandle(OO,HH,LL,CC) coloured(255,0,0,16)
ENDIF
return mysma coloured(r,g,0)