Chọn cổ phiếu và lấy dữ liệu từ Yahoo! Finance

Nhập và quản lý dữ liệu tài chính bằng Python

Stefan Jansen

Instructor

Chọn cổ phiếu theo tiêu chí

  • Dùng thông tin niêm yết để chọn cổ phiếu cụ thể
  • Tiêu chí:
    • Sàn giao dịch
    • Ngành hoặc lĩnh vực
    • Năm IPO
    • Vốn hóa thị trường
Nhập và quản lý dữ liệu tài chính bằng Python

Lấy mã cho công ty lớn nhất

nyse = pd.read_excel('listings.xlsx',sheet_name='nyse', na_values='n/a')
nyse = nyse.sort_values('Market Capitalization', ascending=False)

nyse[['Stock Symbol', 'Company Name']].head(3)
     Stock Symbol             Company Name
1586          JNJ        Johnson & Johnson
1125          XOM  Exxon Mobil Corporation
1548          JPM    J P Morgan Chase & Co
largest_by_market_cap = nyse.iloc[0]  # 1st row

largest_by_market_cap['Stock Symbol'] # Select row label
'JNJ'
Nhập và quản lý dữ liệu tài chính bằng Python

Lấy mã cho công ty lớn nhất

nyse = nyse.set_index('Stock Symbol') # Dùng mã cổ phiếu làm chỉ mục
nyse.info()
Index: 3147 entries, JNJ to EAE
Data columns (total 6 columns):
 #   Column                 Non-Null Count  Dtype  
 --  ------                 --------------  -----  
 0   Company Name           3147 non-null   object 
 1   Last Sale              3079 non-null   float64
 2   Market Capitalization  3147 non-null   float64
...
nyse['Market Capitalization'].idxmax() # Chỉ mục của giá trị lớn nhất
'JNJ'
Nhập và quản lý dữ liệu tài chính bằng Python

Lấy mã cho công ty công nghệ lớn nhất

nyse['Sector'].unique() # Giá trị duy nhất dạng mảng numpy
array(['Technology', 'Health Care', ...], dtype=object)
tech = nyse.loc[nyse.Sector == 'Technology']
tech['Company Name'].head(2)
Stock Symbol                        Company Name            
ORCL                          Oracle Corporation
TSM           Taiwan Semiconductor Manufacturing
nyse.loc[nyse.Sector=='Technology', 'Market Capitalization'].idxmax()
'ORCL'
Nhập và quản lý dữ liệu tài chính bằng Python

Lấy dữ liệu cho công ty công nghệ IPO năm 2017 lớn nhất

ticker = nyse.loc[(nyse.Sector=='Technology') & (nyse['IPO Year']==2017), 
                 'Market Capitalization'].idxmax()

data = DataReader(ticker, 'yahoo') # Start: 2010/1/1 data = data.loc[:, ['Close', 'Volume']]
Nhập và quản lý dữ liệu tài chính bằng Python

Trực quan giá và khối lượng trên hai trục

import matplotlib.pyplot as plt
data.plot(title=ticker, secondary_y='Volume')
plt.tight_layout(); plt.show()

Biểu đồ xấu.png

Nhập và quản lý dữ liệu tài chính bằng Python

Hãy luyện tập!

Nhập và quản lý dữ liệu tài chính bằng Python

Preparing Video For Download...