Spaces:
Sleeping
Sleeping
Commit ·
9cd2583
1
Parent(s): a7eeef4
Update strategy with MCX fixes, new instruments, visitor counter, and feedback mechanism
Browse files- forex_macd_dashboard.html +110 -4
- forex_macd_strategy.py +40 -8
- serve_forex_macd.py +77 -2
forex_macd_dashboard.html
CHANGED
|
@@ -259,6 +259,60 @@
|
|
| 259 |
.instrument-card-container.hidden {
|
| 260 |
display: none;
|
| 261 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
</style>
|
| 263 |
</head>
|
| 264 |
|
|
@@ -267,16 +321,19 @@
|
|
| 267 |
<header>
|
| 268 |
<h1>💱 ModelFX Dashboard</h1>
|
| 269 |
<div class="subtitle">Multi-Timeframe Analysis: Trend • Momentum • Entry (Varies by Category)</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
</header>
|
| 271 |
|
| 272 |
<div class="tabs-container" id="tabs">
|
| 273 |
<button class="tab-btn active" onclick="filterCategory('Active')">Active Signals</button>
|
| 274 |
-
<button class="tab-btn" onclick="filterCategory('
|
| 275 |
<button class="tab-btn" onclick="filterCategory('Forex')">Forex Pairs</button>
|
| 276 |
<button class="tab-btn" onclick="filterCategory('Metals/Energy')">Metals & Energy</button>
|
| 277 |
-
<button class="tab-btn" onclick="filterCategory('Cryptos')">Cryptocurrencies</button>
|
| 278 |
<button class="tab-btn" onclick="filterCategory('Crypto Scalping')">Scalping</button>
|
| 279 |
-
<button class="tab-btn" onclick="filterCategory('
|
| 280 |
</div>
|
| 281 |
|
| 282 |
<div id="dashboard" class="grid">
|
|
@@ -286,6 +343,17 @@
|
|
| 286 |
</div>
|
| 287 |
|
| 288 |
<div class="last-updated" id="lastUpdated"></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
</div>
|
| 290 |
|
| 291 |
<script>
|
|
@@ -318,12 +386,19 @@
|
|
| 318 |
|
| 319 |
async function fetchData() {
|
| 320 |
try {
|
|
|
|
| 321 |
const signalsRes = await fetch('forex_macd_signals.json');
|
| 322 |
-
|
| 323 |
if (signalsRes.ok) {
|
| 324 |
cachedData = await signalsRes.json();
|
| 325 |
}
|
| 326 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
renderDashboard();
|
| 328 |
} catch (error) {
|
| 329 |
console.error('Error fetching data:', error);
|
|
@@ -491,6 +566,37 @@
|
|
| 491 |
|
| 492 |
fetchData();
|
| 493 |
setInterval(fetchData, 30000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
</script>
|
| 495 |
</body>
|
| 496 |
|
|
|
|
| 259 |
.instrument-card-container.hidden {
|
| 260 |
display: none;
|
| 261 |
}
|
| 262 |
+
|
| 263 |
+
/* Feedback Form Styling */
|
| 264 |
+
.feedback-section {
|
| 265 |
+
margin-top: 4rem;
|
| 266 |
+
padding: 2rem;
|
| 267 |
+
background: var(--card-bg);
|
| 268 |
+
border-radius: 1rem;
|
| 269 |
+
border: 1px solid #334155;
|
| 270 |
+
max-width: 800px;
|
| 271 |
+
margin-left: auto;
|
| 272 |
+
margin-right: auto;
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
.feedback-section h2 {
|
| 276 |
+
margin-bottom: 1rem;
|
| 277 |
+
color: var(--accent);
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
.feedback-form {
|
| 281 |
+
display: flex;
|
| 282 |
+
flex-direction: column;
|
| 283 |
+
gap: 1rem;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
.feedback-form textarea {
|
| 287 |
+
background: var(--bg-color);
|
| 288 |
+
border: 1px solid #334155;
|
| 289 |
+
color: var(--text-primary);
|
| 290 |
+
padding: 1rem;
|
| 291 |
+
border-radius: 0.5rem;
|
| 292 |
+
resize: vertical;
|
| 293 |
+
min-height: 100px;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
.feedback-form button {
|
| 297 |
+
background: var(--accent);
|
| 298 |
+
color: white;
|
| 299 |
+
border: none;
|
| 300 |
+
padding: 0.75rem 1.5rem;
|
| 301 |
+
border-radius: 0.5rem;
|
| 302 |
+
cursor: pointer;
|
| 303 |
+
font-weight: 600;
|
| 304 |
+
transition: background 0.2s;
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
.feedback-form button:hover {
|
| 308 |
+
background: #2563eb;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
#feedbackStatus {
|
| 312 |
+
margin-top: 1rem;
|
| 313 |
+
font-size: 0.9rem;
|
| 314 |
+
text-align: center;
|
| 315 |
+
}
|
| 316 |
</style>
|
| 317 |
</head>
|
| 318 |
|
|
|
|
| 321 |
<header>
|
| 322 |
<h1>💱 ModelFX Dashboard</h1>
|
| 323 |
<div class="subtitle">Multi-Timeframe Analysis: Trend • Momentum • Entry (Varies by Category)</div>
|
| 324 |
+
<div id="visitorCounter"
|
| 325 |
+
style="margin-top: 0.5rem; font-size: 0.9rem; color: var(--accent); font-weight: 600;">
|
| 326 |
+
👥 Unique Visitors: <span id="visitorCount">...</span>
|
| 327 |
+
</div>
|
| 328 |
</header>
|
| 329 |
|
| 330 |
<div class="tabs-container" id="tabs">
|
| 331 |
<button class="tab-btn active" onclick="filterCategory('Active')">Active Signals</button>
|
| 332 |
+
<button class="tab-btn" onclick="filterCategory('Intraday IndianMarket')">Intraday IndianMarket</button>
|
| 333 |
<button class="tab-btn" onclick="filterCategory('Forex')">Forex Pairs</button>
|
| 334 |
<button class="tab-btn" onclick="filterCategory('Metals/Energy')">Metals & Energy</button>
|
|
|
|
| 335 |
<button class="tab-btn" onclick="filterCategory('Crypto Scalping')">Scalping</button>
|
| 336 |
+
<button class="tab-btn" onclick="filterCategory('All')">All Assets</button>
|
| 337 |
</div>
|
| 338 |
|
| 339 |
<div id="dashboard" class="grid">
|
|
|
|
| 343 |
</div>
|
| 344 |
|
| 345 |
<div class="last-updated" id="lastUpdated"></div>
|
| 346 |
+
|
| 347 |
+
<section class="feedback-section">
|
| 348 |
+
<h2>Share Your Feedback</h2>
|
| 349 |
+
<p style="color: var(--text-secondary); margin-bottom: 1.5rem;">How is the strategy performing for you? Any
|
| 350 |
+
suggestions for improvement?</p>
|
| 351 |
+
<form class="feedback-form" id="feedbackForm">
|
| 352 |
+
<textarea id="feedbackText" placeholder="Enter your feedback here..." required></textarea>
|
| 353 |
+
<button type="submit">Submit Feedback</button>
|
| 354 |
+
</form>
|
| 355 |
+
<div id="feedbackStatus"></div>
|
| 356 |
+
</section>
|
| 357 |
</div>
|
| 358 |
|
| 359 |
<script>
|
|
|
|
| 386 |
|
| 387 |
async function fetchData() {
|
| 388 |
try {
|
| 389 |
+
// Fetch signals
|
| 390 |
const signalsRes = await fetch('forex_macd_signals.json');
|
|
|
|
| 391 |
if (signalsRes.ok) {
|
| 392 |
cachedData = await signalsRes.json();
|
| 393 |
}
|
| 394 |
|
| 395 |
+
// Fetch visitor count
|
| 396 |
+
const visitorRes = await fetch('/api/visitor-count');
|
| 397 |
+
if (visitorRes.ok) {
|
| 398 |
+
const visitorData = await visitorRes.json();
|
| 399 |
+
document.getElementById('visitorCount').textContent = visitorData.count;
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
renderDashboard();
|
| 403 |
} catch (error) {
|
| 404 |
console.error('Error fetching data:', error);
|
|
|
|
| 566 |
|
| 567 |
fetchData();
|
| 568 |
setInterval(fetchData, 30000);
|
| 569 |
+
|
| 570 |
+
// Feedback Submission
|
| 571 |
+
document.getElementById('feedbackForm').addEventListener('submit', async (e) => {
|
| 572 |
+
e.preventDefault();
|
| 573 |
+
const feedbackText = document.getElementById('feedbackText').value;
|
| 574 |
+
const statusDiv = document.getElementById('feedbackStatus');
|
| 575 |
+
|
| 576 |
+
statusDiv.textContent = 'Submitting...';
|
| 577 |
+
statusDiv.style.color = 'var(--accent)';
|
| 578 |
+
|
| 579 |
+
try {
|
| 580 |
+
const response = await fetch('/api/feedback', {
|
| 581 |
+
method: 'POST',
|
| 582 |
+
headers: {
|
| 583 |
+
'Content-Type': 'application/json',
|
| 584 |
+
},
|
| 585 |
+
body: JSON.stringify({ feedback: feedbackText }),
|
| 586 |
+
});
|
| 587 |
+
|
| 588 |
+
if (response.ok) {
|
| 589 |
+
statusDiv.textContent = '✅ Thank you for your feedback!';
|
| 590 |
+
statusDiv.style.color = 'var(--bullish)';
|
| 591 |
+
document.getElementById('feedbackText').value = '';
|
| 592 |
+
} else {
|
| 593 |
+
throw new Error('Failed to submit feedback');
|
| 594 |
+
}
|
| 595 |
+
} catch (error) {
|
| 596 |
+
statusDiv.textContent = '❌ Error submitting feedback. Please try again.';
|
| 597 |
+
statusDiv.style.color = 'var(--bearish)';
|
| 598 |
+
}
|
| 599 |
+
});
|
| 600 |
</script>
|
| 601 |
</body>
|
| 602 |
|
forex_macd_strategy.py
CHANGED
|
@@ -30,6 +30,17 @@ CONFIG = {
|
|
| 30 |
{"name": "MCX Crude Oil", "symbol": "CL=F", "pip_size": 0.01, "flag": "🇮🇳🛢️", "category": "Metals/Energy"},
|
| 31 |
{"name": "Gold", "symbol": "GC=F", "pip_size": 0.1, "flag": "🥇", "category": "Metals/Energy"},
|
| 32 |
{"name": "Silver", "symbol": "SI=F", "pip_size": 0.005, "flag": "🥈", "category": "Metals/Energy"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
{"name": "EUR/USD", "symbol": "EURUSD=X", "pip_size": 0.0001, "flag": "🇪🇺🇺🇸", "category": "Forex"},
|
| 34 |
{"name": "GBP/USD", "symbol": "GBPUSD=X", "pip_size": 0.0001, "flag": "🇬🇧🇺🇸", "category": "Forex"},
|
| 35 |
{"name": "USD/JPY", "symbol": "USDJPY=X", "pip_size": 0.01, "flag": "🇺🇸🇯🇵", "category": "Forex"},
|
|
@@ -40,6 +51,14 @@ CONFIG = {
|
|
| 40 |
{"name": "EUR/GBP", "symbol": "EURGBP=X", "pip_size": 0.0001, "flag": "🇪🇺🇬🇧", "category": "Forex"},
|
| 41 |
{"name": "EUR/JPY", "symbol": "EURJPY=X", "pip_size": 0.01, "flag": "🇪🇺🇯🇵", "category": "Forex"},
|
| 42 |
{"name": "GBP/JPY", "symbol": "GBPJPY=X", "pip_size": 0.01, "flag": "🇬🇧🇯🇵", "category": "Forex"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
{"name": "Bitcoin", "symbol": "BTC-USD", "pip_size": 1.0, "flag": "₿", "category": "Crypto Scalping"},
|
| 44 |
{"name": "Ethereum", "symbol": "ETH-USD", "pip_size": 0.1, "flag": "Ξ", "category": "Crypto Scalping"},
|
| 45 |
{"name": "Solana", "symbol": "SOL-USD", "pip_size": 0.01, "flag": "☀️", "category": "Crypto Scalping"},
|
|
@@ -243,17 +262,30 @@ def analyze_instrument(instrument: Dict) -> Dict:
|
|
| 243 |
# Get LATEST price for display
|
| 244 |
latest_price = entry_macd.iloc[-1]['Close']
|
| 245 |
|
| 246 |
-
# Special handling for MCX
|
| 247 |
current_price = latest_price
|
| 248 |
-
if name
|
| 249 |
try:
|
| 250 |
-
|
| 251 |
-
if not
|
| 252 |
-
rate =
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
except Exception as e:
|
| 256 |
-
print(f" ⚠️ Conversion failed: {e}")
|
| 257 |
|
| 258 |
# 3. Apply Rules
|
| 259 |
# Trend: MACD Line > 0 AND Price > EMA 200 (Relaxed for BTC/ETH)
|
|
|
|
| 30 |
{"name": "MCX Crude Oil", "symbol": "CL=F", "pip_size": 0.01, "flag": "🇮🇳🛢️", "category": "Metals/Energy"},
|
| 31 |
{"name": "Gold", "symbol": "GC=F", "pip_size": 0.1, "flag": "🥇", "category": "Metals/Energy"},
|
| 32 |
{"name": "Silver", "symbol": "SI=F", "pip_size": 0.005, "flag": "🥈", "category": "Metals/Energy"},
|
| 33 |
+
{"name": "Brent Crude Oil", "symbol": "BZ=F", "pip_size": 0.01, "flag": "🇬🇧🛢️", "category": "Metals/Energy"},
|
| 34 |
+
{"name": "Natural Gas", "symbol": "NG=F", "pip_size": 0.001, "flag": "🔥", "category": "Metals/Energy"},
|
| 35 |
+
{"name": "Platinum", "symbol": "PL=F", "pip_size": 0.1, "flag": "💍", "category": "Metals/Energy"},
|
| 36 |
+
{"name": "Palladium", "symbol": "PA=F", "pip_size": 0.1, "flag": "💎", "category": "Metals/Energy"},
|
| 37 |
+
|
| 38 |
+
{"name": "MCX Gold", "symbol": "GC=F", "pip_size": 1.0, "flag": "🇮🇳🥇", "category": "Metals/Energy"},
|
| 39 |
+
{"name": "MCX Gold Mini", "symbol": "GC=F", "pip_size": 1.0, "flag": "🇮🇳🥇📉", "category": "Metals/Energy"},
|
| 40 |
+
{"name": "MCX Silver", "symbol": "SI=F", "pip_size": 1.0, "flag": "🇮🇳🥈", "category": "Metals/Energy"},
|
| 41 |
+
{"name": "MCX Natural Gas", "symbol": "NG=F", "pip_size": 0.1, "flag": "🇮🇳🔥", "category": "Metals/Energy"},
|
| 42 |
+
{"name": "MCX Copper", "symbol": "HG=F", "pip_size": 0.05, "flag": "🇮🇳🧱", "category": "Metals/Energy"},
|
| 43 |
+
|
| 44 |
{"name": "EUR/USD", "symbol": "EURUSD=X", "pip_size": 0.0001, "flag": "🇪🇺🇺🇸", "category": "Forex"},
|
| 45 |
{"name": "GBP/USD", "symbol": "GBPUSD=X", "pip_size": 0.0001, "flag": "🇬🇧🇺🇸", "category": "Forex"},
|
| 46 |
{"name": "USD/JPY", "symbol": "USDJPY=X", "pip_size": 0.01, "flag": "🇺🇸🇯🇵", "category": "Forex"},
|
|
|
|
| 51 |
{"name": "EUR/GBP", "symbol": "EURGBP=X", "pip_size": 0.0001, "flag": "🇪🇺🇬🇧", "category": "Forex"},
|
| 52 |
{"name": "EUR/JPY", "symbol": "EURJPY=X", "pip_size": 0.01, "flag": "🇪🇺🇯🇵", "category": "Forex"},
|
| 53 |
{"name": "GBP/JPY", "symbol": "GBPJPY=X", "pip_size": 0.01, "flag": "🇬🇧🇯🇵", "category": "Forex"},
|
| 54 |
+
{"name": "AUD/JPY", "symbol": "AUDJPY=X", "pip_size": 0.01, "flag": "🇦🇺🇯🇵", "category": "Forex"},
|
| 55 |
+
{"name": "NZD/JPY", "symbol": "NZDJPY=X", "pip_size": 0.01, "flag": "🇳🇿🇯🇵", "category": "Forex"},
|
| 56 |
+
{"name": "GBP/CHF", "symbol": "GBPCHF=X", "pip_size": 0.0001, "flag": "🇬🇧🇨🇭", "category": "Forex"},
|
| 57 |
+
{"name": "EUR/CAD", "symbol": "EURCAD=X", "pip_size": 0.0001, "flag": "🇪🇺🇨🇦", "category": "Forex"},
|
| 58 |
+
{"name": "AUD/CAD", "symbol": "AUDCAD=X", "pip_size": 0.0001, "flag": "🇦🇺🇨🇦", "category": "Forex"},
|
| 59 |
+
{"name": "CAD/JPY", "symbol": "CADJPY=X", "pip_size": 0.01, "flag": "🇨🇦🇯🇵", "category": "Forex"},
|
| 60 |
+
{"name": "CHF/JPY", "symbol": "CHFJPY=X", "pip_size": 0.01, "flag": "🇨🇭🇯🇵", "category": "Forex"},
|
| 61 |
+
|
| 62 |
{"name": "Bitcoin", "symbol": "BTC-USD", "pip_size": 1.0, "flag": "₿", "category": "Crypto Scalping"},
|
| 63 |
{"name": "Ethereum", "symbol": "ETH-USD", "pip_size": 0.1, "flag": "Ξ", "category": "Crypto Scalping"},
|
| 64 |
{"name": "Solana", "symbol": "SOL-USD", "pip_size": 0.01, "flag": "☀️", "category": "Crypto Scalping"},
|
|
|
|
| 262 |
# Get LATEST price for display
|
| 263 |
latest_price = entry_macd.iloc[-1]['Close']
|
| 264 |
|
| 265 |
+
# Special handling for MCX Instruments (Convert USD to INR with unit factors)
|
| 266 |
current_price = latest_price
|
| 267 |
+
if name.startswith("MCX"):
|
| 268 |
try:
|
| 269 |
+
usdinr_df = fetch_data("USDINR=X", "1d", "5d")
|
| 270 |
+
if not usdinr_df.empty:
|
| 271 |
+
rate = usdinr_df['Close'].iloc[-1]
|
| 272 |
+
|
| 273 |
+
if name in ["MCX Gold", "MCX Gold Mini"]:
|
| 274 |
+
# Convert Ounce to 10g: (Price / 31.1035) * 10
|
| 275 |
+
current_price = (latest_price / 31.1035) * 10 * rate
|
| 276 |
+
elif name == "MCX Silver":
|
| 277 |
+
# Convert Ounce to 1kg: Price * 32.1507
|
| 278 |
+
current_price = latest_price * 32.1507 * rate
|
| 279 |
+
elif name == "MCX Copper":
|
| 280 |
+
# Convert lb to 1kg: Price * 2.20462
|
| 281 |
+
current_price = latest_price * 2.20462 * rate
|
| 282 |
+
else:
|
| 283 |
+
# Crude Oil and Natural Gas: Direct conversion
|
| 284 |
+
current_price = latest_price * rate
|
| 285 |
+
|
| 286 |
+
print(f" 💱 Converted {name}: ${latest_price:.2f} -> ₹{current_price:.2f} (Rate: {rate:.2f})")
|
| 287 |
except Exception as e:
|
| 288 |
+
print(f" ⚠️ Conversion failed for {name}: {e}")
|
| 289 |
|
| 290 |
# 3. Apply Rules
|
| 291 |
# Trend: MACD Line > 0 AND Price > EMA 200 (Relaxed for BTC/ETH)
|
serve_forex_macd.py
CHANGED
|
@@ -6,16 +6,82 @@ Simple HTTP server to serve the Forex MACD dashboard on Hugging Face Spaces.
|
|
| 6 |
import http.server
|
| 7 |
import socketserver
|
| 8 |
import os
|
|
|
|
| 9 |
from pathlib import Path
|
| 10 |
|
| 11 |
# Hugging Face Spaces expects the app to run on port 7860
|
| 12 |
PORT = 7860
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class DashboardHandler(http.server.SimpleHTTPRequestHandler):
|
| 15 |
def do_GET(self):
|
| 16 |
if self.path == '/':
|
| 17 |
self.path = '/forex_macd_dashboard.html'
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def end_headers(self):
|
| 21 |
self.send_header('Access-Control-Allow-Origin', '*')
|
|
@@ -23,11 +89,20 @@ class DashboardHandler(http.server.SimpleHTTPRequestHandler):
|
|
| 23 |
super().end_headers()
|
| 24 |
|
| 25 |
def main():
|
| 26 |
-
os.chdir(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
Handler = DashboardHandler
|
| 29 |
|
| 30 |
# Bind to 0.0.0.0 to be accessible externally
|
|
|
|
| 31 |
with socketserver.TCPServer(("0.0.0.0", PORT), Handler) as httpd:
|
| 32 |
print("=" * 60)
|
| 33 |
print("🚀 Forex MACD Dashboard Server (HF Space)")
|
|
|
|
| 6 |
import http.server
|
| 7 |
import socketserver
|
| 8 |
import os
|
| 9 |
+
import json
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
# Hugging Face Spaces expects the app to run on port 7860
|
| 13 |
PORT = 7860
|
| 14 |
+
BASE_DIR = Path(__file__).parent
|
| 15 |
+
VISITORS_FILE = BASE_DIR / "visitors.json"
|
| 16 |
+
FEEDBACK_FILE = BASE_DIR / "feedback.json"
|
| 17 |
+
|
| 18 |
+
def get_visitor_count(ip_address=None):
|
| 19 |
+
visitors = set()
|
| 20 |
+
if VISITORS_FILE.exists():
|
| 21 |
+
try:
|
| 22 |
+
with open(VISITORS_FILE, 'r') as f:
|
| 23 |
+
visitors = set(json.load(f))
|
| 24 |
+
except:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
if ip_address:
|
| 28 |
+
visitors.add(ip_address)
|
| 29 |
+
try:
|
| 30 |
+
with open(VISITORS_FILE, 'w') as f:
|
| 31 |
+
json.dump(list(visitors), f)
|
| 32 |
+
except:
|
| 33 |
+
pass
|
| 34 |
+
|
| 35 |
+
return len(visitors)
|
| 36 |
|
| 37 |
class DashboardHandler(http.server.SimpleHTTPRequestHandler):
|
| 38 |
def do_GET(self):
|
| 39 |
if self.path == '/':
|
| 40 |
self.path = '/forex_macd_dashboard.html'
|
| 41 |
+
get_visitor_count(self.client_address[0])
|
| 42 |
+
elif self.path == '/api/visitor-count':
|
| 43 |
+
self.send_response(200)
|
| 44 |
+
self.send_header('Content-type', 'application/json')
|
| 45 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 46 |
+
self.end_headers()
|
| 47 |
+
count = get_visitor_count()
|
| 48 |
+
self.wfile.write(json.dumps({"count": count}).encode())
|
| 49 |
+
return
|
| 50 |
+
|
| 51 |
+
return super().do_GET()
|
| 52 |
+
|
| 53 |
+
def do_POST(self):
|
| 54 |
+
if self.path == '/api/feedback':
|
| 55 |
+
content_length = int(self.headers['Content-Length'])
|
| 56 |
+
post_data = self.rfile.read(content_length)
|
| 57 |
+
feedback_data = json.loads(post_data.decode('utf-8'))
|
| 58 |
+
|
| 59 |
+
feedbacks = []
|
| 60 |
+
if FEEDBACK_FILE.exists():
|
| 61 |
+
try:
|
| 62 |
+
with open(FEEDBACK_FILE, 'r') as f:
|
| 63 |
+
feedbacks = json.load(f)
|
| 64 |
+
except:
|
| 65 |
+
pass
|
| 66 |
+
|
| 67 |
+
feedbacks.append({
|
| 68 |
+
"time": Path(__file__).stat().st_mtime,
|
| 69 |
+
"ip": self.client_address[0],
|
| 70 |
+
"feedback": feedback_data.get('feedback')
|
| 71 |
+
})
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
with open(FEEDBACK_FILE, 'w') as f:
|
| 75 |
+
json.dump(feedbacks, f, indent=2)
|
| 76 |
+
except:
|
| 77 |
+
pass
|
| 78 |
+
|
| 79 |
+
self.send_response(200)
|
| 80 |
+
self.send_header('Content-type', 'application/json')
|
| 81 |
+
self.send_header('Access-Control-Allow-Origin', '*')
|
| 82 |
+
self.end_headers()
|
| 83 |
+
self.wfile.write(json.dumps({"status": "success"}).encode())
|
| 84 |
+
return
|
| 85 |
|
| 86 |
def end_headers(self):
|
| 87 |
self.send_header('Access-Control-Allow-Origin', '*')
|
|
|
|
| 89 |
super().end_headers()
|
| 90 |
|
| 91 |
def main():
|
| 92 |
+
os.chdir(BASE_DIR)
|
| 93 |
+
|
| 94 |
+
# Initialize files if not exists
|
| 95 |
+
if not VISITORS_FILE.exists():
|
| 96 |
+
with open(VISITORS_FILE, 'w') as f:
|
| 97 |
+
json.dump([], f)
|
| 98 |
+
if not FEEDBACK_FILE.exists():
|
| 99 |
+
with open(FEEDBACK_FILE, 'w') as f:
|
| 100 |
+
json.dump([], f)
|
| 101 |
|
| 102 |
Handler = DashboardHandler
|
| 103 |
|
| 104 |
# Bind to 0.0.0.0 to be accessible externally
|
| 105 |
+
socketserver.TCPServer.allow_reuse_address = True
|
| 106 |
with socketserver.TCPServer(("0.0.0.0", PORT), Handler) as httpd:
|
| 107 |
print("=" * 60)
|
| 108 |
print("🚀 Forex MACD Dashboard Server (HF Space)")
|