-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
194 lines (166 loc) · 8.06 KB
/
index.html
File metadata and controls
194 lines (166 loc) · 8.06 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
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Universal Trading Calculator</title>
<style>
body { background: #0b0e11; color: #eaecef; font-family: 'Segoe UI', sans-serif; padding: 20px; }
.container { max-width: 550px; margin: auto; background: #181a20; padding: 25px; border-radius: 16px; box-shadow: 0 8px 24px rgba(0,0,0,0.5); }
h2 { text-align: center; color: #f0b90b; margin-bottom: 20px; }
.input-group { margin-bottom: 15px; }
label { display: block; font-size: 12px; color: #848e9c; margin-bottom: 5px; }
input, select { width: 100%; padding: 12px; background: #2b2f36; border: 1px solid #474d57; color: white; border-radius: 8px; box-sizing: border-box; font-size: 14px; }
input:focus { border-color: #f0b90b; outline: none; }
.flex-row { display: flex; gap: 10px; }
.flex-row > div { flex: 1; }
button { width: 100%; padding: 15px; background: #f0b90b; border: none; color: #181a20; font-weight: bold; cursor: pointer; border-radius: 8px; font-size: 16px; margin-top: 10px; }
button:hover { background: #d9a508; }
.result { margin-top: 20px; padding: 20px; background: #2b3139; border-radius: 12px; }
.res-line { display: flex; justify-content: space-between; margin-bottom: 10px; font-size: 14px; }
.green { color: #0ecb81; }
.red { color: #f6465d; }
.suggestions { background: #2b2f36; border: 1px solid #474d57; max-height: 200px; overflow-y: auto; position: absolute; width: calc(550px - 50px); z-index: 100; border-radius: 8px; }
.suggestions div { padding: 12px; cursor: pointer; border-bottom: 1px solid #3b3f46; }
.suggestions div:hover { background: #3b3f46; }
.fx-bar { display: flex; justify-content: space-around; font-size: 11px; color: #848e9c; margin-top: 15px; }
</style>
</head>
<body>
<div class="container">
<h2>Smart Trade Calc</h2>
<div class="input-group">
<label>Instrument (Wyszukaj np. GOLD, DE40, AAPL.US, PKN.PL)</label>
<input type="text" id="search" placeholder="Zacznij pisać..." autocomplete="off">
<div class="suggestions" id="suggestions"></div>
</div>
<div class="flex-row">
<div class="input-group">
<label>Typ</label>
<select id="type">
<option value="buy">LONG (Kupno)</option>
<option value="sell">SHORT (Sprzedaż)</option>
</select>
</div>
<div class="input-group">
<label>Twój Depozyt (PLN)</label>
<input type="number" id="amount" value="1000">
</div>
</div>
<div class="input-group">
<label>Cena Wejścia</label>
<input type="number" id="entry" step="any" placeholder="0.0000">
</div>
<div class="flex-row">
<div class="input-group">
<label>Stop Loss</label>
<input type="number" id="sl" step="any">
</div>
<div class="input-group">
<label>Take Profit</label>
<input type="number" id="tp" step="any">
</div>
</div>
<div class="input-group">
<label>Dźwignia (np. 30 dla 1:30)</label>
<input type="number" id="leverage" value="20">
</div>
<button onclick="calculate()">OBLICZ POZYCJĘ</button>
<div id="results"></div>
<div class="fx-bar">
<span id="usdpln">USDPLN: ...</span>
<span id="eurpln">EURPLN: ...</span>
</div>
</div>
<script>
let instruments = [];
let selectedInstrument = null;
let rates = { USD: 4.0, EUR: 4.30, PLN: 1.0 };
// 1. Pobieranie kursów walut
async function fetchRates() {
try {
const [usdRes, eurRes] = await Promise.all([
fetch("https://api.nbp.pl/api/exchangerates/rates/a/usd/?format=json").then(r => r.json()),
fetch("https://api.nbp.pl/api/exchangerates/rates/a/eur/?format=json").then(r => r.json())
]);
rates.USD = usdRes.rates[0].mid;
rates.EUR = eurRes.rates[0].mid;
document.getElementById("usdpln").textContent = `USDPLN: ${rates.USD.toFixed(4)}`;
document.getElementById("eurpln").textContent = `EURPLN: ${rates.EUR.toFixed(4)}`;
} catch (e) {
console.error("Błąd kursów walut");
}
}
fetchRates();
// 2. Ładowanie bazy instrumentów
fetch("xtb.json")
.then(res => res.json())
.then(data => { instruments = data.instruments; })
.catch(() => console.log("Upewnij się, że plik xtb.json istnieje!"));
// 3. Obsługa wyszukiwarki
document.getElementById("search").addEventListener("input", function() {
let val = this.value.toUpperCase();
let box = document.getElementById("suggestions");
box.innerHTML = "";
if (val.length < 1) return;
let matches = instruments.filter(i => i.symbol.includes(val)).slice(0, 8);
matches.forEach(m => {
let d = document.createElement("div");
d.textContent = m.symbol;
d.onclick = () => {
selectedInstrument = m;
document.getElementById("search").value = m.symbol;
box.innerHTML = "";
// Przykładowa cena, jeśli masz ją w JSON
if(m.price) document.getElementById("entry").value = m.price;
};
box.appendChild(d);
});
});
// 4. Logika obliczeń
function calculate() {
if (!selectedInstrument) { alert("Wybierz instrument!"); return; }
const amount = parseFloat(document.getElementById("amount").value);
const entry = parseFloat(document.getElementById("entry").value);
const sl = parseFloat(document.getElementById("sl").value);
const tp = parseFloat(document.getElementById("tp").value);
const leverage = parseFloat(document.getElementById("leverage").value);
const type = document.getElementById("type").value;
if (!entry || !sl || !tp) { alert("Uzupełnij ceny!"); return; }
// DETEKCJA WALUTY INSTRUMENTU
let currency = "USD"; // domyślnie USA/Surowce
const sym = selectedInstrument.symbol;
if (sym.endsWith(".PL") || sym.includes("W20")) currency = "PLN";
else if (sym.endsWith(".DE") || sym.includes("EU") || sym.includes("DE40")) currency = "EUR";
const currentCrossRate = rates[currency];
// OBLICZENIA
// 1. Wartość pozycji w PLN (Depozyt * Dźwignia)
const posValuePLN = amount * leverage;
// 2. Wartość pozycji w walucie instrumentu
const posValueNominal = posValuePLN / currentCrossRate;
// 3. Ilość jednostek (np. ile uncji złota lub ile akcji Apple)
const quantity = posValueNominal / entry;
// 4. Zysk i Strata
const diffSL = type === "buy" ? (entry - sl) : (sl - entry);
const diffTP = type === "buy" ? (tp - entry) : (entry - tp);
const riskInCurrency = diffSL * quantity;
const rewardInCurrency = diffTP * quantity;
// 5. Powrót do PLN
const riskPLN = riskInCurrency * currentCrossRate;
const rewardPLN = rewardInCurrency * currentCrossRate;
const rr = Math.abs(rewardPLN / riskPLN);
document.getElementById("results").innerHTML = `
<div class="result">
<div class="res-line"><span>Waluta kwotowania:</span> <b>${currency} (kurs: ${currentCrossRate.toFixed(4)})</b></div>
<div class="res-line"><span>Wartość kontraktu:</span> <b>${posValuePLN.toFixed(2)} PLN</b></div>
<div class="res-line"><span>Wielkość (Ilość):</span> <b>${quantity.toFixed(4)} jednostek</b></div>
<hr style="border:0; border-top:1px solid #3b3f46; margin: 15px 0;">
<div class="res-line"><span>Ryzyko (Stop Loss):</span> <span class="red">-${riskPLN.toFixed(2)} PLN</span></div>
<div class="res-line"><span>Zysk (Take Profit):</span> <span class="green">+${rewardPLN.toFixed(2)} PLN</span></div>
<div class="res-line"><span>Stosunek R:R:</span> <b>1 : ${rr.toFixed(2)}</b></div>
</div>
`;
}
</script>
</body>
</html>