## 온라인에는 정보 소스가 많은데,, quandl yahoo wikipedia google 모두 api를 가지고 있다.. 데이터를 가져오는 것이다. ## api를 거져오는 방법인듯 싶다. import matplotlib.pyplot as plt import numpy as np import urllib #url과 웹 요청에 관련된 모듈들 import matplotlib.dates as mdates
## 9 converting date from the internet def bytespdate2num(fmt, encoding='utf-8'): # fmt는 fromat을 의미하는데,, 아래의%Y%m%d따위를 의미하는 듯 싶다, strconverter = mdates.strpdate2num(fmt) def bytesconverter(b): s = b.decode(encoding) return strconverter(s) return bytesconverter
## 8 getting data from the internet def graph_data(stock): stock_price_url ='https://pythonprogramming.net/yahoo_finance_replacement' source_code = urllib.request.urlopen(stock_price_url).read().decode() #Sentex가 말했는데, decode()는 필요없을 가능성이 높지만, 혹시모르니까 놓는다고 햇다 stock_data = [] #empty list split_source = source_code.split('\n') #변수에 split을 쓰는 이유는, 이 자료들을 정리 하고 나누기 위해서 제목을 이렇게 지은것이다... junk들을 버리고 #.split()은 문자열을 분할하는 속성 for line in split_source[1:]: # 문서에 컴마가 나오는데 그걸 통해서 무엇이 있고 나누고 junk는 버리는 작업 split_line = line.split(',') if len(split_line) == 7: #항목이 7개라는 말이다 if 'values' not in line: stock_data.append(line)
date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt(stock_data, #tsla테슬라에 있는 항목들이다. 7개 delimiter=',', unpack=True, # %y = full year. 2015 # %y = partial year 15 # %m = number month # %d = number day # %H = hours # %M = minutes # %S = seconds # 12-06-2014 # %m-%d-%Y converters={0: bytespdate2num('%Y-%m-%d')})
plt.plot_date(date, closep,'-', label='Price')
plt.xlabel('Date') #x축 plt.ylabel('Price') #y축 plt.title('Interesting Graph\n check it out') #title plt.legend() #옆에 label 붙이는 것 plt.show() #실행 graph_data('TSLA')