Finance Homework
goodstudent1Plotting
This is the plotting file da <- read.csv('bac.csv', stringsAsFactors = FALSE) head(da)
tail(da)
str(da) da[1, 1] da[ ,1] da$Date
# Turn the Date column into a date object
da$Date <- as.Date(da$Date, format = "%d/%m/%Y") str(da)
# Plot the data
plot(da$Date, da$BAC, type = 'l', main = "BAC Chart", xlab = "Date", ylab = "Price")
#================
head(da) # function to rebase
rebase <- function(x) { x/x[length(x)]*100 }
BACrb <- rebase(da$BAC)
SPYrb <- rebase(da$SPY)
plot(da$Date, BACrb, type = 'l')
lines(da$Date, SPYrb, type = 'l', col = 'red', lwd = 3)
#===========================
par(mfrow = c(2, 1))
plot(da$Date, da$BAC, type = 'l')
Plots and legends : (Add plots and legends)
http://www.sthda.com/english/wiki/add-legends-to-plots-in-r-software-the-easiest-way
Final file: ( Inputting csv files. Quantmod and automatically uploading data to R.)
# This is my second R file
# import the F.csv file to R
# Save the data as an object called da
# Remove the default that all strings are factors
da <- read.csv('F.csv', stringsAsFactors = FALSE)
# Look at the top of the file head(da)
# look at the bottom of the file tail(da)
# look at the structue of the data str(da)
# change the date as a character to a real date
# the format of the date can be looked up on google
# %d is day with two digits
# %m is month with two digits
# %Y is year with three digits
da$Date <- as.Date(da$Date, format = "%d/%m/%Y")
# Plot the data - use line = 'l' plot(da$Date, da$Close, type = 'l', xlab = "Date", ylab = 'Closing price', main = "My Ford Chart")
# Second chart
da <- read.csv('AAPL.csv', stringsAsFactors = FALSE)
head(da)
tail(da)
str(da)
da$Date <- as.Date(da$Date, format = "%d/%m/%Y")
plot(da$Date, da$Close, type = 'l', xlab = "Date", ylab = 'Closing price', main = "My Apple Chart")
#============================
# install quantmod package install.packages('quantmod')
# make quantmod package available require(quantmod)
# download bac data from yahoo getSymbols('BAC')
# look at the data
head(BAC)
tail(BAC)
plot(BAC$BAC.Close, col = "black")
# bar chart barChart(F)
# get lots of companies and download the data mycompanies <- c("BAC", "AAPL", "F", "GOOG", "TSLA") getSymbols(mycompanies)
http://karolis.koncevicius.lt/posts/r_base_plotting_without_wrappers/
Video 1: download data and plot
https://learn-eu-central-1-prod-fleet01-xythos.s3-eu-central-1.amazonaws.com/5d108c67a3df7/4130777?response-content-disposition=inline%3B%20filename%2A%3DUTF-8%27%27FN285Video1.mp4&response-content-type=video%2Fmp4&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20200515T185629Z&X-Amz-SignedHeaders=host&X-Amz-Expires=21600&X-Amz-Credential=AKIAZH6WM4PLYI3L4QWN%2F20200515%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Signature=7b121ab8bf17b3d0acbd9fec2604fbe0aa9a1d36bd5018caf73ef5ee2e34420c