Python for Finance #1 - Introduction and Getting Real Time Stock Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to my new python for finance tutorial series um in this series i will try to go over pretty much everything you might want to know for using python for the finance so to start things off we are going to do some pretty basic things uh like how to actually get the data like how to actually work with those data in our example we will use some big corporations in the u.s stock exchange but of course you can use it for the entire smp or for the entire dow jones or for the entire nest app or any other market that is much more attractive or relevant to your own stock portfolios but just for illustrative purpose we will stick to those big corporations first later on we are also going to visualize and also going to analyze those captured data for meetings um to help us to formally some kind of training strategies and also to help us to facilitate our own decision making so we have a lot of exciting programming exercise ahead of us so let's go ahead and let's get started in the end of this lecture series you will be able to build something that i've shown right here everything can be done with the use of python and everything is automatic in this lesson we will do a very quick recap on how we can capture and download live data from different websites and store them in a csv format for any applications later in our example we are using web scrapping to capture the data from yahoo finance page but of course you can change it to whatever website you want as long as those websites did not keep you out so this is extremely useful and extremely easy okay so first thing first we are going to import some server libraries that include the penders spds because we're going to save everything into a csv file and then we also need to import the date time because we need to have a time object um so we also need to import the requests and from that from that request we are going we need to handle one of the exception that is called the connection error so just in case if we cannot get anything from that urls we are going to the programs can continue to run so and then the ps4 on ps4 we are going to import the uh beautiful shoe and that's it for the library so there are two functions that i also need to create so the first function is to get get the information from from the url so we are going to define a function called real-time stock we are going to pass a stock cook into it and so first of all we need a url to locate the the the informations that we would like to have and then we are going to try to see if there's anything um that we can get from the url so we just use the request.getua.gets and then use the url so for the web content we need to use a beautiful shoe to help us to capture all the taxes on the taxes from the these url so i will show you how to get this url later on and then from that content web content we are going to find um everything from the div first and then everything inside that inside that div and everything every span inside that different so let me show you we are going to give a uh give past the web content and also a path to it class path to it there are two parameters so we are going to capture things capture the test from from the url and then pass these uh pass this web content into a functions that i'm going to create so this is a div so this is the function that i'm going to have there are two things one is the web content and the other is uh there in the up and the second parameter is the class path so inside that inside these [Music] we are going to search the if search all the div inside this class but because we are going to provide a a specific um class path so it will just locate this path inside the div and then we are going to get everything out from that div so we're going to have this and then in the first row we are going to find all spans so that means first of all we we are going to because there are so many diff um under this web content we are going to find the div with that class path with with a specific class path and then inside that div there are so many spans so we are going to find all the spans and then we are going to put everything into elites put all the expense dot get that's full spend in the spans put everything into a list and then except we're going to handle our first error that is the index error was just in case we cannot get anything from this url so we are going to give a null value to it return test um that is the test yep so we're going to have these functions to help us to find the value inside this web content inside this url so this i'm going to change it i'm going to use it and i will show you how to get the class path and also the url the next things that we need to do is to make sure these error can go correctly this is the accept the connection error but just in case if there's any connection error when we try to get the contents from from this url and it will help us to handle it so we're going to return something so for this example because we're going to return return the price and also the price change so i'm going to just return the price and also the price changes so just in case there's nothing there i'm going to there's a connection along i'm going to put in no value for them so there should be something for this price and also the the changes right here so now let's see how we can assign the url and also assign the class path to it so first of all we are at the finance yahoo.com page and then we search any stock just say for example we are going to search the uh berkshire halfway this company um so we have everything right here and this is the first informations that we would like to have and this is the url for this webpage and the second thing that we would like to capture is the price and also the price change that is right here so we are going to find the cost path right so we just right click on this and then inspect and then we are going to find the outer the most poppers um diff class and that and this is this that one so you can see that there's uh some some some class dance there but um this is the first one that we would like to locate just one once we locate this one um the content inside this is um called the span you can see there's suspend we're going to capture the information from that span and you can see that there's is some information inside this say for example this is a price in the first band and then this is the the price changes uh in the second span so we are going to copy this path and get this banned out from this uh from this diff cast so we are going to paste the path right here and also we are going to paste the class path right here um yep copy to do many information yeah i just need this path and this is going to be put inside here okay and then this is the url i need to change it back to a s format a strange format i should say this is the first pass this should be correct okay and then the stock coke uh should be something like this um say for example the first one is the b okay hyphen b and then we are going to put this back to this code um to complete this pathway so i have to change to this one and this one so now we can assign the value from this tax back to the price and also the check price changes so if tax is not equals to now so else if they are not equal to now so the price and changes so i just copy remember the first value of this test is say is the price and then the sec the first band contains the price and the second spring spans contains the uh the price changes so we can capture all this value so next thing is we can actually test what we can get from this from this function so let's say for example price doll so i'm going to put the inside this yep let's see and there's a error on the get test so [Music] we'll spend okay should be this one there's a typo right here we spend no expense so let's try again good now you can see that we can actually capture the price and also the price changes and that's it for this video i hope you enjoyed it in the next video i will show you how to capture more information from the webpage and how to perform the rap scrapping for multiple stock and how to store them into a usable format so see you in the next video
Info
Channel: eMaster Class Academy
Views: 10,101
Rating: 4.8095236 out of 5
Keywords:
Id: q_MWWVgghsQ
Channel Id: undefined
Length: 15min 18sec (918 seconds)
Published: Wed Jan 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.