Download STOCK PRICES / OPTIONS / FUNDAMENTALS in Python using yfinance Ticker object

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i show how to remotely download stock options and fundamental data using the ticker object of the y finance library first thing to do is to install the wi-finance library by using the pip install command and then import the appropriate libraries we're going to be importing pandas as pd and why finance as yf why finance ticker object is going to be taking several parameters among which these lists are the most important the start and end date the period the valid period intervals the argument actions that takes a boolean and whenever is true it will include information on both dividends and splits of the company the pre-post it's also a boolean that is going to be giving you the pre-post and regular market hours data of the company that you are choosing so let's start by creating a symbol of the company we we want to get the the pricing data uh it is going to be apple and we are going to be setting the ticker object as the ticker and we're going to be saying y fineness equals to ticker and inside we're going to be including the symbol that we have just created so this is the instantiation of the wi-fi and sticker object into the tick variable in our python environment so we are ready to start using to start downloading data using the ticker object of wi-fi so let's say we want to create a obtain a data frame that is going to be taking historical data this is why we're going to be using the history option in the ticker object in the wi-fi finance library so we're going to be setting a start date equals to say 2019 the 1st of january the end date let's say it's going to be 2020. also the first of january so let's run this line of code and we can see if we inspect that um data frame we see that we obtain the pricing the daily price of the stock of apple from this um starting date to the end date that we set in the beginning good so notice that by default the actions is equal to true that is why you are receiving the dividend and stoplit splits information so we're going to be setting it equal to false and round the numbers to two decimal digits with the option true for the rounding a parameter so we're going to be seeing that for the rounding parameter in this case this is going to be taking only a um a boolean let's say ticker then history then the start we're going to be copying the same code that we have just written here and in addition we're going to be saying actions equals to false to not receive the information on dividends and stock splits and we're going to be saying that the rounding is going to be equal to true so we get two decimal digits for every price of apple so let's inspect this data frame and we see that this is exactly what we wanted this is the data that corresponds again to the daily price of the stock of apple but we can increase the granularity of the data for example we're going to be using the period parameter instead of the start and date parameters and set the granularity of the stock price to for example one minute by setting the interval parameter to 1m so let's say let's copy this information this code pattern that we have written here and we're going to be saying inst using instead of a start and end we're going to be using a period equals to say the maximum amount of data that yahoo finance can provide to you so the actions equals to false the rounding equals to true but in this this time we're going to be using the interval parameter and set it to one minute in order to get the um minute by minute price of the stock of apple but notice that when we run the when we request the maximum period um of a available data we receive a message saying that only seven days worth of one minute minute granularity are allowed to be fetched fetched per request so in this case let's comply with these requirements for example instead of setting the period equals to max we're going to be saying that we want uh five days the other information we're going to be saying that is going to be exactly the same in this case we're going to be setting also the pre post equals to false we're going to be explaining what is the difference between a false and true in a minute so let's inspect the data that we received and as you can see you have received the minute by minute price um of apple over the last five days that we have defined here with the um period argument so we can inspect of course the information in order to see what are the ranges of the um of the time of the prices um through time and we can see that it starts from the 11th 15th of november of 2021 at 9 30 the minute by minute price up to the 19th of uh november of 2029 at um 15 359. so this corresponds um with the trading times for this stock so normally notice that this is the uh the normal trading times but you can also request the pre-post market data okay so in this case we can what we can do is set these um pre-post parameter equals to true and what is the thing that we received so let's inspect the data frame the head of the data frame so in this case let's check also the tail and we can see that this is starting from this date the 15th of november which is the same as we had in previously when we set the prepost equals to false but the what changes is the time that we have where the first pricing starts so this is starting from four o'clock and up to eight o'clock for this range of days that we requested so we can say df info in order to take a look at the the range and we can see that this is the price that includes a pre-market hours um the market hours and the post-market data okay so that is how you use the that is the reason why you would use the pre-post equals true so this is for a stock of a company that is traded in normal times but for example if you wanted to download information for bitcoin for example we're gonna be setting the um the ticker of the bitcoin symbol we're going to be saying ticker is going to be equal to y f ticker and equal to and as an argument the symbol so we can say that let's copy these uh information just to get the data for bitcoin the one minute data for bitcoin and first of all let's set the pre-post to false let's inspect the information of these um of this data and then we're going to be downloading the same data but with a pre post equals to true so in this case it makes no difference with regard to the uh range and the prices that you are receiving because this is not a bitcoin is trading all the time this is not traded only in normal market times as you can see for example we receive exactly the same range exactly the same number of information as if we set the prepost equals to true okay good you might wish also wish to download market data for multiple stocks so let's see how to do that so we're going to be saying symbols and suppose that we want information for the stock of apple the stock of um facebook and the stock of google so you need to put only you need only to include the symbols separated by one space and inside quotes so we're going to be calling setting or instantiating the why finance a ticker but this time instead of ticker it is going to be um tickers with an s so we include exactly the symbols that we have created and we are ready to use it so we set and say df then ticker history for example the interval that we're gonna be using um let's say it's gonna be one day and starting just two for the for this example is starting on the 1st of january over 2020. let's set again actions equals to force and the rounding parameter equals to true so i need to include it with an i let's run this line of code and let's inspect this data frame that we have received so notice that we have now received a multi-index pandas data frame with the type of price for example this is the closing price the high and the low price and so on as the first index level and the symbol of the company this apple facebook and google as the second index level so this is the way that we receive the information from yahoo finance but we can of course download this data but we are going to be able also to select um the information if we wish to for example just choose the closing price of a given number of companies of very specific companies so we can use the multi-index to select a specific column or a set of columns on a multi-level data frame so how do we do this so for example let's instantiate the pd index slice function in in pandas um in an object called idx and then we're going to be using the log um [Music] the loc function in order to get the data that we want so what are we going to be using first of all log takes first of all the rows and then the condoms so for the rows we want all of them so we are going to be just putting this column in here so because we want all of them for all of the periods that we have requested but with regard to the information related to the columns suppose that we want just to get the closing price of only say apple and facebook how do we do that well in the loc um function we're going to be calling the idx object that we set and in the idx what do we want to do well we want to include first of all the first level notice that the first level as we said is going to be the type of price that we receive so we're going to be saying okay i want the closing price and then i need to define the second level so given that for example i want information on both apple the closing price of apple and facebook what i can do is just to include as a second parameter for the second that represents the second level the apple and facebook symbols so let's run this command and we can see that we receive what we receive is a data frame that is also a multi-index data frame that has as the first index the type of price which in this case is closing the closing price and as the second level we have the apple and facebook stocks so this is what um how we can filter some information that we might wish to extract from the raw data that we received uh using the history option in the ticker object of wifiness so there is another way of course um to to do that suppose that we want uh the closing price so the only thing that we need to do is first of all call the closing price and then with another square brackets inside another square brackets we're going to be saying apple and facebook so we run this line of code and we obtain the same information with the difference that this time we don't have a multi-index we only have the information corresponding to the closing price for apple and facebook but the symbol is going to be the single index in our pandas data frame okay good so if we wanted to get notice that this information we are receiving as the closing of a high low this is the pricing information and then this symbol information so we can use the group by parameter to inverse the indices of the data frame so how do we do that let's copy this code and we're going to be saying we're going to be writing the interval is going to be also one day the start is um the the same um the same date we're going to be rounding it but we're gonna be using the group by a parameter and we're gonna be saying that we want to group the data by tickers so in this case if we run this line of code and we inspect the data instead of having the pricing type as the first um [Music] as the first level like in the previous case we now have the symbol as the first level okay very well so we can inspect of course the head of the data frame and we have the uh the same information how can we download fundamental data so in this case this is market data that corresponds to the stock of apple on a daily basis we have shown how to do it on a minute by minute basis but let's take a look on how to download in information about the company apple so again we're going to be setting the ticker with the symbol inside so we can obtain a lot of information like for example the information on apple if you run this line of code what you're going to be obtaining is a dictionary is a dictionary that contains information over the company about the zip the sector number of employees the business summary and so on so this is the long format that you receive as a dictionary so if you want to take a look only at the the keys for example we can say ticker info and because this is in the given in the form of a dictionary we can call the keys in order to take a look at what are the um what is what is the um the labels of the information contained in the dictionary okay so for example if you want to take a look at it inspect it in a more um to formal way or a more organized way not formal but organized we can say we can put it into a panda series where we include the information of the ticker so let's take a look at the 15 first pieces of information so as you can see in this case you receive it in a more organized way if you wanted to call only one of these pieces of information then you could say ticker then info and then include in parentheses for example we want to take a look at the long business summary of the company apple and we receive the information for that for that key um let's say for example we want data fundamental data that is going to have a range or a period of say 10 years for the stock of apple so you're going to be instantiating the ticker into the data variable we can we are going to be taking a look at the data for example and we receive the pricing data because this is the history the history function but we can take a look at the individual actions of apple for example individual actions meaning the dividend and stock split we can take a look for example at the dividends and this is for the for that for that same period this is going to be ticker dividends we run this line of code and we receive the dividends that correspond to that period of time that that we set as for example in this case 10 years there are other pieces of information that we that you can get for example the splits of apple the financials for example but notice that this is a more limited scope in the ticket financials that you are going to be requested you can request the financials on a quarterly basis by saying for example portly financials and you receive it on a quarterly basis receive them on a quarterly basis the same with regards to the balance sheet over the stock of apple you can request it as the quarterly balance sheet of apple okay so you have this information but notice that this does not correspond to the um to the period that we have requested this here the information is more limited uh you can request the cash flow the cash flow of apple you can request the earnings for example by saying ticker earnings for vcs again this is limited information you can get the sustainability the the ranking or the scores of this particular company if you're interested in this type of information as well as on the recommendations from the several financial actors finally for example you can also get some the calendar for the events or for the um yes for the events of the company for example the earnings date the announcement and so on okay very well so we can also download xf option date by using the ticker options first of all by calling the ticker options um and we're going to be getting a list of expiration dates so in this case the first thing that we need to do it is just to select an expiration date for example let's call this variable expiration date and it's going to be equal to the ticker to the first element of that um of this list of this tuple of date so we want the um the options on the expiration date over the 26th of november of 2021 so we're going to be running this line of code and then we're going to be saying options is going to be equal to ticker and in this case we can um we're going to be calling the option option chain option with the argument expiration date so what is uh what is it what we're going to be receiving so for example it depends if we want information on options for example on the course of uh of apples of apple pattern it is called with an s so you are going to be receiving the contract symbol the last trade the strike the last price bit ask etc so if you want to take a look at all of the information that is contained in the in this um in the co with regard to the course you set info you request the info and you can see the columns the pieces of information that you receive as well as the number of of entries that you will receive this is with regard to the options course you can also download options puts and this is going to be the sniper box option over the company apple so as you can see you can do exactly the same say options put and then requesting the information it is again this needs to be set with an s so this is it this is how we download a market option and fundamental data by using the ticker object of the library wifi finance i hope that the video is useful and hope to see you on the next one thank you very much
Info
Channel: pythonbox
Views: 109
Rating: undefined out of 5
Keywords:
Id: umD0Pa9arP0
Channel Id: undefined
Length: 24min 49sec (1489 seconds)
Published: Sun Nov 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.