{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "import ipysheet\n", "from analytics import CreditIndex\n", "from functools import partial" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "sheet = ipysheet.sheet(rows=3, columns=4, column_headers=False, row_headers=False)\n", "ipysheet.cell(0, 0, 'indices', read_only=True)\n", "ipysheet.cell(0, 1, 'notionals', read_only=True)\n", "ipysheet.cell(0, 2, 'price', read_only=True)\n", "ipysheet.cell(0, 3, 'newref', read_only=True)\n", "cells = {}\n", "cells[(1, 0)] = ipysheet.cell(1, 0, '', background_color='yellow')\n", "cells[(2, 0)] = ipysheet.cell(2, 0, '', background_color='yellow')\n", "cells[(1, 1)] = ipysheet.cell(1, 1, -10_000_000, read_only=True)\n", "cells[(2, 1)] = ipysheet.cell(2, 1, 10_000_000, background_color='yellow')\n", "cells[(1, 2)] = ipysheet.cell(1, 2, background_color='yellow')\n", "cells[(2, 2)] = ipysheet.cell(2, 2, background_color='yellow')\n", "cells[(1, 3)] = ipysheet.cell(1, 3, background_color='yellow')\n", "cells[(2, 3)] = ipysheet.cell(2, 3, read_only=True)\n", "\n", "indices = {}\n", "def create_indices(indices, i, name):\n", " index_type = name[:2]\n", " series = int(name[2:])\n", " indices[i] = CreditIndex(index_type, series, '5yr', notional=cells[(i, 1)].value)\n", " set_price(indices, i, cells[(i, 2)].value)\n", " change_notional(indices, cells[(2, 1)].value)\n", " set_new_ref(indices, cells[(1, 3)].value)\n", "\n", "def change_notional(indices, notional):\n", " index1 = indices[1]\n", " index2 = indices[2]\n", " index2.notional = notional\n", " index1.notional = -index2.notional * index2.factor * index2.risky_annuity / index1.risky_annuity / index1.factor\n", " cells[(1, 1)].value = index1.notional\n", " \n", "def set_price(indices, i, price):\n", " indices[i].price = price\n", " change_notional(indices, cells[(2, 1)].value)\n", " set_new_ref(indices, cells[(1, 3)].value)\n", "\n", "def set_new_ref(indices, ref):\n", " index1 = indices[1]\n", " index2 = indices[2]\n", " old_price1, old_price2 = index1.price, index2.price\n", " old_pv1 = index1.pv\n", " index1.price = ref\n", " pv_change = index1.pv - old_pv1\n", " index2.pv -= pv_change\n", " cells[(2, 3)].value = index2.price\n", " index1.price = old_price1\n", " index2.price = old_price2\n", "\n", "indices = {}\n", "cells[(1, 0)].observe(lambda change: partial(create_indices, indices, 1)(change['new']), 'value')\n", "cells[(2, 0)].observe(lambda change: partial(create_indices, indices, 2)(change['new']), 'value')\n", "cells[(2, 1)].observe(lambda change: partial(change_notional, indices)(change['new']), 'value')\n", "cells[(1, 2)].observe(lambda change: partial(set_price, indices, 1)(change['new']), 'value')\n", "cells[(2, 2)].observe(lambda change: partial(set_price, indices, 2)(change['new']), 'value')\n", "cells[(1, 3)].observe(lambda change: partial(set_new_ref, indices)(change['new']), 'value')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "sheet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }