Project Intro Video: How to create a Cryptocurrency Trading Bot in Node.js
with elliottminns|6 years experience|Swift
Project Intro Video: How to create a Cryptocurrency Trading Bot in Node.js
Sep 29, 2017 | 7:13 PM4:57
Session 1: Setting up our project and getting Bitcoin prices
Oct 3, 2017 | 6:58 PM56:56
Session 2: Exchange Prices
Oct 5, 2017 | 7:05 PM59:21
Session 3: Trading Algorithm
Oct 10, 2017 | 6:56 PM51:53
Session 4: Transactions & Deployment
Oct 12, 2017 | 7:07 PM54:26
How to create a Cryptocurrency Trading Bot in Node.js
- Cryptocurrency Basics
- Project length: 3h 48m
This tutorial will go through the process of creating a trading bot for Cryptocurrency using Node.js. The tutorial will focus on three Cryptocurrencies, Ether, Bitcoin & Litecoin. First, we’ll look at how to create a wallet for each of these currencies using the Coinbase API, and then add code in to successfully create transactions, finishing with a simple yet well known trading algorithm that also considers trading fee’s and 1 day rolling historical data. Once we’ve created our service, we’ll set it live with test money to see how it does, followed by real cash at the end.
This tutorial will go through the process of creating a trading bot for Cryptocurrency using Node.js. The tutorial will focus on Bitcoin as our currency of choice. First, we’ll look at how to create a wallet for each of these currencies using the Coinbase API, and then add code in to successfully create transactions, finishing with a simple yet well-known trading algorithm that also considers trading fee’s and 90-day rolling historical data. Once we’ve created our service, we’ll set it live with test money to see how it does, followed by real cash at the end.
What are the requirements?
- MongoDB or NOSQL
What is the target audience?
- If you want to learn Algorithmic trading
- If you want to learn Bitcoin
- If you want to learn APIs
- If you want to learn Node.js
- If you want to learn MongoDB
The project outline explains what you will learn in each session
Looking at the starter point for our node project, obtaining a Coinbase API key and making our first request to the API. Getting a Coinbase API key Using the Coinbase API key with the coinbase package Obtaining Bitcoin Prices Setting up a daemon using Forever
Now we begin to look at pricing data, we want to store prices for a period of time and collect a price index at a specific interval. We can also use historical data provided by CoinDesk to start off our algorithm. Store Bitcoin prices in our database Retrieve a rolling window of prices given a date Find min, max, median & mode in our rolling windows
In this session, we'll discuss trading algorithms and the various methods used in the industry, then implement one or two of the most used algorithm for our use case with Bitcoin. Once that's done, we'll run our algorithm against some historical data and see what happens. Looking at various trading algorithms Implementing one or two commonly used trading algorithms Testing our trading algorithms
Once we're confident our algorithm is producing results we'd like to see, we'll start to look at applying transactions using the Coinbase API. We'll also take into account Service fees in order to make sure we are running at a profit. Creating and retrieving a Bitcoin Wallet Costing service fees Applying our algorithm into the desired buy and sell price Registering our transaction with coinbase
It's time to deploy! In this session, we'll load our trading bot into Docker and then begin to run it in the cloud via Digital Ocean using a virtual wallet. Creating a Docker image Uploading onto Digital Ocean Running our app with a virtual wallet & currency
We'll review the results of our virtual wallet and see if we made a profit or loss, if we made a profit, we'll start up with some real money and see how it does. Look at the results of our test data app and see how well we did Run with a live wallet if all went well.
00:00:00-00:05:00 Hi everybody! Welcome! So today was the first session on cryptocurrency trading, building all bought using nodejs. So today we're going to go through setting up the project, anything's ready having a look at the initial get help footage we got set up. And trying to set up an API key with Coinbase and also create a request to get off first they coin balance. So today we're going to start by... So today we're going to start with going onto GitHub and cloning our project. So, if I share my display, here we go. So, URL should be with the project, first of all, we want to basically, just clone this and then we'll go through what the actual project consists of. So, inside this is the typical new JSR Rio. Go into session 1 and just copy that over. So, let's have a little look inside. So the app is pretty simple. Our entry point is index doc - and this just requires our app and calls start on it. And what does that do absolutely nothing at the moment? It just requires a configuration and has an empty method for starting. The configuration itself is just the most a little simple script to allow us to get environment variables either from the process, which is basically what you'll have in your bash RC, or you can set it yourself, or our configuration JSON, which is here. So, inside the configuration - Jason we've set up a few environment variables already. The first one is the Coinbase API key. This is what we'll be using to enter interface with Coinbase, so we'll need to set that up, which we'll do in a minute. The API secret the more and also the idea goes secret which is also relevant to do with the API key. We also have the mongrel URL and Mongo database name, which aren't needed at this stage but they're in there just for some, for the future. We also have our database file as well, which we're not going to be using this session but this is just basically allowing us to connect to our database when we need to. So, if you give me just a second I'm just gonna load up the chap. You're sure I can see everything. Okay, perfect. I don't need this pop out. Okay. Cool. So, the next thing we want to do is we want to use Coinbase to populate our configuration environment variables. This allows basically to interface of Coinbase and create wallets. Bitcoin buys and sell Bitcoin and get price data which is what we're looking to do this session. So, we have a look at the package so Jason we see there are some dependencies already set. So, we have Coinbase which is the official package provided to us by point base, which will allow us to do these API calls. We're forever which we may or may not touch in this session and we have Mongoose, which is just our database / DN wrapper. So, here we are on the coin base API, so we go to developers' corn-based comm. That's where you'll be able to obtain an API key, which we'll need for in for communication. 00:05:00-00:10:00 So, I'm getting started and click start building your first app. It basically goes into some detail about what the API can't do, but we want to go to my apps. So, here I already have a key created, but what you guys were wanting to do is create a new API key. Once you've created an account, you'll get a verification. And I allow you to create a new API key. My key. Won't let me show, because you've got to keep it secret. But I've added mine into my bash Darci so that I can always yeah access it without broadcasting it to the world. So, once you have an API key set up a relay different permissions. Currently, the moment it's got all the different wall opposite permissions, which is what we'll want to use within our box. So, make sure you've got that set up so let's go back. So, I guess right now is the best thing is, well, let's show the app running. So, I use something called node one, which you can install by doing, well, let's make sure we actually install the app first. So, I'll get a lot of our dependencies down. So, we're using a VM to install all of our packages and then we want to use no mods. So the load model is basically a nice monitor that allows any time of change to reload the app anytime there's a change located on the files. So, if we save this you can see it just breeze starts every time. So, I guess the first thing we want to do is probably... See if we can communicate with the coin base. First, you want to get the corn base package. Let's have a look at NPR for the documentation. There we go. So, we want to create a client on a secret. Just give that a guy. So, we're using es6 s, so I'm going to try to make sure you use most of the features. Using our configuration we can get our API keys. So, let's have a look. So, the example they gave his client took get accounts and but what we want to do is we want to have a look at Bitcoin price. So, let's take the code that we have here and see what happens. So, there we go. That was pretty simple. So here we just managed to get the buy price of Bitcoin and we managed to love the total amount. Now, because we're using es6 we really want to be using promises instead of using callbacks as things can get a little messy. So, we're going to do is we're going to move this into his own little file. So, let's call this price. So make a module called pricing. This is gonna basically, handle getting us a price. So, let's start here. Okay. For exports, the real one gets price and get a price that's probably the best thing to do. 00:10:30-00:15:00 So, again we will ever want to be returning promise rather than using a callback. Let's just copy this code for the moment and see what happens. Cool! So, we can make a promise by using your toes. Now, I'm not sure this is gonna Okay. Looks like it is in Utah. Cool. So, this would turn this cool back into a promise, which is what we want to do. We can do the time client up to get a nice. Let's do the client. Alright, let's never going back to our app tile. We can remove some of this junk. I'm gonna do ... I should crash that Center pricing. It's what we call it. Okay. Absolutes do you get five prices because this is what we are getting. So, interestingly, it doesn't seem to like this. That's because we're using the wrong things every day both sigh. Let's try that. Okay. So, it doesn't look like promise fights gonna work. So, let's see if we've got promises to go into the actual API itself doesn't look like it. That's a little annoying but it's not... isn't the last thing. So we can just that's a look soon. So, yep! So, it needs sort of can't sink to do it this way. So, we'll remove the utility is this. I'm older data would do returning auger old-fashioned promise. . . . 00:15:18-00:20:00 So, what if we go wrong? So, I don't have a promise objection. That's the look and see what happens. Alright, so it looks like... There's walking. Let’s see what we get here. It's better to cut it backward. Okay. So, everybody nevertheless pricing. So, it's tidying up our app and extra chairs and it allows us to basically, stuff things, you know, keep things nice and nice and easy. If you ever want to change from corner grace, we can change from Chloe's face because it's all based in this area. The Apple where we start trading may be session three, session for those wondering and shrink hi-so cool asking. Initially, we're just going to be getting the project set up and making sure that we can actually track prices. We can't start trading now. What we want, to make you make some predictions about when is the best time to start trading. So far we've managed to get price and data we've managed to get connected to Coinbase but if we're going to show you we need to know selling data. So, the next thing we want to do is get so far, it is so close. So damn nice coin paste as most what promises. Let's try again. So, we've got a buy price. All right, so we need to supply an API version that's one of the warnings we're receiving. We'll figure that one out. Like actually, let's see what it says. But hey, we can definitely see that the sale price and the buy price are slightly different. The nice thing about this as well as we also get in the currency for those who are lucky enough to be in the US, this is great but we may have to get currency for in a different place. Because the issuer's currency also changes as well as Bitcoin. So, ideally, we want to make sure that we are using the same currency everywhere. So, what we can do is we can have a currency object, which equals BTC DSD. 00:20:00-00:25:00 And again this concept. Let's have a look and see what they say about burning. Again that it says anything about versioning, which is interesting. So for the moment we're gonna ignore that warning. And then I'll take another look later and figure out what why. So, as you see every time we save the app reloads and we get our price data. You can see that the price data is changing. So, let's do a con so... Let's log it out a little bit differently. Alright. Cool. So, you'll notice that our sell price has actually gone up, but our private buy price has not. So, we're going to want to be looking for things in the algorithm that are fluctuating like this. If the buy price is slowly getting closer to the sale price that's always a good time to buy. Because our sell is technically going higher. It's also a good time to sell and things like that. So, the next thing we want to do is start tracking this data. So, well, actually the next thing we'll probably want to do is we're going to make this data available to people who aren't using US dollars. So, let's see what happens if you change it to the euro. So, the euro does not change the price. Let's just make sure that this is correct. Okay. So, this isn't working. So, what we can't do is do this. . . . . Why did I choose MongoDB? So, I chose MongoDB one because it's something I have to miss familiarity with and see because it's unstructured data. It's a little bit easier to get going quickly. As we're not doing any relationships or anything like that was only going to be looking at mid max values in and medium values, which we'll talk about later. So, we're gonna be looking at like the rolling averages of basically, stock law the Bitcoin price. So, we didn't need something heavy like SQL or anything relational as it's not needed. We basically just need a persistent store and MongoDB is great for that. It's also really simple to get up and running. Of course, feel free to plug in any database you feel most comfortable with. Okay. So, now, if we try this figure... They're gay. So, now we've got a different price. So, we can do something nice. Yeah. Well, that we want a fur for printing purposes to be nice but first of all we probably want to just resolve the data. We don't want to result in the entire object, because we don't care about any warnings and things like that. Now, this will crash. Alright, here we go. So, because I am British uh-huh even though I'm not in the States. So I'm going to change my currency. . We got great British pounds. The rest of the tutorial will focus on your state. Until it's something Goods live because my account will be tied to British pounds. Okay. So, what's next? Well, it's all well and good getting the price every earn once, I really know the time we run the app, but really what we want to do is we want to have this running consistently. Booker is a way of deploying applications via containers. It's a really simple process people get scared of it at first, but it just simplifies the whole process of building on your machine trying to then put it on a server and it doesn't work for some reason or another. So, docker allows you basically to create an image of your app, which will run on any docker instance. For more information, if you get a docker com, it will give you a nice overview of what docker is. But we'll be getting to it in a later session. So, you all understand a little bit of how it works and the benefits as well. All right, so this is what we're going to be looking at forever. So, we can either use forever here or we can use it in our index. But we'll refactor it out. So far of all basically allow us to run the app consistently. If you look at the guy for forever. Okay. So, we want to use it within our current problem from the command line. So basically, we could start it as a server, which isn't really the case in our usage, because we're not a server we are a process that's running in the background, also known as a daemon. So, here the daemon seems right. However, it may not be right for us because it requires a file. Now, I think, the fire will be an output, which is probably quite important for us. So, maybe we should just try to start the moment before we demonize it. Okay. So, I'm just going to find a nice example of how to do this. I think I found one actually here from 2010. Let's go something a little bit more recent because that's a long time. So, he's probably armed we want this is a nice way of installing it a service but it's not what we're looking for. We're looking for a way to basically keep it running forever. 00:30:00-00:35:00 Now, it could she, because our database instance also keeps it running as a daemon, which is quite nice. So, rather than getting too much into forever which wasn't meant to be in this session, we're just going to start the server. So, if we do database connect, this will keep our service running, although it should. Some reason it doesn't. So, let's figure out why. Say, okay. Let's try this. So, we're going to go through making a wallet in another session. So, you should be able to create a wallet programmatically and using the Coinbase API. We will also be able to download... Get our wallet back for future use. So, you don't need to make a wallet you do need to make an account with Coinbase through in order to get API apps, API access. So the problem here is we have a database. So I was running in circles there. Cool. Okay. Let's put this back to how it was. Forevers no defined not a problem. Cool! So our app connects. Basically, I said before we want to put this at the beginning. Great! So, we basically have here that we're connecting to a database, which is just keeping the process alive. So, it's a really nice way to make demon I was going to try and work and use forever but the problem with using forever is we actually don't need it. And it's just gonna have another layer of difficulty. Now, if our database fails the app probably will fail as well. So, we'll use forever when we apply with docker. So that it automatically brings to process backup what we need to. So that'd be more in the deployment session. So Iraq is alive. Now, we want to put we get prices I say every two-three minutes. Every... probably... it probably every minute but we can probably do it every 30 seconds. So, how do we do that well the main problem we have is we don't have a... We don't have a socket connection, so we're gonna have to make requests every 30 seconds. So, we've already needed to have some sort of loop. Now, the best thing or the simplest thing is we can do set time out surely this process location. 00:35:00-00:40:00 Never sure whether it's the time first or not. Okay. That is cool back then delay. So this will basically during 40 milliseconds. So, what we want to do here is you want to actually have a little method for this. So it can't get better prices or will do the main loop. Let's call it that. That's probably the main thing we want to do. So, let's call a wait main loop. The fact we're just cool. I mean like it's the main loop what this will do is. Wait for 40 milliseconds and chorus of a gap. Let's make this amazing function. Okay. So, we go to work there but it was we're running a little bit too fast, every 40 milliseconds. So, that's a bit too much, so what we probably want to do is let's go for every ten seconds. So... Let's try that again. So this is good. Nearly from where this is. It's going to wait ten seconds before it does it. So, ideally, we want to do it the first time we load up. So, let's remove that and do this. Okay. So, what we have here is we have basically every 10 seconds. This is going to query and try and see what the current buy and sell price is often for Bitcoin and this is the corn the corn-based API and we've wrapped it up nicely... in this little main loop function. Let's get rid of this tidy that up. so this is what the aim of this session was today, was to basically try and get the buy prices things I doubt from the coin base API. We managed to set up a key when you do it yourself you can either change the key here or you can send the environment variable. And to set an environment variable and they should just do exports my... Let's do a little bit high. So, if you want to set an environment variable when this leads you to do something like exports. So, I use I have a few sessions. Okay, so this is working nicely. Don't worry we're not seeing any changes in the picc line pricing. So, what we probably want to do is... 00:40:00-00:45:00 Well, let's have a look and actually see what bitcoin is currently trading out. So, one Bitcoin according to Google is for two nine seven US dollars. Yeah. That's about right so that's usually around the middle between a buy and sell price. So, the highest demean highest value at the moment or highest vanilla... see, well the most volatile is probably Bitcoin it's also the highest valued, so yeah the highest valued is definitely a coin. And that's kind of like trading it around well as you can see four thousand... four thousand three hundred US dollars, which is a lot but you can buy Bitcoin in increments. So, you don't have to spend four thousand three hundred dollars just to buy your first piece of Bitcoin. If we look at the graph of Bitcoin you can actually see when dusk. You can see how much it's grown over a year. So, just a year ago it was trading at six hundred dollars. Now, it's trading at 4300 so that's a huge growth in a year, but as you can see it's very volatile. And it's become more volatile later on. So, here it goes up and down. So about a month ago we were looking at about nearly 5,000 and it's not recovered to that yet but you never know. It might do, it might not do. So, what we want to be doing is with our algorithm is basically trying to harness that it is volatile to see that we can buy, when it's good or buy when it's cheap and so when it's expensive. And we'll be looking at one or two average of one or two days worth of data the fun with buying the selling is you also do incur fees. So, we want to be taking these fees into consideration. And from what it looks like here but if the coin is not changed that much. So, it's probably a good time now to I guess maybe save this data to the database. It's something we need to be doing and it's something before what you want to do. Now, getting the buy price is nice but we're making to API calls here when we don't actually need to. So, I think we do get all price well a nice new method to do that. Because ideally, we want to be keeping these prices matched together. So, let's have a look at the wallet. Look at the price data again. How many people go with... 14 people. Great! Okay! So, what we really want is the spot price. Just spot prices basically, it's kind of that Center price that we're looking at. So, it's in between the buy price and the selling price. So, my assumption is we can do that by doing this. 00:45:00-00:50:00 Let's come at this out. Number two. So, this is, hopefully, get us a spot price and they haven't, unfortunately, to try here. All right. So get spa price doesn't exist. But it doesn't exist here, so let's do a get spot price. All right. Cool! Times do not find that makes sense. Okay. So, we're also getting the spot price as well. Now, the spot price should be roughly between these two. Three equip the math we do. Suppose that died huh. That's 42 I'm 907 plus 45 4.13. Yeah. So, that's roughly around the spot prices in between. Give or take the buy price on the selling price. So, these are all three of these are probably that makes use of full information we can have for our algorithm. We just want to make sure that we're gonna be storing these for future use. Now, if you can see, it doesn't seem like the Bitcoin trading is actually changing that much. Let me just have a little look at Coinbase. See why that is. Yeah. It's still roughly the same. Go say more. Before we're going to do is we probably want to reduce our API calls, because we will be very limited. So, let's see if we can just get prices I might be the coolest thing. . . What do you mean by lava? Where would we... What letters are and the map-request for making? Oh... Okay. Yeah. So, we want to lower the number of requests who are making just because we're making quite a few API's may they usually rate limit you anyway. 00:50:00-00:55:42 So, if you make a lot of cools, they'll briefly block your key. Because it's a lot of strain on their service. So, we really want to make sure that we're just being friendly neighbors and we're not hammering their service too much. But we're you know getting enough data that we want from them. So, it's a little about the sort of like you know, making sure that our app is gonna work for as long as it possibly can. And it doesn't run into any errors while we're running because the most important thing is we wanted to run. We don't want it to crash or suddenly be blocked by the API when we want to make a cultural trade. So, let's see if this will get us all were the prices. You know, what I'm just gonna look at this. Okay. So get fries is another function that's mine. So, it might be that we don't always get awkward about the prices which would surprise me. All right. Yeah. So, interestingly, it doesn't give us all the prices at once. We'd have to... how nice this by the person because it's not a coin based face. I kept on quite sure. Okay. So, yeah! We can't get over the prices in one go, which is fine. What we do probably want is we want the spot price for the buy price I'm going to the sale price. So, that's probably fine. The spot price is the main thing we'll be using because that's grant market place I'm a fireplace and sell price I guess it may be slightly... slowly different for that. But what we'll be doing is we'll probably be selling some sort of Delta that we want to be used to make sure that we actually get our value from. And using this Delta we can basically adjust our ideal spot prices or ideal sell price. So, we'll probably be fixated on what the sale price is we'll be tracking the spot price mainly. But we'll only sell when our target self price is reached and where any buyer on the outside of write price is huge. Cool. Let's see how we're doing four times. There are another five minutes. So, that's basically up to this session we've managed to create our interface for connector... for getting different prices of the high price sale price and spot price. Let me just check what we're doing in the next session. If I can even find out. Here we go. So in the next session, we'll probably be looking at storing these prices in the database and doing some queries within the database to actually get all the different values that we want. So today we managed to set up a demon but we didn't use it forever, which is kind of cheating but at the same time, it's not that we need to do using the actual forever stuff and we'll move to deploy them. So, yeah. That's it for the first session. I will upload this... yeah, I will push the completed work to Github. So, if you guys want to take a look you have a little play around a little bit of documentation just to show that we give you guys a little bit of a clue. Cool. So, yeah! Yeah. Thanks for watching! Again I'll be back on Thursday for session number two. I look forward to seeing you then and, yeah, have a great day!
Invite a Friend
Education Ecosystem referral program offers you a great opportunity to earn additional LEDU by inviting Friends!Invite a Friend