Course materials · AI Applications

1. Revisit the meaning of coding (programming)

English translation

Contents

!pip install yfinance
Collecting yfinance
  Downloading yfinance-0.1.63.tar.gz (26 kB)
Requirement already satisfied: pandas>=0.24 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.1.5)
Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.7/dist-packages (from yfinance) (1.19.5)
Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.7/dist-packages (from yfinance) (2.23.0)
Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/dist-packages (from yfinance) (0.0.9)
Collecting lxml>=4.5.1
  Downloading lxml-4.6.3-cp37-cp37m-manylinux2014_x86_64.whl (6.3 MB)
     |████████████████████████████████| 6.3 MB 16.3 MB/s 
[?25hRequirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24->yfinance) (2.8.2)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=0.24->yfinance) (2018.9)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance) (1.15.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (2021.5.30)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (1.24.3)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20->yfinance) (2.10)
Building wheels for collected packages: yfinance
  Building wheel for yfinance (setup.py) ... [?25l[?25hdone
  Created wheel for yfinance: filename=yfinance-0.1.63-py2.py3-none-any.whl size=23918 sha256=493432dc80209ff2b0e51db0bd76b98443dce0d6dc35edd47ee070faf2cce10a
  Stored in directory: /root/.cache/pip/wheels/fe/87/8b/7ec24486e001d3926537f5f7801f57a74d181be25b11157983
Successfully built yfinance
Installing collected packages: lxml, yfinance
  Attempting uninstall: lxml
    Found existing installation: lxml 4.2.6
    Uninstalling lxml-4.2.6:
      Successfully uninstalled lxml-4.2.6
Successfully installed lxml-4.6.3 yfinance-0.1.63
Mounted at /content/drive

1.1 The main purpose of coding == 'Automation'

Automation

1.2 Automation example 1 - Automatically retrieve and plot KOSPI closing prices

from matplotlib import pyplot as plt
import yfinance as yf

# Daily KOSPI Adjusted Close. Internet access is required on the first run.
df = yf.download("^KS11", period="5y", auto_adjust=False,
                 multi_level_index=False, progress=False)
if df.empty:
    raise RuntimeError("KOSPI 데이터를 받지 못했습니다. 네트워크 연결을 확인한 뒤 다시 실행하세요.")
y_values = df['Adj Close']
x_values = range(len(y_values))

plt.plot(x_values, y_values)
plt.show()
[*********************100%***********************]  1 of 1 completed
<Figure size 432x288 with 1 Axes>
실행 결과

1.3 Automation example 2 - Automatically retrieve news headlines

import urllib.request
import xml.etree.ElementTree as ET

feed_url = 'https://www.yna.co.kr/rss/news.xml'
with urllib.request.urlopen(feed_url, timeout=15) as response:
    feed = ET.fromstring(response.read())

headlines = [item.findtext('title', default='').strip()
             for item in feed.findall('./channel/item')[:20]]
for title in headlines:
    print(title)