Skip to content

Instantly share code, notes, and snippets.

@BioSciEconomist
Last active April 26, 2019 22:28
Show Gist options
  • Save BioSciEconomist/dabc994b59c787ba81c7001bf162b3b0 to your computer and use it in GitHub Desktop.
Save BioSciEconomist/dabc994b59c787ba81c7001bf162b3b0 to your computer and use it in GitHub Desktop.
Basic technical analysis (code excerpt)
# *-----------------------------------------------------------------
# | PROGRAM NAME: corn technical analysis
# | DATE: 4/21/18
# | CREATED BY: MATT BOGARD
# | PROJECT FILE:
# *----------------------------------------------------------------
# | PURPOSE:
# *----------------------------------------------------------------
library(TTR)
library(quantmod)
library(xts)
#----------------------------------
# December 2018
#---------------------------------
dec18 <- read.csv(file="/Users/amandabogard/Google Drive/AGECON/Commodities and Futures/dec18corn.txt")
names(dec18)
head(dec18, n=10)
# convert to xts object
CZ18_DAILY <- xts(dec18[c("Open","High","Low","Close")], order.by = as.Date(dec18$Date))
# plot series
chartSeries(CZ18_DAILY['2018-04-16::2018-04-25'], type = "bars", theme = chartTheme("white"))
# add technicals
# addRSI(n=14)
# addSMA(n = 20, on = 1, with.col = Cl, overlay = TRUE, col = "brown")
# addSMA(n = 50, on = 1, with.col = Cl, overlay = TRUE, col = "blue")
# addSMA(n = 90, on = 1, with.col = Cl, overlay = TRUE, col = "orange")
# addMACD(fast = 12, slow = 26, signal = 9, type = "EMA")
#----------------------------------
# December 2019
#---------------------------------
# read data from text file
dec19 <- read.csv(file="/Users/amandabogard/Google Drive/AGECON/Commodities and Futures/dec19corn.txt")
names(dec19) # check
head(dec19, n=10) # check
# convert to xts object
CZ19_DAILY <- xts(dec19[c("Open","High","Low","Close")], order.by = as.Date(dec19$Date))
# plot series
chartSeries(CZ19_DAILY, type = "bars", theme = chartTheme("white"))
# add technicals
addRSI(n=14)
addSMA(n = 20, on = 1, with.col = Cl, overlay = TRUE, col = "brown")
addSMA(n = 50, on = 1, with.col = Cl, overlay = TRUE, col = "blue")
# addSMA(n = 90, on = 1, with.col = Cl, overlay = TRUE, col = "orange")
addMACD(fast = 12, slow = 26, signal = 9, type = "EMA")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment