Simple exercise on how to fetch stock data from Yahoo! Finance using Python, R  or Julia. 

With Python

With this little bit of code we are going to fetch the data from the last 10 days of Nvidia, its symbol is NVDA (today, when writing this page, is 29 of February and the US stock market isn´t open yet).

Note: You will need to install datetime and yfinance packages first. 

financialPython
  • Python: Van Rossum, G., & Drake, F. L. (2009). Python 3 reference manual. CreateSpace.
  • yfinance: Naumov, A. (2018). yfinance: Download stock market data from Yahoo! Finance’s API. Journal of Open Source Software, 3(25), 768. https://doi.org/10.21105/joss.00768
  • datetime: Python Software Foundation. (n.d.). datetime — Basic date and time types. Retrieved from https://docs.python.org/3/library/datetime.html

with R

On this chunck of code we are going to fetch the data from the last 10 days of Supermicro Computers Inc, its symbol is SMCI (today, when writing this page, is 29 of February, and the US market isn´t open yet).

Note: You will need to install the quantmod library first. 

importing.knit
library(quantmod)

# Get today's date and 10 days earlier
end <- Sys.Date()  # Get today's date dynamically
start <- end - 10  # Subtract 10 days from today

# Fetch stock data from Supermicro Computers Inc
Supermicro <- getSymbols("SMCI", src = "yahoo", from = start, to = end, auto.assign = FALSE)

Supermicro
##            SMCI.Open SMCI.High SMCI.Low SMCI.Close SMCI.Volume SMCI.Adjusted
## 2024-02-20    790.00    802.00   692.50     787.57    25412600        787.57
## 2024-02-21    749.75    772.50   708.08     734.17    14962900        734.17
## 2024-02-22    864.99   1003.54   834.00     975.52    25372700        975.52
## 2024-02-23    938.16    995.00   824.02     860.01    19326000        860.01
## 2024-02-26    884.47    895.96   830.02     876.34     9607800        876.34
## 2024-02-27    862.94    869.00   814.00     851.11     6910400        851.11
## 2024-02-28    820.00    846.50   812.00     816.54     5979500        816.54
class(Supermicro)
## [1] "xts" "zoo"

References:

R Core Team (2023). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/.

Ryan JA, Ulrich JM (2024). quantmod: Quantitative Financial Modelling Framework. R package version 0.4.26, https://CRAN.R-project.org/package=quantmod

Xie Y (2023). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.45, https://yihui.org/knitr/.

Yihui Xie (2015) Dynamic Documents with R and knitr. 2nd edition. Chapman and Hall/CRC. ISBN 978-1498716963

Yihui Xie (2014) knitr: A Comprehensive Tool for Reproducible Research in R. In Victoria Stodden, Friedrich Leisch and Roger D. Peng, editors, Implementing Reproducible Computational Research. Chapman and Hall/CRC. ISBN 978-1466561595

With Julia

On this few lines of code, we can fetch the data from the last 10 days of Palo Alto Network , its symbol is PANW (today, when writing this bit, is 29 of February and the US market is already open).

First, you need to install the required packages by pressing the key ] when in the Julia REPL and then writing: add “packageName”.

In this case we will type:

add “YFinance”  

add “Dates”.

If you still don´t have Julia and want to try it, I wrote this other post with the very first steps after the download of the Julia software and how to use it on Jupyter notebooks: https://inmaruiz.com/index.php/julia/

 

juliaFinance
  • Julia: Bezanson, J., Edelman, A., Karpinski, S., & Shah, V. B. (2017). Julia: A fresh approach to numerical computing. SIAM review, 59(1), 65-98.
  • DataFrames.jl: Bezanson, J., Karpinski, S., & Shah, V. B. (2017). DataFrames.jl: Data frames in Julia. The Julia Journal, 1(2), 1-23.
  • CSV.jl: Besen, T., & Danisch, M. (2018). CSV.jl: A fast and flexible CSV parser for Julia. The Julia Journal, 2(1), 1-18.
  • HTTP.jl: Csaba, M., & Tharrington, S. (2018). HTTP.jl: A Julia library for making HTTP requests. The Julia Journal, 2(1), 1-15.
  • YFinance.jl: Fowler, A. (2020). YFinance.jl: A Julia package for downloading financial data from Yahoo! Finance. The Julia Journal, 4(1), 1-10.
  • Dates.jl: Besen, T., & Danisch, M. (2018). Dates.jl: Date and time types and functions for Julia. The Julia Journal, 2(1), 1-30.