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
| def nearest(dates, dateRef): | |
| # Get the previous days before the reference date (dateRef) | |
| prevDate = dates[dates < dateRef] | |
| return prevDate[-1] # return the last date | |
| def getReturn(period, number, ticker, dt, val): | |
| # Subset the dataset by stock index | |
| df = msi.loc[msi.ticker == ticker].reset_index() | |
| # Get all business days | |
| existingDates = df['Date'].unique() |
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
| lastDate = msi.loc[msi.Date == '2020-09-30'].reset_index().drop(['index'],axis=1) |
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
| fig, axes = plt.subplots(3,2, figsize=(12, 8),sharex=True) | |
| pagoda = ["#965757", "#D67469", "#4E5A44", "#A1B482", '#EFE482', "#99BFCF"] # for coloring | |
| for i, k in enumerate(region_idx.keys()): | |
| # Iterate for each region | |
| ax = axes[int(i/2), int(i%2)] | |
| for j,t in enumerate(region_idx[k]): | |
| # Iterate and plot for each stock index in this region | |
| ax.plot(chBegin.index, chBegin[t], marker='', linewidth=1, color = pagoda[j]) | |
| ax.legend([ticker[t] for t in region_idx[k]], loc='upper left', fontsize=7) | |
| ax.set_title(k, fontweight='bold') |
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
| # Transform the data to be ticker column-wise | |
| chBegin = msi.groupby(['Date', 'ticker'])['chBegin'].first().unstack() | |
| # Fill null values with the values on the row before | |
| chBegin = chBegin.fillna(method='bfill') |
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
| # Get the data for 4 Jan 2010 | |
| begRef = msi.loc[msi.Date == '2010-01-04'] | |
| def retBegin(ticker, val): | |
| start_val = begRef.loc[begRef.ticker == ticker, 'Close'].values[0] | |
| return (val/start_val - 1) * 100 | |
| msi['chBegin'] = msi.apply(lambda x: retBegin(x.ticker, x.Close), axis = 1) |
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
| region_idx= | |
| { 'US & Canada' : ['^GSPC', '^DJI', '^IXIC', '^RUT','^GSPTSE'], | |
| 'Latin America' : ['^BVSP', '^MXX', '^IPSA'], | |
| 'East Asia' : ['^N225', '^HSI', '000001.SS', '399001.SZ', '^TWII', '^KS11'], | |
| 'ASEAN & Oceania' : ['^STI', '^JKSE', '^KLSE','^AXJO', '^NZ50'], | |
| 'South & West Asia' : ['^BSESN', '^TA125.TA'], | |
| 'Europe' : ['^FTSE', '^GDAXI', '^FCHI', '^STOXX50E','^N100', '^BFX'] | |
| } |
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
| stock_list = [] | |
| for s in majorStockIdx.Symbol: # iterate for every stock indices | |
| # Retrieve data from Yahoo! Finance | |
| tickerData = yf.Ticker(s) | |
| tickerDf1 = tickerData.history(period='1d', start='2010-1-1', end='2020-9-30') | |
| # Save historical data | |
| tickerDf1['ticker'] = s # don't forget to specify the index | |
| stock_list.append(tickerDf1) | |
| # Concatenate all data | |
| msi = pd.concat(stock_list, axis = 0) |
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
| # Retrieving List of World Major Stock Indices from Yahoo! Finance | |
| df_list = pd.read_html('https://finance.yahoo.com/world-indices/') | |
| majorStockIdx = df_list[0] | |
| majorStockIdx.head() |
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
| !pip install yfinance # install yfinance | |
| # importing needed libraries/modules | |
| import numpy as np | |
| import pandas as pd | |
| import yfinance as yf |
NewerOlder