-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
171 lines (154 loc) · 6.45 KB
/
main.js
File metadata and controls
171 lines (154 loc) · 6.45 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
// === Mock OHLCV historical data for 1 asset (sample for MVP, add for others similarly) ===
const mockData = {
BTCUSDT: [
// {time, open, high, low, close, volume}
{ time: "2025-11-24T00:00", open: 37000, high: 37250, low: 36900, close: 37150, volume: 890 },
{ time: "2025-11-24T04:00", open: 37150, high: 37400, low: 37080, close: 37390, volume: 950 },
{ time: "2025-11-24T08:00", open: 37390, high: 37600, low: 37300, close: 37500, volume: 1200 },
{ time: "2025-11-24T12:00", open: 37500, high: 37720, low: 37350, close: 37450, volume: 800 },
{ time: "2025-11-24T16:00", open: 37450, high: 37580, low: 37290, close: 37320, volume: 1100 },
{ time: "2025-11-24T20:00", open: 37320, high: 37390, low: 37200, close: 37230, volume: 970 },
// add more for realistic results
],
ETHUSDT: [
// ...similarly fill with ETH mock OHLCV objects for demo
],
// SOLUSDT, BNBUSDT...
};
// === Utility functions ===
// Moving Average (Simple)
function sma(data, period, key = 'close') {
if (data.length < period) return null;
let sum = 0;
for (let i = data.length - period; i < data.length; i++) sum += data[i][key];
return sum / period;
}
// Average True Range (ATR) MA for Volume Confirmation
function atr(data, period = 14) {
if (data.length < period) return null;
let trs = [];
for (let i = data.length - period; i < data.length; i++) {
let high = data[i].high, low = data[i].low, prevClose = data[i - 1]?.close || data[i].close;
let tr = Math.max(high - low, Math.abs(high - prevClose), Math.abs(low - prevClose));
trs.push(tr);
}
return trs.reduce((a, b) => a + b, 0) / period;
}
function volumeMa(data, period, key = 'volume') {
if (data.length < period) return null;
let sum = 0;
for (let i = data.length - period; i < data.length; i++) sum += data[i][key];
return sum / period;
}
// === Main CCI Calculation (returns buy/sell, confidence) ===
function calculateCCI(asset, cfg) {
let data = mockData[asset];
if (!data || data.length < Math.max(cfg.shortMa, cfg.longMa, cfg.volMa)) return null;
// Technical Signal
let shortMaValue = sma(data, cfg.shortMa);
let longMaValue = sma(data, cfg.longMa);
let technical =
shortMaValue && longMaValue
? shortMaValue > longMaValue
? "BUY"
: shortMaValue < longMaValue
? "SELL"
: "NEUTRAL"
: null;
// Volume Confirmation
let volMaValue = volumeMa(data, cfg.volMa);
let lastVol = data[data.length - 1].volume;
let volumeConfirmation = lastVol > volMaValue;
// Macro/Sentiment Filter
let macro = cfg.macroPhase;
// Composite Confidence
let high = false, medium = false, low = false;
let allowed = false;
if (technical === "BUY" && macro === "bull") allowed = true;
if (technical === "SELL" && macro === "bear") allowed = true;
if (macro === "neutral") allowed = true;
let confidence = "Low";
if (technical !== "NEUTRAL" && volumeConfirmation && allowed) confidence = "High";
else if (
(technical !== "NEUTRAL" && volumeConfirmation) ||
(technical !== "NEUTRAL" && allowed) ||
(volumeConfirmation && allowed)
) confidence = "Medium";
return {
technical,
volumeConfirmation,
macro,
confidence,
allowed,
values: { shortMaValue, longMaValue, volMaValue, lastVol },
};
}
// === Save/Load config to/from Firestore (stubbed for MVP) ===
function saveConfig(cfg) {
// TODO: Integrate with Firestore (replace localStorage for MVP)
localStorage.setItem("traderConfig", JSON.stringify(cfg));
}
function loadConfig() {
// TODO: Integrate with Firestore (replace localStorage for MVP)
let str = localStorage.getItem("traderConfig");
if (!str) return null;
return JSON.parse(str);
}
// === UI update/render ===
function renderState() {
let cfg = getCurrentConfig();
let asset = cfg.asset || "BTCUSDT";
let cci = calculateCCI(asset, cfg);
let stateDiv = document.getElementById("current-state");
let alertDiv = document.getElementById("signal-alert");
stateDiv.innerHTML = `
<div class="mb-2 font-semibold">Asset: ${asset}</div>
<div class="mb-2">Technical Signal: <span class="font-bold">${cci?.technical || "..."}</span></div>
<div class="mb-2">Volume Confirmation: <span class="font-bold">${cci?.volumeConfirmation ? "Confirmed" : "Not Confirmed"}</span></div>
<div class="mb-2">Macro Phase: <span class="font-bold">${cfg.macroPhase}</span></div>
<div class="mb-2">Confidence: <span class="font-bold">${cci?.confidence || "..."}</span></div>
<div class="mb-2 text-xs text-gray-500">Short MA: ${cci?.values?.shortMaValue?.toFixed(2)} | Long MA: ${cci?.values?.longMaValue?.toFixed(2)} | Volume MA: ${cci?.values?.volMaValue?.toFixed(2)} | Last Volume: ${cci?.values?.lastVol}</div>
`;
// Alert message (high-confidence only, discipline messaging)
if (cci?.confidence === "High" && cci.technical !== "NEUTRAL") {
alertDiv.className = "p-4 rounded mb-3 text-center font-bold bg-yellow-200 text-yellow-900";
alertDiv.innerText =
`High-Conviction ${cci.technical} Signal Triggered.\nExecute Plan Now. Remain disciplined — minimize risk.`;
alertDiv.style.display = "block";
} else {
alertDiv.innerText = "";
alertDiv.style.display = "none";
}
}
// === UI config handlers ===
function getCurrentConfig() {
return {
shortMa: Number(document.getElementById("short-ma").value),
longMa: Number(document.getElementById("long-ma").value),
timeFrame: document.getElementById("time-frame").value,
volMa: Number(document.getElementById("vol-ma").value),
macroPhase: document.getElementById("macro-phase").value,
asset: document.getElementById("asset").value,
};
}
function restoreUIFromConfig(cfg) {
document.getElementById("short-ma").value = cfg.shortMa || 7;
document.getElementById("long-ma").value = cfg.longMa || 21;
document.getElementById("time-frame").value = cfg.timeFrame || "4h";
document.getElementById("vol-ma").value = cfg.volMa || 20;
document.getElementById("macro-phase").value = cfg.macroPhase || "bull";
document.getElementById("asset").value = cfg.asset || "BTCUSDT";
}
document.getElementById("config-form").addEventListener("submit", function (e) {
e.preventDefault();
let cfg = getCurrentConfig();
saveConfig(cfg);
renderState();
});
window.addEventListener("DOMContentLoaded", () => {
let cfg = loadConfig();
if (cfg) restoreUIFromConfig(cfg);
renderState();
// Optionally, set interval to update signal with new mock data
// setInterval(renderState, 60000); // update every min (for real data)
});