More stock manipulations - Python Programming for Finance p.4

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

Key Takeaways

This video tutorial series covers Python programming for finance, focusing on data manipulation and visualization using libraries like pandas and matplotlib. The specific tools and techniques demonstrated include resampling data, creating candlestick charts, and plotting stock data with volume information.

Full Transcript

what's going on everybody Welcome to part four of our python for finance tutorial what we're going to be talking about in this tutorial is resampling our data since it's a really useful operation that we can do with pandas also because of res sample we can also create a Candlestick all in one tutorial so it'll be great so what we're going to do is like in the previous tutorial we did this nice manipulation of this column that's great but for now I'm going to comment it out also we're not going to graph this information we are going to keep the a cuz that's relatively useful now um and then we're not going to do the head for now actually we'll just uh delete that so the first thing that we're going to do is uh do a resample the idea of a resample with pandas is you can take some information and resample it to us you could resample it down but it won't work but you can also resample it Greater so for example let's say you have sensors that collect data about the temperature and humidity of of your servers and they just kind of randomly collect data um or probably a better term or a better idea okay in your little in your business you want to count how many people walk through a doorway this is very random right the people that might walk through and in your real curiosity is like how many people come through an hour okay but you've got data that comes through maybe every minute or every two minutes sometimes or three minutes or whatever but you really care about that hourly number what you could do if you had a data frame that just contained Columns of people walking through um you could resample it you could resample it to 1 hour of data so it would take all the data resample it to one hour if you use a resample sum um it would add them all up and it would tell you okay every hour this is how many people you get with stock information we have currently daily data but we're going to resample it to 10day data so if you have huge data that you don't need to be huge for example if this was like tick data that was like millisecond data and we didn't need to be working with millisecond data we could resample that data to one day data like we have or 10day data or just minute data so you have a ton of options here so let's go ahead and do that so when you the there's kind of two things that we're going to do here generally when you resample you're probably going to make a new data frame so first let's do DF ohlc this will be its own data frame and we're going to say this is equal to DF adj close for adjust a close do resample and then in here here just like with rolling you put a window um basically so we're going to go with 10 D for 10 days we could do 10 min we could do um six Min we could do any number we want and you could there's all kind you could do a weekly bi-weekly monthly at the beginning of the month the end of the month all kinds of stuff but for now we'll just resample to 10 days um and then here you could say you want to get the mean the average value over 10 days so this is not the same as a rolling right 10day it's this isn't a 10 moving average because it's not a moving average it's just like every 10 days it takes the mean so it's actually going to significantly shrink the size of the data set um so you could do mean you could do sum the total value or in our case we're going to do ohlc for open high low close and this will create data that is open high low close yes we are starting with open high low close data yes Tesla never had a stock split So in theory we could have skipped this step uh but a lot of companies do have stock splits and you won't be able to use that data you're going to have to create your own based on the adjusted close so um there you have that now what we're going to say is we're going to also do this with volume so DF do or DF underscore volume is going to be equal to DF volume and then again we're going to resample by 10 days and in this case we'll use sum you you probably could use mean I think some some makes a little more sense so I'm going to use some because that's the true volume not the average volume over 10 days you probably really want the true volume now let's go ahead and print uh DF ohlc just so we can see what we're dealing with we are not going to run pl. show for now and let's go ahead and run [Music] that so there you have it it's open high low close based on every 10 days of data um and date is the index in our case we actually don't want that that to be the case because we are going to use matplot Libs Candlestick ohlc while I'm thinking about it let's go ahead and import that because I'm going to forget so from matplot li. Finance we're going to import Candlestick ohlc um and then we're also going to import import matplot li. M no matplot li.es as M dates for map plot lib dates because map plot lib does not use datetime dates for whatever ever nonsensical reason they use these like it's like 700,000 it's not Unix time it's who I don't know I have no idea I have no idea why they use MD dates it's unfortunate so um Okay so we've got the head now what cck ohlc wants from us is actually not just simply ohlc it wants date in MDES format date open high low close so it wants all of those things so the way that we're going to do that or generate that is like you can get all of the contents of a data frame which is by just by doing like DF Dov values but that will not give you the column headers or the index so the first thing we want to do is reset the index for dflc so that date is now a column so the way we do that is uh let's see uh D I think we can actually just do DF I can't remember if this will happen in place or not read reset index let's try it in place equals true um and then we'll print this. head let's see that works sure enough it does so we can modify that in place cool uh okay so we've got date now we need to convert it to M dates so the way we're going to do that is we're going to say DF uh just copy this paste date is going to be equal to this map and in this case we're not we're not doing a fancy mapping of any function so like we're not trying to pass any parameters so this is going to work just map and then pass MD dates. dat to num so this will convert our date time um object to an a silly mate number which will be like like I said like 700,000 okay so for example let's now that we've seen now we've done the conversion let's just do this just so you can see what I'm talking about right great 733 952 that's a date to map plot Li okay so now what we're going to do is we've got the columns here or the axes I don't know what I call them columns got the axes we don't need to print this uh we are going to do a1. xaxis do oror date that will basically take these M dates. dat2 number basically the M dates and display them as beautiful dates thanks Matt plot lib so so we've got that now what we're going to do is do Candlestick ohlc what axes ax1 and then what's the data dflc Dov values and then we're going to say width equals 2 that's just how thick are the little candlesticks themselves color up equals g for green I think the default is black up red down but this will make green up red down and then since we have ax2 we're going to put volume on it but rather than doing the bar we're going to do ax2 fillore between and we're going to fill between basically DF volume. index that's your X um and oh unfortunately we also have to do this whole map nonsense so let's just copy this copy DF volume. index. map so that's your X um um and then Bas and then what are we going to um what's going to be your your Y data basically so what and then between what so DF underscore did we do volume yeah we did right here with some DF volume do values and then zero and that will um that'll fill this is your X this is your Y and it will fill from zero to the Y it's just a little easier to see things I think this way okay let's uh see if we get a result no did I forget I probably forgot a like a let's see what do we got fill between ah this this needs to close here and [Music] then okay let's try this again great so now we've got this beautiful open high low close Candlestick graph so the idea of Candlestick graphs is they kind of condense Direction open high low and Clos data all in like one pretty simple line so um so here we can see like for example this is the low point this is your open um or it could be in this case this would be open this is closed because the price went up but in this case this was open this was closed this was also the high high this was the low it closed above the low uh so once you get used to reading them uh they're pretty useful because they're just so condensed yet have so much information to them um and a lot of people like to read the charts not someone who buys into that but uh they are very descriptive nonetheless and as you can see we got this nice fill between rather than the bars it's a lot easier to see that oops I zoomed in anyway uh that's that if you have questions comments concerns whatever uh do feele to leave them below otherwise we're going to be getting into um more kind of complex manipulations and eventually adding much more data to uh what we've got because right now we're dealing with just one stock but our end goal is actually to bring in all of the S&P 500 combine them all into one data frame and start actually running something that's going to give us kind of a relationship of all these companies so that's what we're going to be doing in the coming tutorials if you have questions comments concerns whatever feel free to leave below otherwise I will see you in the next tutorial

Original Description

Hello and welcome to part 4 of the Python for Finance tutorial series. In this tutorial, we're going to continue learning about more manipulations and do a candlestick graph. https://pythonprogramming.net https://twitter.com/sentdex https://www.facebook.com/pythonprogramming.net/ https://plus.google.com/+sentdex
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 use Python for finance, covering data manipulation and visualization. It demonstrates how to resample data, create candlestick charts, and plot stock data with volume information. The tutorial is useful for beginners in finance and Python programming.

Key Takeaways
  1. Resample data to reduce data size
  2. Create a new data frame with resampled data
  3. Use resample to change data frequency from daily to 10-day
  4. Plot stock data with Candlestick ohlc
  5. Plot volume on a secondary axis with fill_between
💡 Resampling data is crucial for reducing data size and improving the efficiency of data analysis and visualization in finance.

Related Reads

📰
The Modern Browser Testing Stack: AI, CI, Human Review, and the Cost of Maintenance
Learn how to build a modern browser testing stack using AI, CI, and human review to reduce maintenance costs
Dev.to · Simon Gerber
📰
Build an AI Error Explainer in Python
Learn to build an AI-powered error explainer in Python to turn stack traces into actionable debugging JSON
Dev.to AI
📰
I Built 4 Interactive Engineering Tools You Can Run in Your Browser
Learn how to build interactive engineering tools that run in your browser, making complex concepts more engaging and accessible
Dev.to · Dhananjay kumar Seth
📰
Teaching Smarter with AI: How Teachers Can Save Time, Create Better Lessons, and Personalize…
Learn how AI can help teachers save time, create better lessons, and personalize learning for their students
Medium · AI
Up next
Anyword's Performance Boost AI Integration
Anyword
Watch →