Get Economic Data Using the FRED API
7K views
Dec 11, 2024
Learn how to use the Python FRED API to get economic data from the Federal Reserve. š Subscribe for more: https://bit.ly/3lLybeP š Follow along: https://analyzingalpha.com/fred-api-python/ 00:00 Intro 01:30 Install FRED API 01:58 Get Imports 03:11 Get FRED API Key 03:25 Connect to FRED 04:11 Use get_series method 15:05 Create Get Bars Method 20:24 Get Economic Data #fredapi #economics #algotrading #python Videos are intended strictly for information, education, and entertainment purposes. Do not base any investment decisions based upon these materials. Please read the disclaimer: https://analyzingalpha.com/disclaimer
View Video Transcript
0:00
hello world today we're going to talk to
0:02
fred
0:03
actually i don't mean that fred and i
0:05
might be dating myself we're going to
0:07
talk to the federal reserve more
0:09
specifically we're going to make api
0:11
calls to the federal reserve economic
0:14
database short for fred now this is
0:17
probably could have been a youtube short
0:18
it's going to be a short video but i'm
0:20
going to use some of this data because
0:21
there's tons of great data from the
0:23
federal reserve and it's high quality in
0:26
some of our economic analysis in the
0:27
next video
0:29
where we try to figure out what moves
0:30
the market so i hope you're excited
0:32
let's create some code
0:35
let's check out the fred website
0:37
you can get to it by going to
0:38
fred.stlouisfed.org
0:41
and simply you can just search for
0:43
really any data set that you want um and
0:46
see if it's there
0:48
look it says
0:50
816 000 u.s and international time
0:52
series datas from 107 107 sources so
0:56
obviously there's tons of data so we'll
0:58
check out yeah the 10-year treasury
1:00
right so we'll do 10-year
1:02
you can see 10-year treasury minus
1:05
2-year treasury
1:06
market yield et cetera there's just all
1:08
sorts of stuff i'm obviously not going
1:10
to go through that
1:11
gdp
1:13
let's see um
1:16
m3
1:18
look at different monetary
1:21
data sets so
1:23
so there's all sorts of cool stuff
1:25
so with that being said let's go ahead
1:27
and access that data so the first thing
1:29
we'll do
1:30
is we'll put a heading here install
1:34
red api so if you don't have it
1:37
installed already make sure you've got
1:39
your virtual environment activated and
1:41
then simply type bang tip install red
1:45
api i'm not going to hit enter here
1:47
because i already have it installed and
1:49
i don't want to you know muddy up the
1:51
screen too much and make it as clear as
1:53
possible but if you don't have it
1:54
installed that's all you need to do
1:57
the next thing we'll do is we'll import
1:58
the various modules and packages we need
2:01
to import
2:03
packages and modules
2:06
and the difference between modules and
2:07
packages modules just really one file
2:09
packages is a set of files
2:12
now
2:13
we can import what we need we'll do
2:14
import
2:17
spread api as fa
2:20
import pandas as pd
2:24
okay so now
2:25
you've got that now the next thing that
2:28
you'll need to do is you know connect to
2:30
fred now in order to do that you need
2:33
your own api key in fact i'm going to go
2:36
back here and grab my api key so from
2:38
local settings import read as settings
2:42
now
2:44
basically all you need is a api key if
2:46
you're not familiar with what i'm doing
2:48
here i just have a dictionary file
2:50
that has you know that it's
2:52
a python file that is i'll give an
2:55
example of it but
2:56
it's simply fred equals and then api key
3:00
is the value and then coming the key and
3:03
then the value is the api key
3:06
again like i said once i upload this to
3:08
github i'll provide an example too
3:11
all right so assuming you already have
3:12
your fred api key which all you need to
3:14
do is sign up with an account with that
3:16
at that previous website super easy to
3:18
do
3:19
fred equals fa because we're referencing
3:22
now the the module here and then fred
3:26
settings
3:27
api key where you would just put in your
3:29
direct api key or you could have another
3:31
file
3:32
whichever you'd like to do
3:34
enter
3:36
now we've got
3:37
an object
3:39
that connects to the thread right
3:41
we get a fret object all right now what
3:44
do we want to query let's check out um
3:47
i'm going to check out the gdp
3:49
we can go back over here
3:52
search for gdp
3:55
first domestic product and you can see
3:57
here
3:58
here's the table name right so we can
4:00
see the gdp is going up over time which
4:02
makes sense
4:04
like you've got a covet dip there
4:06
everything was shut down but in order to
4:08
get this time series it's super easy to
4:10
do so we'll do gdp equals
4:16
and we'll just print the gdp out to
4:18
screen
4:22
do tail that might be easier so you can
4:24
see i was at 1946
4:27
so it goes back a ways and it looks like
4:29
that is
4:31
quarterly so you could probably find um
4:34
other data sets that are more frequent
4:36
but just understand here's the frequency
4:39
but that's your quarterly data set let's
4:41
try another one
4:43
we'll try uh currency in circulation in
4:46
fact we'll do the weekly
4:50
currency it's
4:52
oops
4:53
wrong here let's try it again
4:58
okay so there might be an issue with
5:00
fred at the moment let's see if we can
5:02
get it from the api good thing is i
5:05
already know um
5:07
the database name i was just going to
5:08
show you just remember basically after
5:11
the
5:11
name of every
5:13
you know data set it shows you the
5:16
um essentially the database name for for
5:19
lookup purposes so i'll do weekly
5:21
currency and circle circulation
5:24
do fred dot get series
5:28
w c u r c i r
5:30
and see if that worked
5:35
perfect it looks like it did we've got
5:36
it all the way back to 1984.
5:40
okay so if you want to merge this data
5:42
set which i'll do now
5:44
you can do df equals pd.merge we'll just
5:47
say pdp
5:49
and weekly currency and circulation will
5:52
emerge on the index
5:54
true
5:55
right index
5:58
through
5:59
and then print that out
6:01
do what i do here
6:04
oh that's right i have to name them
6:06
name equals
6:11
and we'll also do
6:13
ddp.name equals gdp
6:18
now we can merge them
6:21
and you can see now that's merged and
6:23
you'll notice
6:24
uh you know the weekly is now
6:27
quarterly because we did we performed an
6:29
inner merge
6:31
see i told you that was going to be a
6:32
quick one that might be my shortest
6:34
video today regardless now you know how
6:36
to query the federal reserve api to get
6:38
all sorts of awesome economic data and
6:41
we as you know we're going to use that
6:42
economic data to potentially figure out
6:44
what moves the market in the next video
6:46
which is right here so if you like this
6:48
video i'd really appreciate if you hit
6:50
the thumbs up or even subscribe let's
6:52
the google algorithm know that this is a
6:54
video we're sharing with the wider
6:55
audience so
6:56
hope you like the video and i hope to
6:58
see you in the next one bye
#Currencies & Foreign Exchange
#Economy News
#Finance