Simple exercise on how to fetch stock data from Yahoo! Finance using Python, R or Julia.
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.
import datetime
import yfinance as yf
end = datetime.datetime.today() # Get today's date dynamically
start = end - datetime.timedelta(days=10) # Subtract 10 days from today
Nvidia = yf.download('NVDA', start=start, end=end)
Nvidia
[*********************100%%**********************] 1 of 1 completed
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2024-02-20 | 719.469971 | 719.559998 | 677.340027 | 694.520020 | 694.520020 | 70483300 |
2024-02-21 | 680.059998 | 688.880005 | 662.479980 | 674.719971 | 674.719971 | 69029800 |
2024-02-22 | 750.250000 | 785.750000 | 742.200012 | 785.380005 | 785.380005 | 86510000 |
2024-02-23 | 807.900024 | 823.940002 | 775.700012 | 788.169983 | 788.169983 | 82711200 |
2024-02-26 | 797.000000 | 806.460022 | 785.049988 | 790.919983 | 790.919983 | 50397300 |
2024-02-27 | 793.809998 | 794.799988 | 771.619995 | 787.010010 | 787.010010 | 39170500 |
2024-02-28 | 776.200012 | 789.330017 | 771.250000 | 776.630005 | 776.630005 | 39194600 |
type(Nvidia)
pandas.core.frame.DataFrame
Nvidia.info()
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 7 entries, 2024-02-20 to 2024-02-28 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Open 7 non-null float64 1 High 7 non-null float64 2 Low 7 non-null float64 3 Close 7 non-null float64 4 Adj Close 7 non-null float64 5 Volume 7 non-null int64 dtypes: float64(5), int64(1) memory usage: 392.0 bytes
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.
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
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/
import DataFrames, CSV, HTTP, YFinance, Dates
using YFinance, DataFrames
get_prices("PANW",range="1d",interval="90m") |> DataFrame
Row | ticker | timestamp | open | high | low | close | vol |
---|---|---|---|---|---|---|---|
String | DateTime | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | PANW | 2024-02-29T14:30:00 | 317.0 | 320.69 | 313.57 | 316.3 | 1.17352e6 |
2 | PANW | 2024-02-29T14:55:05 | 316.53 | 316.53 | 316.53 | 316.53 | 0.0 |
Instead of range can use the start and end dates : startdt and an enddt but we need the date in string format
using Dates
today = Dates.trunc(Dates.today(), Dates.Day)
last10days = today - Dates.Day(10)
println(today)
println(last10days)
2024-02-29 2024-02-19
typeof(today)
Date
The type of our variable 'today' is Date and we need to convert it to String
hoy = Dates.format(today, "yyyy-mm-dd")
hace10dias = Dates.format(last10days, "yyyy-mm-dd")
println(hoy)
typeof(hoy)
2024-02-29
String
get_prices("PANW", startdt=hace10dias, enddt=hoy) |> DataFrame
Row | ticker | timestamp | open | high | low | close | adjclose | vol |
---|---|---|---|---|---|---|---|---|
String | DateTime | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | PANW | 2024-02-20T14:30:00 | 362.85 | 369.29 | 359.82 | 366.09 | 366.09 | 1.04588e7 |
2 | PANW | 2024-02-21T14:30:00 | 275.2 | 275.98 | 260.09 | 261.97 | 261.97 | 4.39373e7 |
3 | PANW | 2024-02-22T14:30:00 | 274.13 | 276.81 | 265.0 | 267.82 | 267.82 | 1.92973e7 |
4 | PANW | 2024-02-23T14:30:00 | 276.95 | 284.32 | 271.62 | 282.09 | 282.09 | 1.48783e7 |
5 | PANW | 2024-02-26T14:30:00 | 288.52 | 313.88 | 286.79 | 302.78 | 302.78 | 2.19372e7 |
6 | PANW | 2024-02-27T14:30:00 | 317.4 | 327.57 | 307.55 | 314.75 | 314.75 | 2.13543e7 |
7 | PANW | 2024-02-28T14:30:00 | 311.28 | 316.85 | 307.81 | 316.15 | 316.15 | 7.9707e6 |