CCXT - Cryptocurrency Exchange Trading Library Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another video today i'm going to be talking about c c x t i have the github page open here and cc xt is short for cryptocurrency exchange trading and i'm going to be doing a little bit more uh crypto content in the near future because number one uh you know it's pretty hot right now i wanna stay uh current even though i do a lot of stock and option content i would also like to do more crypto videos uh my crypto videos are actually becoming some of my most popular so i want to continue to provide uh what's interesting to people and what is uh current and also i like that the the crypto market is open 24 7. i'm recording on saturday night right now due to the way my schedule is with my day job so i like that i can uh trade crypto and build some trading bots on the weekend because price data is always coming in and so i like that i can show some live trading in these types of videos the second reason i want to do this is i've received some emails recently saying oh what happened to you know your trading bots and what the channel is about i've recently done a variety of videos on databases and dashboards and visualizations and in caching redis caching time series databases things like that and those are great tools to have but also a lot of people really just want to see trading bots and indicators and strategies and buying and selling stuff which it which attracted a lot of people to this channel so i want to come back to that and provide a little bit more of that content but i still like to get away from that as well i don't want this channel to be just about you know creating trading bots i don't want to be the trading bot guy even though that is part of the channel i want to cover a broad variety of programming tools for the financial markets so i want to go beyond that as well but i'm going to bring it back in a little bit to trading again and so today i'm going to talk about ccxt and do a little short tutorial on this for the video for part one and then we'll add you know some strategies for uh part two but first i want to do a quick overview of the ccxt uh library this is fairly new to me i was looking into it recently some people had asked about it so i want to cover this so what's unique about this why do i need this ccxt cryptocurrency trading api so i have the github page open up here github.com ccxt if i click on that and it says it's a javascript python php cryptocurrency trading api with support for more than 120 bitcoin and altcoin exchanges so what is cool about this if you remember some of my previous videos on crypto i did a series of videos on the binance api and we used the python binance client to make some trades there and also there's some videos if you're watching closely in some of my stock videos even i actually did some tutorials on coinbase pro in there just to show some things on some live data and so i used uh some coinbase pro libraries to do that and got websocket data from a coinbase pro however um one weakness to those is that i had to and they're not really a weakness but i used a variety of different libraries that are very specific to each exchange so no matter what exchange you use a lot of times they have their own python package which is completely different and what this ccxt library attempts to do is to provide a common interface for all those libraries so it makes all of those functions and classes very abstract right and so not only does it have support for python if you don't want to put code in python you maybe you're a javascript coder or a php uh coder you know you can use any of these languages as well and i might even demonstrate those javascript and php versions of this so i wanted to cover this library in full and see what it is capable of but you see it has support for many cryptocurrency exchanges so not everyone is on coinbase or binance there's all these other exchanges i've reached link had some people reach out to me for a brand deal so there's like a buy bit and bid tracks and all the all these other exchanges which i haven't had time to fully look into but this supports those exchanges out of the box so i don't really need to know a lot about the underlying exchange or a very specific api and how it authenticates and all its method names i can just use this common library that exposes a common set of methods and so if you look here right at the overview we have a list of methods that are supported uh by this ccxt library and so you see you can load the market load markets fetch markets get fetch a ticker fetch the order book open high low close data create orders and so there's often like a common set of things we want to do with all of these different exchanges or brokers right we're getting historical data we're getting quotes we're creating market orders limit orders and all that right and there's no reason we have to have completely different libraries we need to have different differently named methods for all that what's nice about this is it provides a common interface to the to all these different exchanges so all i need to do is provide a couple of api keys uh the api key and api secret that all of them have and then i can just call a method called create order and i'll create an order on whatever exchange that i'm using and so i want to demonstrate the basics of this uh first how to get it installed and and how to use it and so let's go ahead and get started with this and see how we can use this so on the right side of my screen here i have a visual studio code editor as usual it's empty and so what i'm going to do is create a new folder so i'm just going to open and i'll just call this a ccxt bot and let's just create a new folder here so in this new folder i'm going to create a couple files usually i create a config.pi where i'll store any api keys or credentials that i need all in one place and i create a requirements.txt where i list the names of any dependencies i need and so i'm going to be using ccxt and i'm also going to use a package called schedule when we get to the trading bot portion of this series and so i'm going to put a schedule in there as well and so if i do pip3 install r requirements.txt that should install ccxt and the scheduling package or you can just install it on its own so i can do pip install cc xt and you'll be good to go and so i'll create another uh file called bot.pi and now that i have cc xt i can just do import cc xt and you can just try to run this real quick to make sure it runs without any errors and that means you have the package installed successfully so here is the documentation for ccxt and we need to see what we can do so the first thing we want to do is let's see uh what exchanges are available so let's see the first python example they have in the documentation so let's see usage so you have it you need to type the exchange and then a certain method name and so in python here you see they import ccxt and then they instantiate whatever exchange they're using so if it's a bit max or kraken or binance right it has an exchange id so let's let's do that so import ccxt and you see here i have exchange id equals binance and then exchange class is get attribute of ccxt exchange id and let's break that down a little bit in case you don't know what that means so if i were to print dir ccxt so dir and python right it just lists uh whatever attributes or functions are available on a given object so i print that out and you see uh you see that there's all these attributes like coinbase and and darabit and exchanges and exchanges is one of the attributes so what i could do here is uh print ccxt dot exchanges and we can actually list all the exchanges that are supported so you see this returns a list and this is all the exchanges available so i could technically loop through all these so i could do for exchange in ccxt.exchanges and i can print the exchange and you see this will list all the exchanges one by one and you see their support for coinbase pro and you'll see that binance is listed under here somewhere b i binance binance us i think i'd probably use binance us right okay so that's all the exchanges so if i want to use a particular exchange all i have to do is instantiate it so i can do exchange equals and i can do ccxt.binance and i'll have a binance exchange object so i'll do a print exchange there and you'll see that will actually print out a binance there at the bottom but i could also do a ccxt dot coinbase right so i'll do coinbase like that right and that'll give me the coinbase exchange and you see this example code what they did is they just used a string attribute and they used python's built-in get attribute here and so they just get either binance or a coinbase object and then they run that as a function and give it a couple of parameters to use an api key so you don't need an api key for anything yet unless you're accessing something specific about your account but this exchange object on its own you can do you can call a variety of functions that are that access public information so if i do exchange dot load markets for instance and i store that in variable variable markets equals exchange load markets and then i print those what does that do so i do that and you see it prints a whole bunch of data there right and what this does is show you all the different currency pairs that are available and so i could actually loop through those so i can do for market in markets and i can print mark it like that and i'll remove that one and you can see all these different currencies that are available on a coinbase since i instantiated coinbase but if i instantiated binance or binance us do that and you see there's all these other currencies available right and so uh one feature that's advertised in some of the examples is the ability to trade between different exchanges so you could see you know there's all these exchanges available so if you look through the documentation on the overview here there's a common set of functions that you can call and then there's all these different exchanges and so uh one thing that is possible is that you can look through all these exchanges for some obscure coins and see if they're trading at different prices on different exchanges and maybe there's some kind of arbitrage opportunities or something like that i haven't actually tried that before but it's one thing people talk about when they're talking about this library so maybe that's something to investigate as well and maybe that's not as easy as now as it used to be i'm not sure so um so yeah so there's a bunch of different uh markets that are available and you can see uh what we what we can trade okay so now that we know uh what markets are available uh yeah let's try something simple like uh getting a quote or using one of these um other built-in methods so if i look through and i jump into this you can see there's a variety of functions available so fetch yeah fetch ticker is one of them and so yeah let's see if we can fetch a particular ticker so let's see zen usd um yeah let's try that one so i'm gonna do um exchange dot fetch ticker and let's try that one and so i'll do a ticker equals zen usd and then i'll print ticker if i do that yeah you see we got some data here and we can see um a bit and asks some volume data that sort of in the close that sort of thing so we're just basically fetching a quote and let's see if we want to fetch let's see multiple tickers we could probably pass a list of different tickers ohlcv so we could do ohlc equals exchange dot fetch ohl cv and let's get it for ethereum and let's see usd i think that'll work our usdt and let's print ohlc like that if i do that you'll see we have a lot of data for ethereum and i can loop through that since the list so i'll do four candle in ohlc print candle right and you see we just have historical data for ethereum i didn't even need to provide an api key yet this is all just publicly available so i can either fetch it from binance or i can easily switch it over and do a coin base as well and let's see uh coinbase doesn't have zen usd so i'll get rid of that and let's see if it has eth usd so these uh tickers they have slightly different names on coinbase versus um binance so let's see what it's called on coinbase so for market in markets um if ethereum in market print the market and let's see what it's called on coinbase so yeah they have a whole bunch of symbols here and so they have eth usd like that so they don't have etheusdt so i'll do like that and so if i want to see what it's like on coinbase i can do that okay and fetch ohl cv i think i need to use coinbase pro actually let's see if that works there you go so i have the historical data using coinbase pro and then we have different timestamps here so this fetched i believe 500 is the default so if i jump in there you'll see you can fetch your open high low close data for a given symbol the default time frame is one minute but you can say you want the daily or the five minute or the hourly and you can say since a certain time so if you just want all the ones since yesterday and you can fetch a certain limit so let's say i only want time frame equals 15 minutes let's do that and i let's say i want only limit equals five so i want like five 15 minute bars and let's see if i can fetch that right and so that just returns five 15-minute bars that's a unix time stamp so if i go to unixtimestamp.com and see what time that is type that in you'll see that's from april 3rd at the 45 minute mark and if i check the one after that that's probably at the hour mark so i'll do that so i'll paste that one in right and so that's 15 minutes later and then this is the most recent 15-minute bar so if i type that in yeah you see um that's at the 45 minute mark there and so yeah that's how you get historical data um using ccxt and you see it's very easy to swap between binance and coinbase pro and maybe you want to check different prices between different exchanges or trade but you're on different exchanges um in the same program that sort of thing and that's all possible okay uh so what's next let's see there's like a fetch order book function so we could call order book equals exchange dot fetch order book right and then that might have a symbol of ethereum usd as well and then we could print that out and you get the idea right there's just a bunch of generic functions that we can call and you know access that data right and so uh the next thing we'll do let's try to do something that's not public let's see if we can do something specific to our account and so to do that we actually need to use an api key so i'm going to going to add an api key and this config.pi and so i can do binance api key equals you know some value and binance secret key equals some value and it'll be my binance key so if i go to my dashboard you know i have a variety of accounts available so this one has a few hundred bucks that i can use and if you log into binance you should be able to get an api key so there's part-time larry there's api management and i can get my api key okay so i'm gonna put it in this file and edit it out and see what is possible all right now that i have my api key in i can do a format like this where i use my api key and secret key so this is the python edition and so we just instantiate our class and so you see we just give it a dictionary here with our api key in secret and so for my exchange here when i instantiate coinbase pro or binance so i'll do binance us here okay and then i can just give this a dictionary and it's called api key and i give it my api key which is in config so i'll import uh config okay and then i don't need this one anymore okay so binance us config dot finance api key and my secret is config dot binance secret key just like that um and i'll comment out some more stuff and let's see if i can get some information that's specific to this small account that i have here so i'll do a balance equals exchange dot load let's see what's available to us fetch fetch balance okay and that should show some information about our balance so i'm going to print that out okay and you see i have zero of a lot of things but you see you know i have a fraction of a fraction of ethereum little small amount there you see there's like 450 bucks there so you see it has a break broken down by different currencies and what my balance is so i can fetch my balance there likewise if i want to fetch some coinbase pro information i can use my coinbase pro information here and so i'm going to put my coinbase pro information inside the config file i can swap out and use my coinbase pro api key and my coinbase pro secret and coinbase pro also has a passphrase and so if i go to my coinbase pro here if you click on this on your username there's api here and you should get a key and a secret and also a password and so if i go over here you'll see coinbase pro has a password involved so there'll be an extra attribute for coinbase pro okay config.coinbase pro and i'll call it password and i'll fill these in and edit it out and then i'll show you what it does so i'm going to run this for coinbase pro and fetch my coinbase pro balances and so i'll do four balance imbalances and we'll just see how much i have in each coin so if i print that out you'll see okay so that just does the keys let me print out let me just print the balances as a whole and you will see that uh there's all these different uh symbols here and so you see a usdc yeah you see there's like eight dollars eight us dollars in here and i believe i have a coin called numero yeah so numero nmr um i have a little bit of that um so yeah so now if i do balances believe it's balance as total is the key yeah balance is total and then you'll see numer error there and if i do so if i do my total numerair and if i print a balances total usd right and you'll see i have eight dollars there and i have point two eight numeraire and numero is just some coin uh that's part of this numerai competition that i've mentioned before so numero token and something you stake on these uh machine learning finance comp trading competitions here i haven't used it yet but i bought one just to get ready to do this in the future so this is something i'm going to try out a little bit later so i have some of this particular coin and so that's how you get your balances and that's how you use your api keys and you see how i'm able to switch seamlessly between coinbase pro and binance and i can even make this more dynamic by in my config file if i put my api keys in like a dictionary for each exchange i could switch between coinbase and binance by putting you know exchange id exchange id equals some string similar similar to what they did in the example exchange id equals uh binance here and then getting an attribute dynamically so very easy to swap between exchanges and call the same functions on each one and i guess to wrap this up for this brief tutorial here i'll go ahead and make an order so um in my coinbase pro account here um you see i have like eight bucks here um so what i can do is spend some of it so let's see if i want to create an order i can do exchange dot create and you can see i can create a buy order a limit order uh market order right and so i can create a market buy order and then give it some parameters so i just give it a symbol and an amount and so let's say a numerair is the coin that i'm buying so the symbol here is m nmr usd and let's just say i think that's like 50 bucks so let's just say i want a fraction of one of those i can do 0.01 there and just use some of my dollars to buy a numerair and i can do order equals and to store the results of that and print the order and let's see what happens so i'll use that right and you see i had a 0.28 numerair and then now and my us dollars i had eight dollars and 47 cents um if i run this again for instance you'll see now i have 0.29 you know i bought some more and now i have 7.95 and actually that's before i bought another one so i probably have 0.3 numerair and less money and if you look on the left side here you can see indeed i ran my python code and i have less dollars here so i'm losing dollars by the minute and i have more of these numerator coins so i'm gonna click play again and you're gonna see this number on the left 744 go down to six you know six and change so i'll do that and let's look on the left yeah so six dollars and 93 cents and then if i look at my balance you can see if i type yeah numerair usd you can see i have .31 of those now and yeah so that's pretty cool so now we have the introduction to using a ccxt we're able to use our api key to connect it to our account create buy and sell orders fetch historical data get a quote look at our balance yeah the usual things and so in the next video i'm going to use the scheduling package here so this scheduling package to run some functions periodically so every few minutes or whatever trading strategy we do uh we'll run some code every five minutes or 15 minutes fetch some data maybe apply an indicator or a strategy and then create some orders to create a simple trading bot once again so uh let me know what trading strategy you want to see in the next video maybe there's some script you've seen on trading view that you've always been interested in and you want to see how you'd actually implement that in python code and i'll make a video on it and do it and if no one picks anything i'll just do um some things i've heard in the past for instance uh someone was asking about a super trend so that's that's something we could do so this super trend indicator that i guess detects uh changes in trend here right and gives buy and sell signals so there's some scripts like that in pine script and i can write a python version of this super trend trading strategy here and yeah show you what that looks on python and use ccxt to do that and we'll just fetch some data on a schedule and implement super trend which i believe uses um atr combined with a moving average i think for this strategy people implement it in different ways so i'll talk about this in the next video or if you have a better idea let me know and that will be the subject of the next video so thanks a lot for watching hope you enjoyed it and stay tuned for the next one
Info
Channel: Part Time Larry
Views: 21,876
Rating: 4.9536424 out of 5
Keywords: ccxt, crypto trading, trading bot, binance api, coinbase pro api, python
Id: 2Zdm2ISdm1Q
Channel Id: undefined
Length: 26min 58sec (1618 seconds)
Published: Sun Apr 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.