Spaces:
Running
Running
Deploy Aureon OS (deploy/selfhost stack: nginx edge + fusion API x2 + console + HNC organ + redis + postgres; paper mode) (part 19)
da0ce7b verified Download scripts/traders/coherenceTrader.ts from GaryLeckey/aureon-os: direct link, hf CLI and curl.
- Browser
- Download file 21.3 kB
-
https://huggingface.co/spaces/GaryLeckey/aureon-os/resolve/main/scripts/traders/coherenceTrader.ts
- Command line
-
hf download hf://spaces/GaryLeckey/aureon-os/scripts/traders/coherenceTrader.ts
-
curl -L -o coherenceTrader.ts https://huggingface.co/spaces/GaryLeckey/aureon-os/resolve/main/scripts/traders/coherenceTrader.ts
21.3 kB
| /** | |
| * π΅ COHERENCE-BASED TRADER π΅ | |
| * | |
| * THE REAL AUREON SYSTEM | |
| * | |
| * Based on README methodology: | |
| * - Entry: Coherence Ξ¦(t) > 0.938 | |
| * - Exit: Coherence Ξ¦(t) < 0.934 | |
| * - Win Rate: 85.3% | |
| * - Expected Value: +1.42% per trade (before fees) | |
| * | |
| * Core Formula: | |
| * Ξ(t) = S(t) + O(t) + E(t) | |
| * | |
| * Where: | |
| * Ξ(t) = Lambda - The unified field state | |
| * S(t) = Substrate - 9 Auris nodes respond to market | |
| * O(t) = Observer - Self-referential field awareness (Ξ(t-1) Γ 0.3) | |
| * E(t) = Echo - Memory and momentum (avg(Ξ[t-5:t]) Γ 0.2) | |
| * Ξ = Coherence = 1 - (variance / 10) | |
| * | |
| * Run: npx tsx scripts/coherenceTrader.ts | |
| */ | |
| import * as https from 'https'; | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // CONFIGURATION | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| const CONFIG = { | |
| BALANCE_PER_BROKER: 20, | |
| POSITION_SIZE_PCT: 0.20, // 20% of account per trade | |
| STOP_LOSS_PCT: 0.008, // 0.8% stop loss | |
| TAKE_PROFIT_PCT: 0.018, // 1.8% take profit | |
| // Coherence thresholds (from README) | |
| ENTRY_COHERENCE: 0.938, // Enter when Ξ¦(t) > 0.938 | |
| EXIT_COHERENCE: 0.934, // Exit when Ξ¦(t) < 0.934 | |
| // Scan interval | |
| SCAN_INTERVAL_MS: 3000, | |
| }; | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // THE 9 AURIS NODES (From README) | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| interface AurisNode { | |
| name: string; | |
| emoji: string; | |
| weight: number; | |
| compute: (snapshot: MarketSnapshot) => number; | |
| } | |
| interface MarketSnapshot { | |
| price: number; | |
| volume: number; // 0-1 normalized | |
| volatility: number; // Realized vol | |
| momentum: number; // Price momentum | |
| spread: number; // Bid-ask spread (normalized 0-1) | |
| timestamp: number; | |
| } | |
| // The 9 Auris Nodes from README | |
| const AURIS_NODES: Record<string, AurisNode> = { | |
| tiger: { | |
| name: 'π― Tiger', | |
| emoji: 'π―', | |
| weight: 1.2, | |
| compute: (snap: MarketSnapshot) => snap.volatility * 0.8 + snap.spread * 0.5, | |
| }, | |
| falcon: { | |
| name: 'π¦ Falcon', | |
| emoji: 'π¦ ', | |
| weight: 1.1, | |
| compute: (snap: MarketSnapshot) => Math.abs(snap.momentum) * 0.7 + snap.volume * 0.3, | |
| }, | |
| hummingbird: { | |
| name: 'π¦ Hummingbird', | |
| emoji: 'π¦', | |
| weight: 0.8, | |
| compute: (snap: MarketSnapshot) => 1 / (snap.volatility + 0.01) * 0.6, | |
| }, | |
| dolphin: { | |
| name: 'π¬ Dolphin', | |
| emoji: 'π¬', | |
| weight: 1.0, | |
| compute: (snap: MarketSnapshot) => Math.sin(snap.momentum) * 0.5, | |
| }, | |
| deer: { | |
| name: 'π¦ Deer', | |
| emoji: 'π¦', | |
| weight: 0.9, | |
| compute: (snap: MarketSnapshot) => | |
| snap.volume * 0.2 + snap.volatility * 0.3 + snap.spread * 0.2, | |
| }, | |
| owl: { | |
| name: 'π¦ Owl', | |
| emoji: 'π¦', | |
| weight: 1.0, | |
| compute: (snap: MarketSnapshot) => | |
| Math.cos(snap.momentum) * 0.6 + (snap.momentum < 0 ? 0.3 : 0), | |
| }, | |
| panda: { | |
| name: 'πΌ Panda', | |
| emoji: 'πΌ', | |
| weight: 0.95, | |
| compute: (snap: MarketSnapshot) => | |
| snap.volume > 0.7 ? snap.volume * 0.8 : 0.2, | |
| }, | |
| cargoship: { | |
| name: 'π’ CargoShip', | |
| emoji: 'π’', | |
| weight: 1.3, | |
| compute: (snap: MarketSnapshot) => | |
| snap.volume > 0.8 ? snap.volume * 1.2 : 0, | |
| }, | |
| clownfish: { | |
| name: 'π Clownfish', | |
| emoji: 'π ', | |
| weight: 0.7, | |
| compute: (snap: MarketSnapshot) => | |
| Math.abs(snap.price - snap.price * 0.999) * 100, | |
| }, | |
| }; | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // MASTER EQUATION & COHERENCE CALCULATION | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class CoherenceCalculator { | |
| private lambdaHistory: number[] = []; // Last 5 Lambda values for Echo | |
| private maxHistorySize: number = 5; | |
| /** | |
| * Calculate Substrate: S(t) = Ξ£(node.compute(snapshot) Γ node.weight) / 9 | |
| */ | |
| private calculateSubstrate(snapshot: MarketSnapshot): number { | |
| let sum = 0; | |
| let totalWeight = 0; | |
| Object.values(AURIS_NODES).forEach((node) => { | |
| const nodeResponse = node.compute(snapshot) * node.weight; | |
| sum += nodeResponse; | |
| totalWeight += node.weight; | |
| }); | |
| return (sum / totalWeight) * 0.25; // Scale to 0-1 range | |
| } | |
| /** | |
| * Calculate Observer: O(t) = Ξ(t-1) Γ 0.3 | |
| */ | |
| private calculateObserver(): number { | |
| if (this.lambdaHistory.length === 0) return 0; | |
| return this.lambdaHistory[this.lambdaHistory.length - 1] * 0.3; | |
| } | |
| /** | |
| * Calculate Echo: E(t) = average(Ξ[t-5:t]) Γ 0.2 | |
| */ | |
| private calculateEcho(): number { | |
| if (this.lambdaHistory.length < 2) return 0; | |
| const avg = | |
| this.lambdaHistory.reduce((a, b) => a + b, 0) / | |
| this.lambdaHistory.length; | |
| return avg * 0.2; | |
| } | |
| /** | |
| * Master Equation: Ξ(t) = S(t) + O(t) + E(t) | |
| */ | |
| private calculateLambda(snapshot: MarketSnapshot): number { | |
| const substrate = this.calculateSubstrate(snapshot); | |
| const observer = this.calculateObserver(); | |
| const echo = this.calculateEcho(); | |
| const lambda = Math.max(0, Math.min(1, substrate + observer + echo)); | |
| // Store in history for next iteration | |
| this.lambdaHistory.push(lambda); | |
| if (this.lambdaHistory.length > this.maxHistorySize) { | |
| this.lambdaHistory.shift(); | |
| } | |
| return lambda; | |
| } | |
| /** | |
| * Coherence: Ξ = 1 - (variance / 10) | |
| * Where variance is calculated from recent prices | |
| */ | |
| private calculateCoherence(lambda: number): number { | |
| // Use lambda variance + market microstructure variance | |
| const variance = | |
| Math.abs(lambda - 0.5) + Math.random() * 0.05; // Add market noise | |
| const coherence = Math.max(0, Math.min(1, 1 - variance / 10)); | |
| return coherence; | |
| } | |
| /** | |
| * Main: Calculate full coherence state | |
| */ | |
| public calculate(snapshot: MarketSnapshot): { | |
| lambda: number; | |
| coherence: number; | |
| signal: 'BUY' | 'SELL' | 'HOLD'; | |
| confidence: number; | |
| } { | |
| const lambda = this.calculateLambda(snapshot); | |
| const coherence = this.calculateCoherence(lambda); | |
| let signal: 'BUY' | 'SELL' | 'HOLD' = 'HOLD'; | |
| let confidence = 0; | |
| if (coherence > CONFIG.ENTRY_COHERENCE) { | |
| // Strong buy signal when coherence peaks | |
| if (lambda > 0.6) { | |
| signal = 'BUY'; | |
| confidence = Math.min(1, coherence * 1.2); | |
| } else { | |
| signal = 'SELL'; | |
| confidence = Math.min(1, coherence * 1.2); | |
| } | |
| } | |
| return { lambda, coherence, signal, confidence }; | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // BROKER CONFIGURATION | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| interface Broker { | |
| name: string; | |
| emoji: string; | |
| exchange?: string; | |
| assets: string[]; | |
| } | |
| const BROKERS: Broker[] = [ | |
| { | |
| name: 'Binance', | |
| emoji: 'πͺ', | |
| exchange: 'binance', | |
| assets: ['BTCUSDT', 'ETHUSDT', 'ADAUSDT', 'BNBUSDT'], | |
| }, | |
| { | |
| name: 'OKX', | |
| emoji: 'β', | |
| exchange: 'okx', | |
| assets: ['BTC-USDT', 'ETH-USDT', 'ADA-USDT'], | |
| }, | |
| { | |
| name: 'Kraken', | |
| emoji: 'π¦', | |
| exchange: 'kraken', | |
| assets: ['XBTUSDT', 'ETHUSDT', 'ADAUSDT'], | |
| }, | |
| { | |
| name: 'OANDA', | |
| emoji: 'π±', | |
| exchange: 'oanda', | |
| assets: ['EUR_USD', 'GBP_USD', 'USD_JPY'], | |
| }, | |
| { | |
| name: 'IG Markets', | |
| emoji: 'π', | |
| exchange: 'ig', | |
| assets: ['BTC/USD', 'ES', 'FTSE'], | |
| }, | |
| { | |
| name: 'CMC Markets', | |
| emoji: 'π', | |
| exchange: 'cmc', | |
| assets: ['BTC/USD', 'ES', 'FTSE'], | |
| }, | |
| { | |
| name: 'Capital.com', | |
| emoji: 'π', | |
| exchange: 'capital', | |
| assets: ['BTCUSD', 'ETHUSD', 'ES_F'], | |
| }, | |
| { | |
| name: 'Coinbase', | |
| emoji: 'π ', | |
| exchange: 'coinbase', | |
| assets: ['BTC-USD', 'ETH-USD', 'ADA-USD'], | |
| }, | |
| ]; | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // MARKET DATA FETCHER | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async function fetchBinancePrice(symbol: string): Promise<number | null> { | |
| return new Promise((resolve) => { | |
| https | |
| .get( | |
| `https://api.binance.com/api/v3/ticker/price?symbol=${symbol}`, | |
| (res) => { | |
| let data = ''; | |
| res.on('data', (chunk) => (data += chunk)); | |
| res.on('end', () => { | |
| try { | |
| const json = JSON.parse(data); | |
| resolve(parseFloat(json.price)); | |
| } catch { | |
| resolve(null); | |
| } | |
| }); | |
| } | |
| ) | |
| .on('error', () => resolve(null)); | |
| }); | |
| } | |
| async function fetchPrice( | |
| broker: Broker, | |
| asset: string | |
| ): Promise<MarketSnapshot | null> { | |
| let price: number | null = null; | |
| if (broker.exchange === 'binance') { | |
| price = await fetchBinancePrice(asset); | |
| } else { | |
| // Simulate other brokers with binance data | |
| const baseAsset = asset.replace(/[^\w]/g, ''); | |
| price = await fetchBinancePrice(baseAsset + 'USDT'); | |
| } | |
| if (!price) return null; | |
| // Create realistic market snapshot | |
| return { | |
| price, | |
| volume: 0.6 + Math.random() * 0.3, // 60-90% normalized | |
| volatility: 0.015 + Math.random() * 0.02, // 1.5-3.5% | |
| momentum: (Math.random() - 0.5) * 0.05, // -2.5% to +2.5% | |
| spread: 0.0001 + Math.random() * 0.0003, // Realistic spread | |
| timestamp: Date.now(), | |
| }; | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // POSITION TRACKING | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| interface Position { | |
| broker: string; | |
| asset: string; | |
| direction: 'LONG' | 'SHORT'; | |
| entryPrice: number; | |
| entryTime: number; | |
| size: number; | |
| coherenceAtEntry: number; | |
| pnlGross: number; | |
| pnlNet: number; | |
| fees: number; | |
| } | |
| interface BrokerState { | |
| balance: number; | |
| positions: Position[]; | |
| wins: number; | |
| losses: number; | |
| totalFees: number; | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // MAIN TRADER ENGINE | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class CoherenceTrader { | |
| private coherenceCalc = new CoherenceCalculator(); | |
| private brokerStates: Map<string, BrokerState> = new Map(); | |
| private scanCount = 0; | |
| constructor() { | |
| // Initialize brokers | |
| BROKERS.forEach((broker) => { | |
| this.brokerStates.set(broker.emoji + broker.name, { | |
| balance: CONFIG.BALANCE_PER_BROKER, | |
| positions: [], | |
| wins: 0, | |
| losses: 0, | |
| totalFees: 0, | |
| }); | |
| }); | |
| } | |
| async scan(): Promise<void> { | |
| this.scanCount++; | |
| const timestamp = new Date().toLocaleTimeString(); | |
| console.log(`\nπ SCAN #${this.scanCount} | ${timestamp}`); | |
| console.log('β'.repeat(80)); | |
| let totalSignals = 0; | |
| let buySignals = 0; | |
| let sellSignals = 0; | |
| let totalCoherence = 0; | |
| let signalCount = 0; | |
| // Process each broker | |
| for (const broker of BROKERS) { | |
| const key = broker.emoji + broker.name; | |
| const state = this.brokerStates.get(key); | |
| if (!state) continue; | |
| // Get random asset from broker | |
| const asset = broker.assets[Math.floor(Math.random() * broker.assets.length)]; | |
| // Fetch market data | |
| const snapshot = await fetchPrice(broker, asset); | |
| if (!snapshot) { | |
| console.log(`${broker.emoji} ${broker.name}: β οΈ No price data`); | |
| continue; | |
| } | |
| // Calculate coherence | |
| const result = this.coherenceCalc.calculate(snapshot); | |
| totalCoherence += result.coherence; | |
| signalCount++; | |
| // Display coherence state | |
| const coherenceBar = this.drawCoherenceBar(result.coherence); | |
| const status = | |
| result.coherence > CONFIG.ENTRY_COHERENCE | |
| ? 'π’ ENTRY' | |
| : result.coherence > CONFIG.EXIT_COHERENCE | |
| ? 'π‘ HOLD' | |
| : 'π΄ EXIT'; | |
| console.log( | |
| `${broker.emoji} ${broker.name.padEnd(15)} | Ξ¦=${result.coherence.toFixed(4)} ${coherenceBar} ${status} | ${asset}` | |
| ); | |
| if (result.signal === 'BUY') { | |
| buySignals++; | |
| totalSignals++; | |
| } else if (result.signal === 'SELL') { | |
| sellSignals++; | |
| totalSignals++; | |
| } | |
| // Execute trade if signal strong enough | |
| if (result.coherence > CONFIG.ENTRY_COHERENCE) { | |
| this.enterPosition(broker, key, state, asset, snapshot, result); | |
| } | |
| // Update existing positions | |
| this.updatePositions(key, state, snapshot); | |
| } | |
| console.log('β'.repeat(80)); | |
| const avgCoherence = | |
| signalCount > 0 ? (totalCoherence / signalCount).toFixed(4) : '0.0000'; | |
| console.log( | |
| `π Signals: ${buySignals} BUY | ${sellSignals} SELL | Avg Ξ¦: ${avgCoherence}` | |
| ); | |
| // Print broker summary | |
| this.printBrokerSummary(); | |
| } | |
| private enterPosition( | |
| broker: Broker, | |
| key: string, | |
| state: BrokerState, | |
| asset: string, | |
| snapshot: MarketSnapshot, | |
| result: { lambda: number; coherence: number; signal: 'BUY' | 'SELL' | 'HOLD'; confidence: number } | |
| ): void { | |
| if (state.positions.length >= 3) return; // Max 3 positions per broker | |
| const size = (state.balance * CONFIG.POSITION_SIZE_PCT) / snapshot.price; | |
| if (size <= 0) return; | |
| const position: Position = { | |
| broker: broker.name, | |
| asset, | |
| direction: result.signal === 'BUY' ? 'LONG' : 'SHORT', | |
| entryPrice: snapshot.price, | |
| entryTime: Date.now(), | |
| size, | |
| coherenceAtEntry: result.coherence, | |
| pnlGross: 0, | |
| pnlNet: 0, | |
| fees: snapshot.price * size * 0.002, // Estimate 0.2% round-trip fee | |
| }; | |
| state.positions.push(position); | |
| console.log( | |
| ` β ENTRY ${position.direction} ${asset} @ ${snapshot.price.toFixed(2)} (Ξ¦=${result.coherence.toFixed(4)})` | |
| ); | |
| } | |
| private updatePositions( | |
| key: string, | |
| state: BrokerState, | |
| currentPrice: MarketSnapshot | |
| ): void { | |
| for (let i = state.positions.length - 1; i >= 0; i--) { | |
| const pos = state.positions[i]; | |
| // Calculate P&L | |
| const priceDiff = | |
| pos.direction === 'LONG' | |
| ? currentPrice.price - pos.entryPrice | |
| : pos.entryPrice - currentPrice.price; | |
| pos.pnlGross = priceDiff * pos.size; | |
| pos.pnlNet = pos.pnlGross - pos.fees; | |
| // Exit conditions | |
| if (pos.pnlNet >= pos.entryPrice * pos.size * CONFIG.TAKE_PROFIT_PCT) { | |
| console.log( | |
| ` β EXIT (TP) ${pos.asset} | P&L: +Β£${pos.pnlNet.toFixed(4)}` | |
| ); | |
| state.balance += pos.pnlNet; | |
| state.wins++; | |
| state.totalFees += pos.fees; | |
| state.positions.splice(i, 1); | |
| } else if (pos.pnlNet <= -(pos.entryPrice * pos.size * CONFIG.STOP_LOSS_PCT)) { | |
| console.log( | |
| ` β EXIT (SL) ${pos.asset} | P&L: -Β£${Math.abs(pos.pnlNet).toFixed(4)}` | |
| ); | |
| state.balance += pos.pnlNet; | |
| state.losses++; | |
| state.totalFees += pos.fees; | |
| state.positions.splice(i, 1); | |
| } | |
| } | |
| } | |
| private printBrokerSummary(): void { | |
| console.log('\nπΌ BROKER SUMMARY:'); | |
| console.log('β'.repeat(80)); | |
| let totalBalance = 0; | |
| let totalWins = 0; | |
| let totalLosses = 0; | |
| let totalPnL = 0; | |
| let totalFees = 0; | |
| this.brokerStates.forEach((state, key) => { | |
| totalBalance += state.balance; | |
| totalWins += state.wins; | |
| totalLosses += state.losses; | |
| totalFees += state.totalFees; | |
| const winRate = | |
| totalWins + totalLosses > 0 | |
| ? ((totalWins / (totalWins + totalLosses)) * 100).toFixed(1) | |
| : '0.0'; | |
| console.log( | |
| `${key.padEnd(25)} | Balance: Β£${state.balance.toFixed(2).padStart(8)} | W: ${totalWins} | L: ${totalLosses} | WR: ${winRate}%` | |
| ); | |
| }); | |
| console.log('β'.repeat(80)); | |
| console.log( | |
| `π° TOTAL CAPITAL: Β£${totalBalance.toFixed(2)} | WIN RATE: ${ | |
| totalWins + totalLosses > 0 | |
| ? ((totalWins / (totalWins + totalLosses)) * 100).toFixed(1) | |
| : '0.0' | |
| }% | FEES: Β£${totalFees.toFixed(2)}` | |
| ); | |
| } | |
| private drawCoherenceBar(coherence: number): string { | |
| const width = 20; | |
| const filled = Math.round(coherence * width); | |
| const bar = 'β'.repeat(filled) + 'β'.repeat(width - filled); | |
| return `[${bar}]`; | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // MAIN ENTRY POINT | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async function main(): Promise<void> { | |
| console.log(` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β π΅ COHERENCE-BASED TRADER π΅ β | |
| β β | |
| β Entry Threshold: Ξ¦(t) > 0.938 β | |
| β Exit Threshold: Ξ¦(t) < 0.934 β | |
| β Expected Win Rate: 85.3% β | |
| β Expected Value: +1.42% per trade (before fees) β | |
| β β | |
| β Formula: Ξ(t) = S(t) + O(t) + E(t) β | |
| β - S(t) = Substrate (9 Auris nodes) β | |
| β - O(t) = Observer (self-aware field) β | |
| β - E(t) = Echo (momentum & memory) β | |
| β β | |
| β Brokers: 8 deployed across crypto, forex, stocks, and CFDs β | |
| β Capital: Β£160 total (Β£20 per broker Γ 8) β | |
| β β | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| `); | |
| const trader = new CoherenceTrader(); | |
| // Continuous scanning | |
| while (true) { | |
| await trader.scan(); | |
| await new Promise((resolve) => setTimeout(resolve, CONFIG.SCAN_INTERVAL_MS)); | |
| } | |
| } | |
| main().catch(console.error); | |