Graphing OHLC Candlestick - Tkinter tutorial Python 3.4 part 26

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

This video tutorial series covers how to create a candlestick graph with OHLC data using Tkinter and Matplotlib in Python 3.4, including adding custom labels, titles, and indicators such as EMA and SMA, as well as dynamically updating the graph and resampling data for different time intervals.

Full Transcript

what's going on everybody welcome to another tkinter tutorial video part of our mega series making an actual trading application for Bitcoin so in this video what we're gonna be doing is actually generating this candlestick graph the basic one we don't actually have the indicators or anything like that yet but we're gonna be working on those in the next few videos my dog just decided to barge it and sorry about that anyway let's go ahead and get started so we are in our animate function we just wrote a bunch of this code except exception ize bubble blah so extra enter delete deletes and you should be in line with this elf here now what we're going to do is we're going to do sea sticks for kl6 and sea sticks is gonna equal candle stick underscore ohlc and this will be first of all what's subplot do you want to plot that too we want to plot them in subplot so it'll be just a then the data itself and this is OLC and then it's dates open high/low close so you would say something like this with a panda's data frame so ohlc and then you would say inside of that a list so it's a list within this data frame and the first data would be MPL dates comma then we will have open comma pi comma lo comma close and then outside of these brackets you do period values so it's just the values of each of these little bits now we're gonna say width this is the width of the candlestick so if you have a lot of data you want the candlestick to be very narrow otherwise they're gonna run over each other and if you have very little data you want the sticks to be kind of so you can actually see them you know so width equals candle wit which we set long ago and then you can specify color up and color down so these are like the colors of the candlestick so if price goes up for that candlestick the color will be green in general and then if the price goes down the color will be red now we're using light and dark so we're just gonna say color up equals light color that's actually camel case so light color like that and then color down equals dark color so kind of a long line but that's it so those are that's how we actually plot our candlesticks now any good graph has labels so we're gonna do a dot set underscore Y label and that is going to be price next up we have a special question for hobbies and slope is quite the troublemaker we're gonna say if exchange equals will be a to dot fill underscore between we're gonna say volume data MPL MPO dates and then starting point is zero and and oh you know that we're making a mistake here who OB does not have volume data that is why we actually asked if exchange does not equal who will be a to dot fill between volume data and pl dates between the zero because we should not have negative volume then volume data volume and then a face color face color equals dark color and that is going to be our volume data and then we're gonna do a to set underscore why label if we have it and we're gonna set that as volume that's if the exchange is not equal will be and we've already predefined the a two subplot so we don't really need to throw anything else at this program so now we're going to say a dot X axes dot set underscore major underscore locator and we're going to use M ticker dot max n locator and we're just going to set that to three for now we can change that later on but three is good enough and then we're gonna do the exact same thing here only we're going to format our dates so x-axis set underscore major underscore formatter and that will be M dates that date formatter and just like before percent year - % m - percent day and then we'll do space hours : % m and we don't really need seconds this is not tick data so there's really no point minutes is even a little bit pushing it but that's okay so now we're gonna do PLT dot set P and then we're gonna say a dot get X tick labels and then we're gonna say visible equals false and in theory that really needs to only be the case if the exchange is not will be otherwise we shouldn't be doing that let's probably a glitch in my main code but we'll see so then if the top indicator does not equal none none not known PLT dot set P and then basically we we're just deleting X labels here so paste that stop indicator shouldn't be a zero right now we're gonna do we'll just copy this copy paste instead of top it'll be bottom and instead of a two or a zero it would be a two right yeah so a zero is the actual top indicator and we're removing its date but in this case we're removing the date from volume and we're leaving the dates on the bottom indicator now we use X just I don't think we're really actually well yeah I guess we do uh-huh so never mind so we're gonna use X x equals the length of OLC and then we just pick a random variable it doesn't really matter it's the length of that minus one and this is the data point right that'll be the most recent data point in a list okay so the length is it starts from one right and a list starts at zero so that's why we do negative one so that will be X will be the value or X will be the elements ID of the last element in the list so we can get last price basically that's the whole point of why we're doing that so now we just asked if data pace equals one D for example if that's the case then we're gonna say our title equals exchange that's the pretty exchange name plus quotes space one-day data with space plus oops end quotes plus resample size plus quotes space bars and then we're doing a new line so backslash n that literally makes a new line on the screen last price : then plus the string version of ohlc closed with the element of X and there it is people fancy so now we're gonna do this copy and then paste and paste so we've got 1 day 3 day 7 day and eventually we'll add more and we'll have to make this a little more dynamic but that's okay so then we just need to comment or edit that and that no problem even if you did like 30 day you could probably get away with that but then anything greater you need to change this text as well so we'll have to think about the best way to incorporate that as we move on but cool so enter enter coming down if top indicator does not equal none we need to throw the title on top of it so we go eh-oh dot set underscore title and we set the title to what we just defined this title up here and that's if top indicator is not none but otherwise else the the uppermost graph is going to be the main graph so it would be just a dot set title title ok so so that's that then we will say we'll just we just print here new graph we don't really it's just to alert us that it's been updated new graph and then dat counter equals zero and we have all this extra space let's just pick up all this space failed in the non-tech enemy that's fine and then and if if all of that occurs we will say dat counter equals 9000 so immediately try to reset and then if we scroll up to the very top of our animate function that I'm now lost you'll see actually we didn't if chart load I'm trying to think of where our dad counter is supposed to show itself I suppose that counter is just let's see let me count this now so that would be here if dat counter is greater than twelve and that is I just need to count it so one two three four so now at the very very bottom here after the exception we have one two three four else and then dat counter plus equals one so that continues updating it and when the graph updates that counter is reset to zero and it will only read eight that graph when dat counter is greater than twelve wherever that is here okay so now let me come down here and go to the very bottom and where it says an e equals animation change the interval to 2000 the exchanges mostly update their API information every 2000s 2000 seconds so we'll do that so basically what we're saying is any any graph that is not any graph that is not a tick data graph update there 24 seconds right because we're doing 12 12 times 2 24 24 seconds and so later on we can actually instead of using 12 we can allow the user to set that so they can update as frequently as every 2 seconds and we can even change that as well every 2 seconds if they want or every minute 5 minutes hour whatever they want and that's gonna be useful mostly for automated trading otherwise it really just doesn't matter cuz you're not gonna expect you're not gonna spend any money so it doesn't matter so let's get safe and run this it has been a lot of code since we've actually ran the the code that we've been writing so I imagined we would get an error but I guess that code that we've written hasn't actually shown itself yet so let's pick one day data frame and it I cannot believe it but it looks like we graphed it fabulous okay so let's try a day then let's really push it to the limit guys okay so we've got the data updating we got a pretty cool graph we've got last price we've got a great open high/low close we don't have top indicators the main indicators in theory we just wrote we did write but let's try it no failed unsupported operand not slow down there son I wish I knew it's obviously trying to add an integer in a string let me see if I can solve this before we balance out of this video so that is wow that was too much scrolling my finger hurts okay right okay so so what we've done here is we basically said the string version of each ma 1 + s ma that was why I was getting angry with us so add one more to middle indicator and that's middle indicator you know in our animation function basically so if middle indicator does not equal one add that there and then add that again there and that should do it let's oh oh wait wait wait and this is closing it off so we can just delete that and let's try it out hmm so that was with should have been both EMA's and SMA so let's say we want a three day time frame and let's do a middle indicator here SMA and there's our SMA so we've got man indicator us at EMA of 10 as well just because we're ballers so now we have the SMA here we've got the EMA is in blue and we're all set I think so that's our graph graph page this should be like dashboard we have like a lot of updates to make here but hopefully you're now kind of seeing this application sort of coming them coming together we don't have bottom indicators and we don't have top indicator yet but that's what we're gonna be doing in the next videos but we should be able to I believe even change like all of this data so like we could pick 30-minute bars hopefully so that updates the bars we can even pick 3 hour bars this is three day so this is a good time to point out as well like this the thickness of these bars that's what was candlestick width five minute you can see they're significantly thinner bars now and all that's much higher granularity data so anyways that's it for this tutorial hopefully that was somewhat reward after quite a few videos of code and now we've got a pretty sexy chart coming along here we just need to add the top and bottom indicators and we're all set really then we needed any trading and all of that so that'll be fun but anyways hope you guys are enjoying if you have any questions or comments please feel free to leave them below otherwise as always thanks for watching thanks for all the support the subscriptions and the donations and until next time

Original Description

Tkinter GUI TutorialPlaylist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk This Tkinter GUI programming tutorial covers how to add an OHLC candlestick graph to our Matplotlib graph in our Tkinter GUI. 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 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video tutorial series teaches how to create a candlestick graph with OHLC data using Tkinter and Matplotlib in Python 3.4, including adding custom labels, titles, and indicators, as well as dynamically updating the graph and resampling data for different time intervals. The tutorial covers how to use Tkinter for GUI design, Matplotlib for graphing, and Python 3.4 for programming.

Key Takeaways
  1. Plot candlesticks with subplot
  2. Add labels with set_ylabel
  3. Add volume data with fill_between
  4. Format x-axis with major locator and date formatter
  5. Update graph every 24 seconds
  6. Add EMA and SMA indicators
  7. Change candlestick width and granularity
💡 Using Tkinter and Matplotlib, you can create a custom candlestick graph with OHLC data and add technical indicators such as EMA and SMA, making it a powerful tool for data visualization and analysis.

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →