Install packages
install.packages("quantmod", repos = "https://cloud.r-project.org/")
## Installing package into 'C:/Users/Seth Green/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'quantmod' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\Seth Green\AppData\Local\Temp\RtmpYpxebE\downloaded_packages
Load package
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.4.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.4.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.4.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
Get symbols
getSymbols(c("MSFT", "BAC", "HOOD", "TSLA"), src="yahoo")
## [1] "MSFT" "BAC" "HOOD" "TSLA"
Msft stock prices in given period
getSymbols("MSFT", src="yahoo",from = "2024-10-01", to = "2025-02-01")
## [1] "MSFT"
View of first few rows
head(MSFT)
## MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted
## 2024-10-01 428.45 428.48 418.81 420.69 19092900 417.5552
## 2024-10-02 422.58 422.82 416.71 417.13 16582300 414.0217
## 2024-10-03 417.63 419.55 414.29 416.54 13686400 413.4362
## 2024-10-04 418.24 419.75 414.97 416.06 19169700 412.9597
## 2024-10-07 416.00 417.11 409.00 409.54 20919800 406.4883
## 2024-10-08 410.90 415.66 408.17 414.71 19229300 411.6198
Show open prices
open_prices <- MSFT$MSFT.Open
Find date with highest open price
which.max(open_prices)
## [1] 56
open_prices[which.max(open_prices)]
## MSFT.Open
## 2024-12-18 451.32
Quick line plot of open prices
plot(open_prices, main = "Microsoft Open Prices", col = "blue")
