Trade Cryptocurrencies with Binance API and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we will trade cryptocurrencies with finance api and python so in the last video i've shown you how to access crypto market data and what we'll focus on in this video is to send actual orders to the binance exchange so what we have prepared here is the api documentation provided by binance which you can find on this link and also you can find the github page for python connector so you can install the python connection with pip and access the api directly from your python code so before we start i would like to mention that all examples are taken directly from the github example folder so if you check this repository you will find all examples how to send different kinds of api requests and what we're going to focus on is this trade folder where we have the list of all trade operations that we can perform with finance api before we can send orders through binance api we have to create something called an api key and an api key validates the ownership of your binance account and to create your api key we can go to binance.com and your client area you can create your set of api credentials but binance also offers an option to trade on a demo account where you can practice working with the api and if you go to testnet.finance.vision here you can sign up for an api key without having to sign up at binance directly to create our demo account in our api key we click on generate key and here we can provide our api key with a name so in this case i will use binance test and then click on generate now an api key and a secret key has been created so make sure to save that and in case you're trading with a real api key on the binance exchange make sure that this key is safe and not accessible to anyone else as they will have access to your trading account now that the api key has been generated i'm now going to show you how to use the api in secret key to use the binance api so from binance to spot we import spot as client and here i've prepared the variables where you can copy and paste the api in secret key so for api key we go here where we've created our key and copy that and add it here and the same will do for the secret key so here we have two base urls if you're trading live at binance you would use api.binance.com but as we've created our test account uh on tested.vision we will connect to this api endpoint so to create our client to use the api we will use client is equal to client add the api key add the secret key and the base url is testnet.binance.vision let's now run this cell to create a client and after creating a client you will have access to various methods and the first i'm going to show you is client dot account and with dot account you will get data from your binance account where you can see some of your assets so here we see that when you sign up with tested.vision you will automatically have some assets available already so here we have some bitcoins some binance usds ethereum litecoin etc now i'm going to show you how you can send orders to binance and there are various order types so let's start with a simple one and here we have some parameters to create a market order so let's say i want uh to trade btc usdt i want to buy at market and here i can also specify how much so this is the quantity and to send the order we simply use client dot new order so this is the method and then you pass in the parameters and after running this you then see the result that the result was successful we bought some btc sdt at the current market price as you can see as the stylus has been filled and if we now go back to the account data right so look here we have 0.9960 btc at the moment but if i run this again this number should now be greater and here we see we now own 0.998 btc so it's been incremented by 0.002 which corresponds with the quantity here so the next thing i'm going to show you is how we can set a limit order and here we just use again the parameters btc usdt we say we want to buy at the limit and the time in force specified to gtc which stands for good till cancelled so the limit order will be active until we decide to cancel it and here again i specify the quantity 0.002 and now uh the difference with the market or is you have to specify the price at which you want to set the limit so here the price the buy limit is set at 30 000 is dollars which means that if bitcoin drops below 30 000 that will trigger the limit and again we will use client dot new order passing the parameters and then the limit order will be created so here's the result with the created limit order and how do we check uh whether the limit order is there what we can do is we can use client dot get open orders and what this method does is it will show you a list of all current orders so our limit order should be visible there so let me run this and as you can see we this method here returns all open orders so here this is the order id 2419847 which is the limit order and if you check it with the previous one they correspond so the limit order was created in this cell here is now visible by using get open orders all right so in the next cell i'm going to show you how you can cancel a pending order and to cancel pending order all you have to do is use client dot cancel order here you specify the name of symbol and then you pass in another parameter the order id which is the number here so if i want to cancel this order i will just copy this number here pass it here and if i run this this limit order here should be canceled so let me run this this is the response so this was this result successful so if i run open orders again this list should now be empty because we've now deleted it great so now this list is empty and we now have confirmed that cancel order works and the last thing that i'm going to show you is uh client dot my trades and this will just show you a list of transactions on btc sdt so remember we've sent a market order to btcsdt and here you can find all your transactions that you've performed on this symbol all right so i hope that you've enjoyed this little sneak peek into binance api if you're interested in the entire documentation all the examples i recommend checking out the github repository and also i would like to mention that i will host this trooper notebook on my website so i follow the links in descriptions you can download this super notebook and test all these endpoints yourself thank you for attention and i'll be back with another video
Info
Channel: TraderPy - Algorithmic Trading
Views: 161
Rating: undefined out of 5
Keywords: mt5, metatrader, trading, pandas, plotly, forex, crypto, algo, python
Id: MWTpvSiBAwU
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Thu Dec 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.