파이썬에서 국제 원자재(금, 은, 구리, 원유)의 가격을 가져와 다루는 방법
%matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (14,4)
plt.rcParams['axes.grid'] = True
import pandas as pd
import pandas_datareader as pdr
Gold Fixing Price in London Bullion Market (based in U.S. Dollar)
FRED (세인트루이스 연방준비은행 경제 데이터)
df_gold = pdr.DataReader('GOLDAMGBD228NLBM', 'fred', start='2000-01-01')
print('row count:', len(df_gold))
df_gold.tail(10)
row count: 4605
GOLDAMGBD228NLBM | |
---|---|
DATE | |
2017-08-14 | 1281.10 |
2017-08-15 | NaN |
2017-08-16 | 1270.15 |
2017-08-17 | 1285.90 |
2017-08-18 | 1295.25 |
2017-08-21 | 1287.60 |
2017-08-22 | 1285.10 |
2017-08-23 | 1286.45 |
2017-08-24 | 1285.90 |
2017-08-25 | 1287.05 |
df_gold.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff7977827f0>
# 영국 은(silver)
df_silver = pdr.DataReader('LBMA/SILVER', 'quandl', start='2000-01-01')
print('row count:', len(df_silver))
df_silver.tail(10)
row count: 4462
USD | GBP | EURO | |
---|---|---|---|
Date | |||
2000-01-17 | 5.1250 | 3.13647 | 5.07175 |
2000-01-14 | 5.0950 | 3.09915 | 4.97802 |
2000-01-13 | 5.0900 | 3.09611 | 4.94895 |
2000-01-12 | 5.1450 | 3.12386 | 4.99386 |
2000-01-11 | 5.1550 | 3.13755 | 5.00729 |
2000-01-10 | 5.1450 | 3.14390 | 5.03178 |
2000-01-07 | 5.1350 | 3.12158 | 4.99028 |
2000-01-06 | 5.1400 | 3.11515 | 4.94231 |
2000-01-05 | 5.2850 | 3.22354 | 5.10135 |
2000-01-04 | 5.3025 | 3.23225 | 5.13808 |
df_silver['USD'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff796b736d8>
# LME 구리 가격
df_copper = pdr.DataReader('LME/PR_CU', 'quandl', start='2000-01-01')
print('row count:', len(df_copper))
df_copper.tail(10)
row count: 1409
CashBuyer | CashSeller&Settlement | 3monthsBuyer | 3monthsSeller | 15monthsBuyer | 15monthsSeller | Dec1Buyer | Dec1Seller | Dec2Buyer | Dec2Seller | Dec3Buyer | Dec3Seller | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | ||||||||||||
2012-01-16 | 8020.0 | 8021.0 | 8025.0 | 8025.5 | NaN | NaN | 7990.0 | 8000.0 | 7905.0 | 7915.0 | 7820.0 | 7830.0 |
2012-01-13 | 7965.0 | 7965.5 | 7979.0 | 7980.0 | NaN | NaN | 7945.0 | 7955.0 | 7845.0 | 7855.0 | 7740.0 | 7750.0 |
2012-01-12 | 7945.5 | 7946.0 | 7960.0 | 7961.0 | NaN | NaN | 7915.0 | 7925.0 | 7820.0 | 7830.0 | 7715.0 | 7725.0 |
2012-01-11 | 7689.0 | 7689.5 | 7700.0 | 7700.5 | NaN | NaN | 7650.0 | 7660.0 | 7560.0 | 7570.0 | 7470.0 | 7480.0 |
2012-01-10 | 7640.5 | 7641.0 | 7650.0 | 7652.0 | NaN | NaN | 7600.0 | 7610.0 | 7525.0 | 7535.0 | 7435.0 | 7445.0 |
2012-01-09 | 7502.5 | 7503.0 | 7507.5 | 7508.0 | NaN | NaN | 7475.0 | 7485.0 | 7400.0 | 7410.0 | 7315.0 | 7325.0 |
2012-01-06 | 7514.0 | 7515.0 | 7539.0 | 7540.0 | NaN | NaN | 7520.0 | 7530.0 | 7450.0 | 7460.0 | 7380.0 | 7390.0 |
2012-01-05 | 7470.0 | 7471.0 | 7485.0 | 7485.5 | NaN | NaN | 7460.0 | 7470.0 | 7380.0 | 7390.0 | 7300.0 | 7310.0 |
2012-01-04 | 7651.5 | 7652.0 | 7665.0 | 7665.5 | NaN | NaN | 7620.0 | 7630.0 | 7530.0 | 7540.0 | 7440.0 | 7450.0 |
2012-01-03 | 7660.0 | 7661.0 | 7679.5 | 7680.0 | NaN | NaN | 7640.0 | 7650.0 | 7560.0 | 7570.0 | 7470.0 | 7480.0 |
df_copper['CashSeller&Settlement'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff796cd50b8>
국제 원유 가격을 대표하는 3대 유종이 있으며, 생산지와 대표적인 소비지역이 다르다.
FRED 링크는 각각 다음과 같다.
# 서부 텍사스 중질유(WTI Crude), monthly
df_wt = pdr.DataReader('POILWTIUSDM', 'fred', start='2000-01-01')
print('row count:', len(df_wt))
df_wt.tail(10)
row count: 210
POILWTIUSDM | |
---|---|
DATE | |
2016-09-01 | 45.167273 |
2016-10-01 | 49.885238 |
2016-11-01 | 45.623182 |
2016-12-01 | 52.013636 |
2017-01-01 | 52.559545 |
2017-02-01 | 53.401500 |
2017-03-01 | 49.584783 |
2017-04-01 | 51.168000 |
2017-05-01 | 48.559565 |
2017-06-01 | 45.167727 |
df_wt.columns = ['WTI Crude']
df_wt.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff796cdf438>
# 두바이유 (Dubai Crude), monthly
df_du = pdr.DataReader('POILDUBUSDM', 'fred', start='2000-01-01')
print('row count:', len(df_du))
df_du.tail(10)
row count: 210
POILDUBUSDM | |
---|---|
DATE | |
2016-09-01 | 43.817273 |
2016-10-01 | 48.263333 |
2016-11-01 | 43.775000 |
2016-12-01 | 51.762727 |
2017-01-01 | 53.434545 |
2017-02-01 | 54.171000 |
2017-03-01 | 51.164348 |
2017-04-01 | 52.447500 |
2017-05-01 | 50.304348 |
2017-06-01 | 46.336818 |
df_du.columns = [' Dubai Crude']
df_du.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff796c6ea20>
DataReader 함수의 항목을 리스트로 전달하면, 한번에 여러 항목의 데이터를 가져올 수 있다
# POILDUBUSDM: 두바이유
# POILWTIUSDM: 서부 텍사스 중질유
df_oil = pdr.DataReader(['POILDUBUSDM', 'POILWTIUSDM'], 'fred', start='2000-01-01')
print('row count:', len(df_oil))
df_oil.tail(10)
row count: 210
POILDUBUSDM | POILWTIUSDM | |
---|---|---|
DATE | ||
2016-09-01 | 43.817273 | 45.167273 |
2016-10-01 | 48.263333 | 49.885238 |
2016-11-01 | 43.775000 | 45.623182 |
2016-12-01 | 51.762727 | 52.013636 |
2017-01-01 | 53.434545 | 52.559545 |
2017-02-01 | 54.171000 | 53.401500 |
2017-03-01 | 51.164348 | 49.584783 |
2017-04-01 | 52.447500 | 51.168000 |
2017-05-01 | 50.304348 | 48.559565 |
2017-06-01 | 46.336818 | 45.167727 |
df_oil.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff796d63160>
df_oil['diff'] = df_oil['POILDUBUSDM'] - df_oil['POILWTIUSDM']
df_oil.tail(10)
POILDUBUSDM | POILWTIUSDM | diff | |
---|---|---|---|
DATE | |||
2016-09-01 | 43.817273 | 45.167273 | -1.350000 |
2016-10-01 | 48.263333 | 49.885238 | -1.621905 |
2016-11-01 | 43.775000 | 45.623182 | -1.848182 |
2016-12-01 | 51.762727 | 52.013636 | -0.250909 |
2017-01-01 | 53.434545 | 52.559545 | 0.875000 |
2017-02-01 | 54.171000 | 53.401500 | 0.769500 |
2017-03-01 | 51.164348 | 49.584783 | 1.579565 |
2017-04-01 | 52.447500 | 51.168000 | 1.279500 |
2017-05-01 | 50.304348 | 48.559565 | 1.744783 |
2017-06-01 | 46.336818 | 45.167727 | 1.169091 |
df_oil['diff'].plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7ff791329128>
가격의 차이를 좀 더 분명하게 표현해 보자. 0을 기준으로 낮으면 파란색, 높으면 빨간색으로 표시한다.
ax = df_oil['diff'].plot(color='k')
x = df_oil.index
y = df_oil['diff']
ax.fill_between(x, 0, y, where=y > 0, facecolor='r', alpha=0.6, interpolate=True)
ax.fill_between(x, 0, y, where=y <= 0, facecolor='b', alpha=0.6, interpolate=True)
<matplotlib.collections.PolyCollection at 0x7ff79114bef0>