Plotting live bitcoin price data - Tkinter GUI development series p. 9
Key Takeaways
Plots live bitcoin price data in a Tkinter GUI graph
Full Transcript
Hey everybody, what is going on? Welcome to another Python programming tutorial with Tkinter where we're making this trading application for Bitcoin. Uh where we left off, we were just basically orienting our code uh to support the next change that we want to make which is uh incorporating a live graph of Bitcoin prices. So uh you need to make sure that you've got pandas and numpy installed at this point and you need to make sure the pages are um you've got start page and bttte page and uh that's basically it where we need to be at this point. So we come up here um and we look at our animate function. This is basically where the core of our code is going to be. So the animate function works with mapplot lib where it updates um information uh through mapplot lib but we can also use this to update a lot of things. So the animate function actually can update um our tkinter as well. We can throw tkinter code in here and treat animate um to update our tkinter code. And even though tkinter does have an update function for itself as well but I see no reason why we want to you know have two separate updating functions. So, we'll probably put most of this stuff in animate. But anyway, this is where the core of all of our data crunching is going to happen. So, this will be just a quick example of how we might uh do something like this. But let's go ahead and get started. So, we've got animate. Um, and basically this entire bit of code here, this was just for sample data that I could show you guys live data. Um, deletes. Now, we're actually going to do the real frigin deal. So, the first thing that we need is a data link. Now, I just showed you a moment ago. Well, not well, a moment ago for me anyway. Uh, this is the API. Uh, like I said before, yeah, that's the username and password. It's not my main account. That's actually the account that we'll be using uh here. But if you want to try and guess the password, have at it. I think there's uh like $20 worth of Bitcoin in there. So, big money. So, anyways, um this is the account. Uh and what we're interested in is the last trades. So, we're going to be looking for uh like we've got method info, ticker, depth, boom, trades. So, we go to trades. And the reason why we want trades is because that'll give us historical data, too. So, if we just clicked on ticker, ticker is only going to give us uh the high, the low, close. Well, it's not going to even give us close. There's no such thing. But the high, the low, last price, the current volume, um the last update, that kind of stuff. Um but we want more than that. We want to be able to populate a graph right away. So, we would actually go to trades and then we would click sample request, right? We could click on this one right here. And this is just a list of all the last trades. I think it's probably 50. But as you can see, you can modify this parameter uh to be up to the last 2,000 trades using the get parameter limit. So, as you can see here, we get this is basically the um the type, right? So, BTC to USD, that's the only type we have. So we would we're going to be using JSON because this is JSON format. So we would come into BTCUSD. Then we see it's whether it's was a bid or an ask. And these are um actually cleared prices. So So these are all um trades that really did occur. And then we can come down here. So that was all bid. And it turns out that really we've had almost no asks. That's quite interesting. Um that'll be interesting to visualize here in a moment. Um so just a bunch of bids. uh I guess just a real increase in price for the most part. So anyway, uh and then you've got bid, you've also got volume and you've got uh the time stamp. So we can use all these to generate a graph. If you're not familiar, this is a Unix time stamp. You'll see a whole lot more of that as we go. So uh let's get started. So first of all, the data link is what? So we're going to call this data link and we're going to say data link equals and basically we can just copy and paste uh this link, right? We can just do that. But the other thing too though is we're going to add a parameter. So as we saw here, the parameter is limit. So if you don't already know, the way that you add parameters uh would be with a question mark and then limit equals 2000 for example. And then if you wanted to add more parameters, you would actually use the and sign and then you would say uh more parm equals whatever um just for for you guys to know. So uh that's it. That's actually the only parameter I believe here. Oh, it actually says the default is 150. Um, but anyways, the max is 2,00. So, that's what we want. More data, more better. So, anyway, data. So, the data from this request is going to equal URL.est. Let me move this up a little bit. URLIB.reest URL open. And then we want to open the data link. Now, if you're following along this part of the tutorial and you are in Python 2, this little chunk of code here, including what we just wrote, is going to defer greatly. So, um it's been a while since I've written a URL lib 2 command, but it's very much it's a lot simpler because there's no difference between strings and bytes in Python 2. In Python 3, there are. So, uh we have to do the conversion there. So normally I think it would be something like url lib 2 url open and then you would put the data link and then you would probably just do read and that would return to data in string format what you want something like that. Um but this is not a Python 2 tutorial but just to get you on the right track. If you have any serious problems I can probably help you out later. So once we have that again this is in bytes so we have to we have to uh decode that. So then we would say data dot readall and then we want to do decode and what are we going to decode and that's going to be in UTF8. So now uh now we're ready to load this with JSON and the JSON string. So we're going to say data now equals uh JSON.load S for load string and that would be data. So now we've done we've done that. Now we can since we know uh the only um if you're not familiar with JSON I guess probably what I'll try to do is for one I'll put a link to my pandas tutorial series in the description if I remember if I forget someone remind me then I will also put a link there's a I have a a small like five minute JSON tutorial. Uh so if you're a little confused about how that works basically works the same way that we would like slice lists. Um, so for example, it's almost like this is a list of lists. Like think of it that way. And so or even a d a dictionary is a better way to to to call it or better thing to call it. So think of this as a dictionary. And so the key would be BTCUSD and the value is basically all of this. But then um then the value is a list of lists, let's say. And then so then we would say BTCUSD type. And then if we said ask, it would be anything with a type of ask, right? And then that would return everything. So um that might be really confusing. So let's just show what I'm talking about. So now we would say data equals uh data and then the element of uh BDCUSD. So that's this value uh right here. So when we say data BDCUSD that is corresponding to this block of code that goes all the way down here. Okay. So that's what data BDCUSD is. Um, now we're going to use pandas to crunch all this data. So the well basically it'll be 2,000 rows of this much data. That's kind of a lot of data. Um, and pandas does a really good job of of crunching it. Plus for tick data, we're going to do a difference between bid and ask. So um, that too. So data equals data BTCUSD. Uh, then we're going to say data equals PD. Data frame. mind uh the camel casing here or I guess it's not officially camel casing. I'm not really sure cuz you're capitalizing the first letter, but anyways, note the casing. Uh, PD dataf frame. And then what do we want to do that to? Data. So now data is a is a Pandanda's data set. Um, so then what we're going to say is the buys because we want to plot buys and sells. So we're going to say all the buys equals data and then we're going to say and then in brackets here, what you do is you throw kind of like sort of an if statement, but uh you'll see what I mean. So uh or I guess ai not a conditional a comparison operator I suppose. So buys equals data where data uh data type and that is corresponding to here right. So where data type equals what? Well buys are where we have a bid. So someone made a bid to buy. So we're type uh equals um bid. So that's where what buys are. And then conversely we would say sells equals basically this exact same thing only instead of bid it was an ask. So the price that these people are asking. So that's what bid and ask is if you're not familiar. Now, uh, we need to take that a little bit further and we need to convert this Unix timestamp into a, uh, what happened? I Okay, we've got pan picture of a panda here. I pulled the wrong wrong window. I'm like looking at that. What? Uh, see these are Unix timestamps and mapplot lib does not recognize a Unix time stamp. You have to convert it. Um it luckily it recognizes the various numpy times um as well as I think it's like it's got like a NPL format or something. Anyway, uh we're going to convert that. That's why we needed numpy. Um also you'll find numpy is used with pandas. So you need numpy as well. So anyways, uh we're going to say buys um and keep in mind this buys became a pandas set because data was pandas. So buys is a Pandanda's data frame of data where the data type equals bid. So now we're going to say buys uh date stamp. So basically we're going to convert this column because keep in mind uh or actually we're going to add a column. My bad. So we're adding this column buys of date stamp and we're going to say date stamp equals basically timestamp converted. Okay. So, the way that we're going to do that is buys date stamp uh equals MP array and then we're going to say it's an array of uh the buys and then timestamp. So that's buys timestamp references the pi uh the buys data frame timestamp. So basically a data frame is like a CSV basically uh you know an Excel spreadsheet basically and you've got timestamp column and then under that column you'll have you've all these times um so so buys equals MP arrays buys timestamp but then we're going to do something tricky here and we're actually going to say as type and then we're going to convert that to date time 64 s Now um that gives us a date stamp. Finally uh we're going to say by dates equals buys and actually we'll put this in in parenthesis buys date stamp and then dot to list and then um that will be our like buys um string that we're going to throw at mapplot lib. You'll see what I mean in a little bit. So then we're going to do basically the exact same thing. Um here let's just go ahead and uh we'll take this copy paste and we need to change basically everything to cells. So buy day stamp by time stamp as type and then finally cells and then we'll call this cell data. Okay. So now we have our data sets. Now we need to do a clear. Then we're going to do a plot date. That's mapplot lib information there. We're going to plot dates. Now we have um date. Oh, you know what? We need we need to not buy data. We're calling this by dates and then sell dates. So we plot date and then we're going to plot uh let's say by dates and then we plot by um and so by dates is this right? that we converted to list. So by dates is a list. Get back over here. Come here. Uh is a list of these timestamps that were converted to the numpy date time 64. Uh so we're plotting that as our date. And then um that so that's the x parameter. And then the for the y we're plotting by. And then what do we want there? Well, we're going to go ahead and plot the price, right? That's the y that we want. So that would be like 3618. 868 for example. So by uh price and then we want to plot with a color and then uh later on maybe a label but we'll we'll get to that in a moment. Uh and then we're going to basically do the exact same thing. So a.plot_ate sell dates and then sell uh price like that. And uh later on we can add a legend in all of that. Uh but otherwise uh we're all set. So let's go ahead and plot this before we go make it pretty and make sure that we've we've done what we need to do. Uh okay, we got an error. JSON object must be string not HTTP response. Let's see what we've done there. Close out of this. Uh read all.deode UTF8. We did URL open the data link. Let's see JSON data. And then we convert at this point here. Hold on now folks. Let's see what have we what have we done wrong here. Data link data dot. Oh, I see. Okay. So, what we did is uh this is not a basically data.all just it just did it. It didn't assign it to data. So, this needs to be data equals data readall. Um, common common mistake to make a lot of times where you think you're you're modifying the variable by doing that and and you're really not. Um, so anyways, let's let's try that one more time. I hopefully that'll be it as far as errors are concerned, but we'll see. Buy is not defined. Buy is what we want, right? So, I got a buy is not defined error. Um, I kind of covered over the first error. Sorry about that. Uh, so this is the name error. Buy is not defined, meaning it's not defined. So, we come down here. We see that we're calling this buys sells. So add an s and s. Try one more time here. Let's see what we get this time. No red code. That's good to see. So we'll hit agree. And uh we get our sort of a graph. This isn't quite the graph I wanted, but it is a graph. Um and we can see it is actually in time. A couple of things. One, the size is inappropriate. Uh, I have noticed that when I, uh, increase the size here, uh, while I'm filming, for whatever reason, it like flashes black and stuff. It's really crazy. It's not what's happening on my screen, but it happens on the camera for whatever reason. So, I won't do that to you guys, but obviously we need to make the graph a little bigger, but we do see time stamps down here. We've got buys and sells, and we just saw it update just now. Um, that's a lot more buys and sells than I swear we saw in this. Um, I guess we're starting to get a lot of asks now, but just a few moments ago there was like only buys and I'm just not seeing that in this graph. But anyway, I'll take their word for it. Uh, anyway, so here's a graph. That's exciting. Obviously, I don't really like the idea of scatter plots on on CFBTC. I actually do um have buys and sells typically marked this way, but on such a um high frequency graph, I don't really see the point. But like for here, I do mark the buys and sells as uh you know, scatter plots, but I actually kind of like it as a line more often. But uh anyway, that I guess you could you could stick with that kind of stuff as well. But um anyway, if you want to keep it that way, go for it. Um I'm going to go ahead and and show us how we can convert that to a line. This just looks really messy. Like it's visually really hard for me to connect the connect the dots literally here. Um part of that is because of the the size of the graph. So like uh we could probably remedy that by zooming in or something, but even then it's it's really hard. So anyway, in the next video, what we'll do is we'll make sure this is a proper line graph uh as well as um we'll add labels to it and we'll make the size a little better um because this isn't really proper. So anyways, uh stay tuned for that. If you guys have any questions or comments on this video, uh feel free to leave them below. Otherwise, um, let me let me close this. And really, most of our code was basically right here, this massive function. So, I'll leave it here. If you got lost somewhere, pause it, try to copy that code. I will also put this code up on pythonprogramming.net. So, check that out if you're lost. Otherwise, as always, uh, you can leave comments, questions below. Thanks for watching. Thanks for all the support and subscriptions. Till next time.
Original Description
In this Python 3 with Tkinter programming tutorial, we cover pulling live bitcoin prices to plot in our tkinter GUI graph.
Tkinter GUI TutorialPlaylist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
Related Reads
📰
📰
📰
📰
How I use python to save hours every week
Dev.to AI
What Are AI Software Solutions and How Can They Transform Your Business?
Dev.to · upwork floating infotech
AI Shorts generators you can actually edit (not a black box)
Dev.to · Reel Mint
I Wanted To Automate Website Testing Without Writing Hundreds Of Scripts. AI Changed The Approach.
Medium · AI
🎓
Tutor Explanation
DeepCamp AI