Skip to content

Instantly share code, notes, and snippets.

@drewgriffith15
Created January 16, 2015 20:55
Show Gist options
  • Select an option

  • Save drewgriffith15/e34560476a022612aa60 to your computer and use it in GitHub Desktop.

Select an option

Save drewgriffith15/e34560476a022612aa60 to your computer and use it in GitHub Desktop.
Fibonacci Retracements in R
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)
#library(griffun)
load.packages('forecast,quantmod,lmtest,TTR')
tickers = spl('SPY')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T)
for(i in data$symbolnames) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
bt.prep(data, align='remove.na', fill.gaps = T)
## Calculate Fibonacci Retracements over last 90 periods
hi <- last(Hi(data$SPY),90)
lo <- last(Lo(data$SPY),90)
FR100 <- max(hi)
FR0 <- min(lo)
last90 <- last(data$SPY,90)
last90$FR100 <- FR100
last90$FR0 <- FR0
last90$FR79 <- FR100 - (FR100 - FR0) * 0.786;
last90$FR62 <- FR100 - (FR100 - FR0) * 0.618;
last90$FR50 <- FR100 - (FR100 - FR0) * 0.500;
last90$FR38 <- FR100 - (FR100 - FR0) * 0.382;
last90$FR24 <- FR100 - (FR100 - FR0) * 0.236;
# last90$FR124 <- FR100 + (FR100 - FR0) * 0.236;
## Plot
thm <- chart_theme()
#thm$col$line.col <- 'black'
chart_Series(last90, theme=thm,name="SPY (OHLC)")
add_Series(last90[,7],on=1)
add_Series(last90[,8],on=1)
add_Series(last90[,9],on=1)
add_Series(last90[,10],on=1)
add_Series(last90[,11],on=1)
add_Series(last90[,12],on=1)
add_Series(last90[,13],on=1)
# add_Series(last90[,14],on=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment