Learn how to connect to the FTX exchange using the REST API in Python. This will get you up to speed on how to use a REST API; however, I do recommend CCXT for crypto algos: https://github.com/ccxt/ccxt
✌️ FTX US link: https://bit.ly/3ESQbNr
👍 Subscribe for more: https://bit.ly/3lLybeP
🔗 Follow along: https://analyzingalpha.com/ftx-rest-api-python
#crypto
#api
#algotrading
00:00 Intro
00:16 Import FTX API
01:25 Use the FTX Market API
02:20 Get FTX Markets
05:00 Get Individual FTX Market
06:50 Get Historical FTX Data
10:30 Get FTX Order Book
14:00 Get FTX Trades
Show More Show Less View Video Transcript
0:00
today you're going to learn how to use
0:01
the ftx api specifically the rest api in
0:05
python to grab market data and trade
0:08
data asset data and potentially even
0:11
trade data but first things first what
0:13
we'll do is we'll go ahead and get our
0:15
imports i'll go you know i'll type in
0:17
each one and cover why i'm importing it
0:20
as we go so import date time pretty
0:22
self-explanatory but we need to be able
0:24
to manipulate
0:25
date time data
0:27
import requests
0:29
we need to be able to make api requests
0:31
to the ht you know via https to the api
0:35
import pandas as pd so we can manipulate
0:38
the data as a data frame from client
0:40
import ft
0:42
x client now this is something that was
0:44
downloaded from github it isn't in pip
0:48
so you just have to be careful because
0:49
if someone says oh yeah download my
0:51
client from my repository they
0:53
theoretically could put something not so
0:55
nice or some malicious code in the
0:57
client and you never want that so
0:59
anytime you're downloading connectivity
1:01
stuff like this
1:02
just do a quick glance and make sure
1:05
that it's from a legitimate source and
1:06
there's nothing funky going on in there
1:09
and then from local settings import
1:12
ftx
1:14
us as settings this is just my api key
1:17
and api secret i've covered the local
1:19
settings file multiple times
1:21
but i'll put i'll put an example in
1:23
github
1:24
all right perfect so with the imports
1:27
out of the way the next thing we want to
1:28
do is use the market api so using
1:32
the ftx market
1:34
api
1:43
so there's a lot of information that we
1:44
can get
1:46
from
1:47
you know the market api i'm going to go
1:49
ahead and paste this
1:52
make it a little easier for you whenever
1:53
you're
1:55
you know browsing the api but basically
1:58
here's you know what we'll be greeted
1:59
with after making an api request
2:01
you can easily
2:03
see this stuff on the ftx us docs so go
2:06
to the rest api you can see markets
2:09
and we can see your response format you
2:12
know order book get a single market
2:14
um historical prices there's all sorts
2:16
of stuff and by the time that
2:19
you're done with this tutorial you'll be
2:20
able to read this too right it's pretty
2:23
easy to do so you'll you'll see what i
2:25
mean so let's go ahead and make our
2:27
first request so we'll do a get request
2:30
to the markets
2:31
api
2:32
create an api url
2:35
https
2:37
ftx us api
2:40
we'll do the api will be markets
2:44
and the url
2:45
equals
2:46
api plus
2:48
api
2:50
nope i've got that backwards api
2:54
url plus api
2:56
and then if i type that out we should
2:58
see
2:59
the proper url and that looks correct
3:03
okay so now what we'll do is we'll use
3:05
the requests to make a you know to get
3:07
the market response data okay so we'll
3:10
say markets
3:13
equal requests
3:15
dot get and our url that we just created
3:18
and we'll get that in json
3:20
and then what we'll do is we'll say data
3:22
equals markets result well i'll show you
3:25
how this might be helpful to see how
3:27
this data is returned first so you can
3:29
see that it's
3:30
returned as json
3:32
scroll down here
3:34
and that's essentially what you get
3:36
so success is true and then we have this
3:39
you know results dictionary so what
3:40
we'll do is we'll just say data equals
3:43
markets
3:44
result and then data and you can see now
3:49
we've got just the results
3:52
in a manner that we can use
3:54
for pandas right so let's scroll down
3:57
here
3:58
create a new column i'm sorry row we'll
4:01
say df equal pd
4:02
data frame
4:04
data
4:05
df equals
4:07
df.set index i'm just cleaning it well
4:11
again i'll just show you
4:13
the nitty gritty so you can see that
4:15
it's not in the best of format we want
4:18
to make it more readable so what we can
4:20
do is we'll just do df dot set index
4:24
we'll set the index to the name which is
4:25
just the ticker here and then just to
4:29
make it a little more legible right here
4:31
i'm just going to slice it just to print
4:33
it out
4:37
okay
4:42
all right and you can see let's see what
4:44
i do there
4:46
oh i messed that up
4:48
there we go
4:49
all right so you can see here um
4:51
what it looks like we just have a data
4:53
frame with some data here
4:56
pretty simple right we just made a
4:58
request
4:59
got json back convert it to a data frame
5:01
so let's do let's do that again well
5:03
this time we'll get
5:06
individual
5:08
market data
5:12
this time we'll create a get request to
5:14
markets and then it'll be the market
5:17
name so we'll do
5:22
uh ethereum why don't we do it here and
5:24
so market name equal
5:26
f usd
5:29
do path
5:30
equals we'll do the string oops
5:33
it says markets
5:34
and then market name
5:41
url equals api url plus path and then
5:44
url and you can see that you know we now
5:48
have the
5:49
url for the single market we can check
5:52
this because we can go to
5:55
markets
5:57
single market and you can see right here
6:01
you know get markets market name that's
6:03
why you know essentially that comment
6:05
there is exactly what we need so
6:08
we've also got
6:10
you know the response which we saw
6:12
previously
6:13
and you can even do order book depth and
6:15
stuff like that so basically
6:17
pretty simple that's what we need so
6:20
we've got the url formatted correctly
6:23
now we just go ahead and
6:26
make that same request we'll do response
6:28
equal request dot get url json
6:32
df equals pd data frame
6:35
result uh response sorry and then we'll
6:37
do result right because remember
6:39
that dictionary had multiple results
6:41
we'll do df here
6:43
right so now we have
6:46
that data frame with the
6:48
response for that single market so you
6:50
can see the ask
6:52
the base currency bid all of that type
6:54
of stuff
6:55
so pretty simple so you might say you
6:57
know what leo i actually want to get the
6:59
historical data well we can do that too
7:01
get
7:02
historical data
7:05
okay so let's see what we need here
7:09
get historical prices we can see this is
7:12
the
7:14
essentially the requests we need to make
7:16
so let's go ahead and
7:19
get
7:22
okay
7:26
all right and then perfect so now what
7:28
we need to do is just construct that so
7:30
we need
7:31
a resolution
7:33
equals
7:35
60 seconds times 60
7:38
minutes times 24 so that will give us
7:41
you know how many minutes because the
7:45
historical date is in minutes
7:47
how many minutes in a day
7:49
and that's
7:51
86 400. so we've got the resolution down
7:55
now we need to do the start and
7:59
we'll go ahead and do start equals date
8:01
time date time 2022 1-1
8:06
time stamp
8:07
actually let's make sure because i think
8:09
we can only get a month so it's february
8:12
so we'll just do 2-1 yep
8:15
okay
8:17
and then we'll get start
8:20
okay so that's our start date
8:23
okay now let's go ahead and construct
8:25
that string so
8:26
path equals
8:28
we'll do an f string
8:31
markets
8:32
market name which we have above right
8:36
candles
8:37
equal candles resolution
8:40
equals
8:41
resolution
8:44
and
8:45
start
8:46
equals start
8:48
okay
8:49
then we'll do url equals api url plus
8:52
path and url let's see what we got there
8:56
so okay so we've got our api then we've
8:59
got our market
9:01
then we have candles resolution
9:04
and the start okay so hopefully we got
9:06
that correct and then what we can do is
9:08
the same exact request we did before so
9:10
respond sql request
9:12
get url and grab json or convert it to
9:16
json router
9:17
df equals
9:19
pd data frame
9:22
result
9:24
i'm sorry response result
9:26
df
9:27
date
9:29
uh well let's see here
9:31
we'll just see what this looks like okay
9:33
so we can see we've got this data frame
9:35
now we can make it a little prettier
9:39
df date
9:41
equals pd
9:43
to date time
9:45
df and then that's that start time
9:48
that'll convert that to a date
9:51
and then what else do we need we'll set
9:53
the index to date so df equals df set
9:56
index
9:57
date
9:59
and we can drop that start time and time
10:03
so df equals drop sorry df drop
10:08
columns equal
10:10
start time
10:12
and time
10:14
let's see if we got that correct
10:18
okay so that looks correct
10:21
uh let's see volume
10:23
okay so it looks pretty
10:26
i guess it is by minute so maybe there
10:27
wasn't much volume there okay
10:30
all right so that's how to get
10:32
historical data obviously you can loop
10:34
through it if you want to get multiple
10:36
assets but
10:38
pretty simple now the next thing we're
10:40
going to do is go ahead and get
10:42
order book data
10:45
and you'll notice
10:46
let's go to the api
10:48
order book
10:49
you know here's the api requests we need
10:52
to make and here's the
10:54
parameters and here's a response format
10:56
okay and i'll go ahead and put that in
10:58
the jupyter notebook when i'm done just
11:00
to make things easier so let's go ahead
11:03
and well i should have copied that
11:06
copy this
11:09
make a comment
11:12
all right so we'll do depth
11:15
equals 20
11:16
path equals
11:19
market
11:20
markets
11:24
market name
11:27
order book
11:29
and then
11:30
depth we'll pass that depth
11:33
okay and we'll do url equals api url
11:36
plus path you know we've done this
11:38
previously let's make sure our
11:41
url is constructed correctly that looks
11:44
good and now let's make the request so
11:47
response equals request dot get url
11:50
json
11:51
do bids equals pd
11:54
data
11:55
frame
11:57
and again i already know how this is
12:00
formatted so we get the results and then
12:02
from there we just take the bids
12:04
okay and then we'll do ask equal pd data
12:08
frame
12:10
results
12:11
and
12:11
[Music]
12:14
type
12:15
asks
12:17
and now
12:19
bid
12:20
columns equal
12:21
well let's go ahead i'll show you what
12:24
what happens here so we'll just say bits
12:28
oh let's see what i do there
12:30
um
12:36
no results
12:39
okay
12:40
path api url
12:43
that looks good
12:45
request dot get url json
12:49
bids pd data frame
12:52
let's see let's see what we got with
12:54
that response this is always fun to do
12:57
live on youtube right
12:59
okay so we did get that bids
13:02
and asks okay so result and then
13:05
okay
13:06
so bids equals pd.data frame
13:10
results
13:12
and bids
13:19
result
13:21
that's what i did
13:23
okay so
13:29
there we go
13:30
okay so we can see now that we've got
13:31
the bids in the ass but zero and ones
13:34
don't really help us that much so we'll
13:36
do is we'll do bids
13:38
columns equal
13:40
bid price
13:41
and
13:42
bid amount
13:45
and
13:46
asks dot columns equal
13:50
you guessed it ask price
13:54
ask amount
13:57
so now we can
14:00
we'll just do bids the head
14:03
all right so that that makes things a
14:05
lot more easier to see now what we want
14:07
to do is we'll just merge these because
14:09
it'll just be better to have them you
14:11
know one table at least i think so so df
14:13
pd merge
14:15
and then it's just bids asks and what do
14:17
we want to merge on we want to merge on
14:19
the left and right indices so we'll do
14:22
and when i say left and right that means
14:25
the index of both data frames right so
14:27
left index
14:28
which is bids uh we'll do true
14:31
and then the right index
14:35
which is asks is true
14:38
and that should merge them hopefully
14:39
this won't make a liar out of me
14:41
uh let's
14:42
see
14:44
bit i keep doing that bids and asks all
14:47
right there we go perfect so now we've
14:49
got our bids and asks
14:52
and if we really want to we can also get
14:54
some summary statistics on this
14:57
df
14:58
describe
15:00
right so you can see you know mean
15:03
standard deviation and all of that so
15:06
perfect okay so now what we'll do is
15:09
we'll go ahead and get the trades okay
15:14
get trades
15:20
so you're starting to see a pattern
15:21
right it's pretty easy once you start
15:24
using apis um you know it's pretty
15:27
pretty trivial trivial so here's what we
15:29
here's the request
15:32
okay
15:33
make sure i got that right get trades
15:36
yep
15:44
okay
15:45
now what we want to do is
15:50
put that there and create the url so
15:53
path equals
15:55
f
15:56
to markets
16:00
market name
16:03
which we have as you know from previous
16:05
uh
16:06
previously set as fusd
16:09
or eth i guess it's ethereum
16:12
so url equals api url plus
16:15
path
16:16
and url let's see if that gives us the
16:19
correct url it looks like it's the it is
16:22
so what we'll do is now we'll get the
16:24
trades again
16:25
response equals request
16:30
dot get url json
16:33
df equals pd data frame and you know the
16:37
drill at this point result
16:40
i cannot results and df
16:42
head
16:44
oh boy
16:46
maybe i should quit today
16:49
okay
16:52
that of that frame yeah
16:54
there we go okay
17:15
i guess i'm gonna have to check okay
17:17
let's see here all right
17:18
look at the response
17:21
okay so that's the problem right so it
17:23
looks like something
17:25
goofed up
17:26
and that's
17:28
that's what it is i messed up the api so
17:31
um what we do now is now that we've
17:33
corrected that we can get the respon
17:35
proper response this is a problem about
17:37
live coding on youtube but that's okay
17:39
i'll keep the mistakes in there because
17:41
no one's perfect
17:42
and then
17:45
okay under here okay perfect so now what
17:47
we do now we've got
17:49
uh the trades right
17:51
so pretty easy so
17:53
now we can do this with futures too so
17:56
if you're interested in futures for
17:58
working with ftx features
18:02
and it's really more of the same right
18:04
we just get the url
18:06
so we'll say url equals
18:09
https ftx.com
18:13
futures
18:14
to different ul
18:16
equal
18:18
requests
18:19
dot get
18:21
url
18:22
json
18:24
response
18:26
let's see what you can see here where i
18:28
go to
18:29
futures right so
18:45
and search for oops
18:48
futures
19:02
and now you can also work with futures
19:05
if you want to it's pretty easy so we'll
19:06
just do url
19:33
now you can also grab futures too and
19:36
it's pretty easy it's the same thing
19:38
right url https only this time it'll be
19:41
a dot com
19:44
features
19:55
and that's it i mean if you wanted to
19:56
work with futures or any other markets
19:58
it's the same process but pretty simple
20:01
to use the ftx api
20:04
and
20:05
for the most part almost most
20:08
and that's it it's pretty easy to use uh
20:11
you know the ftx api or almost any api
20:14
for that instance once you get the hang
20:15
of it right just make a request the api
20:18
and you know manipulate the data in the
20:20
format that you want and then analyze
20:22
the results right so hopefully this was
20:24
helpful if it was
20:26
please go ahead and hit the thumbs up
20:28
button and i will see you in the next
20:30
one thanks bye
#Business & Industrial
#Finance
