เลือกหุ้นและดึงข้อมูลจาก Yahoo! Finance

การนำเข้าและจัดการข้อมูลทางการเงินใน Python

Stefan Jansen

Instructor

เลือกหุ้นตามเกณฑ์ที่กำหนด

  • ใช้ข้อมูลรายชื่อหุ้นเพื่อเลือกหุ้นที่ต้องการ
  • เกณฑ์การเลือก:
    • ตลาดหลักทรัพย์
    • กลุ่มอุตสาหกรรมหรือธุรกิจ
    • ปีที่เข้าตลาด (IPO Year)
    • มูลค่าตลาด (Market Capitalization)
การนำเข้าและจัดการข้อมูลทางการเงินใน Python

ดึง ticker ของบริษัทที่มีมูลค่าตลาดสูงสุด

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'
การนำเข้าและจัดการข้อมูลทางการเงินใน Python

ดึง ticker ของบริษัทที่มีมูลค่าตลาดสูงสุด

nyse = nyse.set_index('Stock Symbol') # Stock ticker as index
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() # Index of max value
'JNJ'
การนำเข้าและจัดการข้อมูลทางการเงินใน Python

ดึง ticker ของบริษัทเทคโนโลยีที่มีมูลค่าตลาดสูงสุด

nyse['Sector'].unique() # Unique values as numpy array
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'
การนำเข้าและจัดการข้อมูลทางการเงินใน Python

ดึงข้อมูลบริษัทเทคโนโลยีที่มูลค่าตลาดสูงสุดและ IPO ปี 2017

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']]
การนำเข้าและจัดการข้อมูลทางการเงินใน Python

แสดงราคาและปริมาณการซื้อขายบนแกนคู่

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

ugly_plot.png

การนำเข้าและจัดการข้อมูลทางการเงินใน Python

มาฝึกกันเถอะ!

การนำเข้าและจัดการข้อมูลทางการเงินใน Python

Preparing Video For Download...