Build a Real-Time Crypto Trading Bot in under 100 Lines of Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This video pretty much started it for me. My background is Machine Learning. I came to crypto trading expecting to learn a bit about trading bots then building my own Machine Learning-driven bot that trades better than humans.

Well.. after building my first bot - pretty much 99% just like Part Time Larry build here - I realized how little I understood trading. I then started to study trading and I traded alongside my bot, tinkering with it as I learned.

My bot was making something horrible like $0.25/day. But after a few weeks, I was trading and making $15-$25/day regularly. I then looked for what crypto bot softwares were out there and what kind of returns they had. The first decent bot platform I found was 3Commas.

After using 3Commas for a couple weeks I recreated my bot from scratch - not at all resembling the bot I started with.

Now I only use my own bots and I couldn't be happier. But Part Time Larry & 3Commas were HUGE in getting me started.

👍︎︎ 2 👤︎︎ u/startuptaylor 📅︎︎ Aug 03 2021 🗫︎ replies

If you release a software or a script, I hope you implement a dev fee, or at least let the users pay for it. Something like the T-Rex miner does.

👍︎︎ 2 👤︎︎ u/Ok-Passenger-7987 📅︎︎ Aug 03 2021 🗫︎ replies

Do you think, first of all, that it would be safe to learn the bot to take position on new listed cryptos and sell like 15 mins after if it's on a profit trend, and secondly, is it possible?

👍︎︎ 2 👤︎︎ u/Ok-Passenger-7987 📅︎︎ Aug 03 2021 🗫︎ replies

I am thinking of implementing Bot then fixing it with some AI like OpenAI or something, not sure yet. I got a preview invite from GPT 3 but I am still in early stage of Bot development.

Any suggestions are welcome 🙏

👍︎︎ 2 👤︎︎ u/PotentiialMatter 📅︎︎ Aug 06 2021 🗫︎ replies
Captions
hey welcome back to my channel if you've never been here before i discuss the intersection of software development in the financial markets so if you're someone who's always had an interest in financial markets whether it's stocks and options or the crypto market and you're interested in coding whether it is to analyze some market data whether it's to back test some type of trading strategy or you want to build some type of trading bot you're in the right place also if you're someone that has software development experience perhaps you're a professional software engineer and you've always been interested in how traders apply technical analysis and indicators in order to find good trade ideas uh this is the right place for you as well so like and subscribe and i'll be delivering new tutorials every week where i build entire projects and show you how to build them from scratch step by step in this tutorial i'm going to be showing you how to build an automated trading bot in under 100 lines of code i'm going to be using python ta lib binance and websockets to retrieve real-time price data and apply technical indicators to this price data in order to execute trades automatically so first i'm going to show you what we're building in the end and then i'm going to show you step by step how to build it so let's go ahead and get started so i have visual studio code open here and i'm going to show you what we're building first you can see at the top we're importing a variety of libraries that we need for this project we're going to be using web sockets so we're going to need the websocket client we're using json data we're going to need the numpy library ta lib we have a local config file and we're going to pretty print some data just so that we can analyze the data that's coming in from binance and we're also going to be using the python binance package so we're going to need to install all of those packages first at the top here i have a number of constants written out where we define some values that we're going to be using in our program and this allows us to easily configure how our program's running so since i'm using the rsi indicator as a simple example of an indicator we can use to make trades i'm using i'm defining some rsi values here so we're using like the defaults of rsi period 14 an overbought threshold of 70 and an oversold threshold of 30. and i'll talk more about what the rsi is in a moment but those are the comment the default numbers that are used and if we wanted to change those defaults then we could just change them right here the symbol we're going to be trading is ethereum so i have the symbol coded in here if we wanted to trade bitcoin we could just change the symbol to bitcoin btc usd we're going to buy just a small amount since we're just demonstrating this for educational purpose so um i'm buying .05 ethereum and since ethereum is over 400 us dollars now that's actually spending about 20 so we're gonna buy about 20 25 worth of ethereum and buy and sell it based on an rsi indicator uh we're going to be getting our data from a websocket and so if you look up uh binance websockets in their official documentation they have a candlestick stream here and it has a specific url format and this is the data format and we're going to be using a websocket stream here and i'll talk about that a little bit more in a moment but we're going to be streaming ethereum usdt data and we're going to be getting one minute candlesticks and the reason i'm showing a really short time frame candlesticks is because uh people are really interested in short-term data on this channel i've been doing a lot of daily examples or weekly and monthly examples but people a lot of viewers out there have been very interested in very short-term trading so they want to see a lot of action happen real quick also it's easier to make a video and show it actually executing when we don't have to record this across multiple days so i'll actually be able to show you a data in real time and show you candlesticks that are closing and new data points being added to our series for analysis by ta lib all right we're going to be tracking all of the closing prices of each candlestick so we initialize an array or a list and we're tracking whether or not we're already in the position we're going to be initializing a binance client so that we can execute buy and sell orders against binance and i'm going to go into all this in detail in a moment i'm just skimming over this to show you where we're going and then we'll show you how to get there okay we're going to define a function to order and so let's start at the beginning of the program we're actually going to connect to a websocket so we're going to create a websocket app we're going to give it a socket to listen on so that'll be our websocket stream and then we're going to specify a number of callback functions uh whenever we listen on the stream so we're going to be listening for data to come in and we're going to call different functions whenever we get a new message and when we open a connection so we're going to run the websocket and then each time we receive a message we're going to be receiving some price data and then when we receive that price data we're going to be processing that price data in the form of candlesticks we're going to look at the close of the candlesticks and apply a technical indicator to those closes in real time and whenever our rsi is above the overbought threshold we're going to execute an order and then when our rsi is below the oversold threshold uh we're going to execute a cell so you kind of see where we're going with this so i'm going to show you how to do this step by step so first i'm going to draw a diagram and then we're going to install some of the dependencies so let's get started with actually building this thing out so if you've been to this channel before you may notice i'm trying a few different things this time i'm trying a little bit better camera and lights and this arm here to see if i can move around a little bit better rather than putting a webcam on top of a set of books and things like that and i'm also going to try some illustrations with the ipad here to see if that helps explain some concepts a little bit better so in this tutorial i'm going to start off with a diagram of what we're building and so i'm going to draw uh binance here so this is going to represent binance right and what binance is going to do for us it's going to going to be our broker so we're going to execute orders through the binance api but uh it's also going to be our data provider and so our data provider and our broker are not always the same thing in this case we can get the data from binance and there's a variety of ways to get data from binance we can use the rest api to pull data from binance as we did in the previous tutorials but in this video we're going to use websockets and so the way websockets work right is websock binance provides a bunch of streams so there's the streaming data coming through and those streams all have different names and what we can do is have a client connect to one of those websocket streams and maintain a persistent connection rather than always requesting data with an http http request and so uh binance you know it's producing these streams of data right and let's say one of these streams of data is the bitcoin prices right so it'll have bitcoin usdt uh ethereum usdt right usdt and then maybe it has litecoin and a bunch of other cryptocurrencies so these streams are all being pushed out at all times and so this data is being published and on these streams we have streams of price data right and so what you'll get is open high low enclosed data candlesticks and also individual ticks that make up those uh candlesticks so um we'll get a price for ethereum for instance of you know 432.21 and then 432.32 and so forth right and so there's a bunch of prices coming in and as new orders come in these prices fluctuate up and down right and so it provides these streams of data and then now all we need to do is provide some type of client to connect and read this data from this websocket server right and so we've already built a couple of different clients before we've built the browser client right and we use javascript to connect to this stream of data and then we've also used wscat which is a node.js tool that we can use from the command line to connect to this data stream and then now we're going to use the python websocket client right python websocket client socket client and this is going to be reading data from these streams so this data is always being provided and we just need these clients to walk up to this beautiful river of data and drink this data from the stream and then in our little python client here we're going to read in this data right and so python right is going to get some data coming in and we're going to process that data and so we're going to be interested in just the closing prices so it might be closing prices of one two three four 5 right and then we're going to use ta lib to apply the rsi indicator but we could apply any of our candlestick patterns we've talked about in previous videos and we're going to apply the rsi indicator to this series of price data and then based on the value of that it's going to give us a value like 42.31 and so forth and then if that number is greater than 70 we're going to hit binance as a broker here okay binance is our broker and they give us an api for buying and selling so if it's greater than 70 we're going to issue a cell http request to binance and if it's less than 30 then that means the stock is oversold and we're going to issue a buy right and that's the basic mechanics of what we're building we can add you know different logic here to uh you know and use any indicators that we want any patterns whatever you want to fill in here but i'm just showing the general flow of data applying a strategy to that data and then issuing buys and sell orders so now that we have the diagram written out let's go ahead and install the websocket client and ta lib so that we can start processing this price data so as usual i have a new visual studio code editor open i have a new folder i called it rsi bot there's nothing in it yet i'm going to create a new file called requirements.txt and this is where i usually keep track of the different python dependencies we have and the various third-party libraries that we rely on and so we're going to need a few libraries in order to build this project the first thing we we rely on is python binance so if you look this up finance there is a wrapper around the binance api it provides some easy to use methods for placing orders and creating a binance client so you just need to install python dash binance the next thing we need is ta lib which is the technical analysis library so if you look up python talib you'll see a reference to that and all we need to do is do a pip install of that and so if you go to the documentation here it has some installation instructions and i've done this in other videos as well so you do pip install ta lib some users of windows have reported they have different instructions they need to use so make sure you review that a lot of people are installing installing it separately using a wheel file since windows is a little bit different in this regard on osx i had to type brew install ta lib first i believe and then install it with pip okay so we need python binance we need ta lib we need numpy which is used for calculations on numpy arrays so it provides a way to perform uh really efficient um optimal calculations on series of numbers all right and then we also what else do we need i think that's it oh we also need the websocket client for python and so if you look up uh python websocket client uh you'll get this page here and you see the package is called websocket client and so we need to install that as well i already have these packages installed so if you don't have them installed you need to go ahead and do that so i'm going to create a new file called pythonbot.pi and i have my visual studio code editor configured to activate a virtual environment and so if i run this you'll see it'll it'll run this automatically and i'm already have this virtual environment activated and so i just need to do pip install dash r requirements text and it will install all of the packages that are listed in this requirements.txt and so we should have everything good to go now and so now what do we need to do so we're going to import websocket which is part of this a websocket client package and then we're going to go ahead and create a new websocket client so we're going to do ws equals websocket dot web socket app and then we need to give it a socket to read on so we need some type of a stream that we're going to read and so we need to look at our binance documentation right and see the names of the different streams we use and so if you look here at k-line candlestick streams they provide some streams and then we get all this price data and so you see we get a time stamp here and then we also get this open high low and close data that they provide so we just need to find the url format that they use so if we look uh for wss colon slash you see we have this base endpoint of stream.binance.com so this is where all the data streams from and so let's create a variable uh for that we're gonna call this uh socket and so we'll put that in quotes and that'll be the base there and then we also need the address for the stream so we need a slash ws so we'll need a slash ws and then we need to give it a stream name and so if we look at our candlestick streams um we should see oh yeah so we just need symbol and then at k line and then an interval so i'm going to put that in there as well and so we need a symbol so the symbol we're interested in is ethereum usdt which provides prices for ethereum and then we want candlesticks or k lines and then we need to provide some interval here and so which stream are we interested in so they provide streaming data across multiple time frames and so binance will give us a one minute candlestick stream three minutes five minutes 15 minutes or a daily stream or whatever time frame we're interested in and since we're trying to retrieve data more frequently and want more candlesticks so that we can show this in a video we're going to do a short time frame so we're going to use the one minute candlesticks so instead of interval here i'm going to put uh 1m and so that'll give us our socket and so our websocket app needs to listen on a socket and then it'll connect right and so what this websocket app requires us to do is to give it some callback functions so it'll connect to this socket and then it wants us to define a variety of functions here and since i have the visual studio code python extension installed i get some auto completion here so i can see what's available and so what we need is a function for when we open the connection we need it to call a function we need to provide it a close so when the connection is closed we need to give it a function to execute to show it what to do and then we also need an on message and this is the most important one right and so we give it those parameters and so we can define these functions here on open and then like that and then we can print opened connection and so we can say on open equals and then we pass it the name of this function right on open equals on open on close equals on close so i'll define the logic i want to execute when the websocket is closed so print close connection right and then on message right and then we can print received message right and so we pass it these callback functions so after we connect we receive all these different events when we're connected to the websocket and then we tell python what functions to execute when these events happen and so let's see if we can run this okay and we need one more thing we need ws dot run and it's called run forever and we run that we initialize the app we give it the callback functions and we say run the websocket app and let's see if this actually runs i'm gonna hit my play button here and run the bot and it says error on close takes positional arguments but one was given all right so let's look at our websocket documentation here and see what it needs and so it actually needs a reference to the websocket so that'll be called ws right and so each of these functions need to receive an input of ws and the onmessage1 receives an actual message and so we need some input parameters so these get a reference to the websocket and then this one gets a reference to the websocket and a copy of the message right so i added those input parameters and now i'm going to run it again so i ran it and it just said closed connection it didn't say open so there must be something wrong with our connection so let's see what happened so if i look up here you'll notice i actually left this angle bracket in oops now run it again and now it says opened connection and now it says received message received message so we're receiving messages successfully but we don't know what messages we're receiving and that's what this reference is for this variable message so every time this onmessage event fires we receive the message itself and so instead of just presenting printing receive message let's print the actual message and so i'm going to stop that and run it again you'll see it opens the connection and there look at that just like that we're receiving messages and those messages have open high low enclosed data on the candlestick so if you look at the documentation here you'll see that we get this json data format and in the attribute key or the attribute k here actually has the candlestick data that we're interested in and so we can parse that data out and start using it and so let's see if we can parse this out so we're receiving this data in json format and so all we need to do is parse those messages and so to do that we need to import json because we're getting json data instead of just message we're going to do a json message equals json.loads and so what that will do is take a json string and it will convert it to a python data structure that we can use and so i'm going to do json message equals json.loads and i can then print the json message right and it's not that easy to read this here and so i'm going to use the python pretty printing functionality so i'm going to also import pprint and so if i do a print dot p print json message like that this will give me a little easier format to use and so let's think about what data we're interested in here right so we're getting our price data for ethereum and we see uh the opening price was 429.46 for so 429.46 right and so uh we're getting this candlestick data in in real time right right and you'll also see what it does is um the timestamp so this t here is a timestamp and so if i type that into unixtimestamp.com for instance you'll see what time it is where i am so it actually gives it this utc time and so you'll see this is for august 16th at 1 in the morning utc time and i'm like 7 hours before that all right so we have these time stamps coming in and then we know the prices that correspond uh to that timestamp so that's good um but we don't really want like all the data so this is this is just streaming data as it comes in more and more prices right but we're only interested in the open high low and close like the final value for that candlestick and so what i've noticed is see that x equals true there that'll tell you when it's the final value of the candlestick right so if you look at this documentation you'll see x false is this k line closed and so this tells us if it's the last tick that we received so this is the very end of the candlestick right and so you'll see these time stamps are going to stay the same between between data data points right so you'll see that's 8 000 at the end you'll see that one is 8 000 thousandth in so we're getting more and more data but it's for the same candlestick and it's only when that x becomes true that we got like a new candlestick has started right so i'm gonna stop that real quick and let's let's think about this so since we're going to be using the rsi indicator all we really care about is the close of each candlestick so i just need to capture the closing value right so we have since we know that when x is true it's the close of the candlestick we can say is candle closed closed equals and then i also want to reference to just this part so i'm going to do a candle equals message k right and so i'll say is candle closed is candle x and then the candle closing price so the close is candle and then c is the close so we have a ref an easy reference to whether the candle just closed and what that close was so if uh is candle closed print candle closed at dot format close and then let's just print the closing price of each candlestick as it comes in so if i run this again you'll see that data starts to stream in and it says stream string indices must be integers oh and that's because i renamed it from a message to json message so let me just call that json message so candle is a reference to json message and then if i run this again let's see what happens all right and so we're streaming in data and we're only going to capture the final closing price of the candle uh when it actually closes okay right there you can just see that x became a true right there that means it was the very last tick of that candlestick so x is true and so we printed our candle closed at and so the closing price for that candle was 429 31 and then we continue to stream you know data second by second until we get another close and so we have a way to get the closing price so now what we want to do is we're thinking in terms of the rsi indicator which uh can process our data based on a series of closes and so what we want to do is actually track a series of closes so at the top here i'm going to create a global variable and i'm just going to call it closes and start it as an empty list like that and then here i can get a reference to that so i can do global closes and so if the candle is closed let's go ahead and append closest dot append the close right and then over here i can just print closes and then i can also print the actual closes themselves so the string closes and then the closes and then you'll see these start to build up over time and so we'll eventually get a series of dozens and hundreds of closes minutely closes right and then what we're going to do is convert those to a numpy array and then have ta lib apply an indicator to those closing values so you can see i've just rerun the program again and it built a list so we just appended a closing candlestick price to our list of closes and printed out the list and it looks like it printed out as a string so we're getting a string so for numpy to operate on that we need to actually convert this to a number and so instead of just appending the close there let's go ahead and cast it to a float and so we'll do float of close and just uh keep uh keep appending these numbers to our list and eventually you'll see this list starts to build up and so yeah that's pretty much all we need for the websocket client part of this we're successfully getting ethereum pricing pricing data from the websocket similar to the diagram i showed so the second part of that is we need to talk about the rsi indicator and ta lib and how it applies to these closing prices so i'm going to let this run and continue to build up candlesticks for a while and then let's go back to our diagram and show you how the rsi indicator works and i know a lot of people already know that but i want to break this down even further step by step and try to teach these principles from scratch so in my previous tutorials on binance we already talked about the rsi a little bit we showed it using trading view and also wrote a back test to show us the results of trading with the rsi sometimes on some time frames it works out well and other times it works out poorly and you lose money and so for the examples we gave we looked at bitcoin over the daily time frame for the year 2020 here and you see uh this is bitcoin and in march it tanked along with the market right and you can see the rsi goes below if you look at the bottom here under 30. uh so it's oversold there you make a buy we're probably getting bitcoin below 5000. so 4 000 something we bought it when it's oversold we got one oversold signal perfect and then we get this overbought signal and sell it um at what uh over eight thousand nearly nine thousand uh per bitcoin and so that's a that's a great trade right you made over fifty percent you got one oversold and one over bought you make one trade not many commissions to pay a great you know fifty sixty percent return so uh that's awesome that's what we want to happen but we showed how that's not always the case you can uh show your strategy here and it looks like it worked for one time period but then often uh as we demonstrated we wrote this backtrader uh backtesting uh function here where we downloaded 15-minute candlestick data for binance for the year and you see here we showed where uh these uh entry points are not always great so we should we talked about some of the flaws right so here uh bitcoin went down and it flagged oversold actually right here right right here and then you see it popped up real quick and then gap down even lower here and so you can see where it got even more oversold on this particular chart and and so you are underwater with your position so one thing that can happen is something that's oversold can stay oversold and an asset that is overbought can stay overbought and you miss out on a lot of your returns right and so this is a particular case where we showed where you could sometimes be underwater by trading with this strategy so the important point is you know to back test your strategy over multiple time frames or at least have a back test to apply that applies to the time frame that you're using and then the other lesson is that usually people don't use the rsi indicator by itself usually they use it in conjunction with other strategies like the macd or other indicators it's not often a good idea to use it by itself and the reason we're using it by itself here is because this lesson isn't necessarily about uh finding the optimal strategy or combination of multiple multiple indicators the idea is simply showing a real the mechanics of subscribing to a websocket data feed uh applying some technical indicators using talib to that data feed and then executing those buys and sell orders against our brokerage account uh using a simple set of rules and so this provides us kind of a hello world example of a similar setup a simple set of rules similar to most hello world examples are like a moving average crossover strategy and so this is an rsi strategy and we're going to buy and sell based on whether the rsi is overbought or oversold right and so that's great we're looking at this rsi oscillating on a chart but you know we haven't really talked about like how do you even calculate the rsi where does that number even come from and so i'm going to go even more in depth in this tutorial and draw a quick picture and do some hand calculations to show you how that's calculated if you don't want to dive into that much detail feel free to skip along but i'm trying out some different teaching techniques in this particular video and so i'm going to work on some diagrams here and illustrate how this number is actually calculated so let's talk about the relative strength index a little more and to start the discussion i'm going to pull up a textbook example an analogy that i got out of this book it's a technical analysis explained by martin pring pretty good book i'll put a link in case you're interested in it pretty lengthy book but i took a couple snapshots that i'm going to show on the ipad here so on the ipad here i have a couple screenshots from the book and this is on the chapter on momentum and so this analogy in the introduction i thought was pretty good so the concept of upside momentum is illustrated in the following example when a ball is thrown into the air it begins its trajectory at a very fast pace it possesses strong momentum the speed at which the ball rises gradually diminishes until finally comes to a temporary standstill the force of gravity then causes it to reverse course the slowing down process known as the loss of upward momentum is a phenomenon that is also experienced in financial markets the flight of a ball can be equated to a market price the price's rate of advance begins to slow down noticeably before the ultimate peak in prices is reached and then it goes on to talk about if you throw a ball inside of a room and it hits the ceiling it bounces back down and then on chapter 14 of the book on the first page it shows you the rsi formula this is all over the internet nothing specific to this book but i was just pointing out the formula the relative strength indicator was developed by wells wilder it's a momentum indicator oscillator that measures the relative internal strength of a market against itself and then we have this formula rsi equals 100 minus 100 over one plus rs where rs is the average of x days up closest divided by the average of x days down closes and i'm going to show you how to calculate that using ethereum price data that we're getting from these web sockets right now so we're going to go back to our program and we're going to get some price data and i'm going to write it down and calculate an rsi to show you what an oversold condition or value looks like on the relative strength index so by default we're going to be using the rsi 14 which is 14 periods and we're using minutes in this case now whether or not the rsi is meant to apply to ethereum data over the course of minute candlesticks that we're not going to get in that debate right now typically in the past they've applied this to daily time frames but i'm going to apply it to the minute time frame and show you how you would calculate whether something's overbought or oversold on the minute by minute uh time frame right and so what i've done here is got some data points that we got from our ethereum data and i wrote down the minutes so to calculate uh the rsi 14 right we need 15 data points actually right and then i have the closing prices here and so what i have is two columns here up and down and we're going to get the difference to show how much the uh price of ethereum went up or down on each uh between each of these candlesticks and so here from 44 32 73 to 432 80 this went up by seven cents right and then it went back down right by seven cents then next it went down by .03 next it went up by 0.16 and then it went down pretty strongly by what's at 0.67.67 and basically we just keep filling this in on our chart and i've calculated this in advance just to save a little bit of time so we actually had more down days than updates in this particular case and so i have the values of .01 0.2 and 0.18 and then for downward changes we have 0.07.03 0.67 0.04 we have .05 0.03 0.35 0.29 and .07 right and so you can see that obviously we have a lot more down days so for these 14 price changes we have nine down days and only five updates so it's very likely this might be oversold because it's had strong downward momentum like for instance this is a large change in price down and so is this one and so is this one right and so what we want to do is add up both of these columns and divide them each by 14. so for the sum of our updates i have 0.38.39 0.55 so that would be 0.62 and we would divide that by 14 and then for the sum of our down days i got a 1.6 so 1.6 which we'd also divide by 14 right so if we look back at our definition of rsi we have rsi equals 100 minus 100 divided by 1 plus rs where rs equals the average gain over the average loss right so our rs equals the average gain so the total gain divided by the period so 0.62 divided by 14 is 0.044 0.044 and then divide that by 1.6 divided by 14. 1.6 divided by 14 is 0.114 0.114 okay and then i can just divide both of those values point zero four four divided by point one one four and i get uh point eight three or point three eight six rs equals point three eight and so we do 100 minus 100 over 1 plus 0.386 right and then so if we do that we get 1 plus 0.386 so that's 100 divided by 1.386 which is 72.15 and then if we do 100 minus 72.15 we get an rsi of 27.85 that number is less than 30 and that's how we calculate an oversold signal and so on the other hand if on this column we had a whole bunch of updates that were really strong updates and the upward price momentum was a lot more than the downward price uh momentum then we would have 100 minus a smaller number and if we got a number over 70 that would indicate an overbought value all right so yeah that's how you calculate the relative strength index on paper and now what we're going to actually do is install ta lib since someone's already written a python library to calculate all these things for us because it'd be very tedious for us to keep track of all this stuff on paper i picture people doing this decades ago like calculating this on paper but there's no reason you know we have python to calculate these things for us we have python to analyze data for us so there's no reason for a reason for us to stare at charts all day this can all be automated so let's try to automate all of this trading for us okay i'm back in visual studio code and we've been watching all of the price data stream in from the binance websockets and when it's a clo the closing tick of a candle we've been capturing the final open high low and close data for that candlestick and we've been appending the final close here um into a list called closes and so you can see we accumulated a number of closes so now what we want to do is apply ta libs rsi indicator to this series of closes and get an rsi value so in order to do that we need to go ahead and first import ta lib so at the top here we've already imported websocket json and print and so we're going to import ta lib and numpy because we're going to need those for our calculations all right and then also we're going to set up a constant for our rsi that way we can tweak it so i'm going to say rsi period equals 14 and these are just the default settings that people uh normally use but we can adjust these if we want it to be more overbought or more oversold or we want to do a different uh period so i'm going to do rsi overbought equals 70 and rsi oversold equals 30. and those are going to be our various thresholds that we're going to use to determine whether to execute or buy or sell order okay i'm going to create another constant here called a trade symbol and we're going to be trading ethereum here and so this is the symbol from binance it that we figured out from our lat one of our previous videos and then we're going to determine a quantity that we're going to buy so i'm logged into my binance account so i can check the prices if you don't have a binance account for trading i'm going to leave a referral link if you want to support the channel and want to sign up using that link feel free to do so so in my finance account i say buy crypto and i'm just going to see how much ethereum happens to be so let me see what 20 worth is well i'll go to the symbol for ethereum right and let's say i want to buy a twenty dollars worth looks like that's about point zero four six so i'm gonna just say point zero five ethereum is the amount i wanna buy so i'm gonna do uh trade quantity as a value and i'm just going to store this and these are just going to be our settings for our bot and then we can tweak them if we want to adjust our bot's behavior so we have those uh constants in um and we're listening on the websocket already and so the next thing we're going to do is keep track of the number of closes so we've been appending uh the closes as they close we're appending it to this list and to calculate the rsi we're going to calculate the rsi 14 and so to calculate the rsi for over 14 periods right as we calculated on paper um you know the first just one or two closes isn't enough so we need at least 15 closes in order to even start calculating an rsi that we can use and so we're going to say first of all if the length of closes so let's make sure we have 15 closes first is greater than rsi period right so if we have 15 closes which is greater than our side period 14 then we can actually start using a ta lib right so we have a list of closing prices that we've been accumulating and we've converted them to floats in the latest version of the code and for talib to operate on this it expects a numpy array so we're going to take this list and convert it to a numpy array and so i'm going to say np closes equals numpy and you can just do numpy dot array and just give it a regular python list and that'll convert it to a numpy array of closes and then to calculate the rsi we can do rsi equals talib dot capital rsi and so if you didn't watch previous videos if you search python ta lib pull this up you'll see there's a number of indicators available and if you look at momentum indicators here and look at rsi you have this rsi and you just need to close and then the time period by default is 14 but you can override that so we're going to just use the default period and i'm going to pass it an array of closes so we're going to call the rsi function which is part of the ta lib package and we're going to pass it np closes which is our numpy array of closes and we're going to give it the default period or we can just pass it you know rsi period here and if we change the period to 10 for instance a 10 would go here which is good so then we'll start calculating rsi values so what this is going to do is calculate a series of rsi's values so once we get 15 closes we'll get one rsi value but on the 16th close we'll get the second rsi value and so forth so this rsi is going to actually be multiple values and i'll show you what that looks like in a second so let's go ahead and just print out the rsis that it's calculating right and so i'll print all rsis calculated so far and this will be a series of rsis and then what we also want to do is get the last rsi calculated right so the last rsi calculated is going to be the one we used in order to make our trading decision and so we want to get the last rsi and set it equal to the last value of this series and so we can use a negative index to start from the end so rsi negative one there is going to be our last rsi value and we'll print that as well so we'll just say print the current rsi is and dot format and this is just for debugging purposes and i'll say last rsi and then what we want to do is say if the last rsi is greater than our rsi oversold or overbought right if the rsi is greater than our overbought then we can print sell sell sell sell right and then we can also say if last rsi is less than rsi oversold then we print buy buy buy right and so we have a condition under which to buy and sell so there's one problem with this so far is if we were to buy when ethereum is oversold right if it's less than rsi oversold what happens if is uh what if the rsi is 29 and then the next minute it's 28.5 and the next minute it's 28 right it's still oversold each time around so we would keep buying over and over again but that's not really what we want to do for this particular strategy maybe there's another strategy where you'd want to do this but we want to just buy one time a fixed amount when it's oversold the first time and then if we're already in the position we don't want to keep uh keep buying more and more and likewise if it's already overbought you know we we're going to just sell what we have we're not going to sell it in pieces or anything so what we want to do is check if we're already in the position that way we don't need to keep buying over and over again because we don't have unlimited money we're working with we're just looking to risk about 20 bucks here right and so what we want to do is check if we're in position first so uh up at top here we're going to keep track of the state so at the beginning of our program when we first started we're going to say imposition is false right we don't own it and then over here if the rsi is oversold we'll first check if we're in the position right and if we're already in the position we can just say it is oversold sold but you already own it nothing to do right so there's nothing to do here otherwise if we're not a position then we want to buy right so we're going to leave a comment here i want to say put finance order logic here right and on the other side of the coin here when it's overbought if we're in position then we want to actually sell right so we'll plug in our logic for up so we'll say put binance order as cell logic here and i'll put by order logic there otherwise if we're on the position there's nothing to sell and so we'll just say print we don't own any nothing to do right so we'll say it is over bought but we don't own any right all right so we have some messaging in place and so we have the mechanics of this working and so now all we need to do is actually fill in the binance ordering code and so i'm going to run this real quick and let it run for a second just to make sure we don't have any errors which we do have an error so let me fix that so i forgot an equal sign and let that run for a while and make sure we're still receiving messages we're still receiving messages which is good and this should keep running so now the next thing we need to do is we need to pull in some uh the code to actually buy and sell crypto using the binance api and as we said earlier we're using the python binance package so if we look up python binance api you get this library here and there's methods for ordering and let's find those ordering endpoints so under account there's order endpoints and place an order right so it's client.createorder and now we're going to piggyback off our previous video if you followed along we built out this app.pi here and i'm going to pull it in just to show you what we did in previous videos if you want to learn more we actually built a web application where you could buy and sell crypto and we built a front end for showing real time charts and just to show that off real quick i'm going to do flask run and i'll post the source code for this as well and i think i already have it running actually so i'm going to run it on localhost 5000 we called this coinview and um here we were actually plotting the real-time prices of bitcoin so we called the application coin view it let us buy and sell crypto from the web it showed our current balances that we had in our binance account and were able to tweak some settings and so we could actually use this as the front end for this bot and we could enter in our rsi settings here and publish that to the websocket for uh our client to read and then you know we could tweak our settings from the web similar to how you can do uh with training view so this we're talking about how you would build uh that type of functionality yourself so this is the front end we are working with um and what i wanted to show you here is the actual source code so this is available in the repository i'll post a link below but we had in our app.pi we have a binance client and so we're going to copy what we did here since we've already written this code before so we need the binance client and we need these enums which are some constant values that we're going to use so i'm going to copy these two lines here and i'm going to put them in our program so i'm going to import the binance client and i'm also going to copy this part where we instantiated a new client and so we're just going to do this at the top here um we're going to create a new client okay all right so we have some constants we have a client object we're going to instantiate it and we're going to put a config.pi with our api keys and so we need an api key and an api secret so i'm going to do api key equals and api secret equals and i'm going to fill this in with my api keys from the binance site so if i go back to binance you can look at your account and there's this api management here where you can find your api keys and copy them to this config and i'll put these in here and not show you mine now that i have my api keys inside of my config file i just import config and then i can do just like this config dot api key and config.api secret and it'll use my api key and secret from this config file and then i'm not going to commit that to the source code repository but i'll put a sample config dot pi where you can fill in uh your own or i'll commit i'll commit it with empty strings just so you know um to uh fill it in there and then you'll notice i have this tl d equals us there and since i'm in the us and i'm using binance us it required this tld parameter here or it didn't work i forgot the error it gives but that's the reason that's there so make adjustments to that as needed so now that we have our client instantiated we're just going to make a quick function for ordering so we need to decide what that looks like so let's go back to the app.pi that we had for our other application where we actually create buy orders so we had this by endpoint that we you can use from the web and the same functionality can be used in a function from the command line and we can abstract this out and build our own little library if we want to but it's pretty abstract at this point because we're already using the python binance client and so our code can be reused inside of our command line program and so i'm going to make an order function here and we're going to have that order function accept a couple of things looks like we need a symbol right and since we're not using a form anymore we want that symbol to come from a function input so we'll have a couple parameters so to order something we're going to need a symbol we're going to need a quantity that we want to buy and then we need a side so whether it's a buy or a cell and we need an order type right and so we're going to call this function from our websocket client and we're going to give it the necessary parameters to buy or sell so the symbol is going to be the symbol that comes in when we call the function the quantity is not going to come from a form it's going to come from our function call so uh right now it's filled in with side by and order type market these are actually constants that come from this pi uh finance enums library and you see they already have a bunch of constants defined and some of those are side by side cell these intervals and so forth so that way they can define these string values all in one place and so we're just importing them and using them right here and i want to be able to make this function work for both buying and selling so i want the side to come in as a parameter and so i'm going to use side that comes in from here not the constant and then i'm just leave the order type mark it actually i'm just going to give it a default order type of market and then you can override it if you want to and so type equals order type and then so uh the parameters from what the websocket we're going to call we're going to say quantity simple side i'm going to change the order of these a little bit i'm going to say side so it'll be by and then i have the quantity and then the symbol that seems more natural to me so it would be like by .05 ethereum okay so that's our order function so client.createorder and let me just verify this one last time right so i'm gonna put these on one line so it's easy for me to read okay and so what we'll do here and let's wrap this in a try catch block that way if there's any exceptions from this we'll say the order failed so i'll choose try right and then let's just return true let's print out what happened so let's print out the order and then we turn true so we'll say print sending i'm going to print sending order so we're going to print a lot of debugging information because we're going to run this for the first time soon so i'm going to print sending order order equals create order with these parameters i'm going to print the order and return true if for some reason exception happens uh let's return false here okay matter of fact though we can return true at the end here so if no exception happens if all this stuff happens and we'll return true at the end if an exception happens we'll return false all right so that's our order function okay and so now all we got to do let's just get our function signature here and let's just call this from within here where we said to put our binance cell logic in by logic so we're going to say order succeeded equals order and then we're going to say side is uh side cell when it's overbought quantity is going to be the quantity that we defined at the top so we said trade quantity we called it so we'll say trade quantity and trade symbol which we have defined at the top and our order type is going to be market so we'll leave that as the default take off this colon all right so that's order succeeded so for in position and the rsi that was last is overbought then we need to sell so we're going to do an order cell or quantity of a certain symbol and that looks good and then if the order succeeded then we're no longer in the position so we can set in position equals false so so that we don't keep trying to uh sell it over and over again and then otherwise then uh it's overbought we don't own any okay that's good and then let's look at our oversold condition so if last rsi is below the oversold threshold we can do and and if we're not in position yet let's put our binance by order logic here so we'll say order succeeded equals order side by trade quantity trade symbol so we'll buy 0.05 ethereum and if the order succeeds we'll say in position equals true because now we own ethereum all right and i think that's all we need if there's no syntax errors we should be able to just run this program and have it buy and sell orders in our binance account and have it run overnight and come back and see if we made or lost money and make adjustments from there and we can just leave this running forever so i'm gonna stop the program i'm gonna run this just to make sure there's no uh syntax errors it looks like um it's running so it looks like there's no fatal errors at the gate and so i'm gonna let this run for a while and just see what happens and see if we can capture any buys and sells and so i'll come back and edit the video out if this runs for hours and hours it's possible it can run you know for hours and not trigger an oversold conditions so it just depends what happens i'm running this in real time i have no idea what's going to happen and so it looks like it's running and then we had a close of 428.29 and it's 9 40 pm pacific time on saturday and so we're receiving messages i'm gonna let this accumulate we need to get 15 minutes of candlestick data first before we even get a single rsi so i'm going to walk away and come back when this is further along so i'm back and i've let the program run for a little while it's after 10 pm and i'm looking here and you can see how this program has been running and it gradually calculated and stored a bunch of closes in a list here and we kept showing our closest ss list screw and then eventually it grew to 15 closes and luckily enough for us it didn't take us a long time to get an oversold condition it actually happened pretty quickly just a coincidence of when we started our program but you can see that once we had 15 candles it started calculating an rsi value and the very first rsi we got was 26.11 and it was actually an oversold condition all right so if you were watching really closely earlier i didn't know that noticed this until after this caught the oversold condition but the buy order actually didn't execute after the oversold condition hit and there's a reason why so if you look in our oversold code here order succeeded equals order and i had trade symbol twice when i needed trade quantity and then trade symbol and that's just a result of letting the autocomplete work and i hit tab too early and so it filled it in with trade symbols so need to double check that there and so i fixed that and ran it but unfortunately i can't show you the buy execute in real time just since because it actually didn't happen so we successfully detected the oversold condition but didn't execute the buy so what we can do right now is show you what would have happened real quick so i'm going to go into trading view here now that a little bit of time has passed and i'm going to show you uh the results and so it's actually this period right after 10 pm here ta lib actually calculated this was an oversold condition uh right at this point a little after 10 pm so we would have made the purchase here and then uh the overbought next overbought condition would have been after these powerful candlesticks up here and so we would have bought it around here and sold it around here and that would have been a profitable trade which is good however that doesn't mean that much right because i actually let this program run overnight so this is the next morning that i'm recording this right now and so i let this program run from midnight till 9 00 a.m and just went to sleep and let this execute trades while i slept to see how good our trading bot would perform right and so now that it's the morning i'm going to see what happened right and so i'm going to expect that i woke up this made thousands of trades and i woke up and made millions of dollars right is that what we expect i don't know let's see what happened all right so if i go into my order history in binance uh you can see what happened and also to show you uh that i really ran this overnight i can go to my editor and you can see where we accumulated tons of prices in these closes right and you can also see our messages like overbought but we don't own any nothing to do candle close and so we have hundreds of data points here and hundreds of rsis that are calculated right and so we need to see what actually happened right and so if we go back to binance let's see if we made our millions of dollars i'm gonna go to my order history starting after midnight and see what happened and you'll see that just after midnight we made a buy and at six a.m we made a sell so we made a whopping one trade that entire period and so we can go back and verify that everything worked correctly but there was actually only one trade and we actually lost money so you can see uh bit uh ethereum price was 426.68 here at 1241 and we bought it so it must have been oversold and then you can see it wasn't overbought till 6 11 a.m in the morning and then we put we sold it right and so we bought .05 ethereum that cost us 21.34 cents and then we sold it for 21 and 13 cents so that lost 21 cents right there and we also had a transaction fee so .02 right two cents on each side so 21 cents plus four cents so all that effort and waiting overnight and we lost 25 cents right but that's not really unexpected right we already back tested uh these types of strategies this short time short term rsi on its own in previous videos and showed you know this is not really a good strategy to use so why did we use it well we want to go through the pain of actually building something like this out and seeing how it operates and so we did achieve the goal the goal of building something end to end and studying our results and part of the process is getting a bad result and then tweaking that until you find what works for you so like the first time i ever traded a stock option for instance i just went on an app i clicked buy it a weed option or something like that and uh and i pretty much expected to lose the money and just that feeling of losing money made me actually pay more attention it like made me learn really quickly so then all of a sudden i it motivated me to learn what i was doing it motivated me to research all the terms and try different things by short short term option buy one that's in the money buy one further out of the money buy one that's longer dated and just kind of observe the price action and learn from my mistakes so what we've done is build a trading bot based on a simple indicator and it made a trade overnight while we're sleeping so we achieved the hands-free automation parts but we have not yet demonstrated a strategy where we made some money consistently we made money on the first trade but the overnight trade was a loss and one final thing i'm going to do is verify that this was correct was there only really one buy and one cell let's look at what happened in the price in rsi overnight so i'll pull up the chart and verify that our program was correct so that next time we run it we know that it's working so i'll go back to this chart and if you look at overnight starting on august 16th at midnight you'll see where the oversold reading occurs so this is the rsi bouncing up and down this is midnight right here and you can see indeed 41 minutes after the hour right you'll see that this uh price action downwards so you see all these red candles here going down it had a lot of downward price momentum and it got oversold and that's when we made our purchase right there a little above 426. right and let's see what happened why did we lose money here right it's oversold right uh it bounces but it doesn't get overbought and goes back it's still oversold but we're already in the position so we didn't we didn't do anything we didn't add goes back up never gets overbought again and then goes all the way back down and gets deeply oversold we're still in the position right so we didn't do anything and then if we keep going you can see that uh it kind of downward trends and just keeps getting oversold uh periodically and then just kind of bouncing up to the middle and so it never gets an extreme overbought reading and so this runs just all night and just bounces and oversold and so you can see ethereum last night at least uh kept selling off over and over again so it would come back up keep selling off and getting oversold again so looks like overnight if we look i'm zooming out right and so we started with ethereum at 428 and then it just gets oversold and it looks like it just starts this big downtrend overnight from about 3 a.m till 6 a.m it just keeps down trending and so there's never really a good entry point that would have worked uh using this indicator and so if you would have coincidentally started your program here then you would have got the entry point and felt like you were smart right and so you can see it never really gets overbought until right here uh after 6 a.m and that's when our sell order triggered so it's overbought and then it got overbought over and over again and then we just said we're already overbought there's nothing to do so indeed our program was correct we left it on overnight there was one oversold reading where we entered and then one overbought reading much later when we exited the trade and we actually exited as a loss because it kept downtrending and got more and more oversold and so by the time it got overbought again it was actually under the price that we originally bought it at so that's the lesson there i hope you learned something from studying what can go wrong in with this particular indicator and also i hope you learned how to build a bot from scratch because now that we have ta lib installed and we're reading price data you can plug in any indicators you want or any other algorithms that that you want or you can plug in your you know some proprietary data sets and all these things and all you got to really do is swap out the strategy because we already have the brokerage execution part we already have the real-time data part and we already have some libraries for performing a technical analysis so you just need to decide what strategy you want to take so that's it for this tutorial i know it was a long one i was experimenting with some new things like different cameras different lighting and ipad and drawing and also i'm even trying a new uh video editing software called screenflow i was using camtasia before so i'm just putting this all together and experimenting a little bit to find the best format for teaching you this type of content and i hope you liked the video we covered a lot of material we learned how to connect to websockets from the python websocket client we retrieved real-time data from binance over web sockets we were able to use ta lib which we've discussed to apply technical indicators to this real-time data and we were able to in real time execute buy and sell orders using the binance rest api and the python binance package so we covered how to build a trading bot end to end and you know this video was a bit over an hour long so congratulations if you were able to reach till the end or you can watch this in chunks later on so i'm going to post this final source code to this to the github repository at github.com hackingthemarkets so you can follow along with this video using the final source code and if you want to support the channel you can sign up for binance using the referral link below and also just subscribe to the channel and follow along follow me on twitter i'm going to be posting new tutorials every single week and my goal is to make all this content better and better over time and to get better at editing these videos and communicating these ideas so that other people can learn how to do this themselves so uh thanks a lot for watching and stay tuned for the next video
Info
Channel: Part Time Larry
Views: 226,801
Rating: 4.9695816 out of 5
Keywords: cryptocurrency, ethereum, python, binance, api, tutorial, trading bot, automated trading, algorithmic trading, websockets, real-time data, talib, ta-lib, technical analysis, rsi, overbought, oversold, tradingview
Id: GdlFhF6gjKo
Channel Id: undefined
Length: 70min 18sec (4218 seconds)
Published: Sun Aug 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.