-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
94 lines (77 loc) · 3.63 KB
/
script.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
document.getElementById("addRow").addEventListener("click", addRow);
document.getElementById("calculate").addEventListener("click", calculate);
document.getElementById("inputTable").addEventListener("click", deleteRow);
function addRow() {
const row = document.getElementById("inputTable").insertRow(-1);
const tickerCell = row.insertCell(0);
const entryCell = row.insertCell(1);
const tpCell = row.insertCell(2);
const slCell = row.insertCell(3);
const winProbabilityCell = row.insertCell(4);
const deleteCell = row.insertCell(5);
tickerCell.innerHTML = '<input type="text" class="ticker">';
entryCell.innerHTML = '<input type="number" class="entry">';
tpCell.innerHTML = '<input type="number" class="tp">';
slCell.innerHTML = '<input type="number" class="sl">';
winProbabilityCell.innerHTML = '<input type="number" class="winProbability">';
deleteCell.innerHTML = '<button class="deleteRowBtn">x</button>';
const tickerInput = tickerCell.querySelector(".ticker");
tickerInput.focus();
}
function deleteRow(event) {
if (event.target && event.target.classList.contains("deleteRowBtn")) {
const row = event.target.parentElement.parentElement;
row.parentElement.removeChild(row);
}
}
function calculate() {
const tickers = Array.from(document.getElementsByClassName("ticker")).map(el => el.value);
const entries = Array.from(document.getElementsByClassName("entry")).map(el => parseFloat(el.value));
const tps = Array.from(document.getElementsByClassName("tp")).map(el => parseFloat(el.value));
const sls = Array.from(document.getElementsByClassName("sl")).map(el => parseFloat(el.value));
const winProbabilities = Array.from(document.getElementsByClassName("winProbability")).map(el=> parseFloat(el.value));
const tradingStack = parseFloat(document.getElementById("tradingStack").value);
const kellyValues = entries.map((entry, i) => {
const rr = Math.abs((tps[i] - entry) / (entry - sls[i]));
return winProbabilities[i] - ((1 - winProbabilities[i]) / rr);
});
let adjustedTradingSizes;
if (sum(kellyValues) < 1) {
adjustedTradingSizes = kellyValues.map(kelly_value => kelly_value * tradingStack);
} else {
const totalKellyValue = sum(kellyValues);
adjustedTradingSizes = kellyValues.map(kv => kv * tradingStack / totalKellyValue);
}
const resultsTable = document.getElementById("resultsTable");
for (let i = resultsTable.rows.length - 1; i > 0; i--) {
resultsTable.deleteRow(i);
}
tickers.forEach((ticker, i) => {
const row = resultsTable.insertRow(-1);
const tickerCell = row.insertCell(0);
const rrCell = row.insertCell(1);
const sizeCell = row.insertCell(2);
tickerCell.textContent = ticker;
rrCell.textContent = (tps[i] - entries[i]) / (entries[i] - sls[i]);
sizeCell.textContent = adjustedTradingSizes[i].toFixed(2);
});
}
function sum(arr) {
return arr.reduce((acc, val) => acc + val, 0);
}
function setTradingStackContainerWidth() {
const dataTable = document.querySelector('.data-table');
const tradingStackContainer = document.querySelector('.trading-stack-container');
if (dataTable && tradingStackContainer) {
tradingStackContainer.style.width = dataTable.offsetWidth + 'px';
tradingStackContainer.style.margin = '0 auto';
}
}
setTradingStackContainerWidth();
window.addEventListener('resize', setTradingStackContainerWidth);
window.onload = function() {
document.getElementById("tradingStack").focus();
};
document.getElementById('darkModeToggle').addEventListener('change', function () {
document.body.classList.toggle('dark-mode');
});