Skip to content

Instantly share code, notes, and snippets.

@ahlusar1989
Created December 29, 2022 18:56
Show Gist options
  • Save ahlusar1989/46168ba0fa2eedd84415b678c8e2ee29 to your computer and use it in GitHub Desktop.
Save ahlusar1989/46168ba0fa2eedd84415b678c8e2ee29 to your computer and use it in GitHub Desktop.
DCA Cases 1- 3: Bitcoin case study
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "-ZgrtjtLUi6a"
},
"source": [
"## <font color=\"blue\">Imports</font>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import random\n",
"# change syntax and name of Pandas DataReader import\n",
"from pandas_datareader import data as pdr\n",
"# import yfinance as pandas data-reader currently not stable\n",
"import yfinance as yf\n",
"from datetime import datetime\n",
"import datetime\n",
"\n",
"import numpy_financial as npf\n",
"from datetime import date, datetime\n",
"import plotly.express as px\n",
"\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"# set the numpy random seed to allow replication of results\n",
"np.random.seed(seed=1989)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[*********************100%***********************] 1 of 1 completed\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Close</th>\n",
" <th>Adj Close</th>\n",
" <th>Volume</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Date</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-09-17</th>\n",
" <td>465.864014</td>\n",
" <td>468.174011</td>\n",
" <td>452.421997</td>\n",
" <td>457.334015</td>\n",
" <td>457.334015</td>\n",
" <td>21056800</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-18</th>\n",
" <td>456.859985</td>\n",
" <td>456.859985</td>\n",
" <td>413.104004</td>\n",
" <td>424.440002</td>\n",
" <td>424.440002</td>\n",
" <td>34483200</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-19</th>\n",
" <td>424.102997</td>\n",
" <td>427.834991</td>\n",
" <td>384.532013</td>\n",
" <td>394.795990</td>\n",
" <td>394.795990</td>\n",
" <td>37919700</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-20</th>\n",
" <td>394.673004</td>\n",
" <td>423.295990</td>\n",
" <td>389.882996</td>\n",
" <td>408.903992</td>\n",
" <td>408.903992</td>\n",
" <td>36863600</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-21</th>\n",
" <td>408.084991</td>\n",
" <td>412.425995</td>\n",
" <td>393.181000</td>\n",
" <td>398.821014</td>\n",
" <td>398.821014</td>\n",
" <td>26580100</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Open High Low Close Adj Close \\\n",
"Date \n",
"2014-09-17 465.864014 468.174011 452.421997 457.334015 457.334015 \n",
"2014-09-18 456.859985 456.859985 413.104004 424.440002 424.440002 \n",
"2014-09-19 424.102997 427.834991 384.532013 394.795990 394.795990 \n",
"2014-09-20 394.673004 423.295990 389.882996 408.903992 408.903992 \n",
"2014-09-21 408.084991 412.425995 393.181000 398.821014 398.821014 \n",
"\n",
" Volume \n",
"Date \n",
"2014-09-17 21056800 \n",
"2014-09-18 34483200 \n",
"2014-09-19 37919700 \n",
"2014-09-20 36863600 \n",
"2014-09-21 26580100 "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# first, we need to get a hold of the appropriate data\n",
"# so we can eventually simulate our dollar cost average method\n",
"data = yf.download(\n",
"\n",
" # download only the tickers we want\n",
" # In this example I will use 'BTC-USD' which\n",
" # attempts to track the performance of BTC-USD\n",
" tickers = \"BTC-USD\",\n",
"\n",
" # What period of time are we interested in?\n",
" # valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max\n",
" # In our case we will choose 10y\n",
" period = \"10y\",\n",
"\n",
" # Next we need to define our interval.\n",
" interval = \"1d\",\n",
"\n",
" # Ensure our data is grouped properly.\n",
" group_by = 'ticker',\n",
")\n",
"\n",
"# take a peek at our data\n",
"data.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# now we need to define the parameters of our dollar cost average\n",
"\n",
"# the starting principal\n",
"p = 0\n",
"\n",
"# the amount to dollar cost average per month\n",
"monthly_add = 100\n",
"\n",
"# starting date\n",
"starting_year = 2014\n",
"starting_month = 10\n",
"\n",
"# ending date\n",
"ending_year = 2022\n",
"ending_month = 12"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 2014-10-31\n",
"1 2014-11-30\n",
"2 2014-12-31\n",
"3 2015-01-31\n",
"4 2015-02-28\n",
"dtype: datetime64[ns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# calculate the number of periods (number of months)\n",
"periods = (ending_year - starting_year - 1) * 12 + (ending_month - starting_month + 1)\n",
"\n",
"# create a date variable based on starting year and month above\n",
"starting_date = str(starting_year)+'-'+str(starting_month)+'-01'\n",
"\n",
"# use starting_date and number of periods (months) to create a series of all\n",
"# contribution dates\n",
"contribution_dates = pd.Series(pd.date_range(str(starting_date), freq=\"M\", periods=periods))\n",
"contribution_dates.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"82 2021-08-31\n",
"83 2021-09-30\n",
"84 2021-10-31\n",
"85 2021-11-30\n",
"86 2021-12-31\n",
"dtype: datetime64[ns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"contribution_dates.tail()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# If the day is not a trading day then move it one day previous and store\n",
"for i in range(len(contribution_dates)):\n",
" if contribution_dates[i] not in data.index:\n",
" contribution_dates[i] = contribution_dates[i] - pd.Timedelta(\"1 day\")\n",
"\n",
"# Once again print out the dates to see how we did\n",
"for i in range(len(contribution_dates)):\n",
" if contribution_dates[i] not in data.index:\n",
" print(contribution_dates[i], contribution_dates[i].day_name())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Iterate one more time though the list to move dates to the left\n",
"for i in range(len(contribution_dates)):\n",
" if contribution_dates[i] not in data.index:\n",
" contribution_dates[i] = contribution_dates[i] - pd.Timedelta(\"1 day\")\n",
"\n",
"# Print all the results out that are not trading days. Hopefully,\n",
"# the printout will be NULL\n",
"for i in range(len(contribution_dates)):\n",
" if contribution_dates[i] not in data.index:\n",
" print(contribution_dates[i], contribution_dates[i].day_name())"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Close</th>\n",
" <th>Adj Close</th>\n",
" <th>Volume</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Date</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2014-09-17</th>\n",
" <td>465.864014</td>\n",
" <td>468.174011</td>\n",
" <td>452.421997</td>\n",
" <td>457.334015</td>\n",
" <td>457.334015</td>\n",
" <td>21056800</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-18</th>\n",
" <td>456.859985</td>\n",
" <td>456.859985</td>\n",
" <td>413.104004</td>\n",
" <td>424.440002</td>\n",
" <td>424.440002</td>\n",
" <td>34483200</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-19</th>\n",
" <td>424.102997</td>\n",
" <td>427.834991</td>\n",
" <td>384.532013</td>\n",
" <td>394.795990</td>\n",
" <td>394.795990</td>\n",
" <td>37919700</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-20</th>\n",
" <td>394.673004</td>\n",
" <td>423.295990</td>\n",
" <td>389.882996</td>\n",
" <td>408.903992</td>\n",
" <td>408.903992</td>\n",
" <td>36863600</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2014-09-21</th>\n",
" <td>408.084991</td>\n",
" <td>412.425995</td>\n",
" <td>393.181000</td>\n",
" <td>398.821014</td>\n",
" <td>398.821014</td>\n",
" <td>26580100</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Open High Low Close Adj Close \\\n",
"Date \n",
"2014-09-17 465.864014 468.174011 452.421997 457.334015 457.334015 \n",
"2014-09-18 456.859985 456.859985 413.104004 424.440002 424.440002 \n",
"2014-09-19 424.102997 427.834991 384.532013 394.795990 394.795990 \n",
"2014-09-20 394.673004 423.295990 389.882996 408.903992 408.903992 \n",
"2014-09-21 408.084991 412.425995 393.181000 398.821014 398.821014 \n",
"\n",
" Volume \n",
"Date \n",
"2014-09-17 21056800 \n",
"2014-09-18 34483200 \n",
"2014-09-19 37919700 \n",
"2014-09-20 36863600 \n",
"2014-09-21 26580100 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Definitions\n",
"\n",
"1. **Shares** – The current number of shares of the security owned. This includes the current month’s contribution.\n",
"\n",
"2. **Principal** – This keeps track of how much principal (no investment growth) has been used to purchase the security over time.\n",
"\n",
"3. **Current Value** – This field uses the Share count and final adjusted price for the month to determine the current market value at that time.\n",
"\n",
"4. **Benchmark Return** – This keeps tabs on how much the security has gone up in terms of percentage points since the beginning of the overall time period up to that point. This number is not averaged or annualized. It’s just the raw return over the entire time period.\n",
"\n",
"5. **DCA Return** – This is the amount of return based on the running total. This is not adjusted for each month, rather it gives a total snapshot of how much has been returned. There are other ways to calculate this metric."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Close</th>\n",
" <th>Adj Close</th>\n",
" <th>Volume</th>\n",
" <th>Shares</th>\n",
" <th>Principal</th>\n",
" <th>Current Value</th>\n",
" <th>Benchmark Return</th>\n",
" <th>DCA Return</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Date</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2021-08-31</th>\n",
" <td>47024.339844</td>\n",
" <td>48189.550781</td>\n",
" <td>46750.093750</td>\n",
" <td>47166.687500</td>\n",
" <td>47166.687500</td>\n",
" <td>34730363427</td>\n",
" <td>8.335470</td>\n",
" <td>8200</td>\n",
" <td>393156.495939</td>\n",
" <td>13841.400472</td>\n",
" <td>4694.591414</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-09-30</th>\n",
" <td>41551.269531</td>\n",
" <td>44092.601562</td>\n",
" <td>41444.582031</td>\n",
" <td>43790.894531</td>\n",
" <td>43790.894531</td>\n",
" <td>31141681925</td>\n",
" <td>8.337753</td>\n",
" <td>8300</td>\n",
" <td>365117.675832</td>\n",
" <td>12843.592821</td>\n",
" <td>4299.008143</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-10-31</th>\n",
" <td>61850.488281</td>\n",
" <td>62406.171875</td>\n",
" <td>60074.328125</td>\n",
" <td>61318.957031</td>\n",
" <td>61318.957031</td>\n",
" <td>32241199927</td>\n",
" <td>8.339384</td>\n",
" <td>8400</td>\n",
" <td>511362.336962</td>\n",
" <td>18024.489588</td>\n",
" <td>5987.646869</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-11-30</th>\n",
" <td>57830.113281</td>\n",
" <td>59113.402344</td>\n",
" <td>56057.281250</td>\n",
" <td>57005.425781</td>\n",
" <td>57005.425781</td>\n",
" <td>36708594618</td>\n",
" <td>8.341138</td>\n",
" <td>8500</td>\n",
" <td>475490.142924</td>\n",
" <td>16749.507821</td>\n",
" <td>5494.001681</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2021-12-31</th>\n",
" <td>47169.371094</td>\n",
" <td>48472.527344</td>\n",
" <td>45819.953125</td>\n",
" <td>46306.445312</td>\n",
" <td>46306.445312</td>\n",
" <td>36974172400</td>\n",
" <td>8.343298</td>\n",
" <td>8600</td>\n",
" <td>386348.466671</td>\n",
" <td>13587.132440</td>\n",
" <td>4392.424031</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Open High Low Close \\\n",
"Date \n",
"2021-08-31 47024.339844 48189.550781 46750.093750 47166.687500 \n",
"2021-09-30 41551.269531 44092.601562 41444.582031 43790.894531 \n",
"2021-10-31 61850.488281 62406.171875 60074.328125 61318.957031 \n",
"2021-11-30 57830.113281 59113.402344 56057.281250 57005.425781 \n",
"2021-12-31 47169.371094 48472.527344 45819.953125 46306.445312 \n",
"\n",
" Adj Close Volume Shares Principal Current Value \\\n",
"Date \n",
"2021-08-31 47166.687500 34730363427 8.335470 8200 393156.495939 \n",
"2021-09-30 43790.894531 31141681925 8.337753 8300 365117.675832 \n",
"2021-10-31 61318.957031 32241199927 8.339384 8400 511362.336962 \n",
"2021-11-30 57005.425781 36708594618 8.341138 8500 475490.142924 \n",
"2021-12-31 46306.445312 36974172400 8.343298 8600 386348.466671 \n",
"\n",
" Benchmark Return DCA Return \n",
"Date \n",
"2021-08-31 13841.400472 4694.591414 \n",
"2021-09-30 12843.592821 4299.008143 \n",
"2021-10-31 18024.489588 5987.646869 \n",
"2021-11-30 16749.507821 5494.001681 \n",
"2021-12-31 13587.132440 4392.424031 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Establish new columns with our starting principal, number of shares\n",
"# Current Value of our shares, the running return of the selected security\n",
"# and the Dollar Cost Averaging Running Return\n",
"data['Shares'] = p / data.loc[contribution_dates.iloc[0], 'Adj Close']\n",
"data['Principal'] = p\n",
"data['Current Value'] = p\n",
"data['Benchmark Return'] = 0\n",
"data['DCA Return'] = 0\n",
"\n",
"for i in range(1, len(data.loc[contribution_dates])):\n",
"\n",
" # Calculate the number of Shares added by the contribution and add to previous month's amount\n",
" data.loc[contribution_dates.iloc[i], 'Shares'] = data.loc[\n",
" contribution_dates.iloc[i-1], 'Shares'] + (monthly_add / data.loc[contribution_dates.iloc[i], 'Adj Close'])\n",
"\n",
" # Add the monthly contribution at the beginning of each month to the principal\n",
" data.loc[contribution_dates.iloc[i], 'Principal'] = data.loc[contribution_dates.iloc[i-1], 'Principal'] + monthly_add\n",
"\n",
" # Use the present value of the security to find the total value of all shares owned\n",
" data.loc[contribution_dates.iloc[i], 'Current Value'] = data.loc[contribution_dates.iloc[i], 'Shares'] * data.loc[contribution_dates.iloc[i], 'Adj Close']\n",
"\n",
" # Calculate the running return of the security based on the current month's price (iterative)\n",
" data.loc[contribution_dates.iloc[i], 'Benchmark Return'] = (data.loc[contribution_dates.iloc[i], 'Adj Close'] - data.loc[contribution_dates.iloc[0], 'Adj Close'])/data.loc[contribution_dates.iloc[0], 'Adj Close']*100\n",
"\n",
" # Calculate the running return of the DCA balance on the current month's price (iterative)\n",
" data.loc[contribution_dates.iloc[i], 'DCA Return'] = (data.loc[contribution_dates.iloc[i], 'Current Value'] - data.loc[contribution_dates.iloc[i], 'Principal'])/data.loc[contribution_dates.iloc[i], 'Principal']*100\n",
"\n",
"# Create a new DataFrame that only has rows corresponding to the contribution dates\n",
"monthly_data = data.loc[contribution_dates]\n",
"\n",
"# Print out the last 5 months worth of results\n",
"monthly_data.tail()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"monthly_data = np.around(monthly_data, 2)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# difference between current value less the principal\n",
"monthly_data['Current Value less Principal'] = (\n",
" monthly_data['Current Value'] - monthly_data['Principal']\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Case 1: DCA with no divestment (monthly periodicity)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Total Investment $\n",
"# Total Return from September 2014 - December 2021\n",
"# IRR\n",
"\n",
"\n",
"total_investment = monthly_data.iloc[-1]['Principal']\n",
"total_dca = monthly_data['DCA Return'].sum()\n",
"x = npf.irr( monthly_data['DCA Return'].values)\n",
"# to compute IRR you need to use the following formula: IRR = (1 + x)**12 - 1\n",
"irr = (1 + x) ** 12 - 1"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Investment 8600.00\n",
"Total DCA Return 110867.87\n",
"Total IRR Return (Annualized) 11.03%\n"
]
}
],
"source": [
"print(\"Total Investment {:.2f}\".format(total_investment))\n",
"print(\"Total DCA Return {:.2f}\".format(total_dca))\n",
"print(\"Total IRR Return (Annualized) {:.2f}%\".format(irr))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Case 2: DCA + Divestment (monthly periodicity)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"monthly_data['Should we divest?'] = monthly_data[\n",
" 'Current Value less Principal'].apply(lambda x: \"Yes\" if x > 0 else \"No\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Yes 77\n",
"No 10\n",
"Name: Should we divest?, dtype: int64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"monthly_data['Should we divest?'].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Total Return in X yrs $ = Total investment + Sum of divestments\n",
"# IRR - should not change"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"total_principal_investment = monthly_data.iloc[-1]['Principal']\n",
"\n",
"total_divested = monthly_data.loc[monthly_data['Should we divest?'] == \"Yes\", \n",
" ['Current Value less Principal']].sum(axis = 0)[0]\n",
"\n",
"total_dca = monthly_data['DCA Return'].sum()\n",
"x = npf.irr( monthly_data['DCA Return'].values)\n",
"# to compute IRR you need to use the following formula: IRR = (1 + x)**12 - 1\n",
"irr = (1 + x) ** 12 - 1"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Investment: 8600.00\n",
"Total Return (Total Invested + Divested Excess Across 77 - out of 87 - monthly periods): 7426432.52\n",
"Annualized Total IRR Return (As in Case 1): 11.03%\n"
]
}
],
"source": [
"print(\"Total Investment: {:.2f}\".format(total_investment))\n",
"print(\"Total Return (Total Invested + Divested Excess Across 77 - out of 87 - monthly periods): {:.2f}\".format(\n",
" total_principal_investment + total_divested))\n",
"print(\"Annualized Total IRR Return (As in Case 1): {:.2f}%\".format(irr))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x11cbfb588>"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYkAAAD4CAYAAAAZ1BptAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAbyklEQVR4nO3df5Dc9X3f8eeLk7DPOHD8UCg6QaWONfKQUhC+Yly5nhpqJKATaRiH4Kax6lLrj0DH9iSKT+l0qJ0fyFUTGtKEGQ0QC09iII4siLEjq4hOO7RgTpFACKxwNibo+KEL0kEabowQ7/6xn4Xvrfa7v7S3+93d12Pm5nY/3x+f9+5J3/d+P79WEYGZmVk1p3Q7ADMzKy4nCTMzy+UkYWZmuZwkzMwsl5OEmZnlWtDtANrtnHPOiaVLl3Y7DDOznrJnz56/jYhFleV9lySWLl3KxMREt8MwM+spkl6oVu7mJjMzy+UkYWZmuZwkzMwsl5OEmZnlcpIwM7NcfTe6ycys3+zYO8WWnQd5aWaWxSPDbFy9gnUrRztSd0N3EpJ+Imm/pH2SJlLZWZJ2SXou/T4zlUvS7ZImJT0l6dLMedan/Z+TtD5T/pF0/sl0rGrVYWY2KHbsnWLT9v1MzcwSwNTMLJu272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EfF7mU0PAuvT4/WU2u5qnec0ST9TfkypDb2tIx3aFWtEvAK8KGlFKroSeKZocWZ8hnlqampjrH8DXC7pA+mcVwLPFjBOJP1s+n0BcB3wp+2Ks5VYUxzbgV+OiGzCegJYLmlZupu8IZ2jaHHOu3bFWuM89XWis6WIP8DHKd2iPUXp1nsfpXbas4GHgecojQA5K+3/Dyh9En8DmEmPT6c0AuPJ9HMA+I9FjTVtuwSYSOfaQRoJUcA4TwNeA84o8t8/bfsK8ENKHw6+AbyvoHH+b0ofCp4ErizAe3onpTua8r4TmXNdQ+lO50e0+f9Um+P8JqW+qGPpvb6xiLHmnaeRGLwsh5mZ5RrY5iYzM6vPScLMzHI5SZiZWS4nCTMzy+UkYWZmuZwkzMwsl5OEmZnl+v/wDe3ADAidoAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(y=monthly_data['Current Value less Principal'], x=monthly_data.index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Case 3: Compounding on the divestment"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"monthly_data['time_for_compounding'] = np.arange(1, monthly_data.shape[0] + 1)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Open</th>\n",
" <th>High</th>\n",
" <th>Low</th>\n",
" <th>Close</th>\n",
" <th>Adj Close</th>\n",
" <th>Volume</th>\n",
" <th>Shares</th>\n",
" <th>Principal</th>\n",
" <th>Current Value</th>\n",
" <th>Benchmark Return</th>\n",
" <th>DCA Return</th>\n",
" <th>Current Value less Principal</th>\n",
" <th>Should we divest?</th>\n",
" <th>time_for_compounding</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Date</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2015-06-30</th>\n",
" <td>257.04</td>\n",
" <td>267.87</td>\n",
" <td>255.95</td>\n",
" <td>263.07</td>\n",
" <td>263.07</td>\n",
" <td>44533800</td>\n",
" <td>3.08</td>\n",
" <td>800</td>\n",
" <td>809.59</td>\n",
" <td>-22.24</td>\n",
" <td>1.2</td>\n",
" <td>9.59</td>\n",
" <td>Yes</td>\n",
" <td>9</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Open High Low Close Adj Close Volume Shares \\\n",
"Date \n",
"2015-06-30 257.04 267.87 255.95 263.07 263.07 44533800 3.08 \n",
"\n",
" Principal Current Value Benchmark Return DCA Return \\\n",
"Date \n",
"2015-06-30 800 809.59 -22.24 1.2 \n",
"\n",
" Current Value less Principal Should we divest? \\\n",
"Date \n",
"2015-06-30 9.59 Yes \n",
"\n",
" time_for_compounding \n",
"Date \n",
"2015-06-30 9 "
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# sanity check\n",
"monthly_data[monthly_data['Should we divest?'] == \"Yes\"][:1]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"# rate\n",
"r = 0.05\n",
"# nmber of months in year\n",
"n = 12\n",
"\n",
"total_divestments_compounded = []\n",
"for i in range(monthly_data.shape[0]):\n",
" if monthly_data.iloc[i]['Should we divest?'] == 'Yes':\n",
" P = monthly_data.iloc[i]['Current Value less Principal']\n",
" t = (monthly_data.shape[0]) - monthly_data.iloc[i]['time_for_compounding']\n",
" final_amount = int(P) * (np.power((1 + r / n), n * t))\n",
" total_divestments_compounded.append(final_amount)\n",
" else: continue"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Investment: 8600.00\n",
"Total Return (Total Invested + with 5% compounding from the time of divestment): 21913903.29\n",
"Annualized Total IRR Return (As in Case 1 and 2): 11.03%\n"
]
}
],
"source": [
"total_compounded = sum(total_divestments_compounded)\n",
"\n",
"print(\"Total Investment: {:.2f}\".format(total_investment))\n",
"print(\"Total Return (Total Invested + with 5% compounding from the time of divestment): {:.2f}\".format(\n",
" total_principal_investment + total_compounded))\n",
"print(\"Annualized Total IRR Return (As in Case 1 and 2): {:.2f}%\".format(irr))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"lst = list(monthly_data.columns)\n",
"lst.remove('Should we divest?')\n",
"for c in lst:\n",
" monthly_data[c] = monthly_data[c].astype(int).apply(lambda x: f'{x:,}')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"monthly_data.to_excel('bitcoin_monthly_102014_122021.xlsx')"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment