-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.js
85 lines (76 loc) · 2.52 KB
/
calculate.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
// 確保 DOM 完全加載後再執行腳本
document.addEventListener("DOMContentLoaded", (event) => {
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker.register("/service-worker.js").then(
function (registration) {
// 註冊成功
console.log(
"ServiceWorker registration successful with scope: ",
registration.scope
);
},
function (err) {
// 註冊失敗 :(
console.log("ServiceWorker registration failed: ", err);
}
);
});
}
var currentDirection = "long"; // 初始化變量
function selectDirection(direction) {
document.getElementById("long").classList.remove("selected");
document.getElementById("short").classList.remove("selected");
document.getElementById(direction).classList.add("selected");
currentDirection = direction;
}
// 定義計算結果的函數
function calculateResult(
direction,
positionSize,
openingPrice,
stopLossPrice,
maxLoss
) {
positionSize = parseFloat(positionSize);
openingPrice = parseFloat(openingPrice);
stopLossPrice = parseFloat(stopLossPrice);
maxLoss = parseFloat(maxLoss);
var result = 0;
if (direction === "long") {
result =
maxLoss /
(((openingPrice - stopLossPrice) / openingPrice) * 100) /
(positionSize / 100);
} else if (direction === "short") {
result =
maxLoss /
-(((openingPrice - stopLossPrice) / openingPrice) * 100) /
(positionSize / 100);
}
return result;
}
// 為做多和做空按鈕添加點擊事件
document.getElementById("long").addEventListener("click", function () {
selectDirection("long");
});
document.getElementById("short").addEventListener("click", function () {
selectDirection("short");
});
// 為計算按鈕添加點擊事件
document.getElementById("confirm").addEventListener("click", function () {
const positionSize = document.getElementById("position-size").value;
const openingPrice = document.getElementById("opening-price").value;
const stopLossPrice = document.getElementById("stop-loss-price").value;
const maxLoss = document.getElementById("max-loss").value;
// 執行計算並更新結果
const result = calculateResult(
currentDirection,
positionSize,
openingPrice,
stopLossPrice,
maxLoss
);
document.getElementById("resultSpan").innerText = result.toFixed(2);
});
});