[PYTHON] I tried to simulate the dollar cost averaging method

Introduction

When buying stocks and investment trusts, there is a dollar cost averaging method as a buying method. Risk can be reduced by purchasing products such as stocks and mutual funds ** on a regular basis ** at a fixed price **.

In the slides I often see, I felt that I was not convinced because it was written assuming an arbitrary transition of stock price / base price and the number of data was small. Obviously, the risk is reduced in theory. Therefore, it is an attempt to simulate based on the standard price data of actual investment trusts.

Simulation assumptions

Investment trust used for simulation

Sumitomo Mitsui Trust Assetment's Foreign Stock Index e

Evaluation index

In this simulation, we will compare the ROI one year after the start of purchase.

ROI = \frac{Assets held one year later-1 year investment amount}{1 year investment amount}

Method 1 (Bulk purchase)

I will purchase a certain amount (100,000 yen) at a certain timing.

Method 2 (Regular purchase of a certain number of units)

From a certain timing, we will purchase a certain number of units (10,000 yen) on the 25th of every month for one year.

Method 3 (fixed amount subscription)

From a certain timing, I will purchase a fixed amount (10,000 yen) on the 25th of every month for one year.

simulation

Risk evaluation is performed by changing the purchase start time in 100 ways and comparing the resulting ROI distribution.

import pandas as pd
df = pd.read_csv('http://www.smtam.jp/fund_data/csv/110057.csv', 
                 names=['day', 'price', 'income', 'asset'], skiprows=1)
df.loc[:, 'day'] = pd.to_datetime(df.day)
print df.shape
df.head(10)

image

import matplotlib
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, figsize=(12, 8))
df.plot('day', 'price', ax =ax, label=u'Base price')
ax.set_ylabel(u'Base price')
ax.set_xlabel(u'date')

image

import  numpy as np
def package(timing):
    num = []
    invest = []
    price = 100000
    for i, r in df.iterrows():
        if i == timing:
            num_add = int(10000.0 * price/ r.price)
            num.append(num[i-1] + num_add  if len(num) > 0 else num_add)
            invest.append(invest[i-1] + price if len(invest) > 0 else price)
        else:
            num.append(num[i-1] if len(num) > 0 else 0)
            invest.append(invest[i-1] if len(invest) > 0 else 0)
    return num, invest

def constant_num(start_timing):
    num = []
    invest = []
    for i, r in df.iterrows():
        if r.day.day == 25 and i >= start_timing:
            num_add = 10000
            invest_add = r.price 
            num.append(num[i-1] + num_add  if len(num) > 0 else num_add)
            invest.append(invest[i-1] + invest_add if len(invest) > 0 else invest_add)
        else:
            num.append(num[i-1] if len(num) > 0 else 0)
            invest.append(invest[i-1] if len(invest) > 0 else 0)
    return num, invest


def doller_cost(start_timing):
    num = []
    invest = []
    price = 10000
    for i, r in df.iterrows():
        if r.day.day == 25 and i >= start_timing:
            num_add = int(10000.0 * price/ r.price)
            num.append(num[i-1] + num_add  if len(num) > 0 else num_add)
            invest.append(invest[i-1] + price if len(invest) > 0 else price)
        else:
            num.append(num[i-1] if len(num) > 0 else 0)
            invest.append(invest[i-1] if len(invest) > 0 else 0)
    return num, invest


def sim(function, label, color):
    fig, ax = plt.subplots(1, figsize=(12, 8))
    final_ratio = []
    for i in np.arange(0, 100) * 10:
        num, invest = function(i)
        num = pd.Series(num)
        invest = pd.Series(invest)
        df.loc[:, 'ratio'] = (num * df.price / 10000.0 - invest.astype(float)) / invest.astype(float)
        df.plot('day', 'ratio', ax=ax,alpha=0.1, color=color)
        
        final_ratio.append(df.iloc[i + 365].ratio)
    ax.set_title(label)
    ax.set_ylabel('ROI')
    ax.set_xlabel(u'date')
    ax.legend_.remove()
    return final_ratio

package_ratio = sim(package, u'Bulk purchase', 'blue')
constant_ratio = sim(constant_num, u'Fixed number of subscriptions', 'green')
dc_ratio = sim(doller_cost, u'Fixed amount subscription', 'red')

fig, ax = plt.subplots(1, figsize=(12, 8))
pd.Series(package_ratio).hist(ax=ax, color='blue', alpha=0.3, normed=True, label=u'Bulk purchase')
pd.Series(constant_ratio).hist(ax=ax, color='green', alpha=0.3, normed=True, label=u'Fixed number of subscriptions')
pd.Series(dc_ratio).hist(ax=ax, color='red', alpha=0.3, normed=True, label=u'Fixed amount subscription')
ax.set_xlabel('ROI')
ax.set_ylabel('freq(normed)')
ax.legend()

image image image image

What I found

Recommended Posts

I tried to simulate the dollar cost averaging method
I tried to move the ball
I tried to estimate the interval.
I tried to simulate how the infection spreads with Python
I tried to simulate ad optimization using the bandit algorithm.
I tried to summarize the umask command
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
[Linux] I tried to verify the secure confirmation method of FQDN (CentOS7)
I tried to build a super-resolution method / ESPCN
I tried web scraping to analyze the lyrics.
I tried to build a super-resolution method / SRCNN ①
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried the least squares method in Python
I tried to correct the keystone of the image
I tried to build a super-resolution method / SRCNN ③
I tried to build a super-resolution method / SRCNN ②
LeetCode I tried to summarize the simple ones
I tried to implement the traveling salesman problem
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to debug.
I tried to paste
I tried to graph the packages installed in Python
I tried to detect the iris from the camera image
I tried to summarize the basic form of GPLVM
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried clustering ECG data using the K-Shape method
I tried to approximate the sin function using chainer
I tried to put pytest into the actual battle
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to solve the problem with Python Vol.1
I want to publish the product at the lowest cost
I tried to redo the non-negative matrix factorization (NMF)
I tried the simplest method of multi-label document classification
I tried to identify the language using CNN + Melspectogram
I tried to notify the honeypot report on LINE
I tried to complement the knowledge graph using OpenKE
I tried to classify the voices of voice actors
I tried to compress the image using machine learning
I tried to summarize the string operations of Python
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
I tried to organize SVM.
I tried to implement PCANet
I tried the changefinder library!
I tried to reintroduce Linux
I tried to introduce Pylint
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried to find the entropy of the image with python
I tried to find out the outline about Big Gorilla
I tried to introduce the block diagram generation tool blockdiag
I tried porting the code written for TensorFlow to Theano
[Horse Racing] I tried to quantify the strength of racehorses
I tried to analyze the whole novel "Weathering with You" ☔️