How To Build a Simple Cryptocurrency Live Trading Bot with Python using the Binance API

879.055 visualizações

Transcrição hello and welcome so let’s do some automated cryptocurrency live trading with real money i think it’s easier to understand what is going on by visualizing in a chart what we are going to do so first of all we are requesting live data from binance via the binance api in specific we are using the binding socket manager then we are doing some data cleaning to the requested data with python and store the cleaned up data in a sql database this database is being feeded every second with live price data from this first python script after that we are accessing this live updated database with a second python script then apply a simple trend following trading strategy and execute real money orders before we are starting we seriously.

    Need to talk so i took a look at my analytics and i’m thankful for all people watching my videos but please be so kind if you enjoy them or can extract value out of them to consider subscribing thank you very much in advance to better follow along i recommend to watch my videos on python and sql you don’t.

    Need sql syntax knowledge but you should have an idea about the intersection of python and sql also please check out the video on the binance api all links are in the video description some disclaimers i’m.

    Not reading them out but please check them out beforehand okay let’s get started okay so let’s get started with the first script this script is getting live price data from binance using the bind socket manager and storing this live data into a sqlite database we.

    Need some libraries to achieve that we.

    Need pandas for data cleaning we.

    Need sql alchemy to establish a connection to our sqlite database which we haven’t created yet and we.

    Need to import from binance client we.

    Need to import the client and also the socket manager okay so in the first step as in my previous video on the binance api we are setting up the client by providing our api key and our secret key so i already created these variables in the script here they’re just.

    Not visible so you would.


--*--

    Need to define them in the script here right so in my case they’re defined okay we have set up the client and.

    Now we can use this client to use the binance socket manager so i will link the official documentation here in the video description and there are many ways to implement web sockets here with the python wrapper and i’m using the manual way so this one here but you can also use this one or this one just take a look at that i’m using this way in case you’re wondering so what do i have to do first of all i’m setting up the binding socket manager so i’m defining bsm here use the biden’s circuit manager and provide the client with that i’ve set it up and.

    Now i have to define my socket so which data do i want to pull and there are many possibilities to pull data you can pull candlestick charts or candlestick price data and stuff like that but i’m just using simple price data and i’m therefore i’m creating a socket variable use bsm and provide the trade sockets here and define the symbol for what i’m i want to get data and i’m just using the bitcoin here in relation to the us dollar stable coin right so we can execute that and we have to find our socket here.

    Now how is this working regarding the documentation we can use the manual way here by providing it in the following way we can use a weight then use the socket and the magic method uh enter here or a enter here then define our message as the receive function and print out this message and let’s execute that and you will understand what’s going on here if we’re executing that we are getting something like this and this is containing some information but essentially it is just a timestamp with the time we are getting this price data which is this e here in a unix timestamp format and some other information but we only are interested in this p which is the price on this timestamp here right and this is basically what we are going to do in the.


--*--

    Next step we want to transform this dictionary here into a more readable format right and we have pandas to achieve that so this is the.


--*--

    Next step we are creating a function which is transforming uh this into a readable format here so i’m creating a function here create frame or create data frame which is taking the message received from the bind socket manager as an argument and what this function is doing is this is transforming this message first of all to a data frame so i’m setting up a variable here df and create a data frame out of this message here in the.


--*--

    Next step i’m just filtering this for the e the event time the s the symbol and the price here the p so this is my.

    Next step i’m just using a log function to achieve that take all rows here which is basically sensors before because there’s just one row and we are providing as this the symbol again e this is the timestamp and p which is the price okay data filtered perfect.


--*--

    Now we are transforming those column.

    Names into readable column.

    Names so we are defining the columns as symbol time and price right and.


--*--

    Now you have to understand that when you’re using a dictionary like that you won’t get the price which is here as a float value but as a string value you see that here so this is a string format and we want to have that as a float value why because we want to make some calculations with the price data and and so on so this is what i’m doing.


--*--

    Next step i’m just transforming the price into a float so i’m using the f price as type float okay perfect.

    Now i want to get rid of this basically.

    Not readable time format and use a standard time format here and pandas has a.

    Nice function for that so i’m using pd to daytime provide the time here and take the unit as ms and this is just transforming this timestamp into a real one in quotation marks because this also real one but in a.


--*--

    Not readable format at least for me.

    Not readable format and after that we are returning the data frame so.

    No worries we are testing that out that you will understand what this is about so if i’m calling this function.

    Now and provide our message here you will see that i’m just getting a data frame which is containing a timestamp with the exact time as you see here and the price as a float floating value right so this is what this function is about.

    Now the idea in general is.


--*--

    Now to use this data frame and store that into a sqlite database right and to access the sqlite database we have to define an engine as i said in the beginning i’ve explained that in my python sql videos so i’m just writing it down here so i’m creating an engine here and use the sql acme library create an engine and use sql or sqlite as we’re working with sql lite here and i’m just defining a database here let’s call that btc usd stream.

    Now i want to update this database every time i’m receiving a.

    New price from binance and how can i achieve that by combining this with the create frame function and then store that frame to a sql database and we have the handy to sql function which i’ve explained in my python and sql videos so how can i do that i’m using an enter superior y2 so you have to stop that manually when you’re finished otherwise you will have a very very large database with the tile right so what do we have to do we are applying or we are creating a variable first and then we are applying the create frame function to the message then we are using the to sql function here define a table.

    Name btc usdt provide our engine which is our btc usdt stream use if that already exists i want to append that and i’m providing the index as false and in the end i’m printing out the frame so that we are getting an output what actually is going on and when i’m clicking this or when i’m executing this i’m getting an endless request to the binance server that the binary server is providing me price data and i’m storing every second i’m storing a.

    New row in this format into my sqlite database right and let’s execute that and see what is going on and.

    Now we see we are getting live price data here pretty.

    Nice right and it’s.


--*--

    Not only printed out but it is stored in our sqlite database right so every row you see here is being stored in our sqlite database and we will request this data in some seconds when opening up our second script but that’s it for.

    Now so we have successfully created a live updated database okay.

    Now let’s open up the second script we can call that crypto live trading and first of all i have to import my api key and my secret key so i’m just running a.

    Notebook where i’ve stored that so you can skip this step you can just define that as variables here again and i have to import sql alchemy again pandas and also the client let’s zoom a bit in here i think it’s better okay binance sorry from binance import client and i’m setting up the client again api key and api secret and set up the engine again alchemy create engine [Music] sqlite then we call that btc usdt stream right hopefully and.


--*--

    Now i’m showing you how this working i’m just creating a variable the f here use the read sql function and read out the created btc usdt table right and provide the engine here and.

    Now you will see when we are printing out this variable that this having live data right so if i’m executing this again so remember this so or you can remember the rows 641 as the index here if i’m executing that again i’m getting.

    New rows as i’m as i’m getting live updated data here right and this is kind of awesome right so of course you can plot that so as a project for you you can use matplotlib to create a live chat out of that pretty interesting thing and.

    Now you can analyze this very yeah granular i hope this word exists very small piece data i’m talking about and you.


--*--

    Now you can analyze patterns on a very granular base right so we could plot this price here and see that here’s something going on in just some seconds right and this is kind of at least in my opinion kind of awesome to trade such yeah small time horizons here right so if i’m updating that getting a.


--*--

    New chart here so yeah but enough talking let’s trade a bit and we are creating a trend following strategy here so i have a video on my channel on that so i’m just writing down the strategy here so this is a really simplified strategy of course and it’s kind of dependent on your feedback if i’m continuing this series so my last video performed really bad i don’t know why that is maybe you’re more interested in stocks but i think it would be pretty interesting to apply some technical analysis to this data and continue this series at least i think that’s that’s pretty.

    Nice because cryptocurrencies are rather in my ex experience um traded with technical indicators i don’t know why that is but um they work pretty good on cryptocurrencies so our strategy is um if the asset or if the the the crypto was rising by x percent so by some percentage we want to buy and we want to exit the trade when profit is above 0.1.15 or loss is crossing minus 0.1 percent right so again we are just checking how was the asset performing over the last let’s say 60 seconds and if we are crossing a certain threshold we are buying the asset right and we are selling when the profit is above 0.15 or the loss is crossing minus 0.15 percent okay so let’s code that we are just calling that strategy we.

    Need an entry point which is just this x here right the asset should have performed higher than this entry uh parameter and then we are buying we have to define a look back period so how much time do we want to go back so if this is 60 we are going 60 time steps back so one time step as you saw is just one second roughly so if this is 60 we are going back 60 seconds if this 120 we are going back two minutes and so on right we.

    Need a quantity which we want to trade and we.

    Need to set a boolean value here this will make sense in some seconds it’s just telling us if we have an open position important for the selling condition okay so first of all i’m defining an infinite loop here and this loop is just requesting data from the database with this function here right so we are just getting fresh data with every iteration so we can yeah basically just copy paste this line here and this is i said requesting live data.

    Now we are defining a look back period here and this is just filtering this data frame for the last defined time steps here so i’m just using minus look back and this is just filtering until this time period all right then i’m accumulating the returns over this lookback period so.

    Now i’m just applying a cumulative return formula i’ve explained that in many other videos on my channel i’m.

    Not going into details here look back period price we’re taking the percentage exchange at a one take the cumulative product out of that and subtract the one here so this is just uh yeah summing up in quotation marks the returns over this look back period.

    Now we are saying if.

    Not open position so if we don’t have an open position we want to check if this cumulative return is above our defined entry right so simple if clause here if this cumulative return and.

    Now i’m taking the last valid entry of this cumulative return series this is basically just the very last row so i’m taking the last valid index here and i’m just getting the value of the most recent cumulative return and this should be larger than the entry and what is happening.

    Now.

    Now if this is the case we want to place a buying order right and i’ve explained that in my last video already so we’re just using the client create an order define the symbol as btc usdt of course you can use parameters even more right so you can provide the symbol as a parameter to this function i’m just.

    Not doing that here um we have to define the side as the by side because we want to buy the asset and we’re just placing a market order here and the quantity is just my provided quantity with that we have placed a buying order so we want to print out this order afterwards that we are getting the feedback that uh buying order has been placed and we are getting some conditions what was the price of this order what was the time and so on after that we are setting the open position to true and after that very important we are breaking this while loop here because this shouldn’t get any more data and should.

    Not open up positions after we have opened up the position here this is why we are breaking this loop after that okay this is our buying condition condition right and.

    Now we.

    Need to define our selling condition so i’m going here and say okay if the open position is true i’m again using a while loop which is getting data as above here so i can just copy paste and um defining that is really important the since buy returns so first of all i’m slicing this data frame for only the the the time steps after i’ve bought the asset so i’m defining a since by variable here use the f log and i’m providing a condition here the time should be larger than the transaction time of the order so this order has a property which is called transaction time and this is including a timestamp which we can translate to a.

    Normal timestamp so we are just using the true datetime function again and provide the order and then take the trends let’s structure that better transact time here and we have to provide the unit as ms here so this is just transforming this unix timestamp to a readable timestamp and is enabling enabling us to to use this condition so again this is just filtering for only the the time after we have bought the asset okay.

    Now we are doing the following we are specifying if the length of this synth by data frame is larger than one why that because otherwise we could if we as we are working with second updated data we are we could get the situation that we are getting an empty frame here right so this is what we want to avoid with that otherwise we would get an error here so this way i’m providing this condition and.

    Now i have to provide or i have to calculate the return since i bought this asset and this is basically just copy paste from the syntax above so this is just again the cumulative return formula percentage change plus one then the cumulative product and then minus one and can i zoom a bit out here yes so i think it’s better if you see all the syntax um how can we structure that yeah whatsoever um we are one second oh.

    No i think that’s better right maybe a bit small but i think you should see that.

    Now um yeah.

    Now we wanna again take the very last entry same logic as above so we wanna get the cumulative return um in the very last entry which is the cumulative return until.

    Now so i’m just defining a last entry variable here use since by red and again use this last well index function since by red last valent index and yeah and.

    Now i have so this variable contains the cumulative return or the return until.

    Now from the buying point on so we can only define our selling condition so if this last entry is larger than 0.0015 so 0.15 or this last entry is lower than minus 0.015 then we want to place a selling order right so we want to define an order here client create or uh and i’m just copy pasting.

    Now don’t want to waste your time here so i have to amend this to sell same quantity as the buying quantity and what else do we.

    Need to do we have to uh to print out this order again that we are getting a feedback that the selling order has been placed and very important we have to break this while loop once the selling order has been executed okay.

    Nice so this is the trend following strategy let’s execute that and let’s test that out okay so to make it a bit more engaging i’ve opened up the live chart of bitcoin which is pretty sad today and i probably wouldn’t trade the strategy today uh but it is how it is let’s place our trades or let’s execute our function and see if we are lucky placing trades so let’s say assets should have risen by 0.1 percent or last 60 seconds and we want to buy 0.1 let’s see so you see the threshold is.

    Not yet crossed ah.

    Now it’s crossed you saw.


--*--

    Now it’s cross and we bought the asset and.


--*--

    Now the asset is being sold if we are crossing um 0.15 profit or minus 0.15 percent um loss here so this is.

    Not happening right.

    Now or didn’t happen yet but let’s be a bit patient so i said our buying order was placed let’s take a look here looks.

    Not so good for us oh there we are getting the message that the sell order has been placed and we should get a message here as well right so we have successfully executed both orders and.

    Now let’s take a look at if we made profit i doubt it but let’s see we bought for 47k and yeah we made a slight profit here right we bought for 47.7k and we sold for 47.8k here right as a reminder you should stop the live data script after you have finished trading for this day otherwise you will have a really really huge database really soon so i’m stopping this and i’m.

    Not getting live data anymore and yeah that’s it so probably.

    Not the best day to trade today but.


--*--

    Nevertheless um we place two trades and i think it’s very interesting this topic right and i would continue producing stuff here like applying some technical indicators and some yeah more long-term a test of those trading strategies so yeah let it run for one day for example i think such topics are pretty interesting but i think you are rather into stocks so um i would be happy if you provide me feedback if this is interesting for you i’m.

    Not quite sure and yeah thank you very much for watching and i’m looking forward to seeing the upcoming videos

Subscribe
Notify of
guest
0 Comentários
Inline Feedbacks
View all comments