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 wannabe arbitrage experts out there. We’re going to looking at some ultra-simple
python code for grabbing the last buy price from 4 exchanges, well, 3 exchanges and one
kind of exchange. That’s Bitstamp, BTC-e, Bitfinex and Coinbase which is not officially
an exchange. And we’re going to be using this thing called
APIs and since it’s our first video on API’s, we might as well just quickly define it. An
API is an application program interface. So basically just gives anybody on Earth access
to companies’ data that is not traditionally set up in a 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
Google’s maps. Okay, so you don’t need to go to Google’s
maps page to access their data and in fact a lot of other applications, Airbnb, Uber,
Yelp, etcetera can all access Google’s map data and include Google’s maps on their
own websites. This is all done through backend API’s. And
here’s sort of an example of some of the code that might be stuffed into the website to
grab Google’s 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 API’s are
private. So banks have private API’s, Google would have private APIs for calling proprietary
data or protected data. And then they have public APIs. Today we are
going to be looking specifically at 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
from certain applications to others. And it’s a very particular format but it’s very easy
to read. Let’s take a look at a 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 alphabetized.”
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,
BTZ-e, Bitfinex, Coinbase and Kraken. I left Kraken off my list so let’s go add them
back there. No need to leave them off their grate. 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 so that every 2 minutes, it’ll update but if you
change the time here to 1 second, it’ll update every second so you can keep calling
all the prices. But I’m going to keep it at 2 minutes. And so let’s just run it.
In a few seconds I’ll have the last buy price from all 5 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 ran this little average right? I came down here, I took all
the 5 prices right here and I divide it by 5 over here and I get the average price right
here all the way down to 6 decimal places 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 live
Bitcoin price and then the US dollar value and multiplied 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. 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.
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 it’s the last buy price. You get the date, you get
the current bids, you get this kind of weird average price over time, you 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. 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 time stamp
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 switched it back to the last. So really, all we’re doing is we’re importing
time so we could have it loop every couple of minutes or every couple of seconds. We’re
importing JSON so that our software Python code would read JSON really well.
And then we’re importing request which gets us the ability to grab this information from
the API URL. Okay, and then I stuff that JSON right in to 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,
you’ll see that these numbers have changed, right? It went to 649. So these are live data
feeds, okay? So now, let’s stuff in the BTC-e ticker.
Don’t want that quote at the beginning that would be… fail. Okay and here’s out BTC-e
ticker. 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, they’re 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 up to be the same thing. And that’s
it. So here we go. We got BTC-e Litecoin ticker. I’m calling
that. Bitfinex, all the stuff will be here on my GitHub repository. Here’s a lot of
API pages where you can learn about different APIs.
They write a lot about how their APIs work. One of my favorite pages on API is on blockchain.info.
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 they’ll run you through
all sorts of examples of how those APIs work. Now we’ll be doing more API 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 it a little bit differently but in the end, it’s pretty
much the same stuff. Then, there’s my while true so there’s
my 2 minute counter that I’m going to loop it around. And here’s my 2 minutes, a hundred
and twenty seconds down here. I actually call my functions and stuff them into a variable.
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 slap them onto floating point if they’re
not and then I print them all out. And then I do my average where I add them all up dividing
by 5 but you could also do 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. We always here that Coinbase gets its pricing from Bitstamp
or something like that. We’re just going to print the difference and let’s make it
clear, “Difference between Coinbase and Bitstamp.”
And with Coinbase, you could 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 in to your API above but that should
give me the price right there. Okay, so let’s run my software and of course we’re getting
new prices because everything has been updated and here’s my difference between Coinbase
and Bitstamp. And that’s a difference right now is $7.82.
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 right here. The ones that changed the most
and see what changes. 649.98, 639, 649.96, 639, I don’t even remember. Let’s run
it again. 649.96, 639, this definitely changed. Maybe
it’s our Coinbase price changing. So we get our live feed again so I can go down here
now and just to make it pretty live, I’ll do it every couple of seconds and then we’ll
run it and just kind of let it sit there. I’ll pull this window up and you’ll see
my live data feed. Of course you can pop these numbers in the grass or do whatever you want.
You can see them move all over the place. This is your little API data feed and here
we go. There we go, would have been nice if I put
a space in between there. Let’s do that before we send this to our GitHub. Print.
Print. Maybe put something cool in there. Print. I like this thing right here. Print.
Okay, and you see it there streaming and now you run it again so it’ll 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
2 seconds. If you got really speedy internet, maybe it’ll go a lot faster for you.
But I’m getting something here, what, every 4-5 seconds. And that’s your data feed.
Those are your APIs. The code would be on GitHub.
We’re going to be doing lots more APIs later. Remember that you could go much further. You
can actually do buy and sell by using private APIs where you got to insert your private
keys. Bitfinex has private keys. You can do buying
and selling automatically from your own website or whatever thing you built. You can also
turn the API’s inward towards Bitcoin so using Bitcoind and we’ll be doing a lot
of that. Hope this gets you started into the really
fun world. I think it’s amazing stuff, the API’s. And there it’s going again. So
please remember to subscribe, comment, like. Do whatever it is that you do. We’ll catch
you at the next video.