Loading Financial Data from the Web
Ticker Symbol (회사를 나타내는 표식(약자))
- BA: Boeing (US Stock)
- MSFT: Microsoft Corp (US Stock)
- ^DJI: Dow Jones Industrial Average (US Stock Index)
- EURUSD=X: Exchange Rate for Currency Pair EUR/USD (Forex)
- GC=F: Gold Price (Precious Metal / Commodity)
- BTC-USD: Bitcoin in USD (Cryptocurrency)
import pandas as pd
#pip install yfinance on your Conda terminal first.
import yfinance as yf
from datetime import date
symbol = ["BA", "MSFT", "^DJI", "EURUSD=X", "GC=F", "BTC-USD"]
start = "2014-10-01"
end = date.today()
df = yf.download(symbol, start, end)
df
#save to csv file
df.to_csv("multi_assets.csv")
Inspection
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.float_format = '{:.4f}'.format
plt.style.use("seaborn")
df = pd.read_csv("multi_assets.csv", header = [0, 1], index_col = 0, parse_dates = [0])
df
df.Close # outer index level
close = df.Close.copy() # select daily close prices only
close.describe()
Visualization
close.BA.dropna().plot(figsize = (15, 8), fontsize = 13)
plt.legend(fontsize = 13)
plt.show()
close.dropna().plot(figsize = (15, 8), fontsize = 13)
plt.legend(fontsize = 13)
plt.show()
Normalizing (정규화)
close.BA.div(close.iloc[0,0]).mul(100)
close.iloc[0] # first Price all tickers
norm = close.div(close.iloc[0]).mul(100)
norm
Another Example
- Load Stock Price Data for General Electric (GE) and another ticker symbol of your choice from 2015-01-02 until 2020-12-31.
Go to https://finance.yahoo.com/ and get the right ticker symbol. For instruments traded outside the US, you have to add a country/exchange suffix.
Check the suffix list here https://help.yahoo.com/kb/exchanges-data-providers-yahoo-finance-sln2310.html As an example, the suffix for the National Indian Stock Exchange is .NS -> Ticker Symbol for Reliance is Reliance.NS
- Select Close prices only and create a price chart for GE.
- Normalize the stock prices for GE and the Ticker Symbol of your choice (Base Value: 1) and visualize! What´s the final normalized price for GE on 2020-12-30?
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
pd.options.display.float_format = '{:.4f}'.format
plt.style.use("seaborn")
start = "2015-01-02"
end = "2020-12-31"
symbol = ["GE", "Reliance.NS"]
df = yf.download(symbol, start, end)
df
close = df.Close.copy()
close
close.GE.dropna().plot(figsize = (15, 8), fontsize = 13)
plt.legend(fontsize = 13)
plt.show()
norm = close.div(close.iloc[0]).mul(1)
norm
norm.dropna().plot(figsize = (15, 8), fontsize = 13)
plt.legend(fontsize = 13)
plt.show()