Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generate chart with 10y yield oscillator (10y yield minus 260d moving average), +/- 1 stdev dashed lines
recplot = function(var, maintitle="", ylab="", ylim=NULL)
if ( exists("USRECD") == FALSE ) { getSymbols("USRECD", src="FRED", env=.GlobalEnv) }
a = na.locf(cbind(USRECD, var))
a = a[ .index(a) %in% .index(var) ]
par(xaxt="n", yaxt="n")
barplot(a$USRECD, border="grey", col="grey")
par(new=T, xaxt="s", yaxt="s")
plot(a[,2], main=maintitle, ylab=ylab, ylim=ylim)
getSymbols("DGS10", src="FRED")
recplot(DGS10 - SMA(na.omit(DGS10), n=260))
dsd = sd(na.omit((DGS10 - SMA(na.omit(DGS10), n=260))))
abline(h=dsd, lty=2)
abline(h=-dsd, lty=2)
legend("topright", c("10y minus its 1y mean", "+/- 1 stdev"), col="black", lty=c(1, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment