Updated monthly signal for Relative Strength Investment on My Blog However, until now, I had to manually acquire data and calculate in Excel.
This time, I wrote python code to judge the monthly signal. I'm a beginner with python for several months, so please forgive me though it may be unsightly code. As far as I can see, it seems to be working properly.
[Monthly_check]RS_signal.ipynb
import numpy as np
import pandas as pd
from datetime import datetime
import urllib.request
#Obtain and save investment trust data csv from the SMTAM website.
#To minimize scraping, once scraped, save it as a csv file.
#JE:Japanese stocks, EE:Emerging market equities, IE:Developed country stocks, JB:Japanese bond, EB:Emerging Market Bonds, IB:Developed Government Bonds, IR:Developed country REIT, JR:Nippon REIT
url_list = {'JE':'https://www.smtam.jp/chart_data/140833/140833.csv',
'EE':'https://www.smtam.jp/chart_data/140841/140841.csv',
'IE':'https://www.smtam.jp/chart_data/140834/140834.csv',
'JB':'https://www.smtam.jp/chart_data/140835/140835.csv',
'EB':'https://www.smtam.jp/chart_data/140842/140842.csv',
'IB':'https://www.smtam.jp/chart_data/140836/140836.csv',
'IR':'https://www.smtam.jp/chart_data/140838/140838.csv',
'JR':'https://www.smtam.jp/chart_data/140837/140837.csv'}
for key in url_list:
url = url_list[key]
title = "{0}.csv".format(key)
urllib.request.urlretrieve(url,title)
[Monthly_check]RS_signal.ipynb
#Create data that summarizes the cumulative base prices of all funds
assets = ['JE','EE','IE','JB','EB','IB','JR','IR']
df_all = pd.DataFrame()
for asset in assets:
asset_file = "{0}.csv".format(asset)
df = pd.read_csv(asset_file, skiprows = [0], names = ['date','nav', 'div', 'aum'],
parse_dates = True, index_col = 0)
df['div'] = pd.to_numeric(df['div'],errors='coerce')
df['div'] = df['div'].fillna(0)
df['cum_nav'] = (df['nav'] + df['div']) / df['nav'].shift(1)
df[asset] = df['cum_nav'].cumprod()
df_all[asset] = df[asset]
#Signal judgment by converting daily data to monthly data
dfm = df_all.resample('M').ffill()
dfm = dfm[dfm.index < datetime.now()]
calc = pd.DataFrame(columns = ['JE','EE','IE','JB','EB','IB','JR','IR'])
calc.loc['asset class'] = ['Japanese stocks','Emerging market stocks','Developed country stocks','Japan bond','Emerging market bonds',
'Developed government bonds','Nippon REIT','Developed country REIT']
calc.loc['3 months'] = (dfm.iloc[-1] / dfm.iloc[-4] -1)*100
calc.loc['6 months'] = (dfm.iloc[-1] / dfm.iloc[-7] -1)*100
calc.loc['12 months'] = (dfm.iloc[-1] / dfm.iloc[-13] -1)*100
calc.loc['mean'] = (calc.loc['3 months']+calc.loc['6 months']+calc.loc['12 months'])/3
calc.loc['rank'] = calc.loc['mean'].rank(ascending = False).astype(int)
calc.loc['latest nav'] = dfm.iloc[-1]
calc.loc['12ma NAV'] = dfm.iloc[-12:].mean()
calc.loc['Buy/Sell'] = np.where(calc.loc['latest nav'] > calc.loc['12ma NAV'], 'Buy', 'Sell')
#Display monthly signal judgment result
date = dfm.index.max()
print(str(date.year) + 'Year' + str(date.month) + 'The signal at the end of the month is as follows.')
calc.T.set_index('rank')[['asset class', 'Buy/Sell']].sort_index()
When executed, the following will be displayed (should be) on the Jupyter Notebook.
Here is a blog post that reflects the above signal. ↓ [Relative Strength Investment Monthly Signal Commentary (based on the end of March 2020)]
Monthly Signal Excel Update It took decades of manual work to complete. It has to be used for decades to be cost effective.
The code that judges the signal is really amateurish. I would like to improve when I improve.
Recommended Posts