Video - Bitcoin 101 - Calling All APIs - Coding Live Price Data From Bitcoin

Confused about APIs and JSON? Looking to run an arbitrage business or build a data intensive site? Well this video is for you. This is the first of our videos looking at programming calls for exchange data. In just a few lines of code, we grab the last price from four exchanges (Btc-e, Kraken, Bitstamp and Bitfinex) and pseudo exchange extraordinaire, Coinbase. James walks you through the first steps of building a live feed of this data.


Hello, this is James D'Angelo and welcome to the Bitcoin 101 Blackboard series.  Today we've got a little video that looks to benefit all you want to be arbitrage experts out there.  We're going to be looking at some ultra-simple, Python code for grabbing the last buy price from four exchanges.  Well, three exchanges and one kind of exchange.  That's Bitstamp, BTC-e, BitFinex and Coinbase which is not officially in exchange.  And we're going to be using this thing called APIs and since it's our first video on APIs we might as well just quickly define it.

So an API is an application program interface.  So, basically just gives anybody on earth access to company's data that isn't traditionally set up in the way that it might be on a website.  And the most standard example that you hear when people talk about APIs is the one for Googles maps.  Okay, so you don't need to go to Googles maps page to access their data and in fact a lot of other applications Airbnb, Uber, Yelp etc can all access Googles map data and include Googles maps on their own websites.  And this is all done through back in APIs.  And here's sort of an example of some of the code that might be stuffed into the website to grab Googles maps.

And again, this is Google making this available.  Okay.  So, this is a very modern thing, allowing others to come in and grab your data and use your data in sort of a controlled fashion.  That's an API.  And some APIs are private so banks have private APIs, Google will have private APIs for calling proprietary data or protected data.  And then they have public APIs.  Today, we're going to be looking specifically a public APIs to grab some public data from these exchanges.  Okay.

The only other term that you might not be familiar with is Json and Json stands for JavaScript Object Notation.  And it's just the format for how the data is transferred from certain applications to others.  And it's a very particular format but it's very easy to read and let's take a look at it.  Very basic example of Json, right.  So here is how the format is done.  You've always got these curly brackets and then you sort of have the bigger headers employees and then very obvious you have first name colon John, last name Doe and so these applications can talk to each other and they can say, well give me your employees, give me the first name of all your employees.  For example, give me the last name, alphabetize and you can do a number of data manipulation once you get the Json information from any database.  Okay, so that's it.

Now, we're ready to go.  Let's jump inside the code.  And this code all it does is it looks at Bitstamp, BTC-e, BitFinex, Coinbase and Kraken and I left kraken off my list so let's go add them back there and no need to leave them off, they're great.  Kraken right here and I know that they're doing a lot of good stuff right here in Massachusetts.  So let's put them right back up there.  And what it does is, it calls each of these through these python functions right here and then it just delivers their latest price.  And I've got it in a while loop and so that every two minutes it will update but if you change the time here to one second it will update every second so you can keep calling all the prices.

But I'm going to keep it at two minutes and so let's just run it.  In a few seconds, I'll have the last buy price from all five exchanges.  And here we go.  We've got the Bitstamp price, USD boom got that.  BTC-e, Coinbase price, Kraken price, BitFinex price and then, I even run this little average, right.  So, I came down here.  I took all the five prices right here and I divided by five over here and I get the average price right here.  All the way down to six decimal places.  Okay, but that's pointless.  And for fun I even grab the BTC-e Litecoin price in Bitcoins and then I converted that to dollars.  And you can see that's in the code right here as well.  So, I took the lite Bitcoin price and then the US dollar value and multiply them together to get the dollar price.  Okay.

So let's look at how this code works.  And it's really simple stuff.  All you're basically doing is you're calling the API address for that exchange.  Okay.  So, all these exchanges work to put their data in accessible format for you to grab and you can take the address from this code.  And you can copy it and you can go to your web browser.  And you can just paste that right in there.  And you hit return and you'll get the latest price right here.  So, you get the high price of 659, the last price 648 so that's the last buy price.  You'll get the date, you get the current bids so you get this kind of weird average price over time.  You've get the volume so how much is being traded, the low price and the ask price, all there in this big Json format.

And what we do is because we're doing something very simple, we're just grabbing the last price.  Okay, and you'll see that here on our code where we're just asking for the last but you could turn around and grab anything.  You could turn your code into taking the bid or the vwap or the volume or the timestamp just by typing that in here instead so I could ask for the bid, right or I could ask for the buy price.  And when I run my code I'll actually get the buy price.  But we switch it back to the last.  Okay.  So really, all we're doing is we're importing time so we can have it loop every couple minutes or every couple seconds.  We're importing Json so that our software Python code read Json really well and then we're importing requests which gets us the ability to grab this information from the API url.  Okay.  And then I stuff that Json right into a variable right here and then I just query it for the last value and then I just stamp and repeat.  Okay, so we did that for Bitstamp, we can now do it for BTC-e, the Bitcoin price and just for fun we'll grab that right here and we'll go to oh, just for a second before we move, let's just take a look at the last price and I'm going to update and you'll see that these numbers have changed.  Right, went to 649.  So these are live data feeds.  Okay.

So now it's stuff in the BTC-e ticker don't want that quote at the beginning that would be fatal.  Okay, and here's our BTC-e ticker so let's blow it up so we can read it really nicely on our screen.  And oh, it doesn't wrap around that's not so great, but here we've got the high, low average volume last.  That's the one we're going for buy sell, their pretty close that's kind of exciting.  Updated, so when was it sold at exactly what time.  Then the server time is actually when you're actually calling the server.  Those times can often be different but when there's a lot of trading going on they turn out to be the same thing.  And that's it.  So here we go.  We've got BTC-e Litecoin ticker.  I'm calling that BitFinex all of this stuff will be here on my get hub repository.  Here is a lot of APIs pages where you can learn about different APIs.  They write a lot about how their APIs work.

One of my favorite pages on APIs is on  They've got this great collection of APIs.  That you can call to get all sorts of data and you can see view documentation on any one of these.  And it run you through all sorts of examples of how those APIs work.  And we'll be doing more APIs calls in the future.  But let's get back to our code and just finish this up.  So, I've got Bitstamp, BTC-e, Litecoin, BitFinex, Coinbase, Kraken which came out to be a little different because when you make the request you've got to actually tell it a bit of information about what you want.  So, you have to write a little bit differently but in the end it's pretty much the same stuff.

Then there is my While True so there is my two minute counter that I'm going to loop it around.  Okay, and here's my two minutes 120 seconds down here.  I actually call my functions and stuff them into a variable.  Right.  And I make sure them floating point basically I want to be sure that there are numbers that I can do calculations on later.  So, I slab them into floating point if they're not and then I print them all out.  Okay.  And then I do my average where I add them all up dividing by five but you can also do other fun stuff here, right.  You could, for example, let's print -- let's see what the difference is between the Coinbase price and the Bitstamp price.  Okay, we always hear that Coinbase gets its pricing from Bitstamp or something like that.  We're just going to print the difference.  Okay, and let's make it clear.  Different between Coinbase and Bitstamp and with Coinbase you can also get a spot price which is more of an average price between the buy and the sell.  It all depends on what you type into your API above.  But that should give me the price right there.  Okay.  So, let's run my software and of course we'll get new prices because everything's been updated.  And here is my difference between Coinbase and Bitstamp and that's the difference right now its $7.82 cents, so about a 10%, bigger than 10% difference.  That's pretty big.

And let's just run it again.  Let's keep an eye on these numbers up here the ones that change the most on and see what changes 649.98, 639, 649.96, 639.  I don't even -- I don't even remember.  Let's run it again.  649.96, 639.  This definitely changed.  Maybe it's our Coinbase price changing.  Okay, so we get our live feed.  And again, so I can go down here now just to make it pretty live.  I'll do it every couple seconds and now we'll run it.  And just kind of let it sit there.  So make, pull this window up and you'll see my live data feed.  Of course, you can pop these numbers into graphs or do whatever you want you can see them move all over the place.  This is your little API data feed.  Okay and here we go.  There we go.  Would have been nice if I put a space in between them.  Let's do that before we send this to our get hub print, maybe put something cool in there, prints I like this thing right here, print.  Okay.

And you see it there streaming and now let's run it again.  So, to look a little prettier.  There's my little bar every few seconds.  Of course, it's taking time to ping all these networks, it's using my really crappy internet so it takes more than every two seconds.  If we got really speedy internet maybe it will go a lot faster for it.  But I'm getting something here what every four or five seconds.  And that's your data feed.  Those are your APIs, the code will be on get hub, we're going to be doing lots more APIs later.  Remember that you can go much further right, you can actually do buy and sell by using private APIs.  We have got insert your private keys, BitFinex has private keys, you can do buying and selling automatically from your own website or whatever thing you build.  You can also turn the APIs inward towards Bitcoin.  So, using Bitcoin D.  And we'll be doing a lot of that.  Okay, so hope this gets you started into the really fun world.  I think it's amazing stuff the APIs.  And there it's going again.  So please remember to subscribe, comment, like, do whatever it is you do, we'll catch at the next video.


Written by James DeAngelo on June 10, 2014.