-
Notifications
You must be signed in to change notification settings - Fork 7
/
foss.js
533 lines (443 loc) · 17 KB
/
foss.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
$(document).ready(function () {
// Enable Tooltips
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
// Tutorial and listeners
// Define the tutorial data structure
var tutorialData = {
1: { "title": "Inputs", "focus-on": "bitcoin_fv", "info": "<h5>This is the discount rate</h5> Here, you can do xyz", "default_value": 3 },
2: { "title": "Inputs2", "focus-on": "page-content-wrapper", "info": "<h5>Tddddhis is the discount rate</h5> Here, you can do xyz", "default_value": 4 },
};
// Start tutorial on button click
$("#start-tutorial").click(function () {
startTutorial(tutorialData);
});
$('.toggleTable').click(function () {
let icon = $(this).find('.rotate-icon');
let table = $(this).closest('.datagroup').find('.table'); // Find the table element in the corresponding col-6 div
// Toggle table visibility and animate the arrow icon
if (table.is(":visible")) {
table.closest('.table-responsive').slideUp(900, function () {
icon.toggleClass('fa-chevron-down fa-chevron-up rotate');
});
} else {
table.closest('.table-responsive').slideDown(900, function () {
icon.toggleClass('fa-chevron-down fa-chevron-up rotate');
});
}
});
$('.toggleCard').click(function () {
console.log("toggleCard");
let icon = $(this).find('.rotate-icon');
let card = $(this).parent().parent().find('.showhidecard');
// Toggle card visibility and animate the arrow icon
card.slideToggle(900, function () {
icon.toggleClass('fa-chevron-down fa-chevron-up rotate');
});
});
$('.cds-btn').click(function () {
$('html, body').animate({
scrollTop: $('#cds-assumptions').offset().top
}, 900);
});
$('.debt-btn').click(function () {
$('html, body').animate({
scrollTop: $('#debt-assumptions').offset().top
}, 900);
});
$('.met-btn').click(function () {
$('html, body').animate({
scrollTop: $('#met-assumptions').offset().top
}, 900);
});
// Startup Functions
window.BTC_price = 30000;
getBTCPrice();
updateAllTotals();
update20YrCDSandPD();
updateBitcoinFairValue();
generate_prob_def_chart();
generate_debt_chart();
buildTable();
// Updates BTC Price every 2 minutes
window.setInterval(function () {
getBTCPrice();
}, 120000);
// Listener
$("#horizon-premium").on("change", update20YrCDSandPD);
let chartUpdateTimeout = null;
$("#horizon-premium-slider").on("input", function () {
const sliderValue = $(this).val();
$("#horizon-premium-display").text(sliderValue + "%");
$("#horizon-premium").val(sliderValue);
update20YrCDSandPD();
updateBitcoinFairValue();
buildTable();
// Clear the existing timeout if it exists
if (chartUpdateTimeout) {
clearTimeout(chartUpdateTimeout);
}
// Set a new timeout to update the chart after 2 seconds
chartUpdateTimeout = setTimeout(() => {
// Update the chart here
// For example, if you have a function called updateChart() to update the map:
}, 2000);
});
// Event delegation for handling editable cells
$('#cdsTable tbody').on('click', '.editable', function () {
let cell = $(this);
if (!cell.find('input').length) {
let input = $('<input class="text-right">', {
type: 'number',
value: cell.attr('data-value')
}).css('width', '70px');
input.on('blur', function () {
let newValue = parseInt(input.val().replace(/,/g, ''));
if (isNaN(newValue)) newValue = 0;
cell.text(newValue.toLocaleString());
cell.attr('data-value', newValue);
input.remove();
update20YrCDSandPD();
buildTable();
});
cell.parent().find("td:eq(0) input[type='checkbox']").prop("checked", true);
cell.text('').append(input);
input.focus();
}
});
// Checkbox listener
$("#cdsTable tbody").on("click", "input[type='checkbox']", function () {
updateBitcoinFairValue();
update20YrCDSandPD();
updateAllTotals();
buildTable();
});
// Event delegation for handling editable cells
$('#debtTable tbody').on('click', '.editable', function () {
let cell = $(this);
if (!cell.find('input').length) {
let input = $('<input class="text-right">', {
type: 'text',
value: cell.attr('data-value')
}).css('width', '100px');
input.on('blur', function () {
let newValue = parseInt(input.val().replace(/,/g, ''));
if (isNaN(newValue)) newValue = 0;
cell.text(newValue.toLocaleString());
cell.attr('data-value', newValue);
input.remove();
updateAllTotals();
buildTable();
});
cell.text('').append(input);
input.focus();
}
});
});
function getBTCPrice() {
// Get BTC Price
fetch("/realtime_btc")
.then((response) => response.json())
.then((data) => {
$("#bitcoin_price").html(formatNumber(data.btc_usd, 0, '$ '));
window.BTC_price = data.btc_usd;
updateAllTotals();
update20YrCDSandPD();
updateBitcoinFairValue();
buildTable();
});
}
function updateAllTotals() {
let totalDomestic = 0;
let totalInternational = 0;
let totalUnfunded = 0;
$('#debtTable tbody tr').each(function () {
let domestic = parseInt($(this).find('td:eq(0)').attr('data-value').replace(/,/g, ''));
let international = parseInt($(this).find('td:eq(2)').attr('data-value').replace(/,/g, ''));
let unfunded = parseInt($(this).find('td:eq(4)').attr('data-value').replace(/,/g, ''));
totalDomestic += domestic;
totalInternational += international;
totalUnfunded += unfunded;
let total = domestic + international + unfunded;
$(this).find('td:eq(5)').text(total.toLocaleString()).addClass('total');
});
$('#totalDomestic').text(totalDomestic.toLocaleString());
$('#totalInternational').text(totalInternational.toLocaleString());
$('#totalUnfunded').text(totalUnfunded.toLocaleString());
$('#grandTotal').text((totalDomestic + totalInternational + totalUnfunded).toLocaleString());
sortTable($('#debtTable tbody'));
updateBitcoinFairValue();
};
function update20YrCDSandPD() {
const horizonPremium = parseFloat($("#horizon-premium").val().replace("%", "")) / 100;
$("#cdsTable tbody tr").each(function () {
const row = $(this);
const fiveYrCDS = parseFloat(row.find("td:eq(2)").text().replace(/,/g, ""));
const probabilityOfDefault5 = 1 - Math.exp(-fiveYrCDS / 10000 * 5);
const twentyYrCDS = fiveYrCDS * (1 + horizonPremium);
const probabilityOfDefault = 1 - Math.exp(-twentyYrCDS / 10000 * 20);
row.find("td:eq(3)").html(
probabilityOfDefault > 0.85
? '<span class="float-center text-warning"><i class="fas fa-skull"></i></span>'
: formatNumber(probabilityOfDefault5 * 100, 2) + '%'
);
row.find("td:eq(6)").text(formatNumber(twentyYrCDS, 2));
row.find("td:eq(7)").html(
probabilityOfDefault > 0.85
? '<span class="float-center text-warning"><i class="fas fa-skull"></i></span>'
: formatNumber(probabilityOfDefault * 100, 2) + '%'
);
// Uncheck the row checkbox if probabilityOfDefault > 0.85
if (probabilityOfDefault > 0.85) {
row.find("td:eq(0) input[type='checkbox']").prop("checked", false);
}
});
sortTable($('#cdsTable tbody'), 'td:nth-child(4)', ascending = true);
updateBitcoinFairValue();
}
function updateBitcoinFairValue() {
const totalBitcoins = 20999999.9769;
let totalDebt = 0;
let weightedAvgProbabilityOfDefault = 0;
let bitcoinFairValue = 0;
$("#cdsTable tbody tr").each(function () {
const row = $(this);
const isChecked = row.find("td:eq(0) input[type='checkbox']").is(":checked");
if (isChecked) {
const countryCode = row.find("th:eq(0)").text().trim();
const probabilityOfDefault = parseFloat(row.find("td:eq(7)").text().replace(/,/g, "").replace("%", "")) / 100;
const twentyYrCDS = parseFloat(row.find("td:eq(6)").text().replace(/,/g, ""));
// Get the total debt for the corresponding country in the debtTable
$("#debtTable tbody tr").each(function () {
const debtRow = $(this);
if (debtRow.find("th:eq(0)").text().trim() === countryCode) {
const debtValue = parseInt(debtRow.find("td:eq(5)").text().replace(/,/g, ""));
totalDebt += debtValue;
weightedAvgProbabilityOfDefault += probabilityOfDefault * debtValue;
}
});
}
});
if (totalDebt > 0) {
weightedAvgProbabilityOfDefault /= totalDebt;
bitcoinFairValue = totalDebt * weightedAvgProbabilityOfDefault / totalBitcoins * 100000;
}
$("#total_debt_sel").text(formatNumber(totalDebt, 0));
$("#wa_pd").text(formatNumber(weightedAvgProbabilityOfDefault * 100, 0) + '%');
btc_diff = (bitcoinFairValue / window.BTC_price) - 1;
if (btc_diff < 1) {
btc_diff = (btc_diff) * 100;
$("#bitcoin_fv").html("$ " + formatNumber(bitcoinFairValue, 0) + " <span class='text-60-small text-white'>(" + formatNumber(btc_diff, 2) + "%)</span>");
} else {
btc_diff = btc_diff + 1;
$("#bitcoin_fv").html("$ " + formatNumber(bitcoinFairValue, 0) + " <span class='text-60-small text-white'>(" + formatNumber(btc_diff, 2) + "x)</span>");
}
}
function buildMapData() {
let mapData = [];
$("#cdsTable tbody tr").each(function () {
const row = $(this);
const countryName = row.find("th:eq(0)").text().trim();
const cds = parseFloat(row.find("td:eq(2)").text().replace(/,/g, ""));
const probabilityOfDefault = parseFloat(row.find("td:eq(7)").text().replace(/,/g, "").replace("%", ""));
let totalDebt = 0;
$("#debtTable tbody tr").each(function () {
const debtRow = $(this);
if (debtRow.find("th:eq(0)").text().trim() === countryName) {
totalDebt = parseInt(debtRow.find("td:eq(5)").text().replace(/,/g, ""));
}
});
// Retrieve the ISO 3166-1 alpha-3 country code from the countryCodeMapping object
const countryCode = countryCodeMapping[countryName] || '';
// Retrieve the flag URL directly from the table row
const flagUrl = row.find("img").attr("src");
mapData.push({
"country": countryName,
"cds": cds,
"probabilityOfDefault": probabilityOfDefault,
"totalDebt": totalDebt,
"flagUrl": flagUrl
});
});
return mapData;
}
function buildTable() {
var countriesToHighlight = buildMapData();
$("#country-table").html('')
countriesToHighlight.forEach(country => {
const countryCode = country.code;
const countryName = country.country;
const flagUrl = country.flagUrl; // Use the flag URL from the table data
const cds = formatNumber(country.cds, 0);
const probabilityOfDefault = formatNumber(country.probabilityOfDefault, 0);
const totalDebt = formatNumber(country.totalDebt / 1000000, 2);
const countryBox = `
<td class="country-box">
<img class="flag" src="${flagUrl}" alt="${countryName} flag" />
<div class='country-text'>
<span class="text-large">
${countryName}</span><br>
CDS <strong class="float-end">${cds} bps</strong><br>
Default prob <strong class="float-end">${probabilityOfDefault}%</strong><br>
Debt <strong class="float-end">${totalDebt} tri</strong>
</div>
</td>
`;
// Append the country box to the table
$("#country-table").append(countryBox);
});
}
const countryCodeMapping = {
'Australia': 'AUS',
'Austria': 'AUT',
'Belgium': 'BEL',
'Canada': 'CAN',
'China': 'CHN',
'Czech Republic': 'CZE',
'Denmark': 'DNK',
'Egypt': 'EGY',
'Finland': 'FIN',
'France': 'FRA',
'Germany': 'DEU',
'Greece': 'GRC',
'Hungary': 'HUN',
'Indonesia': 'IDN',
'Ireland': 'IRL',
'Italy': 'ITA',
'Japan': 'JPN',
'South Korea': 'KOR',
'Luxembourg': 'LUX',
'Mexico': 'MEX',
'Netherlands': 'NLD',
'Norway': 'NOR',
'Poland': 'POL',
'Portugal': 'PRT',
'Slovakia': 'SVK',
'Spain': 'ESP',
'Sweden': 'SWE',
'Switzerland': 'CHE',
'Turkey': 'TUR',
'United Kingdom': 'GBR',
'United States': 'USA',
'Chile': 'CHL',
'Estonia': 'EST',
'Israel': 'ISR',
'Slovenia': 'SVN',
'Iceland': 'ISL',
'Latvia': 'LVA',
'Colombia': 'COL',
'Lithuania': 'LTU',
'New Zealand': 'NZL',
'Brazil': 'BRA',
'Romania': 'ROU',
'Russia': 'RUS',
'Other Advanced Economies Average': 'OAA',
'OECD Countries': 'OEC'
};
// Function to generate the data for the horizon premium chart.
function generate_prob_def_chart() {
const data = [];
total_debt = parseFloat($("#total_debt_sel").text().replace("%", "")) * 1000000;
bitcoins = 20999999.9769;
for (let i = 0; i <= 80; i += 10) {
const p_def = i / 100;
const valuation = total_debt * 100000 * p_def / bitcoins
data.push([valuation, p_def]);
}
// Create the horizon premium chart.
// Create the horizon premium chart.
Highcharts.chart('horizon-premium-chart', {
chart: {
type: 'spline',
backgroundColor: 'transparent'
},
title: {
text: 'Bitcoin Valuation by Probability of Default',
style: { fontSize: '14px', color: 'white' }
},
xAxis: {
title: { text: 'Bitcoin Price' },
labels: { style: { color: 'white' } },
gridLineColor: '#444',
gridLineWidth: 1,
minorGridLineColor: '#444',
minorGridLineWidth: 1,
minorTickInterval: 'auto'
},
yAxis: {
title: { text: 'Probability of Default (%)' },
labels: {
formatter: function () { return this.value * 100 + '%'; },
style: { color: 'white' }
},
gridLineColor: '#444',
gridLineWidth: 1,
startOnTick: true,
endOnTick: true,
min: 0,
max: 0.8,
},
series: [{
name: 'Probability of Default',
data: data,
color: 'white',
lineWidth: 2,
marker: { enabled: false }
}],
credits: { enabled: false },
legend: { enabled: false }
});
}
// Function to generate the data for the debt level chart.
function generate_debt_chart() {
const data = [];
current_ww_prob = parseFloat($("#wa_pd").text().replace("%", "")) / 100;
total_debt = parseFloat($("#total_debt_sel").text().replace("%", "")) * 1000000;
bitcoins = 20999999.9769;
for (let i = 50; i <= 200; i += 10) {
const valuation = total_debt * 1000 * (i) * current_ww_prob / bitcoins
data.push([valuation, i]);
}
// Create the debt level chart.
Highcharts.chart('debt-level-chart', {
chart: {
type: 'spline',
backgroundColor: 'transparent'
},
title: {
text: 'Bitcoin Valuation by Debt Levels changes',
style: { fontSize: '14px', color: 'white' }
},
yAxis: {
title: { text: 'Debt Level (% of current levels)' },
labels: {
formatter: function () { return this.value + '%'; },
style: { color: 'white' }
},
gridLineColor: '#444',
gridLineWidth: 1,
startOnTick: true,
endOnTick: true,
min: 50,
max: 200,
},
xAxis: {
title: { text: 'Bitcoin Price' },
labels: { style: { color: 'white' } },
gridLineColor: '#444',
gridLineWidth: 1,
minorGridLineColor: '#444',
minorGridLineWidth: 1,
minorTickInterval: 'auto'
},
series: [{
name: 'Debt Level',
data: data,
color: 'white',
lineWidth: 2,
marker: { enabled: false }
}],
credits: { enabled: false },
legend: { enabled: false }
});
}