Candlestick Charts in Python
Skills:
AI Pair Programming80%
Key Takeaways
The video demonstrates how to plot candlestick charts in Python for stock visualization using libraries such as matplotlib and pandas.
Full Transcript
[Music] what is going on guys welcome back in today's video we're going to learn how to visualize stock data using candlestick charts and python so us get right into it now i already made a video on candlestick charts in python i think it's even the first video on this channel so if you scroll down to all those videos you should be able to see a candlestick tutorial however first of all this tutorial was not very high quality compared to today's videos but second of all the knowledge is deprecated so what i taught in that video the libraries should no longer be used they can still be used so if you run the code i think you should be able to get the same results but um there are more modern ways quote unquote to do this and this is what we're going to talk about in today's video so for those of you who don't know what a candlestick is or a candlestick chart is uh it's a financial chart composed of candlesticks and those candlesticks give you information about four values about the open value about the close value about the high and the low value so on a particular day if a stock opens when the stock market opens it trades at a hundred dollars this is the open value if it closes on 120 dollars it's the closing value and if in between that time period you have some spikes on um of 180 or some lows of 70 those would be the high and the low value so this is what a candlestick chart can visualize because a simple line chart you just see one price for example the closing price and that's not all the information that you're maybe interested in now for this tutorial we're going to have to install a bunch of libraries so we're going to say pip install first of all pandas and i'm not even sure if we're going to need panda specifically but we need to have pandas installed then we need pandas data reader this is the library that we're going to use to get uh oh actually not import sorry pip install canvas data reader this is the library we're going to use to get the stock data from the api then we're going to say pip install matplotlib the basic visualization tool then we're also going to install mpl finance which is the actual library that we're going to use to visualize the stock data uh as a candlestick chart and i think that's actually it because numpy should be installed automatically so once you have that we're going to import matplotlib.pyplot splt we're going to import pandas data reader as web we're going to import mpl finance as npf and we're going to import datetime sdt where datetime is a core python module so you don't need to install anything for that now what we're going to do now is we're going to say data equals and we're going to download stock data now you can load your own csv files you can load whatever you want into the script uh i personally am going to get the data from the yahoo finance api and for this we need to say web.datereader then the stock ticker symbol so for example aapl for apple fb for facebook or in my case tsla for tesla and then we're going to say where do we want to get that from i'm going to choose the yahoo finance api and optionally you can specify a start and end date for the range that you're interested in we're going to do that we're going to say start equals dt.datetime and i want to have all the prices from 2019 first of january up until now so end is going to be dt daytime.now and we can specify optionally that start and end date here if we don't do that we're just going to get the full uh range since the ipo and now we can actually go ahead and just say plt.plot data and we're going to plot just a closing value here plt show um and you're going to see what an what an ordinary just a basic line chart looks like and you know you can see the information here you can see that the stock price rose in value a lot but you don't have the full information of what happened in a particular day or on a particular day so for this we use candlestick charts and before we go any further let me just show you what this looks like so we can say print data and you can see the basic structure of the data here once the data is loaded you can see we have high low and some other values here open close and so when we have the volume of the day and we have the adjusted stock price the adjusted close value based on uh stock splits and so on which tesla had one of them um i think a couple of weeks ago or actually maybe a couple of months ago um so this is what we have in that data set here or in that in that data frame but we don't need to actually specify anything to plot a candlestick chart we just have to say mpf dot plot and we can pass the full data frame here and we just have to say type equals candle and we're going to get a candlestick chart now this is not going to be the most beautiful candlestick chart because we don't have any uh design patterns that we specified but you can see it's a candlestick chart when you zoom in so let's zoom into that period here and actually let's zoom a little bit closer um you can see that those are candles right now so the lowest point is the low of the day the highest point is the high of the day and then it depends on the color of the candlestick if the stock price um rose in that day or if it fell in that day so i'm not sure in this particular design if white means that the stock price went up and black means that it went down or the other way around but if white means that the stock price went up this means that the lower end of this white block here is the opening value and this is the closing value and if it's a black stock it's the other way around uh so a black candle this means that we have the opening value here in the closing value here so the value decreased in that particular day but i don't know in this particular style what this means we're going to create our own styles and then it's going to be clear now before we actually go ahead and create our own styles let's take a look at the available styles and for this we're going to say print mpf dot available styles this is a function or a method we're going to call it and we will see a list of the different styles here so you can check out all of them if you want now in this case i'm going to take a look at night clouds because that's a dark theme and that's probably most interesting here for programmers so we're going to say mpf dot plot and we're going to plot the data the type is going to be a candle and the style is going to be night clouds like that so we're now going to get a different style here which is not very customized but we have a different style and you can see here if i zoom into it we have different colors we have a different background again i think white means stock price going up and uh blue means stock price going down but we have a bunch of different i think we can also um look at don't we have a yahoo yeah we have yahoo as a style as well so this should be red and green and there it should be clear if the stock price is rising or falling but this of course is light mode here so you can see this means rising stock price falling stock price and so on all right so let's go ahead and create our own style and for this we're going to start with the colors with the color scheme and for this we're going to say mpf.make market colors and we can specify a bunch of different keyword arguments here but i'm not going to go into too much detail of all the different arguments that we can pass and what they mean uh if you're interested in a perfect customization whatever you want you can check out the documentation the two basic keywords that we need to pass here are up and down and those are just the colors for the up movements and the down movements and i want to have a green color for the up movements so 0 f f 0 0 pure green and for the down movements i want to have a red color so ff 0 0 0 0 and of course a hashtag before that and then we're going to specify wick equals inherit again i'm not going to talk too much about the different things here h equals inherit inherit means basically that you're going to inherit it from the base uh style that you're going to to use as a base style um and we're going to talk about that in a second and then we also have volume equals in so this is the basic uh market colors that we're going to use the most important ones are those what do you want which color do you want to have for an up movement and which color do you want to have for a down movement and now that we have the color scheme what we're going to do is we're going to say mpf style or call it whatever you want mpf style equals mpf dot make mpf style and here we need to specify a base style i mean i don't actually think we need to but we're going to specify a base style so we're going to pick one of those styles here as a base style and we're just going to change what we don't like about this and since i'm a fan of dark themes but i don't like the color scheme of night clouds we're going to take night clouds as a base style and we're just going to change the colors so we're going to say base mpf mpf style is going to be night clouds and the market colors are going to be the colors so this is the style that we're going to use and now we just have to specify style equals mpf style and if i didn't forget anything we should see a perfect candlestick chart yeah that's what i want it to look like um now we can zoom into this here and you can see that we have green and red candlesticks um and we have a dark background so this is in my opinion the perfect candlestick chart you can of course do whatever you want with that you can also say the up movement shall be red and the down movement shall be green even though that's quite confusing you can also say okay up shall be blue so we can do it like that and you have a blue green chart uh blue red chart sorry like that but i don't think this is very yeah i don't i don't think this is aesthetic at all so i like green and red the most uh what we can also do is if we're interested in how many stocks were traded on that particular day how many stocks were bought and saw and sold we can also save volume equals true and we can also see the volume plotted below the actual candlestick chart and they will share the x axis so as you can see we can see the volume here also with the respective colors of on this day that the stock price rise or did it fall so if we zoom into this year it also zooms down here and you can see okay it's moderately most of the time let's zoom out here and you can see here we have a day where a lot of trade happened uh so this is basically a simple candlestick chart in python so that's it for those we hope you enjoyed and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Original Description
In this video we learn how to plot candlestick charts in Python for stock visualization.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💻 Exclusive Content 💻
👥 Patreon: https://www.patreon.com/neuralnine
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 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
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
AI Coding Agents in 2026: 8 Tools That Actually Ship Production Code
Dev.to AI
I wanted a better way to code with ChatGPT, so I built SVI
Dev.to · Aleksandr Razinkin
I Cut 1,300 Lines of CLAUDE.md — My Token Bill Dropped 75%
Medium · Programming
Anthropic Just Dropped a Banger on “Loops” and It Quietly Changes How You Should Use Your Coding…
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI