aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/HY quote reref.ipynb
blob: 80a7463f09f3a89db311c7c75987645e47c92062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import ipysheet\n",
    "from serenitas.analytics.api import CreditIndex\n",
    "from functools import partial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "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 (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.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}