Created
December 16, 2018 04:41
-
-
Save oiehot/d72fe26550069634879c0dd6f33d4223 to your computer and use it in GitHub Desktop.
alphaavantage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import requests | |
import pandas as pd | |
import json | |
# https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo | |
# https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo | |
# https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo&datatype=csv | |
apikey = 'API_KEY_HERE' | |
query_url = 'https://www.alphavantage.co/query?' | |
OPEN_COLUMN = '1. open' | |
HIGH_COLUMN = '2. high' | |
LOW_COLUMN = '3. low' | |
CLOSE_COLUMN = '4. close' | |
VOLUME_COLUMN = '5. volume' | |
def get_stock_price(symbol): | |
'현재 주가를 돌려준다' | |
data = get_time_series_intraday(symbol) | |
first_key = next(iter(data)) | |
return float(data[first_key][CLOSE_COLUMN]) | |
def get_time_series_intraday(symbol, interval='5min', outputsize='compact', datatype='json'): | |
url = query_url + \ | |
'function=TIME_SERIES_INTRADAY' + \ | |
'&symbol=' + symbol + \ | |
'&interval=' + interval + \ | |
'&apikey=' + apikey + \ | |
'&outptusize=' + outputsize + \ | |
'&datatype=' + datatype | |
response = requests.get(url) | |
raw = json.loads(response.text) | |
key = "Time Series (%s)" % interval | |
return raw[key] | |
def get_time_series_daily_csv(symbol, outputsize='full', datatype='csv'): | |
'수정종가' | |
url = query_url + \ | |
'function=TIME_SERIES_DAILY_ADJUSTED' + \ | |
'&symbol=' + symbol + \ | |
'&apikey=' + apikey + \ | |
'&outputsize=' + outputsize + \ | |
'&datatype=' + datatype | |
response = requests.get(url) | |
# response.status_code | |
return response.text | |
def get_time_series_daily(symbol): | |
'csv 데이터를 얻고 DataFrame으로 변환한다.' | |
csv = get_time_series_daily_csv(symbol) | |
df = pd.read_csv(io.StringIO(csv)) | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment