<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "import datetime\n",
    "import time\n",
    "\n",
    "from dateutil import rrule\n",
    "from dateutil.parser import parse\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.ticker import FuncFormatter\n",
    "\n",
    "from beancount.core import data\n",
    "from beancount.ops import holdings\n",
    "from beancount import loader\n",
    "from beancount.reports import holdings_reports\n",
    "\n",
    "plt.style.use('ggplot')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "filename = '/home/alex/finance/money.beancount'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "entries, errors, options_map = loader.load_file(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for entry in entries:\n",
    "    if isinstance(entry, data.Transaction):\n",
    "        dtstart = entry.date\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "net_worths_dict = collections.defaultdict(list)\n",
    "index = 0\n",
    "current_entries = []\n",
    "dtend = datetime.date.today()\n",
    "period = rrule.rrule(rrule.WEEKLY, byweekday=rrule.FR, dtstart=dtstart, until=dtend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "    for dtime in period:\n",
    "        date = dtime.date()\n",
    "        logging.info(date)\n",
    "\n",
    "        # Append new entries until the given date.\n",
    "        while True:\n",
    "            entry = entries[index]\n",
    "            if entry.date >= date:\n",
    "                break\n",
    "            current_entries.append(entry)\n",
    "            index += 1\n",
    "\n",
    "        # Get the list of holdings.\n",
    "        raw_holdings_list, price_map = holdings_reports.get_assets_holdings(current_entries,\n",
    "                                                                            options_map)\n",
    "\n",
    "        # Convert the currencies.\n",
    "        for currency in options_map['operating_currency']:\n",
    "            holdings_list = holdings.convert_to_currency(price_map,\n",
    "                                                         currency,\n",
    "                                                         raw_holdings_list)\n",
    "\n",
    "            holdings_list = holdings.aggregate_holdings_by(\n",
    "                holdings_list, lambda holding: holding.cost_currency)\n",
    "\n",
    "            holdings_list = [holding\n",
    "                             for holding in holdings_list\n",
    "                             if holding.currency and holding.cost_currency]\n",
    "\n",
    "            # If after conversion there are no valid holdings, skip the currency\n",
    "            # altogether.\n",
    "            if not holdings_list:\n",
    "                continue\n",
    "\n",
    "            net_worths_dict[currency].append((date, holdings_list[0].market_value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f2028332470>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lists = net_worths_dict['GBP']\n",
    "x, y = zip(*lists)\n",
    "\n",
    "#fig, ax = plt.subplots()\n",
    "\n",
    "plt.plot(x, y)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}<commit_msg>updated networth to output to dokuwiki<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "import datetime\n",
    "import time\n",
    "\n",
    "from dateutil import rrule\n",
    "from dateutil.parser import parse\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.ticker import FuncFormatter\n",
    "\n",
    "from beancount.core import data\n",
    "from beancount.ops import holdings\n",
    "from beancount import loader\n",
    "from beancount.reports import holdings_reports\n",
    "\n",
    "plt.style.use('ggplot')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Load beancount file\n",
    "\n",
    "filename = '/home/alex/finance/money.beancount'\n",
    "entries, errors, options_map = loader.load_file(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#for entry in entries:\n",
    "#    if isinstance(entry, data.Transaction):\n",
    "#        dtstart = entry.date\n",
    "#        break\n",
    "\n",
    "# Manually set start date, avoids None results\n",
    "dtstart = datetime.date(2015, 1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "net_worths_dict = collections.defaultdict(list)\n",
    "index = 0\n",
    "current_entries = []\n",
    "dtend = datetime.date.today()\n",
    "#period = rrule.rrule(rrule.WEEKLY, byweekday=rrule.FR, dtstart=dtstart, until=dtend)\n",
    "period = rrule.rrule(rrule.MONTHLY, bymonthday=28, dtstart=dtstart, until=dtend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-d2a9d5965411>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0;31m# Append new entries until the given date.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m         \u001b[0mentry\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mentries\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mentry\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdate\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mdate\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m             \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "for dtime in period:\n",
    "    date = dtime.date()\n",
    "\n",
    "    # Append new entries until the given date.\n",
    "    while True:\n",
    "        entry = entries[index]\n",
    "        if entry.date >= date:\n",
    "            break\n",
    "        current_entries.append(entry)\n",
    "        index += 1\n",
    "\n",
    "    # Get the list of holdings.\n",
    "    raw_holdings_list, price_map = holdings_reports.get_assets_holdings(current_entries,\n",
    "                                                                        options_map)\n",
    "\n",
    "    # Convert the currencies.\n",
    "    for currency in options_map['operating_currency']:\n",
    "        holdings_list = holdings.convert_to_currency(price_map,\n",
    "                                                     currency,\n",
    "                                                     raw_holdings_list)\n",
    "\n",
    "        holdings_list = holdings.aggregate_holdings_by(\n",
    "            holdings_list, lambda holding: holding.cost_currency)\n",
    "\n",
    "        holdings_list = [holding\n",
    "                         for holding in holdings_list\n",
    "                         if holding.currency and holding.cost_currency]\n",
    "\n",
    "        # If after conversion there are no valid holdings, skip the currency\n",
    "        # altogether.\n",
    "        if not holdings_list:\n",
    "            continue\n",
    "\n",
    "        net_worths_dict[currency].append((date, holdings_list[0].market_value))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "<KEY>",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f738cda22b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "xs, ys = zip(*net_worths_dict['GBP'])\n",
    "plt.plot(xs, ys)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#Extract data for c3 text for wiki\n",
    "\n",
    "dates = ['x']\n",
    "for value in xs:\n",
    "    dates.append(value.strftime('%Y-%m-%d'))\n",
    "\n",
    "values = ['Net Worth']\n",
    "for amount in ys:\n",
    "    if amount is None:\n",
    "        amount = 0\n",
    "    values.append(str(round(float(amount))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "<c3>\n",
      "data: {x: 'x', \n",
      "           columns:[['x', '2015-01-28', '2015-02-28', '2015-03-28', '2015-04-28', '2015-05-28', '2015-06-28', '2015-07-28', '2015-08-28', '2015-09-28', '2015-10-28', '2015-11-28', '2015-12-28', '2016-01-28', '2016-02-28', '2016-03-28', '2016-04-28', '2016-05-28', '2016-06-28', '2016-07-28', '2016-08-28', '2016-09-28', '2016-10-28', '2016-11-28', '2016-12-28', '2017-01-28', '2017-02-28', '2017-03-28', '2017-04-28'], ['Net Worth', '122035', '125661', '135322', '148239', '150676', '154480', '159120', '160518', '163364', '155050', '171277', '167363', '175541', '199580', '208788', '210809', '213529', '218842', '224078', '227906', '229962', '233791', '235712', '242148', '241824', '247388', '256164', '259098']]\n",
      "       },\n",
      "    axis: {x: {\n",
      "            type: 'timeseries',\n",
      "            tick: {\n",
      "                format: '%Y-%m-%d'\n",
      "            }\n",
      "        },\n",
      "        y : {\n",
      "             label: {\n",
      "          text: 'GBP',\n",
      "          position: 'outer-middle'\n",
      "        }\n",
      "      }\n",
      "    }\n",
      "</c3>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Build text for dokuwiki\n",
    "\n",
    "columns = \"[{}, {}]\".format(dates, values)\n",
    "c3graphs = r\"\"\"\n",
    "<c3>\n",
    "data: {x: 'x', \n",
    "           columns:\"\"\" + columns + r\"\"\"\n",
    "       },\n",
    "    axis: {x: {\n",
    "            type: 'timeseries',\n",
    "            tick: {\n",
    "                format: '%Y-%m-%d'\n",
    "            }\n",
    "        },\n",
    "        y : {\n",
    "             label: {\n",
    "          text: 'GBP',\n",
    "          position: 'outer-middle'\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "</c3>\n",
    "\"\"\"\n",
    "\n",
    "print(c3graphs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
{'fraction_non_alphanumeric': 0.20388731507711677, 'fraction_numerical': 0.06641485678312874, 'mean_word_length': 1.9317185697808534, 'pattern_counts': {'":': 136, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4959626', 'n_tokens_mistral': 5316, 'n_tokens_neox': 4770, 'n_words': 785}