Candlestick Charts in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·5y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches how to create candlestick charts in Python for stock visualization, covering the necessary libraries and code to get started. It's a beginner-friendly tutorial that provides a practical introduction to data visualization in finance. By following this lesson, viewers can learn how to effectively visualize stock data and perform technical analysis.

Key Takeaways
  1. Import necessary libraries
  2. Load stock data
  3. Prepare data for plotting
  4. Create candlestick chart
  5. Customize chart appearance
  6. Analyze chart for trends and patterns
💡 Candlestick charts are a powerful tool for visualizing stock data and performing technical analysis, and can be easily created in Python using popular libraries.

Related Reads

Up next
Vibe Coding with Claude Changed How I Build Things!
PlivoAI
Watch →