Spaces:
Running
Running
Upload lib/market-simulator.js with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869// Simulates real-time market data and applies knowledge base ruleslet currentPrice = 150.00let trend = 0let rsi = 50let volume = 1000000
export function initializeMarket() { currentPrice = 150 + (Math.random() * 10 - 5) trend = Math.random() > 0.5 ? 1 : -1}
export function getMarketTick() { // Simulate price movement const volatility = 0.02 const change = (Math.random() - 0.5) * volatility * currentPrice const trendBias = trend * 0.005 * currentPrice currentPrice += change + trendBias // Simulate RSI movement (mean reversion logic simplified) if (currentPrice > 160) rsi = Math.min(90, rsi + 5) else if (currentPrice < 140) rsi = Math.max(10, rsi - 5) else rsi = 50 + (Math.random() * 20 - 10)
// Simulate Volume volume = 1000000 + (Math.random() * 500000) // Randomly change trend occasionally if (Math.random() < 0.05) trend = trend * -1
return { price: parseFloat(currentPrice.toFixed(2)), rsi: parseFloat(rsi.toFixed(2)), volume: Math.floor(volume), trend: trend > 0 ? 'UP' : 'DOWN', timestamp: new Date().toISOString() }}
export function analyzeMarket(data, knowledgeBase) { const signals = [] knowledgeBase.forEach(rule => { let match = false let reason = ""
if (rule.condition.includes("RSI < 30") && data.rsi < 30) { match = true reason = `RSI is ${data.rsi} (Oversold)` } else if (rule.condition.includes("RSI > 70") && data.rsi > 70) { match = true reason = `RSI is ${data.rsi} (Overbought)` } else if (rule.condition.includes("Volume") && data.volume > 2000000) { match = true reason = `Volume spike detected (${(data.volume/1000000).toFixed(2)}M)` }
if (match) { signals.push({ rule: rule.rule, action: rule.action === 'FOLLOW_TREND' ? (data.trend === 'UP' ? 'LONG' : 'SHORT') : rule.action, confidence: rule.confidence, reason: reason }) } })
return signals}lib/market-simulator.jsAdded+69