Last active
          June 27, 2018 00:08 
        
      - 
      
 - 
        
Save BioSciEconomist/eed01fe8e66bf7f3e950384b80e67f5a to your computer and use it in GitHub Desktop.  
    Example Fibonacci Retracement Levels
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # *----------------------------------------------------------------- | |
| # | PROGRAM NAME: ex fibonacci.R | |
| # | DATE:6/26/18 | |
| # | CREATED BY: MATT BOGARD | |
| # | PROJECT FILE: /Users/amandabogard/Google Drive/ | |
| # *---------------------------------------------------------------- | |
| # | PURPOSE: code based on http://mindymallory.com/R-Companion-Price-Analysis/chapter-2-getting-started.html#working-with-apis | |
| # *---------------------------------------------------------------- | |
| library(RCurl) # necessary for reading data from url | |
| CZ2018 <- getURL("https://www.quandl.com/api/v1/datasets/CME/CZ2018.csv") # pull price data from quandl | |
| CZ2018 <- read.csv(text = CZ2018) # read file from pull above | |
| head(CZ2018) | |
| tail(CZ2018, n = 90) | |
| names(CZ2018) | |
| # plotting with quantmod | |
| library(quantmod) | |
| library(xts) | |
| # cleanup names | |
| CZ2018$Open.Interest <- CZ2018$Previous.Day.Open.Interest | |
| CZ2018$Previous.Day.Open.Interest <- NULL | |
| # modify data for plotting using quantmod functions | |
| CZ2018_chart <- subset(CZ2018, select= -c(Date, Change, Last, Open.Interest)) | |
| CZ2018_chart <- xts(CZ2018_chart, order.by = as.Date(CZ2018$Date)) | |
| colnames(CZ2018_chart)[4] <- "Close" | |
| ## Calculate Fibonacci Retracements over last 90 periods (https://gist.github.com/drewgriffith15/e34560476a022612aa60) | |
| # https://www.investopedia.com/ask/answers/05/fibonacciretracement.asp | |
| # validated against: http://www.patterntrapper.com/fibonacci-calculator.html | |
| hi <- last(Hi(CZ2018_chart),90) | |
| lo <- last(Lo(CZ2018_chart),90) | |
| FR100 <- max(hi) # 429.50 | |
| FR0 <- min(lo) # 360 | |
| last90 <- last(CZ2018_chart,90) | |
| last90$FR100 <- FR100 | |
| last90$FR0 <- FR0 | |
| last90$FR78 <- FR100 - (FR100 - FR0) * 0.786 # 78.6% | |
| last90$FR62 <- FR100 - (FR100 - FR0) * 0.618 # 61.8% | |
| last90$FR50 <- FR100 - (FR100 - FR0) * 0.500 # 50% | |
| last90$FR38 <- FR100 - (FR100 - FR0) * 0.382 # 38.2% | |
| last90$FR24 <- FR100 - (FR100 - FR0) * 0.236 # 23.6% | |
| ## Plot | |
| thm <- chart_theme() | |
| #thm$col$line.col <- 'black' | |
| chart_Series(last90, theme=thm,name="CZ2018") | |
| add_Series(last90[,6],on=1) # FR100 90 day high | |
| add_Series(last90[,7],on=1) # FR0 90 day low | |
| add_Series(last90[,8],on=1) # 78.6% | |
| add_Series(last90[,9],on=1) # 61.8% | |
| add_Series(last90[,10],on=1) # 50% | |
| add_Series(last90[,11],on=1) # 38.2% | |
| add_Series(last90[,12],on=1) # 23.6% | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment