This past summer, my grandfather taught me the tips and tricks of investing in the stock market. We did detailed analyses of various companies, comparing profit margins, price to earnings, and other aspects of company fundamentals. After taking hours to dig through Yahoo Finance and compile stock info into spreadsheets, I began to wonder if there was a more efficient way to research companies. I decided to design code that would extract the data I needed so I could simply glance over the data table to decide if a company was profitable. Here’s how I did it:
去年夏天,我的祖父教给我有关投资股票市场的提示和技巧。 我们对各种公司进行了详细的分析,比较了利润率,价格与收益以及公司基本面的其他方面。 在花了几个小时来浏览Yahoo Finance并将股票信息编译成电子表格之后,我开始怀疑是否存在一种更有效的研究公司的方式。 我决定设计代码来提取所需的数据,因此我可以简单地浏览一下数据表来确定一家公司是否盈利。 这是我的操作方式:
My code takes a list of stock tickers and outputs a pandas data frame where the columns are stock attributes and the rows are the corresponding ticker data.
我的代码获取股票行情清单,并输出一个熊猫数据框,其中列为股票属性,行为相应的行情记录数据。
入门 (Getting Started)
Installing Yahoo Finance API
安装Yahoo Finance API
Before we can run the code, however, we have to install the Yahoo Finance API in our console. To do so, type “pip install yahoo_fin” and hit enter.
但是,在运行代码之前,我们必须在控制台中安装Yahoo Finance API。 为此,请输入“ pip install yahoo_fin”,然后按Enter。
Importing Modules
导入模块
Then we need to import the stock_info module from the yahoo_fin library so we can use its methods to extract stock data. Since we are using a pandas data frame to organize the data, we will also import pandas.
然后,我们需要从yahoo_fin库中导入stock_info模块,以便可以使用其方法提取库存数据。 由于我们使用的是熊猫数据框来组织数据,因此我们还将导入熊猫。