Created
February 16, 2021 15:02
-
-
Save mdbench/4dd2c3feaf5f76dad5a56b09ea163b01 to your computer and use it in GitHub Desktop.
An R-Script template for pulling stock data quickly and easily.
This file contains 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
# This R-Script is a quick template to allow anyone with R or an R UI program to pull stock data quickly and easily. | |
# Install and load the package quantmod into R | |
install.packages("quantmod") | |
library(quantmod) | |
#quantmod stands for: Quantitative Financial Modelling & Trading Framework for R. | |
#More information on quantmod can be found here: https://www.quantmod.com/. | |
#Create place-holder variables for the stock data start and end range. | |
start <- as.Date("2021-02-02") | |
end <- as.Date("2021-02-12") | |
#Pull the stock data. | |
getSymbols("TSLA", src = "yahoo", from = start, to = end) #Change the ticker from TESLA (TSLA) to your desired one (i.e. AAPL). | |
View(TSLA) #View the data so you can make sure it was imported properly. | |
#Plot the data in a time-based, chronological line graph. | |
plot(TSLA$TSLA.Close, main="TSLA", xlab="$", ylab="Days") #TSLA closing plot | |
plot(TSLA$TSLA.Open, main="TSLA", xlab="$", ylab="Days") #TSLA opening plot | |
#Let's manipulate the data a little bit. I try to determine volatility first and foremost. | |
TSLA.Difference <- TSLA$TSLA.High-TSLA$TSLA.Low # Create High-Low variable. | |
TSLA2 <- merge(TSLA, TSLA.Difference) #Merge the variable created into a new data frame. | |
View(TSLA2) #View the data so you can make sure it was imported properly. | |
plot(TSLA2$TSLA.High.1, main="TSLA", xlab="$", ylab="Days") #TSLA high-low difference plot | |
TSLA.Ratio <- (TSLA2$TSLA.High.1/TSLA2$TSLA.Close)*100 #Examine difference as a percentage of the stock's closing price. | |
View(TSLA.Ratio) #View the data so you can determine if there is a substantive difference proportionally. | |
#Want to see stock data in a way that is more typical among trading platforms? Use a Candlestick Chart. | |
candleChart(TSLA, up.col = "green", dn.col = "red", theme = "black") | |
#I hope this concise script helps you explore R and stock data. | |
#It is a great tool to run through common stock options trading statistics (i.e. "The Greeks"). | |
#Examples include: Delta, Gamma, Theta, Vega, and Rho. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment