Package: yfapi
Welcome to YahooFinanceAPIAn unofficial library to capture data from Yahoo Finance API in an easier way. Having access to capture data from some stocks provided by the service. It is possible to configure your capture to select and filter different types of data regarding the stocks provided.First Stepsinitially, install the package through the following command in your terminal:pip install yfapiNote:For those more familiarized and who choose to keep the packages in a venv environment, you can use the commandpython -m venv yfapi.WarningsIf you are running your code onWindows, it is extremely important that you consider indenting your code as in the following example:from yfapi import YahooFinance

if __name__ == '__main__':
	# directly run your code that uses the
	# YahooFinance class here, or even functions that use it.Then we would use it as follows:from yfapi import YahooFinance

if __name__ == '__main__':
	stock_data = YahooFinance('STOCK_TICKER', '1d', '1d')MethodsInitially, it is necessary to instantiate our variable that will receive theYahooFinance class, followed by the following attributes:AttributeTypetickerstrorlistintervalstrrangestrticker: It is the code referring to the stock, Example:AMZN(Amazon),TSLA(Tesla) andAAPL(Apple).interval: It is thetime intervalof how you want the data referring to the "range" attribute. Examples: 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo.range: It is the scale of capture, which can be days, months or years. Examples: 1d, 2d, 5d, 1mo, 2mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max.for that, instantiate as in this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')remember, if you are running the code onWindowsyou should run such functions like this:from yfapi import YahooFinance

if __name__ == '__main__':
	google_data = YahooFinance('GOOG', '1d', '1d')Note: if you instantiate theYahooFinanceclass using alistof tickers for the "ticker" attribute, aloading barwill be shown referring to the amount of tickers present in the list.infoReturns aPandas DataFrame, with the following values referring to stock:tickerchartPreviousClosecurrencycurrentTradingPerioddataGranularityexchangeNameexchangeTimezoneNamefirstTradeDategmtoffsetinstrumentTypepreviousClosepriceHintrangeregularMarketPriceregularMarketTimescaletimezoneFor that, just use the method like this example code:from yfapi import YahooFinance


google_data = YahooFinance('GOOG', '1d', '1d')
google_info = google_data.info()trading_periodReturns aPandas DataFramewith the following data:timezonestartendgmtoffsetit is necessary to insert the argument referring to whichtypeof period you are looking for, being possible to capture "pre", "regular" and "post".ArgTypetypestrFor that, just use the method like this example code:from yfapi import YahooFinance


google_data = YahooFinance('GOOG', '1d', '1d')
google_trading_period = google_data.trading_period("regular")all_valuesReturns aPandas DataFramewith the following data:tickertimecloseopenhighlowvolumeFor that, just use the method like this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')
google_values = google_data.all_values()closeReturns aPandas DataFramewith the following data:tickertimecloseFor that, just use the method like this example code:from yfapi import YahooFinance


google_data = YahooFinance('GOOG', '1d', '1d')
google_close = google_data.close()openReturns aPandas DataFramewith the following data:tickertimeopenFor that, just use the method like this example code:from yfapi import YahooFinance


google_data = YahooFinance('GOOG', '1d', '1d')
google_open = google_data.open()highReturns aPandas DataFramewith the following data:tickertimehighFor that, just use the method like this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')
google_high = google_data.high()lowReturns aPandas DataFramewith the following data:tickertimelowFor that, just use the method like this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')
google_low = google_data.low()volumeReturns aPandas DataFramewith the following data:tickertimevolumeFor that, just use the method like this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')
google_volume = google_data.volume()to_csvIf you have already captured some data using the methods mentioned above, you can export it to a.csvspreadsheet.ArgTypedir (optional)strsep (optional)strdir: Thepathwhere you want your .csv file to be saved, bydefaultis set to thecurrent folderwhere the script is running and named "database.csv", which is also changeable. Your .csv will be saved in the given path, for example:
"my_stock_databases/stock_name.csv".
It is recommended to insert the suffix ".csv", however if it does not exist, it will be inserted at the end of the string received by this argument.sep: It is theseparator/delimiterused in this worksheet.csv, bydefaultit is set to ";", but you can change it to any other. For example acomma(",").For that, just use the method like this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')
google_data = google_data.all_values()
google_data.to_csv("google_database.csv", sep=",")Note: If you haven't captured any data, an error will be returned stating this.to_excelIf you have already captured some data using the methods mentioned above, you can export it to a.xlsxspreadsheet.ArgTypedir (optional)strdir: Thepathwhere you want your .xlsx file to be saved, bydefaultis set to thecurrent folderwhere the script is running and named "database.xlsx", which is also changeable. Your .xlsx will be saved in the given path, for example:
"my_stock_databases/stock_name.xlsx".
It is recommended to insert the suffix ".xlsx", however if it does not exist, it will be inserted at the end of the string received by this argument.For that, just use the method like this example code:from yfapi import YahooFinance

google_data = YahooFinance('GOOG', '1d', '1d')
google_data = google_data.all_values()
google_data.to_excel("google_database.xlsx")Note: If you haven't captured any data, an error will be returned stating this.