{ "cells": [ { "cell_type": "markdown", "id": "velvet-smell", "metadata": {}, "source": [ "**[mark down math functions](https://twinstarinfo.blogspot.com/2019/11/bloggerblogspot-markdown-mathjax.html)** \\\\ **[Finance-data](https://github.com/FinanceData/FinanceDataReader)**\n", "\n", "- ! pip install finance-datareader\n", "- ! pip install beautifulsoup4\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "powerful-capability", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "found : D2Coding\n" ] } ], "source": [ "%matplotlib inline\n", "%config InlineBackend.figure_format = 'retina'\n", "# Loading ... Visualization Modules\n", "import matplotlib.pyplot as plt\n", "import warnings\n", "plt.style.use('seaborn')\n", "warnings.simplefilter(action='ignore', category=FutureWarning)\n", "\n", "from matplotlib.font_manager import fontManager as fm # 한글폰트 확인 및 추가\n", "font_list_check = ['D2Coding', 'NanumGothicCoding', 'NanumGothic']\n", "for _ in font_list_check:\n", " font_counts = [f for f in font_list_check if _ in [_.name for _ in fm.ttflist]]\n", " if len(font_counts) > 1: print(f\"found : {_}\"); plt.rc('font', family=_); break " ] }, { "cell_type": "code", "execution_count": 2, "id": "requested-massage", "metadata": {}, "outputs": [], "source": [ "# S&P 500 지수와 VIX 지수를 다운로드 합니다\n", "import yfinance as yf\n", "df = yf.download(['^GSPC', '^VIX'], \n", " start='1999-01-01', \n", " end='2022-12-31',\n", " progress=False)\n", "df = df[['Adj Close']].copy()\n", "df.columns = df.columns.droplevel(0)\n", "df = df.rename(columns={'^GSPC': 'sp500', '^VIX': 'vix'})" ] }, { "cell_type": "code", "execution_count": 3, "id": "invisible-control", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CloseOpenHighLowVolumeChange
Date
2021-03-25232.34235.34236.90231.6034060000.0-0.0133
2021-03-26236.48231.55236.71231.5525480000.00.0178
2021-03-29232.24236.69236.69231.915490000.0-0.0179
\n", "
" ], "text/plain": [ " Close Open High Low Volume Change\n", "Date \n", "2021-03-25 232.34 235.34 236.90 231.60 34060000.0 -0.0133\n", "2021-03-26 236.48 231.55 236.71 231.55 25480000.0 0.0178\n", "2021-03-29 232.24 236.69 236.69 231.91 5490000.0 -0.0179" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# yahoo finance 를 활용한 주가수집\n", "# loading ... the Financial DataSet\n", "import os\n", "import pandas as pd\n", "\n", "def fd_csv(file_name, code='', date_start='', date_end=''):\n", " if os.path.isfile(file_name): # Checking the File exist\n", " data = pd.read_csv(file_name)\n", " data['Date'] = pd.to_datetime(data['Date']) # datetime 포맷\n", " data = data.set_index('Date') # 변경된 컬럼을 Index 로 설정\n", " else: # if not, download the data\n", " from FinanceDataReader import DataReader as fdr\n", " data = fdr(code, date_start, date_end).reset_index()\n", " data.to_csv(file_name, index=None)\n", " data = data.set_index('Date')\n", " return data\n", "\n", "# Finance DataSet 생성하기\n", "file_name = './data/stock-msft.csv'\n", "data_csv = fd_csv(file_name, 'MSFT', date_start='2020-1-1', date_end='2021-3-31')\n", "data_csv.tail(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "racial-filing", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseVolumeChange
Date
2021-03-2581000821008080081200147588260.002469
2021-03-2681400816008100081500128457780.003695
2021-03-2981700817008100081600148353360.001227
\n", "
" ], "text/plain": [ " Open High Low Close Volume Change\n", "Date \n", "2021-03-25 81000 82100 80800 81200 14758826 0.002469\n", "2021-03-26 81400 81600 81000 81500 12845778 0.003695\n", "2021-03-29 81700 81700 81000 81600 14835336 0.001227" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# yahoo finance 를 활용한 주가수집\n", "# loading ... the Financial DataSet\n", "import os\n", "import pandas as pd\n", "\n", "def yf_csv(file_name, code='', date_start='', date_end=''):\n", " if os.path.isfile(file_name): # Checking the File exist\n", " data = pd.read_csv(file_name)\n", " data['Date'] = pd.to_datetime(data['Date']) # datetime 포맷 변경\n", " data = data.set_index('Date') # 변경된 컬럼을 Index 로 설정\n", " else: # if not, download the data\n", " import yfinance as yf\n", " data = yf.download(code, start=date_start, end=date_end,\n", " progress=False, auto_adjust=True).reset_index()\n", " data.to_csv(file_name, index=None)\n", " data = data.set_index('Date')\n", " return data\n", "\n", "# Finance DataSet 생성하기\n", "file_name = './data/stock-samsung.csv'\n", "data_csv = yf_csv(file_name, '005930', date_start='2020-1-1', date_end='2021-3-31')\n", "data_csv.tail(3)" ] }, { "cell_type": "code", "execution_count": 12, "id": "fitted-calculator", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseVolumeChange
Date
2021-03-238260082900818008180013299907-0.002439
2021-03-248100081600807008100017926638-0.009780
2021-03-2581000821008080081200147588260.002469
2021-03-2681400816008100081500128457780.003695
2021-03-2981700817008100081600148353360.001227
\n", "
" ], "text/plain": [ " Open High Low Close Volume Change\n", "Date \n", "2021-03-23 82600 82900 81800 81800 13299907 -0.002439\n", "2021-03-24 81000 81600 80700 81000 17926638 -0.009780\n", "2021-03-25 81000 82100 80800 81200 14758826 0.002469\n", "2021-03-26 81400 81600 81000 81500 12845778 0.003695\n", "2021-03-29 81700 81700 81000 81600 14835336 0.001227" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_csv['2021-MAR-1':'2021-MAY-1'].tail()" ] }, { "cell_type": "code", "execution_count": null, "id": "emerging-novelty", "metadata": {}, "outputs": [], "source": [ "data_csv" ] }, { "cell_type": "code", "execution_count": 6, "id": "executed-vegetarian", "metadata": {}, "outputs": [], "source": [ "import os\n", "import pandas as pd\n", "\n", "class StockCSV:\n", " \n", " #def __init__(self):\n", " # self.code = '005930'\n", " \n", " def finance(self, code='', date_start='', date_end='', file_name=None):\n", " r'''Finance Data Reacer : CSV file save & loader\n", " https://github.com/FinanceData/FinanceDataReader/blob/master/README.md'''\n", " # case 1 : 파일을 지정한 경우\n", " if file_name:\n", " ## 01 Loading from CSV\n", " if os.path.isfile(file_name): # Checking the File exist\n", " data = pd.read_csv(file_name)\n", " data['Date'] = pd.to_datetime(data['Date']) # datetime 포맷\n", " data = data.set_index('Date') # 변경된 컬럼을 Index 로 설정\n", " ## 02 Loading from Py Module API\n", " else: # if not, download the data\n", " from FinanceDataReader import DataReader as fdr\n", " data = fdr(code, date_start, date_end).reset_index()\n", " data.to_csv(file_name, index=None)\n", " data = data.set_index('Date')\n", " # case 2 : 파일을 지정하지 않은경우\n", " else:\n", " from FinanceDataReader import DataReader as fdr\n", " data = fdr(code, date_start, date_end) \n", " return data\n", " \n", " \n", " def yahoo(self, code='', date_start='', date_end='', file_name=None):\n", " r'''Yahoo Finance : CSV file save & loader'''\n", " # case 1 : 파일을 지정한 경우\n", " if file_name:\n", " ## 01 Loading from CSV\n", " if os.path.isfile(file_name): # Checking the File exist\n", " data = pd.read_csv(file_name)\n", " data['Date'] = pd.to_datetime(data['Date']) # datetime 포맷 변경\n", " data = data.set_index('Date') # 변경된 컬럼을 Index 로 설정\n", " ## 02 Loading from Py Module API\n", " else: # if not, download the data\n", " import yfinance as yf\n", " data = yf.download(code, start=date_start, end=date_end,\n", " progress=False, auto_adjust=True).reset_index()\n", " data.to_csv(file_name, index=None)\n", " data = data.set_index('Date')\n", " # case 2 : 파일을 지정하지 않은경우\n", " else:\n", " import yfinance as yf\n", " data = yf.download(code, start=date_start, end=date_end,\n", " progress=False, auto_adjust=True) #.reset_index()\n", " return data " ] }, { "cell_type": "code", "execution_count": 7, "id": "departmental-abortion", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseVolumeChange
Date
2020-01-025550056000550005520012993228-0.010753
2020-01-0356000566005490055500154222550.005435
2020-01-0654900556005460055500102789510.000000
2020-01-0755700564005560055800100097780.005405
2020-01-0856200574005590056800235011710.017921
.....................
2020-12-2372400740007230073900194113260.022130
2020-12-2474100788007400077800325028700.052774
2020-12-2879000801007820078700400850440.011568
2020-12-297880078900773007830030339449-0.005083
2020-12-3077400813007730081000294174210.034483
\n", "

248 rows × 6 columns

\n", "
" ], "text/plain": [ " Open High Low Close Volume Change\n", "Date \n", "2020-01-02 55500 56000 55000 55200 12993228 -0.010753\n", "2020-01-03 56000 56600 54900 55500 15422255 0.005435\n", "2020-01-06 54900 55600 54600 55500 10278951 0.000000\n", "2020-01-07 55700 56400 55600 55800 10009778 0.005405\n", "2020-01-08 56200 57400 55900 56800 23501171 0.017921\n", "... ... ... ... ... ... ...\n", "2020-12-23 72400 74000 72300 73900 19411326 0.022130\n", "2020-12-24 74100 78800 74000 77800 32502870 0.052774\n", "2020-12-28 79000 80100 78200 78700 40085044 0.011568\n", "2020-12-29 78800 78900 77300 78300 30339449 -0.005083\n", "2020-12-30 77400 81300 77300 81000 29417421 0.034483\n", "\n", "[248 rows x 6 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reader = StockCSV()\n", "# reader.finance('005930', '2020-01-01','2021-01-01', './data/test.csv')\n", "reader.finance('005930', '2020-01-01','2021-01-01').tail()" ] }, { "cell_type": "markdown", "id": "diverse-fundamental", "metadata": {}, "source": [ "### **2) BOK EOS 한국은행 통계정보 API**\n", "- **[한국은행 API Key 확인하기](https://ecos.bok.or.kr/jsp/openapi/OpenApiController.jsp?t=myAuthKey#)**\n", "- **[한국은행 통계코드 검색](https://ecos.bok.or.kr/jsp/openapi/OpenApiController.jsp?t=guideStatCd&menuGroup=MENU000004&menuCode=MENU000024)**\n", "- **[Python Tutorial](https://ecos.bok.or.kr/jsp/openapi/OpenApiController.jsp?t=guideDevGuide&menuGroup=MENU000004&menuCode=MENU000017)**\n", "- **[Python Example](https://hayjo.tistory.com/57)** // **[파이썬 자료수집 예시](https://bjecondata.blogspot.com/2019/08/open-api-ecos-ii.html)**" ] }, { "cell_type": "code", "execution_count": 8, "id": "considered-fossil", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
통계명항목명1통계코드항목코드1항목코드2항목명2항목코드3항목명3단위URL
01.1.주요 통화금융지표예금은행요구불예금회전율010Y002AAAA5NaNNaNNaNNaN회/월http://ecos.bok.or.kr/api/StatisticSearch/APIC...
11.1.주요 통화금융지표화폐발행잔액(말잔)010Y002AAAA11NaNNaNNaNNaN십억원http://ecos.bok.or.kr/api/StatisticSearch/APIC...
21.1.주요 통화금융지표화폐발행잔액(평잔)010Y002AAAA12NaNNaNNaNNaN십억원http://ecos.bok.or.kr/api/StatisticSearch/APIC...
\n", "
" ], "text/plain": [ " 통계명 항목명1 통계코드 항목코드1 항목코드2 항목명2 항목코드3 항목명3 단위 \\\n", "0 1.1.주요 통화금융지표 예금은행요구불예금회전율 010Y002 AAAA5 NaN NaN NaN NaN 회/월 \n", "1 1.1.주요 통화금융지표 화폐발행잔액(말잔) 010Y002 AAAA11 NaN NaN NaN NaN 십억원 \n", "2 1.1.주요 통화금융지표 화폐발행잔액(평잔) 010Y002 AAAA12 NaN NaN NaN NaN 십억원 \n", "\n", " URL \n", "0 http://ecos.bok.or.kr/api/StatisticSearch/APIC... \n", "1 http://ecos.bok.or.kr/api/StatisticSearch/APIC... \n", "2 http://ecos.bok.or.kr/api/StatisticSearch/APIC... " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# https://bjecondata.blogspot.com/2019/08/open-api-ecos-ii.html\n", "import pandas as pd\n", "code_table = pd.read_csv('./data/BOK_ECOS_EXAMPLE.csv')\n", "code_table.head(3)" ] }, { "cell_type": "code", "execution_count": 9, "id": "asian-sight", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
통계명항목명1통계코드항목코드1항목코드2항목명2항목코드3항목명3단위URL
09.2.3 소비자동향조사(기대 인플레이션율)(전국)물가인식040Y003FMANaNNaNNaNNaNNaNhttp://ecos.bok.or.kr/api/StatisticSearch/APIC...
19.2.3 소비자동향조사(기대 인플레이션율)(전국)<0%040Y003FMAAANaNNaNNaNNaNNaNhttp://ecos.bok.or.kr/api/StatisticSearch/APIC...
29.2.3 소비자동향조사(기대 인플레이션율)(전국)0~1%040Y003FMAABNaNNaNNaNNaNNaNhttp://ecos.bok.or.kr/api/StatisticSearch/APIC...
\n", "
" ], "text/plain": [ " 통계명 항목명1 통계코드 항목코드1 항목코드2 항목명2 항목코드3 항목명3 \\\n", "0 9.2.3 소비자동향조사(기대 인플레이션율)(전국) 물가인식 040Y003 FMA NaN NaN NaN NaN \n", "1 9.2.3 소비자동향조사(기대 인플레이션율)(전국) <0% 040Y003 FMAAA NaN NaN NaN NaN \n", "2 9.2.3 소비자동향조사(기대 인플레이션율)(전국) 0~1% 040Y003 FMAAB NaN NaN NaN NaN \n", "\n", " 단위 URL \n", "0 NaN http://ecos.bok.or.kr/api/StatisticSearch/APIC... \n", "1 NaN http://ecos.bok.or.kr/api/StatisticSearch/APIC... \n", "2 NaN http://ecos.bok.or.kr/api/StatisticSearch/APIC... " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 필요한 자료코드 추출하기\n", "search_text = '인플레'\n", "code_index = [no for no, _ in enumerate(code_table['통계명'].to_list())\n", " if _.find(search_text) != -1]\n", "code_example = code_table.iloc[code_index, :].reset_index(drop=True)\n", "code_example.head(3)\n", "# code_example['URL'][:3].to_list()" ] }, { "cell_type": "code", "execution_count": 10, "id": "enormous-bunny", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
cpi
Date
2002-02-014.4
2002-03-014.3
2002-04-014.3
\n", "
" ], "text/plain": [ " cpi\n", "Date \n", "2002-02-01 4.4\n", "2002-03-01 4.3\n", "2002-04-01 4.3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from data.finance import bok_eos_api\n", "eos_table = bok_eos_api('040Y003', date_end='20210101', code2='FMB') # 7년 단위로만 추출가능\n", "eos_table['DATA_VALUE'] = list(map(lambda x : float(x), eos_table['DATA_VALUE']))\n", "eos_table = eos_table.rename(columns={'DATA_VALUE':'cpi', 'TIME':'Date'})\n", "\n", "eos_table['Date'] = pd.to_datetime(eos_table['Date'], format='%Y%m')\n", "eos_table = eos_table.set_index('Date') # eos_table.head(3)\n", "eos_table = eos_table[['cpi']]\n", "# ploting the DataSet\n", "eos_table.head(3)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 5 }