-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
747 lines (644 loc) · 26 KB
/
index.html
File metadata and controls
747 lines (644 loc) · 26 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dollar Cost Averaging Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
.entry-row {
display: flex;
gap: 10px;
margin-bottom: 10px;
align-items: center;
}
.entry-row select, .entry-row input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 8px 16px;
margin: 10px 0;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.remove-btn {
background-color: #f44336;
}
.remove-btn:hover {
background-color: #da190b;
}
#result {
margin-top: 20px;
font-size: 1.2em;
font-weight: bold;
}
.dca-container {
border: 1px solid #ccc;
border-radius: 8px;
margin: 20px 0;
padding: 20px;
background-color: #f9f9f9;
}
.dca-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.dca-title {
font-size: 1.5em;
font-weight: bold;
border: none;
background: transparent;
padding: 5px;
width: 200px;
}
.dca-title:focus {
outline: 1px solid #4CAF50;
background: white;
}
.remove-dca-btn {
background-color: #f44336;
padding: 5px 10px;
font-size: 0.9em;
}
#add-new-dca {
background-color: #2196F3;
margin: 20px 0;
font-size: 1.1em;
}
#add-new-dca:hover {
background-color: #1976D2;
}
.action-buttons {
display: flex;
gap: 10px;
margin: 20px 0;
}
.action-buttons button {
flex: 1;
background-color: #2196F3;
font-size: 1.1em;
margin: 20px 0;
}
.action-buttons button:hover {
background-color: #1976D2;
}
#import-input {
display: none;
}
.export-import-btn {
background-color: #2196F3;
}
.export-import-btn:hover {
background-color: #1976D2;
}
.input-mode-select {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
background-color: white;
}
.entry-row.no-price input:nth-of-type(2) {
background-color: #f0f0f0;
}
.wallet-info {
width: 100%;
background: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
display: flex;
align-items: center;
gap: 20px;
box-sizing: border-box;
margin-top: 20px;
}
.qr-code {
width: 128px;
height: 128px;
}
.wallet-address {
display: flex;
flex-direction: column;
gap: 5px;
}
.wallet-address span {
font-weight: bold;
color: #666;
}
.wallet-address code {
font-family: monospace;
background: #f5f5f5;
padding: 8px;
border-radius: 4px;
font-size: 14px;
word-break: break-all;
}
.entry-headers {
display: flex;
gap: 10px;
margin-bottom: 5px;
font-weight: bold;
color: #666;
}
.entry-headers span {
padding: 8px;
}
.header-operation {
width: 180px; /* Adjust based on the input-mode-select width */
}
.header-trade {
width: 80px; /* Adjust based on the buy/sell select width */
}
.header-amount {
flex: 1;
}
.header-price {
flex: 1;
}
.header-action {
width: 35px; /* Width of the remove button */
}
/* Mobile Responsive Styles */
@media (max-width: 768px) {
body {
padding: 10px;
margin: 10px auto;
}
h1 {
font-size: 1.5em;
}
p {
font-size: 0.9em;
}
.action-buttons {
flex-direction: column;
}
.action-buttons button {
width: 100%;
margin: 5px 0;
}
#add-new-dca {
margin: 10px 0;
}
.dca-container {
padding: 15px;
}
.dca-header {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
.dca-title {
width: 100%;
font-size: 1.3em;
}
.remove-dca-btn {
width: 100%;
}
.entry-headers {
display: none;
}
.entry-row {
flex-direction: column;
gap: 8px;
border: 1px solid #e0e0e0;
padding: 10px;
border-radius: 8px;
background-color: white;
margin-bottom: 15px;
}
.entry-row select,
.entry-row input {
width: 100%;
font-size: 16px;
min-height: 44px;
}
.input-mode-select {
width: 100%;
margin-right: 0;
}
button {
width: 100%;
min-height: 44px;
font-size: 16px;
margin: 5px 0;
}
.wallet-info {
flex-direction: column;
text-align: center;
padding: 15px;
}
.wallet-address {
width: 100%;
}
.wallet-address code {
font-size: 12px;
}
}
@media (max-width: 480px) {
body {
padding: 5px;
margin: 5px auto;
}
h1 {
font-size: 1.3em;
}
p {
font-size: 0.85em;
}
.dca-container {
padding: 10px;
}
.dca-title {
font-size: 1.2em;
}
#result {
font-size: 1em;
}
.entry-row {
padding: 8px;
}
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/10.4.3/decimal.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/davidshimjs/qrcodejs/qrcode.min.js"></script>
</head>
<body>
<h1>Dollar Cost Averaging Calculator</h1>
<p>
This calculator is designed to help you calculate the average price per unit of a security when you are dollar cost averaging.
<br>
It is useful for calculating the average price of a security when you are buying and selling it multiple times.
<br>
You can use either comma (,) or period (.) as decimal marker. Scientific notation is also supported - e.g "1,23e-8" or "1.23e-8" = "0.0000000123".
</p>
<div class="action-buttons">
<button id="add-new-dca" onclick="addNewDCASet()">+ Add New Asset</button>
<button class="export-import-btn" onclick="exportData()">Export Data</button>
<button class="export-import-btn" onclick="document.getElementById('import-input').click()">Import Data</button>
<input type="file" id="import-input" accept=".json" onchange="importData(event)">
</div>
<div id="dca-sets-container"></div>
<div class="wallet-info">
<div class="qr-code" id="qrcode"></div>
<div class="wallet-address">
<span>Donate to the developers Metamask (ERC-20) (BSC):</span>
<code>0xaf8817fe4ec64888f8a4586cf6b1432058a80066</code>
</div>
</div>
<script>
// Template for a new DCA set
const dcaSetTemplate = `
<div class="dca-header">
<input type="text" class="dca-title" value="New Asset" onchange="saveState()">
<button class="remove-btn remove-dca-btn" onclick="removeDCASet(this)">Remove Asset</button>
</div>
<div class="entries-container">
<div class="entry-headers">
<span class="header-operation">Operation</span>
<span class="header-trade">Trade</span>
<span class="header-amount">Amount</span>
<span class="header-price">Price</span>
<span class="header-action"></span>
</div>
<div class="entry-row">
<select class="input-mode-select" onchange="handleInputModeChange(this)">
<option value="per-unit">Per Unit Price</option>
<option value="total-value">Total Value</option>
<option value="no-price">No Price (Airdrop/Mining)</option>
</select>
<select>
<option value="buy">Buy</option>
<option value="sell">Sell</option>
</select>
<input type="text" placeholder="Number of units" required>
<input type="text" placeholder="Price per unit" required>
<button class="remove-btn" onclick="removeEntry(this)">X</button>
</div>
</div>
<button onclick="addEntry(this)">+ Add Entry</button>
<button onclick="calculate(this)">Calculate Average</button>
<div class="result"></div>
`;
function addNewDCASet() {
const container = document.getElementById('dca-sets-container');
const newSet = document.createElement('div');
newSet.className = 'dca-container';
newSet.innerHTML = dcaSetTemplate;
container.appendChild(newSet);
saveState();
}
function removeDCASet(button) {
if (confirm('Are you sure you want to remove this asset?')) {
button.closest('.dca-container').remove();
saveState();
}
}
function handleInputModeChange(select) {
const row = select.closest('.entry-row');
const priceInput = row.querySelector('input:nth-of-type(2)');
const typeSelect = row.querySelector('select:nth-of-type(2)');
row.classList.remove('no-price', 'total-value', 'per-unit');
row.classList.add(select.value);
switch(select.value) {
case 'no-price':
priceInput.value = '0';
priceInput.disabled = true;
priceInput.placeholder = 'No price (airdrop/mining)';
typeSelect.value = 'buy';
typeSelect.disabled = true;
break;
case 'total-value':
priceInput.disabled = false;
priceInput.placeholder = 'Total value';
typeSelect.disabled = false;
break;
case 'per-unit':
priceInput.disabled = false;
priceInput.placeholder = 'Price per unit';
typeSelect.disabled = false;
break;
}
saveState();
}
function addEntry(button) {
const container = button.previousElementSibling;
const newRow = document.createElement('div');
newRow.className = 'entry-row';
newRow.innerHTML = `
<select class="input-mode-select" onchange="handleInputModeChange(this)">
<option value="per-unit">Per Unit Price</option>
<option value="total-value">Total Value</option>
<option value="no-price">No Price (Airdrop/Mining)</option>
</select>
<select>
<option value="buy">Buy</option>
<option value="sell">Sell</option>
</select>
<input type="text" placeholder="Number of units" required>
<input type="text" placeholder="Price per unit" required>
<button class="remove-btn" onclick="removeEntry(this)">X</button>
`;
container.appendChild(newRow);
saveState();
}
function removeEntry(button) {
const row = button.parentElement;
const container = row.parentElement;
if (container.getElementsByClassName('entry-row').length > 1) {
row.remove();
saveState();
}
}
function parseNumberInput(str) {
// Replace comma with period for decimal parsing
str = str.trim().replace(',', '.');
return new Decimal(str);
}
function formatDecimal(decimal) {
// Convert to string without scientific notation, showing all significant digits
let str = decimal.toFixed(32);
// Remove trailing zeros after decimal point
str = str.replace(/\.?0+$/, '');
// If it's a whole number (no decimals), add .0 for readability
if (!str.includes('.')) {
str += '.0';
}
return str;
}
function calculate(button) {
const container = button.closest('.dca-container');
const rows = container.getElementsByClassName('entry-row');
let runningUnits = new Decimal(0);
let runningCost = new Decimal(0);
for (const row of rows) {
const inputMode = row.querySelector('.input-mode-select').value;
const type = row.querySelector('select:nth-of-type(2)').value;
const unitsStr = row.querySelector('input:nth-of-type(1)').value.trim();
const valueStr = row.querySelector('input:nth-of-type(2)').value.trim();
try {
const units = parseNumberInput(unitsStr);
let transactionValue;
let price;
if (!units || units.isNaN() || units.isZero()) {
alert('Please enter a valid number of units');
return;
}
switch(inputMode) {
case 'no-price':
transactionValue = new Decimal(0);
price = new Decimal(0);
break;
case 'total-value':
transactionValue = parseNumberInput(valueStr);
if (!transactionValue || transactionValue.isNaN()) {
alert('Please enter a valid total value');
return;
}
price = transactionValue.dividedBy(units);
break;
case 'per-unit':
price = parseNumberInput(valueStr);
if (!price || price.isNaN()) {
alert('Please enter a valid price per unit');
return;
}
transactionValue = price.times(units);
break;
}
if (type === 'buy' || inputMode === 'no-price') {
runningUnits = runningUnits.plus(units);
runningCost = runningCost.plus(transactionValue);
} else {
if (units.greaterThan(runningUnits)) {
alert(`Cannot sell ${formatDecimal(units)} units when you only have ${formatDecimal(runningUnits)} units available.`);
return;
}
const sellRatio = units.dividedBy(runningUnits);
runningCost = runningCost.minus(runningCost.times(sellRatio));
runningUnits = runningUnits.minus(units);
}
} catch (e) {
console.error('Calculation error:', e);
alert('Please enter valid numbers. You can use either , or . as decimal marker. Scientific notation is also supported (e.g., 1.23e-8)');
return;
}
}
const resultDiv = container.querySelector('.result');
if (runningUnits.isZero()) {
resultDiv.textContent = 'Position is fully closed (0 units remaining).';
} else if (runningUnits.isNegative()) {
resultDiv.textContent = 'Error: Negative position detected. Please check your entries.';
} else {
const averagePrice = runningCost.dividedBy(runningUnits);
resultDiv.textContent = `Average Price: $${formatDecimal(averagePrice)} per unit`;
resultDiv.innerHTML += `<br>Net Position: ${formatDecimal(runningUnits)} units`;
resultDiv.innerHTML += `<br>Total Cost Basis: $${formatDecimal(runningCost)}`;
}
}
function saveState() {
const container = document.getElementById('dca-sets-container');
const state = {
html: container.innerHTML,
data: getAllInputData()
};
localStorage.setItem('dcaCalculatorState', JSON.stringify(state));
}
function getAllInputData() {
const data = [];
const containers = document.getElementsByClassName('dca-container');
for (const container of containers) {
const assetData = {
title: container.querySelector('.dca-title').value,
entries: []
};
const rows = container.getElementsByClassName('entry-row');
for (const row of rows) {
assetData.entries.push({
inputMode: row.querySelector('.input-mode-select').value,
type: row.querySelector('select:nth-of-type(2)').value,
units: row.querySelector('input:nth-of-type(1)').value,
price: row.querySelector('input:nth-of-type(2)').value
});
}
data.push(assetData);
}
return data;
}
function loadState() {
try {
const savedState = localStorage.getItem('dcaCalculatorState');
if (savedState) {
const state = JSON.parse(savedState);
document.getElementById('dca-sets-container').innerHTML = state.html;
// Restore input values
const containers = document.getElementsByClassName('dca-container');
state.data.forEach((assetData, i) => {
if (containers[i]) {
containers[i].querySelector('.dca-title').value = assetData.title;
const rows = containers[i].getElementsByClassName('entry-row');
assetData.entries.forEach((entry, j) => {
if (rows[j]) {
rows[j].querySelector('.input-mode-select').value = entry.inputMode;
rows[j].querySelector('select:nth-of-type(2)').value = entry.type;
rows[j].querySelector('input:nth-of-type(1)').value = entry.units;
rows[j].querySelector('input:nth-of-type(2)').value = entry.price;
handleInputModeChange(rows[j].querySelector('.input-mode-select'));
}
});
}
});
} else {
addNewDCASet(); // Add one set by default
}
} catch (e) {
console.error('Error loading saved state:', e);
addNewDCASet();
}
}
function exportData() {
const data = getAllInputData();
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'dca-calculator-data.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function importData(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
try {
const data = JSON.parse(e.target.result);
document.getElementById('dca-sets-container').innerHTML = '';
data.forEach(assetData => {
const container = document.createElement('div');
container.className = 'dca-container';
container.innerHTML = dcaSetTemplate;
document.getElementById('dca-sets-container').appendChild(container);
container.querySelector('.dca-title').value = assetData.title;
// Remove the default first row
container.querySelector('.entries-container').innerHTML = '';
assetData.entries.forEach(entry => {
const newRow = document.createElement('div');
newRow.className = 'entry-row';
newRow.innerHTML = `
<select class="input-mode-select" onchange="handleInputModeChange(this)">
<option value="per-unit">Per Unit Price</option>
<option value="total-value">Total Value</option>
<option value="no-price">No Price (Airdrop/Mining)</option>
</select>
<select>
<option value="buy">Buy</option>
<option value="sell">Sell</option>
</select>
<input type="text" placeholder="Number of units" required>
<input type="text" placeholder="Price per unit" required>
<button class="remove-btn" onclick="removeEntry(this)">X</button>
`;
container.querySelector('.entries-container').appendChild(newRow);
newRow.querySelector('.input-mode-select').value = entry.inputMode;
newRow.querySelector('select:nth-of-type(2)').value = entry.type;
newRow.querySelector('input:nth-of-type(1)').value = entry.units;
newRow.querySelector('input:nth-of-type(2)').value = entry.price;
handleInputModeChange(newRow.querySelector('.input-mode-select'));
});
});
saveState();
alert('Data imported successfully!');
} catch (e) {
console.error('Error importing data:', e);
alert('Error importing data. Please check the file format.');
}
};
reader.readAsText(file);
}
// Reset the input so the same file can be imported again
event.target.value = '';
}
// Add input event listeners to save state when values change
document.addEventListener('input', function(e) {
if (e.target.matches('.entry-row input') ||
e.target.matches('.entry-row select') ||
e.target.matches('.dca-title')) {
saveState();
}
});
// Add QR code initialization
window.addEventListener('DOMContentLoaded', (event) => {
// Initialize QR code
new QRCode(document.getElementById("qrcode"), {
text: "0xaf8817fe4ec64888f8a4586cf6b1432058a80066",
width: 128,
height: 128,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
// Existing DOMContentLoaded handlers if any...
loadState();
});
</script>
</body>
</html>