Sessions is temporarily moving to YouTube, check out all our new videos here.

Trading Cryptocurrencies, Forex, Commodities Stocks and More Using Node.js

flyingunicorn222 speaking at London Node User Group in May, 2017
Great talks, fired to your inbox 👌
No junk, no spam, just great talks. Unsubscribe any time.

About this talk

We'll look at the libraries and tools that @flyingunicorn222 has developed over the last couple years after getting into trading Bitcoin and Ethereum (which turned out later into trading all sorts of things based on technical analysis and a bit of flaky statistics).


- I will jump the introductions so I'll go directly to who owns bitcoin here? Does anybody own bitcoin? I heard about bitcoin a really long time ago when I was working with Andrew at a really nice company, and I was like, "What is this?" And I couldn't really understand. It was really weird and complex, and I really wanted to get into it because it's like, "Okay, it's digital currency. "It's really interesting." Basically if you don't know, blockchain is the database that runs one kind of database, and bitcoin was the one that introduced it, and the block chain is the database that runs behind bitcoin, and what it actually is is a distributed ledger. It's a distributed database where every nodes, the so-called miners, they keep mining transactions, almost like mining gold. Maybe I'm explaining it badly butwhen it comes down to, there is a bunch of people with a copy of the database in their computer, and they are fighting to do the next insert on the database, right? So there is a bunch of people with a whole copy of the database, which means everybody knows what is there, and it's public available contrary to the bank ledgers for instance, and once they get the right to do the next insert on the database, they're gonna be able to go there, and insert the next bit of data, which is called a block, and this block is attached to the previous block. That's why it's called blockchain, and it's almost like Git but it's not Git, it's much more crazy version of Git because people are building currency, and they are building a lot of really complicated stuff inside of it, and one example for instance is ethereum, which is basically building a blockchain where you can build whatever you want into the blockchain. You could basically write bitcoin inside of ethereum, and have your own bitcoin running on ethereum, which is another blockchain. It gets really complicated butwhat it really means in the end is if you had for instance a voting system, let's say the ballot, and everybody would vote on the blockchain, by the time the election finishes, everybody would be able to audit their own vote straight away so by the end of the election day, everybody would go into the blockchain, and would say, "Yes, my vote actually counted "for the right politician," which is something that we don't have today, and in messed countries like United States, you would have to be a lot of money to recount the votes, which is absolutely nuts because they can go to Mars and to the moon but they cannot count the votes for free in real time, which is a funny thing I find, and blockchain basically brings us solution for this. You would be able to audit a lot of data, and more importantly, you could also do peer-to-peer transactions without middlemen so if you want to send money to your girlfriend in China, you just send some money to her, and she can open a blockchain, she can open a wallet on the blockchain pretty much like anybody can open an email, which is also amazing because if you try to open a bank account now, you would have to prove who you are, and tell where you live, and how much money you earn so basically banking is not for everyone, and the blockchain is. It's a global market, it's a global database, and everybody can open their account. So imagine how many people doesn't have an account today, and don't have access to the banking system, boom, that's sorted. You go to a website or if you're a nerd, you download a command line too, and you open your account, and straight away, you can make peer-to-peer transactions without a middleman so it goes without saying that one of our biggest weapons against corruption and nasty banks and things like this, and everybody should get into it, and tell you mama about it and your sister and your brother and everybody. So that was a bit of my religion talk. So basically I got some bitcoins, and I got really fascinated about ethereum, and the markets were going really high, and I was buying bitcoins, and I was earning a lot of money with them, and I was really happy about it because the market was running 24 hours, seven days a week. I did not have to look for job. I did not have to answer emails. I did not have to answer my phone. I just was making money without any clients. My client was sitting down on the computer and trading the market, which is an amazing feeling of freedom. You're not jobless anymore if you can make money on this. Of course it's not easy at all but you can get really deluded and eluded, and lose a lot of money as well. It's really risky but there is some logic to it, and basically when I started, I was just basically staring at the charts like this, and I would be like going to Reddit like Reddit EthTrader, for instance Ethereum Trader, and I would be reading nilch, and I would be like, "Oh wow. "It seems like ethereum's gonna go up "so I'm gonna just buy some ethereum," and then it will go up and you buy, but the thing is once you buy, it's not like you're making money straight away. You have to sit down on your chair and watch the chart, and if the chart is going down when you bought it, you're losing money, and you have no option other than sitting down and waiting, or giving up on your trade, which it's really hard on your feelings and in your head, and it can mess up you pretty much so when I started, let's say I would buy something. Does anybody know how to read charts? It's like this. Basically you see all these green and red rectangles. They basically mean the variation of the price of a currency against other currency so here is the price of ethereum measured in bitcoins, right? Let's say when it say the euro is going up, oh the pound is going down. What does that actually mean? It probably means the value of pounds compared to dollar is going down but maybe I get something else, it's going up so when you're putting a price on something, you're generally putting a price against another currency, right? So here on Poloniex, which is like an exchange where I can go, you put your bitcoins, and you can trade it for a lot of other crazy currencies, and a lot of people doesn't know but it's not only bitcoin that exists. There is actually hundreds of coins that exist, maybe thousands by now, and each of them like you can see you have bitcoin, you have ethereum, you have ripple, NEM, litecoin, ethereum classic, dash, monero, and they all have their own blockchains. They all have their implementation. Some of them are fork of bitcoin, some of them are not forks of bitcoin, and it gets really crazy but some of them are really amazing and life-changing things like ethereum for instance. Once you understand ethereum like, "Oh my God, I can write some codes on my money, "and then put my money somewhere, "and then my money can take decisions without me." It's like, "Oh my God." You can't do this on real money, right? With ethereum, you can literally write code, and put your money inside of the code. Deploy your money to the blockchain, and your money can do stuff without you. It's like the money of the future. No doubt that's like how we're gonna live in the future maybe in 10 years, maybe in 20 years. That's the new money, and that's something really interesting to be involved with, and a really amazing time to be alive because this is proper nuts. Let's come back to the graph. Basically what this means here, you have like periods of five minutes or 15 minutes or 30 minutes or two hours or four hours or one day. Let's say I choose to see period of four hours, and I want to see the last month of data, right? Basically here for every rectangle, it means four hours of transactions on this exchange. I will change to one day so you get a better resolution. Here is the last month of trades on ethereum. When you see a green rectangle, that actually means the price went up, and when you see a red rectangle, it means the price went down. You have this thing here called the body like each of those rectangles and black lines are called candlesticks. Each candlestick have a open price, a close price, the highest price and the lowest price, and what that means is for instance here, let's say I'm looking at the one-day chart so if we get this day here, you'll see that here down at the bottom of the green, you see the price here on the right like 5104, something like this. Can you see? Yeah, like right where my mouse is. At the bottom of the green is where the price opened on that day. At the top of the green is where the price closed the other day. You can see that generally the next candle starts right on the place where the previous day closed, right? The black line, which is called a shadow, shows. Is it still working, yeah? The black line shows all the fluctuation of the price so even though the price opened at the bottom, the price actually went a bit down where this black line is, and actually went a bit up as well higher than the close price but at the end, it ultimately closed at the end of this green rectangle so if you get a red one for instance, this one, it opens here so they open here. So here, the price of this day, which is eighth of May, opened at 5850, 0.05850, and it closed at 52... 5256. If you get the next day, you'll see it's a totally different one. You see that the shadow, which is the black line, it's much taller than a actual body of the candle, and what that actually means here is that the price opened here, and it went all the way up, and it went all the way down, and then ultimately it closed here so this day, there was a big variation during the day. The price went all over the place, and then closed here. If you get this day, you can see the price went up and down but actually closed very, very close to where it actually opened. That's the very, very, very, very basics, right? Ultimately the first thing I must say it's like having bitcoin, ethereum is not about making money with trading. I think just trading is a really interesting subject but if you just buy bitcoins and ethereum, don't quote me on this but theoretically, theoretically in the future, it has grow up for reasons that I can get into details later like how the banks steals your money while you're here for instance, which doesn't happen with bitcoin. So let's say there is two ways of making money in certain markets, right? One way is what everybody knows about investment, which is you would buy when the price is low, for instance here, and when the price goes high like here like a couple of weeks later, you would sell it at a higher price so let's say today it's 11th of March, and you bought one bitcoin worth of ethereum, and then ethereum goes up, up, up, up, and then what happens? The price of ethereum now is much higher, and now that amount of ethereum that you bought, which was like maybe 1,000 ethereums or something was worth one bitcoin but now it's worth two so now you could sell and bank. You make one bitcoin of profit, which translates now to, one bitcoin is today, thanks, God,$2,470. For those who don't know, it more than doubled the price this year, and compared to last year, it's about four times the price like last year, I remember some bitcoins going for $600 so you can imagine how cool it is to have your money in here, right? Like there is no investment anywhere else that would give you this return, and there is no other investments that's closer to coders and technology people than blockchain investments like if you'll invest in euro and blah blah blah, you're like you have to understand how Europe is going, like you have to hear Theresa May, and like oh my God. You don't want to do this, right? What you really want to do is listen about technology, and see how people are innovating financial markets, how people are making the money of the future, and how people like us will be able to destroy a lot of bad things that happen in society like corruptionand fake ballots, right? So going back to the chart, there is another way of making money, which most people doesn't know, and it's actually pretty crazy when you understand how this work, which is you can sell, this happens on any investment. This is pretty standard, right? You can sell something when it's high so let's say you would come here, and you don't have any ethereums whatsoever, right? You have just one bitcoin on your account, and you're gonna come here, and you're gonna sell at 56. Basically you sold something you don't have, which people that invest knows. It's called a short in the market, and when the price goes down, you buy it back so basically what happens here is when the price goes up to 0.56, you sell 1,000 ethereums for instance, which you don't have, and you're gonna do this because the system automatically lends you so you're basically renting some ethereum from somebody, selling this ethereum that you rented from somebody at a higher price. When the price goes down, you buy cheaper, and pay the rent back to the person that rented you the ethereum, which is absolutely nuts but it is standard, and the banks are doing this, the governments are doing this, a lot of people are doing this, and profiting from other people losing money, and that's very standard so this is called short in the market so let's say you started here, you buy back here, you pay back your loan, and yeah, you made some money out of the markets going down. So for instance when there was a big crisis in US, and the house market crashed and everything, there was loads of banks actually short in the market, and making money out of the crash so people are struggling. They don't have any money. The crisis has come, and they have even less money but make sure the banks are profiting on this, which is really not nice to know. Okay so at the beginning, I started by buying ethereum, and selling it there, and then I would spend like loads of hours. I'll get to the coding part as well. I would buy it here, and it would go high and high and high, and I would sell here but what would happen was I was getting really nervous, and the market is 24 hours and seven days a week, and that was really, really hard to keep up with your life when you're losing money, right? When you're earning money, you wake up, you're happy, everything is nice, the sun is shining, you like eating burgers in Shoreditch but when you're losing money, it's definitely not a cool thing to have in the back of your mind while you're doing other things like you're hanging out with your buddies, and you get your phone, and like, "Oh nice, I'm losing money." It's not cool. And there was also like opportunities that I lost because I was sleeping, right? So I was like, "You know what? "I know how to code so maybe code can help me a little bit," and I was like, "Okay so how can I deal with this using code?" The first thing that came to my mind was like, "Yeah, I'm gonna use some Node.js, "and I'm gonna build something cool," so I had to go into technical analysis, and understand how technical analysis work, and also I had to understand how this chart translates into code because now you look at this, and you're like, "Okay, great. "I want to code on this," so how does this translate to code? It's like what is this chart in code? Even though it's quite obvious when you know after you learn, it doesn't really make sense before at least for me so basically what I learned is that this crazy chart here, it actually just translates to one thing that everybody knows here how to deal with, which is an array of objects, which is like how God make all the planet and stuff. So basically I will run an example here. Yeah, I use CoffeeScript, okay? So I will go here. I'll get some code, and I would do is I will, I'm not making music now, I'm gonna go to the Poloniex, which is the exchange where I can buy ethereum and bitcoin and stuff, and I'll go to the API, and I'm gonna check how to get the data. You see there is like, there will be a returnChartData method so returnChartData, yeah. So this returnChartData method on the API will basically give you the options that you have on the UI. You can say, "Give me candles of four hours, "and give me the last one week of candles," right? If I go back to my VI, if you go here, what I'm doing is I'm using this plnx, which is a library made by a guy that abstracts the request you have to do to the API. I'm using moment to calculate dates. Four minutes. Yeah, cool. So I'm gonna go fast. So Poloniex, get moment. I get lodash. I get my payload here. Say I want to get BTC against ethereum, and that's the period, that's the start date, that's the end date, and I'm gonna call this, and it will give me all the chart data of the last 20 days. So I'm gonna just put a return here. So basically if you guys didn't see, that was my payload to get my start date, my end date, the currency pair I want, the period I want. This is period in minutes so this means four hours. The start date, which is say 20 days ago. Again the UNIX timestamp start and end, returnChartData, and then here I'm gonna trace the last 20 days of four hours, candles so basically it just gives me an array of objects. I get the date, the high value, the low value, the open, the close, et cetera, and then I can then do really smart things with it, which I will not go super crazy now because I'm running out of time but basically one way of doing technical analysis here to know if the price is going up or going down, what you would do is you would get for instance one thing that's called a simple moving average, which translates to basically a for loop that just gets the price, and put it all together so let's say if you're gonna do a simple average of the price of the last let's say 40 prices, you get the last 40 prices, do a for loop, you get those 40 price, divide the total by 40, and you're gonna get a line like this, and basically this blue line here now is the average price of the last 40 candles so at every candle that shows up, it will get the last 40 candles, put the close price of them together, divide by 40, and plot this line. Sounds complicated but it's not complicated. It's just a really simple for loop, and what this basically mean is that you get an average price over time, and if you look coincidentally, if the price is going up, it has to cross the line up. There is no other way the price is going to go up if the price doesn't cross the line up, and if the price is going down, it gonna have to cross the line down. It's just how average works, right? You have an average of the last 40 days, and then the price eventually goes down the line of the last 40 days. You know the price is going down, and I'm not saying this strategy is profitable. That's definitely the first strategy you need to learn so basically what you can do is you get this average here, which can be easily calculated with a library like ta-lib, which is technical analysis library for Node.js but honestly it's just a for loop, and basically this ta-lib will give you this line. You don't even have to do the for loop yourself, and basically you can do things like on every four-hour candle, you can check. Is the current candle price below the average price? Yes. Was the current candle price over the average price? Yes, it was. So great, the price just crossed the average line. You know the trend is changing, and now you have a good idea of whether to sell the stock or to sell ethereum or to sell bitcoin or whatever so that's a really, really simple strategy. Just to finish because I could talk about this forever, I'm gonna just show, which is really interesting, and that's something that wasn't really available like if you go tomorrow, and you call like a broker, and say, "Hey, I want to use Node.js to sell and buy Euros. "Can I do it?" They kind of like first ask what is Node.js? The second thing, they're gonna laugh at you. They're gonna ask your address, they're gonna ask you to put that amount of money there. It's nuts. If you open an email one day, and you try to open an account there, you're like, "Man, this doesn't work." I will show here how simple it is to actually. Okay, sorry. I'll just run one node script, and that's it. I'm gonna just long ethereum here. I just run my coffee, and hopefully there is no way. Yeah, that's it. It placed my order. I basically said, "I want to buy ethereum at this price, "that amount of ether," and yeah, my Node.js just bought it. I don't have to open the website. I don't have to do anything, and then if I'm smart, I can make the script better and make money. That's it.