Hi,
Will it possible to do an indicator that calculates the difference in price between two moving averages, example a 20 and 50? To be displayed on a chart.
Regards,
Segie
Hi,
Something like that, with a pixel position from top right corner (hence the negative Xpixel and Ypixel values, customisable as long as they remain negative to indicate going leftward and downward from top right corner).With another use of drawtext, it would also be possible to just keep display on a given barindex rather than a pixel position if preferred.
defparam drawonlastbaronly=true
// Choose pixel fixed position from top right corner with negative X and Y values
Xpixel= -100
Ypixel= -50
ma20= Average[20](close)
ma50= Average[50](close)
diff= ma20-ma50
DRAWTEXT("MA20-MA50= #diff#", Xpixel, Ypixel) anchor(topright, Xshift, Yshift)
return
Works well. Thank you very much
Can you also help to display the difference as a percentage below the calculation?
e.g. MA20=2 and MA50= 8
The difference = -6
(-6/2) X 100 = 300%
Regards,
Segie
JSParticipant
Senior
Hi,
The current calculation is the absolute difference between the two averages:
AbsDiff = (MA20 – MA50)
If you want to calculate the relative difference in percentages:
RelDiff = (MA20 – MA50) / MA50 * 100
Or alternatively written as:
RelDiff = (MA20 / MA50 – 1) * 100
defparam drawonlastbaronly=true
// Choose pixel fixed position from top right corner with negative X and Y values
Xpixel= -100
Ypixel= -50
ma20= Average[20](close)
ma50= Average[50](close)
diff= ma20-ma50
DiffPct=(ma20-ma50)/ma50*100
DRAWTEXT("Abs. Difference=#diff#", Xpixel, Ypixel) anchor(topright, Xshift, Yshift)
DRAWTEXT("Relat. Difference=#diffpct#%", Xpixel, Ypixel-20) anchor(topright, Xshift, Yshift)
return ma20 as "Fast" coloured("red"), ma50 as "Slow" coloured("green")