0:01
are you tired of struggling with the binance API in python or perhaps you're just looking for Clear concrete examples
0:06
of how to use the web sockets or rest apis well you've come to the right place
0:11
my name is Leo Smiggle and I'm an algorithmic Trader and today you can say goodbye to confusing tutorials and
0:18
documentation and hello to an easy to understand tutorial walkthr let's get started but before we dive into the code
0:25
there's one critical question you have to ask yourself and you might not know the answer and that is is where do you
0:31
live are you a US citizen well US citizens and should use the binance.us I
0:37
should back up there's both a binance.us and a binance.com if you're a US citizen and
0:43
live in the US use you know the binance.us otherwise use binance.com don't get cute and use a VPN
0:51
to try to access.com because when you make tons of gains and try to withdraw
0:56
your crypto and you don't you know and they ask for kyc you might run into some problems so uh be for warned so what are
1:03
we covering today well we're going to cover both the rest API and the websockets apis if you you know I'm
1:10
going I'll put the time stamps uh below if you want to skip right to each section uh and within the rest API we're
1:16
going to cover python binance which is the most popular library but we'll also cover request too just because sometimes
1:23
you might want to do something that is outside of the library and for websockets uh you know we're going to
1:29
cover everything everything you need there you might ask yourself what's the difference between the rest API or
1:34
websockets and what should I use if I'm an algorithmic Trader well the key is to understand the difference rest stands
1:40
for representative State transfer and all you really need to know is that with the rest you're making the request
1:46
you're saying hey binance give me this right or give me that with websockets you open up a connection that has lower
1:53
overhead and it essentially or binance essentially sends you data so you know just think of it uh drinking from a hose
2:00
versus well that's a terrible analogy but basically get the point rest you ask for something and with websockets you
2:07
just open up a connection and you get it and have to handle it but we'll see this in the example so now let's open up that
2:13
Jupiter notebook now if you want to follow along you can go to this link right here
2:19
binance API Python tutorial and learn how to do some of the stuff I'm not going to cover like how to get an API
2:25
key right if you need assistance with that go to that link and also uh in the future I will place this Jupiter
2:30
notebook so you can follow along on this link okay so we'll skip getting an API key we're going to assume that you
2:36
already have that we're going to go right to installing the libraries the first thing we'll do is we'll pip
2:41
install python binance which is the primary or most popular uh python
2:47
Library we'll install python. so we can get our API keys out of a EnV file if
2:53
you don't know how to do that I'll provide a link on exactly how to do that uh on this channel we're use. M for
2:59
pretty much anything it just makes it conventional easy and we'll also install pandas which you probably already have so we'll do pip install python binance
3:09
pip install python. MV and pip install
3:14
pandas now I'm going to actually comment this out because I have this pretty outline here and I don't want to have a
3:20
bunch of uh you know requirements fulfilled already uh messages so we'll go ahead and skip that but if you're
3:25
missing any one of these definitely install it so the next thing we'll do is we'll import pandas and we'll load our
3:31
API keys from that EMV file so import pandas as PD which is conventional and
3:38
Fromm import load. MV which again just loads thatv file load. M uh this assumes
3:47
that the uh read file the file is local so read file from local. EMV right so
3:55
this will load all of our um you know uh system uh variables into here
4:03
right okay so let's see here now what we'll do is um go through some basic
4:09
examples but before we do let's just take a step back and understand uh what
4:14
rest requests are so basically all we're doing is we're you know creating a get request to these different URLs to get
4:22
information right um so that's if you're wondering where I'm getting the URL it's from right here and if you're interested
4:28
you read more about that right here but let's just test to make sure that uh everything is working okay so we'll
4:34
first do a simple example with python binance that's that third party uh you know library that wraps you know all of
4:41
these calls and then we'll actually use the request library to call that directly sometimes you want to use the
4:47
request Library if for some reason there's some new functionality out that might not be already in the python binance or you want to do something uh
4:54
unique so first we'll import OS then we'll from binance client import
5:02
client which is that you know again that client that I just talked about we'll get our environment variables API key
5:10
equals OS Enon now I've named it binance API key
5:16
test copy that and then I'll do API secret bance API key this is
5:25
secret right and now we need to create the client so client equals client API
5:33
key API secret and testet equals true
5:38
right because we're going to use that test net if you're a US user you want to um you know make sure that you use the
5:46
top level domain as the US so we'll do client equals client API key API secret
5:55
TLD equals us and that reminds me I have to to have my VPN go through a different
6:02
country cuz I actually am in the US but for these examples since most of the audience is actually non us I'm going to
6:15
okay perfect all right so now I'm on I'm actually going through Switzerland all right so now um I'm going to remove this
6:21
but if you are in the US make sure you have the tldd US and you probably want the test net is true too right okay let
6:27
me enter okay no problems and now let's just see if that did indeed work by we
6:33
using the client get all tickers method which gets all of the tickers latest prices so tickers equal client get all
6:42
tickers tickers perfect so that did indeed work
6:47
you might say you know what this is a little uh hard on the eyes right um you know if you're like me
6:54
and probably stare at a computer too much you might want to make it a little easier on the eyes we'll make it a data
7:00
frame tickers DF head and then enter so uh now we can see our symbol and our
7:07
price in a pretty data frame fantastic okay so the next thing we're going to do is we're going to do the same thing only
7:14
we're going to use the uh request library right so import requests import Json and again if for
7:20
some reason you want to do something special or have some unique requirements this is you know it's what we're doing
7:26
right now is essentially what the request library is is doing I'm sorry what the um you know library that we
7:33
just used uh you know binance client up above uh it's just wrapping you know the
7:39
same thing so if you want to actually look at requests how the request should be done you can actually just go ahead
7:44
and open that on GitHub and see exactly um how they're doing it but anyways
7:50
we'll do the URL htps api1 binance.com right and if you're us I'll
7:58
just do right it should be api. binance it spell. us right so just
8:06
be aware of that right um but we're going to use so the API call will actually go to
8:13
uh the API version 3 ticker price and then the
8:19
headers right um go ahead and pass content type is
8:26
application gson and and we will say the X mbx API key which so API key we got to
8:37
pass our API key is API key right so I'm just going
8:45
to remove this to make I'll put it up here to make it a little easier to see okay so again all we're doing is we're
8:50
creating a you know get request to this URL with this AP I call it passing the
8:56
headers in um and then we'll get a response response equals requests again
9:02
that Library imported up here uh. get because we're making a get request URL
9:08
plus API call plus nope headers equal
9:15
headers and if we do it right uh we should get a response uh back um going
9:21
actually just go ahead and load this turn that into Json and now um we
9:29
can print this response and you see oh we got a response but again hard on the eyes so
9:37
let me put that into a data frame so DF equals PD data
9:44
frame from records response and we'll just do DF
9:49
head so this will get it again should be obviously the price will be different but it's the same exact thing for the
9:56
most part up here interesting the last one's different but again um
10:02
you know might not always be in alphabetical order depending upon yep so you get the
10:08
idea so we get uh we get that response and that gives us the prices and instead
10:13
of using that thirdparty Library we just made a request directly to the binance
10:19
API so now there's a few different end points that we are going to cover we're going to cover the general the market
10:25
data and the account for the sub account accounts and margin Trading um I'm just going to have that on the website for
10:31
interest in time um so just be aware of that so now let's go ahead and get
10:38
started with some basic requests by checking out the server status now we do
10:44
that with what's called an echo or a ping request okay so we'll go down here
10:49
get binance server status and we will uh use the uh client object and use the
10:56
Ping method so we'll just do client ping and we'll notice uh an empty response
11:02
and that is correct um a success is actually an empty response so just be
11:08
aware of that and then actually I'll just say empty response means no errors
11:14
for those of you following in the future and now so the let do let's do something a little bit more uh interesting let's
11:22
get the current binance server time so I want to import date time so we can use
11:27
the from time stamp method so import date time we'll do res equals client get
11:36
server time and you know just before we I'll just print out the rest so um so we
11:43
can see here that this will give us a dictionary uh of you know some integer uh from an Epoch now I already know that
11:50
this is not going to work for our date time because it expects a 10 digit uh
11:55
This binance Server rightfully so does offer additional precision because it is an exchange and one second in in certain
12:03
trades can be an eternity uh so let's go ahead and get this into our um you know
12:09
timestamp format that we're used to seeing so TS equals uh we'll get the value out of this dictionary so server
12:17
time and then we'll divide by th000 okay then we'll say your DT equals
12:24
date time date time from timestamp TS right and what we'll do is we'll just
12:30
format that so your DT stf time for format and then we'll
12:44
string if we did it right we'll get something that is more human readable which is is the 4th of January um almost
12:52
uh you know 1:00 so that is indeed correct this is in eastern so perfect
12:58
okay so that Works um now what we'll do is we'll just get all of the tickers
13:04
right now we've done this before but let's just say that you're not exactly sure you know what we can pass to this
13:10
method right so we can do help client get all tickers and you'll see here it
13:17
actually gives you um you know what you know our inputs in this case there
13:23
aren't any uh but what it returns it returns a list of dictionaries right so and then some examples and what you know
13:31
how errors are raised so that's very useful I we'll use this again likely if
13:36
because I'm sure I won't remember everything off the top of my head but let's go ahead and um you know do this
13:41
one more time because it's such an important method client get all tickers we'll do DF equal PD data frame
13:51
coin info DF head right so we did that
13:56
previously again we know that this is a uh list of dictionary so we can just put
14:02
it right in data frame and get it out easy enough okay so now we might want to get a little bit more detailed
14:09
information on the exchange or symbol uh we'll go ahead and exchange
14:15
info let's see actually you know what before we do that we'll just get the do the exchange info so exchange info
14:22
equals client get exchange info
14:29
this will get us some basic exchange info we can like to see what keys are available to us and we can see
14:38
ex what did I do here get EXC H and
14:44
G oh here it is get exchange info all right so now we
14:50
can see the various Keys we can drill down into uh one of these keys so let's drill down into symbols and that'll give
14:58
us some information about those symbols so do DF PD data frame exchange info
15:05
which is what we just grabbed up here and then we're going to drill down into the uh symbols value by passing it the
15:12
symbols key right so symbols DF and now we can see we've got
15:18
all this information regarding our various assets such as quote Precision
15:24
um let me scroll down here you know and some filters which we're
15:30
going to cover in a second because it's really important when uh placing orders but basically any information that you
15:36
need about one of those keys you can um I'm sorry one of those symbols you can
15:41
grab and now if we want to we can get individual symbol information so um
15:48
symbol info equals client get symbol info btcb USD and then
15:57
symbol info and now you can see uh we essentially
16:02
just took one of these rows and and requested this directly and you can see um things like minimum notional value
16:10
which we'll cover in a little bit right so become very apparent to you uh once
16:16
we start placing orders so now that we're done with some of the general stuff let's dig into some of the market
16:22
data so we'll recreate the btcb USD order book using a panda data frame
16:29
right uh so that kind of the order book is essentially the volume of bids and asks at a certain um the certain price
16:37
right so it kind of give you an idea of where potential support and resistance might be although you can place orders
16:43
and cancel them to trick other people but anyways we're not going to get into that right now so we'll do Market depth
16:49
client get order book passet symbol and that will be btcb USD and then let's see
16:58
what the keys are are Mark well you know what I can just do mark it y there and
17:03
what we'll see is uh last ID uh the bids and asks right and um that's pretty much
17:09
it but again one of those things where it's not real easy on your eyes so what we can do is we can create a data frame
17:14
by passing you know creating a data frame of the bids creating a data frame of the asks and then concatenating them
17:21
all together so do bids PD data frame Market depth
17:30
oh my goodness if I could type bids then we'll rename these columns that's the price right and then
17:38
the bids and we'll do the same thing for the asks asks equal PD data frame Market
17:45
depth asks ask. columns equal price and asks and now
17:55
we'll concatenate them all together DF equals PD con cat pass the bids and the
18:01
asks and then we'll fill the nans because there will be uh Nan values as
18:06
zeros and print DF so you can see here um you know uh below the market price
18:13
you know when bitcoin's below the market price there's lots of bids because people want to buy it at that level and
18:19
then above the market price there's lots of asks so people want to sell it you know for uh at that level so that's
18:26
essentially building it the um you know the order book within pandas so the next
18:31
thing we might want to do is get uh the latest uh 500 trades easy to do we just
18:37
do use the get recent trades method so recent trades equals client get recent
18:44
trades and the symbol is btcb USD which is just the St the binance's stable coin
18:52
and then say DF equals PD data frame we're just going to
18:58
put it in a data frame right recent trades and
19:04
dfad perfect okay so you can see here here's the price the quantity the quote amount the time um and is the buyer the
19:11
maker is best match right now pretty easy but what happens if we want to get historical trades well we can
19:17
essentially you know what this is a good time to use the help method right our
19:24
client get historical trades
19:30
help client get his store eal
19:38
oops all right so now we can see um you know we pass in pams it returns a dictionary uh the symbols required uh so
19:46
we need to pass in a symbol we just can't get all the trades we have a limit and we also need to um pass an ID to
19:55
fetch from if we don't it will just pass the most recent trades which would be
20:00
the exact same as above so now that we know what we need to do uh by using you
20:05
know that help uh we can then go ahead and grab the ID of this right to then
20:14
pass into our historical trades so we'll do ID equals DF lock we'll just do I
20:20
don't know 450 ID then
20:26
historical trades equal client get historical trades pass it that symbol
20:34
btcb USD we'll say the limit is 1,000 although there's unlikely it' have been
20:40
that well who knows there probably wouldn't be that many trades from here but from ID equals
20:45
ID DF create put in a data frame PD data frame
20:51
historical trades and then we'll just go ahead and print out the DF and now we
20:58
see all of the trades we've got about 196 of them from that ID so that's how
21:05
you can do that so um pretty easy there you just it's the same as the get recent
21:11
trades only you have to pass it an ID okay all right so the next thing you might want to do is get the average
21:17
price over a certain period really easy to do you just use the get average price
21:22
method so AVG price equals client get average price symbol
21:29
equals btcb USD and ABG price and you'll notice it gives you uh you know the
21:35
minute so over the last minute the price has been uh this
21:40
so um if you're doing something like you know time weighted um you know execution
21:46
or anything like that um you can use that right so there's like vwap and tww
21:53
and stuff but all right so what do we want to cover next we can get all of the tickers you know same type of
22:00
thing we'll just say uh tickers equals client get
22:06
ticker DF equals PD data frame tickers DF really easy so
22:15
now we have um you know all of these ticker prices again with uh percent
22:22
change uh percent change and things like that and if you're interested use that
22:27
help to get more information so now that we're done with that now we move on to the account endpoints we're going to
22:33
learn how to you know query you know our account balances and our account type and place orders so let's go ahead and
22:39
grab some basic account information pretty easy to do all we need to do is
22:45
use the get account method so we'll go ahead and do info equals client get
22:52
account and print this to the screen and it will give us some basic count information such as you know what are
22:58
our permissions we're on the spot Network here's our assets uh commissions and things like that but maybe we want
23:04
to get uh an individual asset balance that's easy to do using the get asset
23:10
balance method so asset balance equals client get asset balance and we'll pass
23:16
it in an asset this time we'll use ethereum and print it out and we'll essentially get uh this dictionary right
23:24
here right so and which we do so right now um looks like about 100 uh
23:31
ethereum okay so that's all interesting but maybe we want to start uh talking about uh getting our trades so uh let's
23:38
see what trades we've got open using the get my trades method client get
23:45
my trades symbol equals BTC
23:51
BSD and then print that out and we see we do indeed have one btcb USD trade
23:57
right and maybe we want to actually fetch all of our open orders right maybe we have some um you know orders that are
24:05
open that haven't executed yet so they wouldn't be trades uh so we'll do orders equals
24:10
client get all orders again we pass it a symbol ptcb
24:16
USD and I don't have anything open at the moment so we have that one order um you
24:24
know which is essentially the same as that trade right and this should be buy and and filled right so order turns into
24:30
a trade uh we likely already know that but you can have orders that are not you
24:36
know executed on so they wouldn't show up in the trades okay so speaking of orders why
24:41
don't we learn how to place some orders right so orders can be a little bit trickier because you have to make sure that they meet the filter requirements
24:48
as I'll show you in one second so we'll use the create test order method because we don't actually want to place an order
24:54
right away we want to make sure that uh we meet all the criteria and nothing really keeps up so we'll do client
25:00
create test order do symbol equals btcb USD side equals
25:07
[Music] buy uh type equals
25:13
market and quantity equals 0.005 and then we'll say buy order and
25:21
we should get an error an error here which we do and we can scroll down it says minimum Min notion
25:29
now what that means is you know there's different requirements on different each cryptocurrency about various things such
25:35
as the um you know step size or you know how much you know the the most amount
25:40
that you can buy at once uh you know things like that and minimum notional just means that our order didn't meet uh
25:47
you know the price times quantity value um which we can query right here to see
25:52
exactly what that is so we can get that by grabbing the get symbol info information so client get symbol info
26:02
btcb USD and we'll print that out we can see here uh where is
26:11
it I'll just search oh no there it is so it's minimum notional we need it to be
26:16
$10 I know it's $10 because B uh USD is the binance stable coin um so roughly
26:23
$10 although it might not be $10 with all the market turmoil that's going on recently uh but let's just validate um
26:30
well actually before we validate it let's go ahead and put it into uh easy to see uh data frame uh so that way uh
26:38
you can see exactly what I mean by all the different requirements newd uh data
26:45
frame from dict info filters and then we'll just print out
26:51
that data frame and you can see here um now we can see all of the different filters that we need we can see that the
26:57
minimum no is 10 you know we need to bet by this amount the max quantity is 900
27:03
uh Etc so those are the filters that we have to meet so let's create a test
27:08
order uh that will actually pass right um actually before we do that let's just see if um you know see if that minimum
27:16
notional was indeed true so we'll say coin info uh we've already covered this one but we're going to get the latest
27:22
price for btcb USD using the get all tickers method
27:28
Mouse in the way there and then um we'll put it into a data frame we'll filter out btcb USD turn it into a float and
27:36
multiply it uh by the quantity so say DF equals PD data frame coin info now we
27:45
filter out uh the symbol symbol
27:50
equals btcb USD turn that price into a float so we can work work with it
27:59
equals DF price as type float and then we'll grab
28:05
the price itself equals DF price values zero and print out the
28:12
price just to see if we got that correct so it looks like BTC is you know
28:18
16,99 uh relative to buusd again that's their stable coin so let's just multiply
28:25
that by our original quantity and the assumption is that we will be under um
28:32
under $10 which I think is a fairly safe assumption otherwise binance we need to
28:38
have a chat so $845 uh you know cents essentially so we
28:45
uh in order to make this work we need to increase our um quantity so let's do
28:51
that and create an uh another test order that will meet our requirements so buy order equals client create test Order
29:00
symbol actually instead of doing that we'll just copy all this already did it up here so we'll just copy all of this
29:08
and change the quantity so you don't have to sit there watching me type okay
29:14
and then we'll uh increase it by uh by that magnitude of 10 and when we hit
29:20
enter we should get nothing right so when it's a test order again an empty you know response essentially um means
29:28
that it succeeded so let's actually create a real order right so we can do that by using the order um well methods
29:36
you'll see there's various orders but client order and then hit tab you can
29:41
see that we have a bunch of different order methods where we do limit limit buy limit sell Market uh on Oco and all
29:49
of that so essentially um we're just going to use the market bu symbol equals BTC bud and
30:00
quantity equals copy this
30:06
here and then print out the order and this time we should get some information back which we do so we have an order ID
30:13
um and also it was filled you know Market bu Etc so that did indeed go
30:20
through now we can check to see whether or not our order you know what's happened with our order because if it's
30:25
a limit we might not have hit it yet and that's pretty easy to do by using the get order method so we'll do order
30:33
equals client get Order symbol equals btcb USD order uh order ID
30:44
equals order equals um
30:50
order order ID and then print this out and we should get the exact same thing
30:56
essentially what I do here not all parameters were sent so
31:03
client get Order symbol and then order
31:11
ID there we go all right so now we see essentially the same thing is up here right um you know pretty easy to do you
31:18
can cancel in order two by using the cancel order method we don't have any orders to cancel but just to show you do
31:26
clients uh cancel order try to make more typos I guess I
31:33
need some more coffee btcb USD and then order ID order ID equals you know your order
31:42
ID uh but I'll comment that out because obviously it won't work because that order has already gone through so it's
31:49
no longer an order okay so that's pretty much all you need to know about uh the
31:54
rest API it's very simple to use once you understand and you know you just have to make a get request um you know
32:01
using the third party library and you can hit tab to autop populator helper whatsoever pretty easy to do so next up
32:08
we're on going to uh tackle the binance websocket tutorial uh I think uh it's a
32:14
little bit more interesting but basically when do you want to use either or right so you typically want to use
32:20
websockets whenever you're receiving data from an exchange to make trades on because you don't want to have to wait
32:26
to make that you know get request you actually want binance to send you the data in a low overhead way so
32:33
essentially when you're receiving Market data to make trades you're going to use websockets if you're you know want to
32:38
query an order or you want to um you know check account balances and things like that you'll use the the rest
32:46
API so the first thing we'll do is we'll get the required libraries installed this one's an easy one there's only one
32:52
that we need to worry about and that is uh the websocket client so pip install we socket client and we'll run that you
33:00
can see that the requirements already satisfied um and obviously if yours isn't go ahead and install it into your
33:07
virtual environment so now let's go ahead and load the required libraries we're going to be using uh working with
33:12
Json going to use the websocket that we just uh installed and pandas of
33:20
course as BD okay now just like with uh rest we need to connect to a URL and
33:28
this time it's a socket with a different protocol but uh each one is identified by a socket and a symbol so BTC usdt or
33:37
maybe a better way to say it is you know we can create uh one socket per symbol
33:42
so socket equals and again you know I got this from uh the trade streams up
33:49
here um right here so here's the URL and uh trade stream uh it's at trade for
33:56
that stream and here's the payload right and obviously you know as I mentioned before it's very o low overhead that's
34:01
why it's only a single letter uh cuz there's a lot of data going across the wire and trying to be as efficient as
34:07
possible so WSS stream binance.com that would be us
34:13
for uh people using the US exchange 9443 and then pass the symbol and then
34:21
at trade because we're doing the trade stream now let's see if uh we've got everything working correctly instead of
34:27
typing all this out I'm going to copy and paste the following code because if you think about it we're no longer just
34:32
making a request and getting a response uh we're opening a connection we have to handle the messages we have to handle
34:39
any errors we have to handle you know what happens when we open the connection and what happens on close and then what
34:46
we do is we create this websocket app pass all of those uh into all these
34:51
functions into this here so hit enter and then uh let's go ahead and do WS
34:58
equals run forever and then if I run that you'll see that open connection a
35:04
bunch of messages and then a Clos connection right so we see that there
35:10
cool now this is all wonderful but it's a little hard to see so let's go ahead and uh update this and put all of the
35:18
message data into a data frame and on close make it look prettier so I just
35:25
copy and pasted that and I'll just do d F equal pb. datf frame on message um what we'll
35:34
do is we'll take the message put it in a Json then we'll create uh you know a row
35:40
uh of a record for uh this data frame here to uh concatenate it and then we
35:46
will um close out the on message and then format it with columns making the
35:51
time the index and the price column so let's go ahead and do that now so message equals Json loads
36:00
message then we'll create our row of data so we'll do message T and then we will
36:08
do message P so that's the time and the price now we actually can access the DF
36:14
from here so what we'll need to do is we'll use the global keyword as a shortcut now obviously if we were doing
36:20
something in production you'd want to probably have a more substantial class to handle all of this uh but for this uh
36:26
for demonstration purposes this works perfectly fine and we'll concatenate every uh every message onto the data
36:32
frame so PD concat uh we'll create a this here DF and then pd. dataframe from
36:40
records and then we'll pass in that data so that handles our on message now on
36:46
close what we'll do is we will um you know format it and make it look nice so
36:52
the first thing we'll do say the columns is are time and price and then we'll set
36:59
uh the time to a time uh you know something more readable and format that as the index and drop the time column so
37:05
we'll do DF time equals PD to date time
37:11
and then we'll set that to unit is Ms right so now we have that now we want to
37:18
set that time as the index set index DF time we'll do in place because we can't
37:27
create a new new one because we you know we want to use this Global DF and then uh we will drop that cuz now we have two
37:33
you know time column and a Time index so DF do drop columns equals time in place
37:39
equals true and then we will print the DF right now if we did everything
37:46
correctly um we should now see a nicely formatted data frame so WS run forever
37:58
uhoh D obviously I see a typo there from
38:03
records know what I did there okay but
38:09
Rec records try that
38:15
again open connection obviously we don't get any data till I stop it so I'll stop it and now we see here's an index of
38:22
time and here is the closed uh you know that price so fantastic so now let's do
38:29
something uh a little bit even more interesting um you know a lot of times Traders want to trade off a candlesticks
38:36
I actually have on my website if you're interested I back tested every single Candlestick uh using ta lib and
38:43
determined how to trade them and let me tell you it's going to be on my other channel which is about trading um most
38:50
people are trading candle sticks completely wrong but that aside let's go ahead and essentially um you know do the
38:58
same thing only this time we'll trade with ethereum so symbol equals F
39:03
usdt and socket equals uh same thing essentially except
39:09
we're going to use the Kline one minute so WSS stream.
39:15
binance.com 99443 pass in the symbol right and this time we're going
39:21
to use a Kline which is candlesticks one minute and let's just copy and paste
39:28
the boilerplate code again just to see if it's working or if I made a typo on the socket uh which wouldn't surprise me
39:48
forever okay so perfect so we do get this Kline information and you'll see
39:53
here's the Candlestick right here so the K is um here's the information X is
39:59
really what we're interested in so if we do the Kline Candlestick
40:04
um payload where's X right here so is the Kline closed so that just means has
40:11
the closing price been met in that K line then we know the Open high low close right um so let's go ahead and now
40:20
uh go ahead and make you know the whole thing we'll do the whole thing at once we'll get all of the Open high l close
40:27
and volume into a a list and then we will then create a data frame from that
40:33
and we will filter based on whether or not the Kline has been closed so so we got the time the open the high the low
40:41
the close the volume is equal to open high low close
40:47
[Music] volume 1 two 3 4 5 6 one two three y I
40:54
forgot the time okay and then um this time we'll grab this stuff
41:00
again put this right here so on the message uh again first things first
41:06
we're going to put it into Json then we need to uh you know have a if statement to determine if the Candlestick has been
41:13
closed or the Kline whatever you want to call it so message equals Json loads
41:19
message and then we'll do bar equals message K right so this is that
41:25
Candlestick information and with this in there if bar right X is equal equal to
41:32
true then we know that the Candlestick uh that's the closing price so we'll do t. append bar T and then one
41:45
two six so we got the time the open the high the low the close and the
41:53
volume same thing over here tie open high low close volume so that's
42:01
essentially Gathering all of that data and then on close what we'll do is we'll put this into a a nice data frame so
42:09
we'll say bars equal cre this new dictionary here time is equal to the T
42:16
list and then open equal to O
42:28
and volume right so now what we do is we
42:34
create that data frame so DF equals pd. dataframe and this time it'll be from a
42:41
dictionary with bars DF set index
42:47
time uh in place equals true and then we'll print that DF print DF okay so in
42:54
the sake of you know time I'm going to change this the false right so it's going to give us anything that but but
43:00
the closing Candlestick uh values uh but I don't want to have to wait a minute for each one of these to go by so hit
43:06
enter here hopefully I didn't make too many uh errors WS run
43:12
forever and then we run this see an open connection and obviously nothing comes
43:18
uh you know is output until I stop it we're closed and now we see time is the index open high low close and volume
43:26
perfect so that's it for the websockets tutorial um obviously the next step would be to create strategies around uh
43:34
these candles which if you're interested in that type of stuff feel free to check out my website where I back test you
43:39
know and figure out what really works in the markets uh so hopefully you enjoyed this one and hope to see you in the next