From 7993148a95e371c11c31ad8a34d4ff91107143ac Mon Sep 17 00:00:00 2001 From: Bojan Kucera Date: Wed, 4 Jun 2025 21:31:38 -0400 Subject: [PATCH] moved dashboard to top level and created a new system plan --- README.md | 15 - SIMPLIFIED-ARCHITECTURE.md | 782 ++++++++++++++++++ .../.editorconfig | 0 .../.gitignore | 0 .../.postcssrc.json | 0 .../.vscode/extensions.json | 0 .../.vscode/launch.json | 0 .../.vscode/tasks.json | 0 .../trading-dashboard => dashboard}/README.md | 0 .../angular.json | 0 .../package.json | 0 .../public/favicon.ico | Bin .../src/App.tsx | 0 .../src/app/app.config.ts | 0 .../src/app/app.css | 0 .../src/app/app.html | 0 .../src/app/app.routes.ts | 0 .../src/app/app.scss | 0 .../src/app/app.spec.ts | 0 .../src/app/app.ts | 0 .../notifications/notifications.css | 0 .../notifications/notifications.html | 0 .../components/notifications/notifications.ts | 0 .../components/sidebar/sidebar.component.css | 0 .../components/sidebar/sidebar.component.html | 0 .../components/sidebar/sidebar.component.ts | 0 .../pages/dashboard/dashboard.component.css | 0 .../pages/dashboard/dashboard.component.html | 0 .../pages/dashboard/dashboard.component.ts | 0 .../market-data/market-data.component.css | 0 .../market-data/market-data.component.html | 0 .../market-data/market-data.component.ts | 0 .../pages/portfolio/portfolio.component.css | 0 .../pages/portfolio/portfolio.component.html | 0 .../pages/portfolio/portfolio.component.ts | 0 .../risk-management.component.css | 0 .../risk-management.component.html | 0 .../risk-management.component.ts | 0 .../app/pages/settings/settings.component.css | 0 .../pages/settings/settings.component.html | 0 .../app/pages/settings/settings.component.ts | 0 .../components/drawdown-chart.component.ts | 0 .../components/equity-chart.component.ts | 0 .../performance-metrics.component.ts | 0 .../components/trades-table.component.ts | 0 .../dialogs/backtest-dialog.component.ts | 0 .../dialogs/strategy-dialog.component.html | 0 .../dialogs/strategy-dialog.component.ts | 0 .../pages/strategies/strategies.component.css | 0 .../strategies/strategies.component.html | 0 .../pages/strategies/strategies.component.ts | 0 .../strategy-details.component.css | 0 .../strategy-details.component.html | 0 .../strategy-details.component.ts | 0 .../src/app/services/api.service.ts | 0 .../src/app/services/notification.service.ts | 0 .../src/app/services/strategy.service.ts | 0 .../src/app/services/websocket.service.ts | 0 .../src/components/DashboardLayout.tsx | 0 .../src/components/TradingDashboard.tsx | 0 .../src/index.css | 0 .../src/index.html | 0 .../src/main.ts | 0 .../src/styles.css | 0 .../src/styles.scss | 0 .../tsconfig.app.json | 0 .../tsconfig.json | 2 +- .../tsconfig.spec.json | 0 .../trading-dashboard/postcss.config.js | 0 .../trading-dashboard/tailwind.config.js | 52 -- docs/current-system-flow.md | 232 ------ docs/enhanced-architecture.md | 87 -- docs/interface-services/.gitkeep | 0 docs/interface-services/README.md | 26 - .../trading-dashboard/.gitkeep | 0 .../trading-dashboard/README.md | 87 -- 76 files changed, 783 insertions(+), 500 deletions(-) delete mode 100644 README.md create mode 100644 SIMPLIFIED-ARCHITECTURE.md rename apps/{interface-services/trading-dashboard => dashboard}/.editorconfig (100%) rename apps/{interface-services/trading-dashboard => dashboard}/.gitignore (100%) rename apps/{interface-services/trading-dashboard => dashboard}/.postcssrc.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/.vscode/extensions.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/.vscode/launch.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/.vscode/tasks.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/README.md (100%) rename apps/{interface-services/trading-dashboard => dashboard}/angular.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/package.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/public/favicon.ico (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/App.tsx (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.config.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.routes.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.scss (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.spec.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/app.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/components/notifications/notifications.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/components/notifications/notifications.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/components/notifications/notifications.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/components/sidebar/sidebar.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/components/sidebar/sidebar.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/components/sidebar/sidebar.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/dashboard/dashboard.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/dashboard/dashboard.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/dashboard/dashboard.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/market-data/market-data.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/market-data/market-data.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/market-data/market-data.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/portfolio/portfolio.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/portfolio/portfolio.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/portfolio/portfolio.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/risk-management/risk-management.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/risk-management/risk-management.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/risk-management/risk-management.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/settings/settings.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/settings/settings.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/settings/settings.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/components/drawdown-chart.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/components/equity-chart.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/components/performance-metrics.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/components/trades-table.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/dialogs/backtest-dialog.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/dialogs/strategy-dialog.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/dialogs/strategy-dialog.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/strategies.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/strategies.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/strategies.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/strategy-details/strategy-details.component.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/strategy-details/strategy-details.component.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/pages/strategies/strategy-details/strategy-details.component.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/services/api.service.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/services/notification.service.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/services/strategy.service.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/app/services/websocket.service.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/components/DashboardLayout.tsx (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/components/TradingDashboard.tsx (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/index.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/index.html (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/main.ts (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/styles.css (100%) rename apps/{interface-services/trading-dashboard => dashboard}/src/styles.scss (100%) rename apps/{interface-services/trading-dashboard => dashboard}/tsconfig.app.json (100%) rename apps/{interface-services/trading-dashboard => dashboard}/tsconfig.json (95%) rename apps/{interface-services/trading-dashboard => dashboard}/tsconfig.spec.json (100%) delete mode 100644 apps/interface-services/trading-dashboard/postcss.config.js delete mode 100644 apps/interface-services/trading-dashboard/tailwind.config.js delete mode 100644 docs/current-system-flow.md delete mode 100644 docs/enhanced-architecture.md delete mode 100644 docs/interface-services/.gitkeep delete mode 100644 docs/interface-services/README.md delete mode 100644 docs/interface-services/trading-dashboard/.gitkeep delete mode 100644 docs/interface-services/trading-dashboard/README.md diff --git a/README.md b/README.md deleted file mode 100644 index 521e0ea..0000000 --- a/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# stock-bot - -To install dependencies: - -```bash -bun install -``` - -To run: - -```bash -bun run -``` - -This project was created using `bun init` in bun v1.2.15. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/SIMPLIFIED-ARCHITECTURE.md b/SIMPLIFIED-ARCHITECTURE.md new file mode 100644 index 0000000..0d705e9 --- /dev/null +++ b/SIMPLIFIED-ARCHITECTURE.md @@ -0,0 +1,782 @@ +# Stock Trading Bot - Simplified Combined Architecture + +## Overview + +A TypeScript-based simplified microservices architecture for automated stock trading with integrated multi-mode backtesting capabilities. The system combines live trading and backtesting functionality into unified services while supporting live, event-driven, and vectorized backtesting modes. + +## Architecture Diagram + +``` +┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ +│ Data Service │ │Processing Service│ │Strategy Service │ +│ │ │ │ │ │ +│ • Live APIs │────▶│ • Indicators │────▶│ • Live Mode │ +│ • Historical │ │ • Processing │ │ • Event Mode │ +│ • QuestDB │ │ • Validation │ │ • Vector Mode │ +│ • Unified API │ │ • Utils Lib │ │ • Backtesting │ +└─────────────────┘ └─────────────────┘ └─────────────────┘ + │ │ │ + │ ┌─────────────────┐ │ + │ │ Event Bus │ │ + └──────────────▶│ (Dragonfly) │◀─────────────┘ + └─────────────────┘ + │ +┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ +│Execution Service│ │Portfolio Service│ │ Dashboard │ +│ │ │ │ │ │ +│ • Live Brokers │ │ • Positions │ │ • Angular UI │ +│ • Simulation │ │ • Risk Mgmt │ │ • Real-time │ +│ • Mode Switch │ │ • P&L Tracking │ │ • Backtest UI │ +│ • Unified API │ │ • Performance │ │ • Reports │ +└─────────────────┘ └─────────────────┘ └─────────────────┘ +``` + +## Simplified Service Structure + +### 6 Core Services (Instead of 12) + +``` +stock-bot/ +├── apps/ +│ ├── data-service/ # Combined data ingestion & historical +│ │ ├── src/ +│ │ │ ├── providers/ # Live & historical data providers +│ │ │ │ ├── live/ # Real-time APIs (WebSocket, REST) +│ │ │ │ ├── historical/ # Historical data (Yahoo, Alpha Vantage) +│ │ │ │ └── unified.ts # Unified data interface +│ │ │ ├── storage/ # QuestDB time-series storage +│ │ │ ├── services/ +│ │ │ └── index.ts +│ │ └── package.json +│ │ +│ ├── processing-service/ # Combined processing & indicators +│ │ ├── src/ +│ │ │ ├── indicators/ # Technical indicators (uses @stock-bot/utils) +│ │ │ ├── processors/ # Data processing pipeline +│ │ │ ├── vectorized/ # Vectorized calculations +│ │ │ ├── services/ +│ │ │ └── index.ts +│ │ └── package.json +│ │ +│ ├── strategy-service/ # Combined strategy & backtesting +│ │ ├── src/ +│ │ │ ├── strategies/ # Strategy implementations +│ │ │ ├── backtesting/ # Multi-mode backtesting engine +│ │ │ │ ├── modes/ # Backtesting modes +│ │ │ │ │ ├── live-mode.ts # Live trading mode +│ │ │ │ │ ├── event-mode.ts # Event-driven backtest +│ │ │ │ │ └── vector-mode.ts # Vectorized backtest +│ │ │ │ ├── engines/ # Execution engines +│ │ │ │ │ ├── event-engine.ts # Event-based simulation +│ │ │ │ │ ├── vector-engine.ts # Vectorized calculations +│ │ │ │ │ └── hybrid-engine.ts # Combined validation +│ │ │ │ ├── simulator.ts # Market simulator +│ │ │ │ ├── runner.ts # Backtest orchestrator +│ │ │ │ └── metrics.ts # Performance analysis +│ │ │ ├── live/ # Live strategy execution +│ │ │ ├── framework/ # Strategy framework +│ │ │ │ ├── base-strategy.ts +│ │ │ │ ├── execution-mode.ts +│ │ │ │ └── mode-factory.ts +│ │ │ └── index.ts +│ │ └── package.json +│ │ +│ ├── execution-service/ # Combined order execution & simulation +│ │ ├── src/ +│ │ │ ├── brokers/ # Live broker adapters +│ │ │ ├── simulation/ # Simulated execution +│ │ │ ├── unified/ # Unified execution interface +│ │ │ │ ├── executor.ts # Abstract executor +│ │ │ │ ├── live-executor.ts +│ │ │ │ ├── sim-executor.ts +│ │ │ │ └── vector-executor.ts +│ │ │ └── index.ts +│ │ └── package.json +│ │ +│ ├── portfolio-service/ # Combined portfolio & risk management +│ │ ├── src/ +│ │ │ ├── portfolio/ # Portfolio tracking +│ │ │ ├── risk/ # Risk management (uses @stock-bot/utils) +│ │ │ ├── positions/ # Position management +│ │ │ ├── performance/ # Performance tracking +│ │ │ └── index.ts +│ │ └── package.json +│ │ +│ └── dashboard/ # Combined API & reporting +│ ├── src/ +│ │ ├── api/ # REST API +│ │ ├── web/ # Web interface (Angular) +│ │ ├── reports/ # Report generation +│ │ ├── websockets/ # Real-time updates +│ │ └── index.ts +│ └── package.json +│ +├── libs/ # ✅ Your existing shared libraries +│ ├── config/ # ✅ Environment configuration +│ ├── http-client/ # ✅ HTTP utilities +│ ├── logger/ # ✅ Loki-integrated logging +│ ├── mongodb-client/ # ✅ MongoDB operations +│ ├── postgres-client/ # ✅ PostgreSQL operations +│ ├── questdb-client/ # ✅ Time-series data +│ ├── types/ # ✅ Shared TypeScript types +│ ├── utils/ # ✅ Calculations & utilities +│ ├── event-bus/ # 🆕 Dragonfly event system +│ ├── strategy-engine/ # 🆕 Strategy framework +│ ├── vector-engine/ # 🆕 Vectorized calculations +│ └── data-frame/ # 🆕 DataFrame operations +``` + +## Multi-Mode Backtesting Architecture + +### 1. Execution Mode Framework + +```typescript +export abstract class ExecutionMode { + protected logger = createLogger(this.constructor.name); + protected config = new ServiceConfig(); + + abstract name: string; + abstract executeOrder(order: Order): Promise; + abstract getCurrentTime(): Date; + abstract getMarketData(symbol: string): Promise; + abstract publishEvent(event: string, data: any): Promise; +} + +export enum BacktestMode { + LIVE = 'live', + EVENT_DRIVEN = 'event-driven', + VECTORIZED = 'vectorized', + HYBRID = 'hybrid' +} +``` + +### 2. Live Trading Mode + +```typescript +export class LiveMode extends ExecutionMode { + name = 'live'; + private broker = new BrokerClient(this.config.getBrokerConfig()); + private eventBus = new EventBus(); + + async executeOrder(order: Order): Promise { + this.logger.info('Executing live order', { orderId: order.id }); + + // Execute via real broker + const result = await this.broker.placeOrder(order); + + // Publish to event bus + await this.eventBus.publish('order.executed', result); + + return result; + } + + getCurrentTime(): Date { + return new Date(); // Real time + } + + async getMarketData(symbol: string): Promise { + // Get live market data + return await this.marketDataService.getLiveData(symbol); + } + + async publishEvent(event: string, data: any): Promise { + await this.eventBus.publish(event, data); + } +} +``` + +### 3. Event-Driven Backtesting Mode + +```typescript +export class EventBacktestMode extends ExecutionMode { + name = 'event-driven'; + private simulator = new MarketSimulator(); + private eventBus = new InMemoryEventBus(); // In-memory for simulation + private simulationTime: Date; + private historicalData: Map; + + constructor(private config: BacktestConfig) { + super(); + this.simulationTime = config.startDate; + } + + async executeOrder(order: Order): Promise { + this.logger.debug('Simulating order execution', { + orderId: order.id, + simulationTime: this.simulationTime + }); + + // Realistic order simulation with slippage, fees + const result = await this.simulator.executeOrder(order, { + currentTime: this.simulationTime, + marketData: await this.getMarketData(order.symbol), + slippageModel: this.config.slippageModel, + commissionModel: this.config.commissionModel + }); + + // Publish to simulation event bus + await this.eventBus.publish('order.executed', result); + + return result; + } + + getCurrentTime(): Date { + return this.simulationTime; + } + + async getMarketData(symbol: string): Promise { + const data = this.historicalData.get(symbol) || []; + return data.find(d => d.timestamp <= this.simulationTime) || null; + } + + async publishEvent(event: string, data: any): Promise { + await this.eventBus.publish(event, data); + } + + // Progress simulation time + advanceTime(newTime: Date): void { + this.simulationTime = newTime; + } +} +``` + +### 4. Vectorized Backtesting Mode + +```typescript +export class VectorBacktestMode extends ExecutionMode { + name = 'vectorized'; + private dataFrame: DataFrame; + private currentIndex: number = 0; + + constructor(private config: VectorBacktestConfig) { + super(); + this.dataFrame = new DataFrame(config.historicalData); + } + + // Vectorized execution - processes entire dataset at once + async executeVectorizedBacktest(strategy: VectorizedStrategy): Promise { + const startTime = Date.now(); + + this.logger.info('Starting vectorized backtest', { + strategy: strategy.name, + dataPoints: this.dataFrame.length + }); + + // Generate all signals at once using your utils library + const signals = this.generateVectorizedSignals(strategy); + + // Calculate performance metrics vectorized + const performance = this.calculateVectorizedPerformance(signals); + + // Apply trading costs if specified + if (this.config.tradingCosts) { + this.applyTradingCosts(performance, signals); + } + + const executionTime = Date.now() - startTime; + + this.logger.info('Vectorized backtest completed', { + executionTime, + totalReturn: performance.totalReturn, + sharpeRatio: performance.sharpeRatio + }); + + return { + mode: 'vectorized', + strategy: strategy.name, + performance, + executionTime, + signals + }; + } + + private generateVectorizedSignals(strategy: VectorizedStrategy): DataFrame { + const prices = this.dataFrame.get('close'); + + // Use your existing technical indicators from @stock-bot/utils + const indicators = { + sma20: sma(prices, 20), + sma50: sma(prices, 50), + rsi: rsi(prices, 14), + macd: macd(prices) + }; + + // Generate position signals vectorized + const positions = strategy.generatePositions(this.dataFrame, indicators); + + return new DataFrame({ + ...this.dataFrame.toObject(), + ...indicators, + positions + }); + } + + private calculateVectorizedPerformance(signals: DataFrame): PerformanceMetrics { + const prices = signals.get('close'); + const positions = signals.get('positions'); + + // Calculate returns vectorized + const returns = prices.slice(1).map((price, i) => + (price - prices[i]) / prices[i] + ); + + // Strategy returns = position[t-1] * market_return[t] + const strategyReturns = returns.map((ret, i) => + (positions[i] || 0) * ret + ); + + // Use your existing performance calculation utilities + return { + totalReturn: calculateTotalReturn(strategyReturns), + sharpeRatio: calculateSharpeRatio(strategyReturns), + maxDrawdown: calculateMaxDrawdown(strategyReturns), + volatility: calculateVolatility(strategyReturns), + winRate: calculateWinRate(strategyReturns) + }; + } + + // Standard interface methods (not used in vectorized mode) + async executeOrder(order: Order): Promise { + throw new Error('Use executeVectorizedBacktest for vectorized mode'); + } + + getCurrentTime(): Date { + return this.dataFrame.getTimestamp(this.currentIndex); + } + + async getMarketData(symbol: string): Promise { + return this.dataFrame.getRow(this.currentIndex); + } + + async publishEvent(event: string, data: any): Promise { + // No-op for vectorized mode + } +} +``` + +### 5. Hybrid Validation Mode + +```typescript +export class HybridBacktestMode extends ExecutionMode { + name = 'hybrid'; + private eventMode: EventBacktestMode; + private vectorMode: VectorBacktestMode; + + constructor(config: BacktestConfig) { + super(); + this.eventMode = new EventBacktestMode(config); + this.vectorMode = new VectorBacktestMode(config); + } + + async validateStrategy( + strategy: BaseStrategy, + tolerance: number = 0.001 + ): Promise { + + this.logger.info('Starting hybrid validation', { + strategy: strategy.name, + tolerance + }); + + // Run vectorized backtest (fast) + const vectorResult = await this.vectorMode.executeVectorizedBacktest( + strategy as VectorizedStrategy + ); + + // Run event-driven backtest (realistic) + const eventResult = await this.runEventBacktest(strategy); + + // Compare results + const performanceDiff = Math.abs( + vectorResult.performance.totalReturn - + eventResult.performance.totalReturn + ); + + const isValid = performanceDiff < tolerance; + + this.logger.info('Hybrid validation completed', { + isValid, + performanceDifference: performanceDiff, + recommendation: isValid ? 'vectorized' : 'event-driven' + }); + + return { + isValid, + performanceDifference: performanceDiff, + vectorizedResult: vectorResult, + eventResult, + recommendation: isValid ? + 'Vectorized results are reliable for this strategy' : + 'Use event-driven backtesting for accurate results' + }; + } + + // Standard interface methods delegate to event mode + async executeOrder(order: Order): Promise { + return await this.eventMode.executeOrder(order); + } + + getCurrentTime(): Date { + return this.eventMode.getCurrentTime(); + } + + async getMarketData(symbol: string): Promise { + return await this.eventMode.getMarketData(symbol); + } + + async publishEvent(event: string, data: any): Promise { + await this.eventMode.publishEvent(event, data); + } +} +``` + +## Unified Strategy Implementation + +### Base Strategy Framework + +```typescript +export abstract class BaseStrategy { + protected mode: ExecutionMode; + protected logger = createLogger(this.constructor.name); + + abstract name: string; + abstract parameters: Record; + + constructor(mode: ExecutionMode) { + this.mode = mode; + } + + // Works identically across all modes + abstract onPriceUpdate(data: PriceData): Promise; + abstract onIndicatorUpdate(data: IndicatorData): Promise; + + protected async emitSignal(signal: TradeSignal): Promise { + this.logger.debug('Emitting trade signal', { signal }); + + // Mode handles whether this is live, simulated, or vectorized + const order = this.createOrder(signal); + const result = await this.mode.executeOrder(order); + + await this.mode.publishEvent('trade.executed', { + signal, + order, + result, + timestamp: this.mode.getCurrentTime() + }); + } + + private createOrder(signal: TradeSignal): Order { + return { + id: generateId(), + symbol: signal.symbol, + side: signal.action, + quantity: signal.quantity, + type: 'market', + timestamp: this.mode.getCurrentTime() + }; + } +} + +// Vectorized strategy interface +export interface VectorizedStrategy { + name: string; + parameters: Record; + generatePositions(data: DataFrame, indicators: any): number[]; +} +``` + +### Example Strategy Implementation + +```typescript +export class SMAStrategy extends BaseStrategy implements VectorizedStrategy { + name = 'SMA-Crossover'; + parameters = { fastPeriod: 10, slowPeriod: 20 }; + + private fastSMA: number[] = []; + private slowSMA: number[] = []; + + async onPriceUpdate(data: PriceData): Promise { + // Same logic for live, event-driven, and hybrid modes + this.fastSMA.push(data.close); + this.slowSMA.push(data.close); + + if (this.fastSMA.length > this.parameters.fastPeriod) { + this.fastSMA.shift(); + } + if (this.slowSMA.length > this.parameters.slowPeriod) { + this.slowSMA.shift(); + } + + if (this.fastSMA.length === this.parameters.fastPeriod && + this.slowSMA.length === this.parameters.slowPeriod) { + + const fastAvg = sma(this.fastSMA, this.parameters.fastPeriod)[0]; + const slowAvg = sma(this.slowSMA, this.parameters.slowPeriod)[0]; + + if (fastAvg > slowAvg) { + await this.emitSignal({ + symbol: data.symbol, + action: 'BUY', + quantity: 100, + confidence: 0.8 + }); + } else if (fastAvg < slowAvg) { + await this.emitSignal({ + symbol: data.symbol, + action: 'SELL', + quantity: 100, + confidence: 0.8 + }); + } + } + } + + async onIndicatorUpdate(data: IndicatorData): Promise { + // Handle pre-calculated indicators + } + + // Vectorized implementation for fast backtesting + generatePositions(data: DataFrame, indicators: any): number[] { + const { sma20: fastSMA, sma50: slowSMA } = indicators; + + return fastSMA.map((fast, i) => { + const slow = slowSMA[i]; + if (isNaN(fast) || isNaN(slow)) return 0; + + // Long when fast > slow, short when fast < slow + return fast > slow ? 1 : (fast < slow ? -1 : 0); + }); + } +} +``` + +## Mode Factory and Service Integration + +### Mode Factory + +```typescript +export class ModeFactory { + static create(mode: BacktestMode, config: any): ExecutionMode { + switch (mode) { + case BacktestMode.LIVE: + return new LiveMode(); + case BacktestMode.EVENT_DRIVEN: + return new EventBacktestMode(config); + case BacktestMode.VECTORIZED: + return new VectorBacktestMode(config); + case BacktestMode.HYBRID: + return new HybridBacktestMode(config); + default: + throw new Error(`Unknown mode: ${mode}`); + } + } +} +``` + +### Strategy Service Integration + +```typescript +export class StrategyService { + private logger = createLogger('strategy-service'); + + async runStrategy( + strategyName: string, + mode: BacktestMode, + config: any + ): Promise { + + const executionMode = ModeFactory.create(mode, config); + const strategy = await this.loadStrategy(strategyName, executionMode); + + this.logger.info('Starting strategy execution', { + strategy: strategyName, + mode, + config + }); + + switch (mode) { + case BacktestMode.LIVE: + return await this.runLiveStrategy(strategy); + + case BacktestMode.EVENT_DRIVEN: + return await this.runEventBacktest(strategy, config); + + case BacktestMode.VECTORIZED: + return await (executionMode as VectorBacktestMode) + .executeVectorizedBacktest(strategy as VectorizedStrategy); + + case BacktestMode.HYBRID: + return await (executionMode as HybridBacktestMode) + .validateStrategy(strategy, config.tolerance); + + default: + throw new Error(`Unsupported mode: ${mode}`); + } + } + + async optimizeStrategy( + strategyName: string, + parameterGrid: Record, + config: BacktestConfig + ): Promise { + + const results: OptimizationResult[] = []; + const combinations = this.generateParameterCombinations(parameterGrid); + + this.logger.info('Starting parameter optimization', { + strategy: strategyName, + combinations: combinations.length + }); + + // Use vectorized mode for fast parameter optimization + const vectorMode = new VectorBacktestMode(config); + + // Can be parallelized + await Promise.all( + combinations.map(async (params) => { + const strategy = await this.loadStrategy(strategyName, vectorMode, params); + const result = await vectorMode.executeVectorizedBacktest( + strategy as VectorizedStrategy + ); + + results.push({ + parameters: params, + performance: result.performance, + executionTime: result.executionTime + }); + }) + ); + + // Sort by Sharpe ratio + return results.sort((a, b) => + b.performance.sharpeRatio - a.performance.sharpeRatio + ); + } +} +``` + +## Service Configuration + +### Environment-Based Mode Selection + +```typescript +export class ServiceConfig { + getTradingConfig(): TradingConfig { + return { + mode: (process.env.TRADING_MODE as BacktestMode) || BacktestMode.LIVE, + brokerConfig: { + apiKey: process.env.BROKER_API_KEY, + sandbox: process.env.BROKER_SANDBOX === 'true' + }, + backtestConfig: { + startDate: new Date(process.env.BACKTEST_START_DATE || '2023-01-01'), + endDate: new Date(process.env.BACKTEST_END_DATE || '2024-01-01'), + initialCapital: parseFloat(process.env.INITIAL_CAPITAL || '100000'), + slippageModel: process.env.SLIPPAGE_MODEL || 'linear', + commissionModel: process.env.COMMISSION_MODEL || 'fixed' + } + }; + } +} +``` + +### CLI Interface + +```typescript +// CLI for running different modes +import { Command } from 'commander'; + +const program = new Command(); + +program + .name('stock-bot') + .description('Stock Trading Bot with Multi-Mode Backtesting'); + +program + .command('live') + .description('Run live trading') + .option('-s, --strategy ', 'Strategy to run') + .action(async (options) => { + const strategyService = new StrategyService(); + await strategyService.runStrategy( + options.strategy, + BacktestMode.LIVE, + {} + ); + }); + +program + .command('backtest') + .description('Run backtesting') + .option('-s, --strategy ', 'Strategy to test') + .option('-m, --mode ', 'Backtest mode (event|vector|hybrid)', 'event') + .option('-f, --from ', 'Start date') + .option('-t, --to ', 'End date') + .action(async (options) => { + const strategyService = new StrategyService(); + await strategyService.runStrategy( + options.strategy, + options.mode as BacktestMode, + { + startDate: new Date(options.from), + endDate: new Date(options.to) + } + ); + }); + +program + .command('optimize') + .description('Optimize strategy parameters') + .option('-s, --strategy ', 'Strategy to optimize') + .option('-p, --params ', 'Parameter grid JSON') + .action(async (options) => { + const strategyService = new StrategyService(); + const paramGrid = JSON.parse(options.params); + await strategyService.optimizeStrategy( + options.strategy, + paramGrid, + {} + ); + }); + +program.parse(); +``` + +## Performance Comparison + +### Execution Speed by Mode + +| Mode | Data Points/Second | Memory Usage | Use Case | +|------|-------------------|--------------|----------| +| **Live** | Real-time | Low | Production trading | +| **Event-Driven** | ~1,000 | Medium | Realistic validation | +| **Vectorized** | ~100,000+ | High | Parameter optimization | +| **Hybrid** | Combined | Medium | Strategy validation | + +### When to Use Each Mode + +- **Live Mode**: Production trading with real money +- **Event-Driven**: Final strategy validation, complex order logic +- **Vectorized**: Initial development, parameter optimization, quick testing +- **Hybrid**: Validating vectorized results against realistic simulation + +## Integration with Your Existing Libraries + +This architecture leverages all your existing infrastructure: + +- **@stock-bot/config**: Environment management +- **@stock-bot/logger**: Comprehensive logging with Loki +- **@stock-bot/utils**: All technical indicators and calculations +- **@stock-bot/questdb-client**: Time-series data storage +- **@stock-bot/postgres-client**: Transactional data +- **@stock-bot/mongodb-client**: Configuration storage + +## Key Benefits + +1. **Unified Codebase**: Same strategy logic across all modes +2. **Performance Flexibility**: Choose speed vs accuracy based on needs +3. **Validation Pipeline**: Hybrid mode ensures vectorized results are accurate +4. **Production Ready**: Live mode for actual trading +5. **Development Friendly**: Fast iteration with vectorized backtesting + +This simplified architecture reduces complexity while providing comprehensive backtesting capabilities that scale from rapid prototyping to production trading. diff --git a/apps/interface-services/trading-dashboard/.editorconfig b/apps/dashboard/.editorconfig similarity index 100% rename from apps/interface-services/trading-dashboard/.editorconfig rename to apps/dashboard/.editorconfig diff --git a/apps/interface-services/trading-dashboard/.gitignore b/apps/dashboard/.gitignore similarity index 100% rename from apps/interface-services/trading-dashboard/.gitignore rename to apps/dashboard/.gitignore diff --git a/apps/interface-services/trading-dashboard/.postcssrc.json b/apps/dashboard/.postcssrc.json similarity index 100% rename from apps/interface-services/trading-dashboard/.postcssrc.json rename to apps/dashboard/.postcssrc.json diff --git a/apps/interface-services/trading-dashboard/.vscode/extensions.json b/apps/dashboard/.vscode/extensions.json similarity index 100% rename from apps/interface-services/trading-dashboard/.vscode/extensions.json rename to apps/dashboard/.vscode/extensions.json diff --git a/apps/interface-services/trading-dashboard/.vscode/launch.json b/apps/dashboard/.vscode/launch.json similarity index 100% rename from apps/interface-services/trading-dashboard/.vscode/launch.json rename to apps/dashboard/.vscode/launch.json diff --git a/apps/interface-services/trading-dashboard/.vscode/tasks.json b/apps/dashboard/.vscode/tasks.json similarity index 100% rename from apps/interface-services/trading-dashboard/.vscode/tasks.json rename to apps/dashboard/.vscode/tasks.json diff --git a/apps/interface-services/trading-dashboard/README.md b/apps/dashboard/README.md similarity index 100% rename from apps/interface-services/trading-dashboard/README.md rename to apps/dashboard/README.md diff --git a/apps/interface-services/trading-dashboard/angular.json b/apps/dashboard/angular.json similarity index 100% rename from apps/interface-services/trading-dashboard/angular.json rename to apps/dashboard/angular.json diff --git a/apps/interface-services/trading-dashboard/package.json b/apps/dashboard/package.json similarity index 100% rename from apps/interface-services/trading-dashboard/package.json rename to apps/dashboard/package.json diff --git a/apps/interface-services/trading-dashboard/public/favicon.ico b/apps/dashboard/public/favicon.ico similarity index 100% rename from apps/interface-services/trading-dashboard/public/favicon.ico rename to apps/dashboard/public/favicon.ico diff --git a/apps/interface-services/trading-dashboard/src/App.tsx b/apps/dashboard/src/App.tsx similarity index 100% rename from apps/interface-services/trading-dashboard/src/App.tsx rename to apps/dashboard/src/App.tsx diff --git a/apps/interface-services/trading-dashboard/src/app/app.config.ts b/apps/dashboard/src/app/app.config.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.config.ts rename to apps/dashboard/src/app/app.config.ts diff --git a/apps/interface-services/trading-dashboard/src/app/app.css b/apps/dashboard/src/app/app.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.css rename to apps/dashboard/src/app/app.css diff --git a/apps/interface-services/trading-dashboard/src/app/app.html b/apps/dashboard/src/app/app.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.html rename to apps/dashboard/src/app/app.html diff --git a/apps/interface-services/trading-dashboard/src/app/app.routes.ts b/apps/dashboard/src/app/app.routes.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.routes.ts rename to apps/dashboard/src/app/app.routes.ts diff --git a/apps/interface-services/trading-dashboard/src/app/app.scss b/apps/dashboard/src/app/app.scss similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.scss rename to apps/dashboard/src/app/app.scss diff --git a/apps/interface-services/trading-dashboard/src/app/app.spec.ts b/apps/dashboard/src/app/app.spec.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.spec.ts rename to apps/dashboard/src/app/app.spec.ts diff --git a/apps/interface-services/trading-dashboard/src/app/app.ts b/apps/dashboard/src/app/app.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/app.ts rename to apps/dashboard/src/app/app.ts diff --git a/apps/interface-services/trading-dashboard/src/app/components/notifications/notifications.css b/apps/dashboard/src/app/components/notifications/notifications.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/components/notifications/notifications.css rename to apps/dashboard/src/app/components/notifications/notifications.css diff --git a/apps/interface-services/trading-dashboard/src/app/components/notifications/notifications.html b/apps/dashboard/src/app/components/notifications/notifications.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/components/notifications/notifications.html rename to apps/dashboard/src/app/components/notifications/notifications.html diff --git a/apps/interface-services/trading-dashboard/src/app/components/notifications/notifications.ts b/apps/dashboard/src/app/components/notifications/notifications.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/components/notifications/notifications.ts rename to apps/dashboard/src/app/components/notifications/notifications.ts diff --git a/apps/interface-services/trading-dashboard/src/app/components/sidebar/sidebar.component.css b/apps/dashboard/src/app/components/sidebar/sidebar.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/components/sidebar/sidebar.component.css rename to apps/dashboard/src/app/components/sidebar/sidebar.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/components/sidebar/sidebar.component.html b/apps/dashboard/src/app/components/sidebar/sidebar.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/components/sidebar/sidebar.component.html rename to apps/dashboard/src/app/components/sidebar/sidebar.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/components/sidebar/sidebar.component.ts b/apps/dashboard/src/app/components/sidebar/sidebar.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/components/sidebar/sidebar.component.ts rename to apps/dashboard/src/app/components/sidebar/sidebar.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/dashboard/dashboard.component.css b/apps/dashboard/src/app/pages/dashboard/dashboard.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/dashboard/dashboard.component.css rename to apps/dashboard/src/app/pages/dashboard/dashboard.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/dashboard/dashboard.component.html b/apps/dashboard/src/app/pages/dashboard/dashboard.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/dashboard/dashboard.component.html rename to apps/dashboard/src/app/pages/dashboard/dashboard.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/dashboard/dashboard.component.ts b/apps/dashboard/src/app/pages/dashboard/dashboard.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/dashboard/dashboard.component.ts rename to apps/dashboard/src/app/pages/dashboard/dashboard.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/market-data/market-data.component.css b/apps/dashboard/src/app/pages/market-data/market-data.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/market-data/market-data.component.css rename to apps/dashboard/src/app/pages/market-data/market-data.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/market-data/market-data.component.html b/apps/dashboard/src/app/pages/market-data/market-data.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/market-data/market-data.component.html rename to apps/dashboard/src/app/pages/market-data/market-data.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/market-data/market-data.component.ts b/apps/dashboard/src/app/pages/market-data/market-data.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/market-data/market-data.component.ts rename to apps/dashboard/src/app/pages/market-data/market-data.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/portfolio/portfolio.component.css b/apps/dashboard/src/app/pages/portfolio/portfolio.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/portfolio/portfolio.component.css rename to apps/dashboard/src/app/pages/portfolio/portfolio.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/portfolio/portfolio.component.html b/apps/dashboard/src/app/pages/portfolio/portfolio.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/portfolio/portfolio.component.html rename to apps/dashboard/src/app/pages/portfolio/portfolio.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/portfolio/portfolio.component.ts b/apps/dashboard/src/app/pages/portfolio/portfolio.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/portfolio/portfolio.component.ts rename to apps/dashboard/src/app/pages/portfolio/portfolio.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/risk-management/risk-management.component.css b/apps/dashboard/src/app/pages/risk-management/risk-management.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/risk-management/risk-management.component.css rename to apps/dashboard/src/app/pages/risk-management/risk-management.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/risk-management/risk-management.component.html b/apps/dashboard/src/app/pages/risk-management/risk-management.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/risk-management/risk-management.component.html rename to apps/dashboard/src/app/pages/risk-management/risk-management.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/risk-management/risk-management.component.ts b/apps/dashboard/src/app/pages/risk-management/risk-management.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/risk-management/risk-management.component.ts rename to apps/dashboard/src/app/pages/risk-management/risk-management.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/settings/settings.component.css b/apps/dashboard/src/app/pages/settings/settings.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/settings/settings.component.css rename to apps/dashboard/src/app/pages/settings/settings.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/settings/settings.component.html b/apps/dashboard/src/app/pages/settings/settings.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/settings/settings.component.html rename to apps/dashboard/src/app/pages/settings/settings.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/settings/settings.component.ts b/apps/dashboard/src/app/pages/settings/settings.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/settings/settings.component.ts rename to apps/dashboard/src/app/pages/settings/settings.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/components/drawdown-chart.component.ts b/apps/dashboard/src/app/pages/strategies/components/drawdown-chart.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/components/drawdown-chart.component.ts rename to apps/dashboard/src/app/pages/strategies/components/drawdown-chart.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/components/equity-chart.component.ts b/apps/dashboard/src/app/pages/strategies/components/equity-chart.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/components/equity-chart.component.ts rename to apps/dashboard/src/app/pages/strategies/components/equity-chart.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/components/performance-metrics.component.ts b/apps/dashboard/src/app/pages/strategies/components/performance-metrics.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/components/performance-metrics.component.ts rename to apps/dashboard/src/app/pages/strategies/components/performance-metrics.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/components/trades-table.component.ts b/apps/dashboard/src/app/pages/strategies/components/trades-table.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/components/trades-table.component.ts rename to apps/dashboard/src/app/pages/strategies/components/trades-table.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/dialogs/backtest-dialog.component.ts b/apps/dashboard/src/app/pages/strategies/dialogs/backtest-dialog.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/dialogs/backtest-dialog.component.ts rename to apps/dashboard/src/app/pages/strategies/dialogs/backtest-dialog.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.html b/apps/dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.html rename to apps/dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.ts b/apps/dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.ts rename to apps/dashboard/src/app/pages/strategies/dialogs/strategy-dialog.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/strategies.component.css b/apps/dashboard/src/app/pages/strategies/strategies.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/strategies.component.css rename to apps/dashboard/src/app/pages/strategies/strategies.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/strategies.component.html b/apps/dashboard/src/app/pages/strategies/strategies.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/strategies.component.html rename to apps/dashboard/src/app/pages/strategies/strategies.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/strategies.component.ts b/apps/dashboard/src/app/pages/strategies/strategies.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/strategies.component.ts rename to apps/dashboard/src/app/pages/strategies/strategies.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.css b/apps/dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.css rename to apps/dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.css diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.html b/apps/dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.html rename to apps/dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.html diff --git a/apps/interface-services/trading-dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.ts b/apps/dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.ts rename to apps/dashboard/src/app/pages/strategies/strategy-details/strategy-details.component.ts diff --git a/apps/interface-services/trading-dashboard/src/app/services/api.service.ts b/apps/dashboard/src/app/services/api.service.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/services/api.service.ts rename to apps/dashboard/src/app/services/api.service.ts diff --git a/apps/interface-services/trading-dashboard/src/app/services/notification.service.ts b/apps/dashboard/src/app/services/notification.service.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/services/notification.service.ts rename to apps/dashboard/src/app/services/notification.service.ts diff --git a/apps/interface-services/trading-dashboard/src/app/services/strategy.service.ts b/apps/dashboard/src/app/services/strategy.service.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/services/strategy.service.ts rename to apps/dashboard/src/app/services/strategy.service.ts diff --git a/apps/interface-services/trading-dashboard/src/app/services/websocket.service.ts b/apps/dashboard/src/app/services/websocket.service.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/app/services/websocket.service.ts rename to apps/dashboard/src/app/services/websocket.service.ts diff --git a/apps/interface-services/trading-dashboard/src/components/DashboardLayout.tsx b/apps/dashboard/src/components/DashboardLayout.tsx similarity index 100% rename from apps/interface-services/trading-dashboard/src/components/DashboardLayout.tsx rename to apps/dashboard/src/components/DashboardLayout.tsx diff --git a/apps/interface-services/trading-dashboard/src/components/TradingDashboard.tsx b/apps/dashboard/src/components/TradingDashboard.tsx similarity index 100% rename from apps/interface-services/trading-dashboard/src/components/TradingDashboard.tsx rename to apps/dashboard/src/components/TradingDashboard.tsx diff --git a/apps/interface-services/trading-dashboard/src/index.css b/apps/dashboard/src/index.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/index.css rename to apps/dashboard/src/index.css diff --git a/apps/interface-services/trading-dashboard/src/index.html b/apps/dashboard/src/index.html similarity index 100% rename from apps/interface-services/trading-dashboard/src/index.html rename to apps/dashboard/src/index.html diff --git a/apps/interface-services/trading-dashboard/src/main.ts b/apps/dashboard/src/main.ts similarity index 100% rename from apps/interface-services/trading-dashboard/src/main.ts rename to apps/dashboard/src/main.ts diff --git a/apps/interface-services/trading-dashboard/src/styles.css b/apps/dashboard/src/styles.css similarity index 100% rename from apps/interface-services/trading-dashboard/src/styles.css rename to apps/dashboard/src/styles.css diff --git a/apps/interface-services/trading-dashboard/src/styles.scss b/apps/dashboard/src/styles.scss similarity index 100% rename from apps/interface-services/trading-dashboard/src/styles.scss rename to apps/dashboard/src/styles.scss diff --git a/apps/interface-services/trading-dashboard/tsconfig.app.json b/apps/dashboard/tsconfig.app.json similarity index 100% rename from apps/interface-services/trading-dashboard/tsconfig.app.json rename to apps/dashboard/tsconfig.app.json diff --git a/apps/interface-services/trading-dashboard/tsconfig.json b/apps/dashboard/tsconfig.json similarity index 95% rename from apps/interface-services/trading-dashboard/tsconfig.json rename to apps/dashboard/tsconfig.json index e8d2a3f..fc4b18f 100644 --- a/apps/interface-services/trading-dashboard/tsconfig.json +++ b/apps/dashboard/tsconfig.json @@ -1,7 +1,7 @@ /* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */ /* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */ { - "extends": "../../../tsconfig.json", + "extends": "../../tsconfig.json", "compileOnSave": false, "compilerOptions": { "noImplicitOverride": true, diff --git a/apps/interface-services/trading-dashboard/tsconfig.spec.json b/apps/dashboard/tsconfig.spec.json similarity index 100% rename from apps/interface-services/trading-dashboard/tsconfig.spec.json rename to apps/dashboard/tsconfig.spec.json diff --git a/apps/interface-services/trading-dashboard/postcss.config.js b/apps/interface-services/trading-dashboard/postcss.config.js deleted file mode 100644 index e69de29..0000000 diff --git a/apps/interface-services/trading-dashboard/tailwind.config.js b/apps/interface-services/trading-dashboard/tailwind.config.js deleted file mode 100644 index 00bc3e9..0000000 --- a/apps/interface-services/trading-dashboard/tailwind.config.js +++ /dev/null @@ -1,52 +0,0 @@ -/** @type {import('tailwindcss').Config} */ -module.exports = { - content: [ - "./src/**/*.{html,ts}", - ], - theme: { - extend: { - colors: { - primary: { - 50: '#eff6ff', - 100: '#dbeafe', - 200: '#bfdbfe', - 300: '#93c5fd', - 400: '#60a5fa', - 500: '#3b82f6', - 600: '#2563eb', - 700: '#1d4ed8', - 800: '#1e40af', - 900: '#1e3a8a', - 950: '#172554', - }, - success: { - 50: '#f0fdf4', - 100: '#dcfce7', - 200: '#bbf7d0', - 300: '#86efac', - 400: '#4ade80', - 500: '#22c55e', - 600: '#16a34a', - 700: '#15803d', - 800: '#166534', - 900: '#14532d', - 950: '#052e16', - }, - danger: { - 50: '#fef2f2', - 100: '#fee2e2', - 200: '#fecaca', - 300: '#fca5a5', - 400: '#f87171', - 500: '#ef4444', - 600: '#dc2626', - 700: '#b91c1c', - 800: '#991b1b', - 900: '#7f1d1d', - 950: '#450a0a', - }, - }, - }, - }, - plugins: [], -} diff --git a/docs/current-system-flow.md b/docs/current-system-flow.md deleted file mode 100644 index 16cb290..0000000 --- a/docs/current-system-flow.md +++ /dev/null @@ -1,232 +0,0 @@ -# 🔄 Current System Communication Flow - -## Active Services (Currently Running) - -```mermaid -graph TB - %% External Data Sources - subgraph "External APIs" - EXT[Demo Data Generation
Alpha Vantage Ready
Yahoo Finance Ready] - end - - %% Currently Active Services - subgraph "Active Services" - MDG[Market Data Gateway
Port 3001
✅ Running] - TD[Trading Dashboard
Port 5173
✅ Running] - end - - %% Storage Layer - subgraph "Storage" - DRAGONFLY[(Dragonfly
📡 Events & Cache
🔧 Configured)] - end - - %% Data Flow - EXT -->|HTTP Fetch| MDG - MDG -->|REST API| TD - MDG -->|WebSocket Stream| TD - MDG -->|Events| DRAGONFLY - - %% Styling - classDef active fill:#90EE90 - classDef storage fill:#FFE4B5 - classDef external fill:#FFB6C1 - - class MDG,TD active - class DRAGONFLY storage - class EXT external -``` - -## Next Phase Services (Ready to Implement) - -```mermaid -graph TB - %% Current Services - subgraph "Current Layer" - MDG[Market Data Gateway
✅ Operational] - TD[Trading Dashboard
✅ Operational] - end - - %% Next Phase Services - subgraph "Next Phase - Priority 1" - SO[Strategy Orchestrator
🚧 Package Created
📋 Ready to Implement] - RG[Risk Guardian
🚧 Package Created
📋 Ready to Implement] - end - - %% Communication Infrastructure - subgraph "Event Infrastructure" - DRAGONFLY[(Dragonfly Streams
✅ Configured)] - WS[WebSocket Server
✅ Active in MDG] - end - - %% Data Flows - MDG -->|Market Data Events| DRAGONFLY - MDG -->|Real-time Stream| WS - WS -->|Live Updates| TD - DRAGONFLY -->|Market Events| SO - DRAGONFLY -->|All Events| RG - - SO -->|Strategy Events| DRAGONFLY - SO -->|Risk Check| RG - RG -->|Risk Alerts| DRAGONFLY - - %% Styling - classDef current fill:#90EE90 - classDef next fill:#FFE4B5 - classDef infrastructure fill:#E6E6FA - - class MDG,TD current - class SO,RG next - class DRAGONFLY,WS infrastructure -``` - -## Detailed Communication Patterns - -### 1. **Current System (Working)** - -``` -┌─────────────────┐ HTTP REST ┌─────────────────┐ -│ Trading │ ←──────────────→ │ Market Data │ -│ Dashboard │ │ Gateway │ -│ (React/Tremor) │ ←──────────────→ │ (Hono/Bun) │ -└─────────────────┘ WebSocket └─────────────────┘ - │ - ▼ - ┌─────────────────┐ - │ Dragonfly Events │ - │ (Configured) │ - └─────────────────┘ -``` - -### 2. **Next Phase Implementation** - -``` -┌─────────────────┐ -│ Strategy │ -│ Orchestrator │ ──┐ -└─────────────────┘ │ - ▼ -┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ -│ Market Data │→│ Dragonfly Event │←│ Risk Guardian │ -│ Gateway │ │ Stream │ │ │ -└─────────────────┘ └─────────────────┘ └─────────────────┘ - │ │ │ - ▼ ▼ ▼ -┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ -│ Trading │ │ WebSocket │ │ Alert Manager │ -│ Dashboard │ │ Real-time │ │ (Future) │ -└─────────────────┘ └─────────────────┘ └─────────────────┘ -``` - -## Event Flow Diagram - -```mermaid -sequenceDiagram - participant TD as Trading Dashboard - participant MDG as Market Data Gateway - participant DRAGONFLY as Dragonfly Events - participant SO as Strategy Orchestrator (Next) - participant RG as Risk Guardian (Next) - - Note over TD,DRAGONFLY: Current System - Working - - MDG->>MDG: Generate demo market data - MDG->>TD: WebSocket real-time updates - MDG->>DRAGONFLY: Publish MARKET_DATA events - TD->>MDG: HTTP API requests - MDG->>TD: JSON responses - - Note over SO,RG: Next Phase - To Implement - - DRAGONFLY->>SO: Subscribe to MARKET_DATA events - SO->>SO: Analyze market conditions - SO->>DRAGONFLY: Publish SIGNAL_GENERATED events - - DRAGONFLY->>RG: Subscribe to ALL events - RG->>RG: Monitor risk thresholds - RG->>DRAGONFLY: Publish RISK_ALERT events - - DRAGONFLY->>TD: All events via WebSocket - TD->>TD: Update dashboard with alerts -``` - -## Service Dependencies - -### **Current Dependencies (Satisfied)** -``` -Market Data Gateway -├── ✅ Hono (Web framework) -├── ✅ ioredis (Redis client) -├── ✅ @stock-bot/config (Workspace package) -└── ✅ ws (WebSocket library) - -Trading Dashboard -├── ✅ React + TypeScript -├── ✅ Tremor UI (Financial components) -├── ✅ Vite (Build tool) -└── ✅ WebSocket client -``` - -### **Next Phase Dependencies (Ready)** -``` -Strategy Orchestrator -├── ✅ Package.json created -├── ✅ Dependencies specified -├── 📋 Implementation needed -└── 🔧 node-cron (Strategy scheduling) - -Risk Guardian -├── ✅ Package.json created -├── ✅ Dependencies specified -├── 📋 Implementation needed -└── 🛡️ Risk monitoring logic -``` - -## Port & Endpoint Map - -| Service | Port | Endpoints | Status | -|---------|------|-----------|--------| -| **Market Data Gateway** | 3001 | `/health`, `/api/market-data/:symbol`, `/api/ohlcv/:symbol`, WebSocket | ✅ Active | -| **Trading Dashboard** | 5173 | Vite dev server | ✅ Active | -| **Strategy Orchestrator** | 4001 | `/health`, `/api/strategies`, `/api/signals` | 📋 Planned | -| **Risk Guardian** | 3002 | `/health`, `/api/risk-checks`, `/api/limits` | 📋 Planned | - -## Data Types & Events - -### **Market Data Event** -```typescript -interface MarketDataEvent { - type: 'MARKET_DATA'; - data: { - symbol: string; - price: number; - bid: number; - ask: number; - volume: number; - timestamp: Date; - }; - timestamp: Date; -} -``` - -### **Strategy Event (Next Phase)** -```typescript -interface StrategyEvent { - type: 'SIGNAL_GENERATED' | 'STRATEGY_START' | 'STRATEGY_STOP'; - strategyId: string; - signal?: TradingSignal; - timestamp: Date; -} -``` - -### **Risk Event (Next Phase)** -```typescript -interface RiskEvent { - type: 'RISK_ALERT' | 'RISK_CHECK'; - severity: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL'; - message: string; - data: any; - timestamp: Date; -} -``` - -This architecture shows how your current working system will expand into a comprehensive trading platform with clear communication patterns and event flows. diff --git a/docs/enhanced-architecture.md b/docs/enhanced-architecture.md deleted file mode 100644 index d23df93..0000000 --- a/docs/enhanced-architecture.md +++ /dev/null @@ -1,87 +0,0 @@ -# Enhanced Separation of Concerns - -This guide describes how the project architecture has been improved to better separate concerns through a modular libs structure. - -## New Library Structure - -We've reorganized the project's shared libraries for improved maintainability: - -### 1. Shared Types - -Types are now organized by domain: - -``` -libs/types/ -├── src/ -│ ├── market/ # Market data types (OHLCV, OrderBook) -│ ├── trading/ # Trading types (Orders, Positions) -│ ├── strategy/ # Strategy and signal types -│ ├── events/ # Event definitions -│ ├── api/ # API response/request types -│ └── config/ # Configuration types -``` - -### 2. Event Bus (`@stock-bot/event-bus`) - -A consistent event publishing system: - -``` -libs/event-bus/ -├── src/ -│ ├── EventBus.ts # Core event bus implementation -│ └── index.ts # Public API -``` - -### 3. Utils (`@stock-bot/utils`) - -Shared utility functions: - -``` -libs/utils/ -├── src/ -│ ├── dateUtils.ts # Date manipulation helpers -│ ├── financialUtils.ts # Financial calculations -│ ├── logger.ts # Standardized logging -│ └── index.ts # Public API -``` - -### 4. API Client (`@stock-bot/api-client`) - -Type-safe service clients: - -``` -libs/api-client/ -├── src/ -│ ├── BaseApiClient.ts # Common HTTP client logic -│ ├── BacktestClient.ts # Backtest service client -│ ├── StrategyClient.ts # Strategy service client -│ └── index.ts # Public API -``` - -## Service Architecture Improvements - -The intelligence services have been split into focused services: - -1. `strategy-orchestrator`: Core strategy management -2. `backtest-engine`: Dedicated historical testing -3. `signal-engine`: Signal generation - -This provides: -- Better scaling for resource-intensive operations -- Focused codebases for each concern -- Independent deployment cycles -- Clear service boundaries - -## Usage Guidelines - -- Use the shared types library for all data models -- Use the event bus for inter-service communication -- Use the API clients for direct service calls -- Use utility functions instead of duplicating common code - -## Next Steps - -1. Continue migrating services to use the new libraries -2. Add comprehensive tests for each library -3. Create a complete API gateway for external access -4. Document service boundaries with OpenAPI schemas diff --git a/docs/interface-services/.gitkeep b/docs/interface-services/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/docs/interface-services/README.md b/docs/interface-services/README.md deleted file mode 100644 index 4e262eb..0000000 --- a/docs/interface-services/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# Interface Services - -Interface services provide user-facing applications and APIs for interacting with the trading platform. - -## Services - -### Trading Dashboard -- **Purpose**: Web-based user interface for trading operations -- **Key Functions**: - - Real-time portfolio monitoring and visualization - - Trading strategy configuration and management - - Performance analytics and reporting dashboards - - Risk monitoring and alerting interface - - Market data visualization and charting - - Order management and execution tracking - -## Architecture - -Interface services create the user experience layer of the trading platform, providing intuitive and responsive interfaces for traders, analysts, and administrators. They translate complex backend operations into accessible visual interfaces and interactive workflows. - -## Technology Stack - -- **Frontend**: React-based single-page applications -- **Visualization**: Real-time charts and data visualization -- **State Management**: Modern React patterns and state libraries -- **API Integration**: RESTful and WebSocket connections to backend services diff --git a/docs/interface-services/trading-dashboard/.gitkeep b/docs/interface-services/trading-dashboard/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/docs/interface-services/trading-dashboard/README.md b/docs/interface-services/trading-dashboard/README.md deleted file mode 100644 index d31691a..0000000 --- a/docs/interface-services/trading-dashboard/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# Trading Dashboard - -## Overview -The Trading Dashboard service provides a comprehensive web-based user interface for monitoring, analyzing, and controlling trading activities within the stock-bot platform. It serves as the primary visual interface for traders, analysts, and administrators to interact with the platform's capabilities and visualize market data and trading performance. - -## Key Features - -### Portfolio Monitoring -- **Position Dashboard**: Real-time view of all open positions -- **P&L Tracking**: Current and historical profit/loss visualization -- **Risk Metrics**: Visual representation of key risk indicators -- **Performance Analytics**: Strategy and portfolio performance charts - -### Market Visualization -- **Real-time Charts**: Interactive price and volume charts -- **Technical Indicators**: Overlay of key technical indicators -- **Market Depth**: Order book visualization where available -- **Multi-instrument View**: Customizable multi-asset dashboards - -### Trading Controls -- **Order Management**: Create, track, and modify orders -- **Strategy Controls**: Enable, disable, and configure strategies -- **Risk Parameters**: Adjust risk thresholds and limits -- **Alert Configuration**: Set up custom trading alerts - -### Advanced Analytics -- **Strategy Performance**: Detailed analytics on strategy execution -- **Attribution Analysis**: Performance attribution by strategy/instrument -- **Historical Backtests**: Visualization of backtest results -- **Market Correlation**: Relationship analysis between instruments - -## Integration Points - -### Upstream Connections -- Market Data Gateway (for real-time market data) -- Strategy Orchestrator (for strategy status and control) -- Risk Guardian (for risk metrics) -- Order Management System (for order status) - -### Downstream Components -- User Authentication Service -- Notification Service -- Export/Reporting Services - -## Technical Implementation - -### Technology Stack -- **Framework**: Angular with RxJS for reactive programming -- **UI Components**: Angular Material and custom components -- **Styling**: Tailwind CSS for responsive design -- **Charting**: Lightweight charting libraries for performance -- **Real-time Updates**: WebSocket connections for live data - -### Architecture Pattern -- Component-based architecture -- State management with services and observables -- Lazy-loaded modules for performance -- Responsive design for multiple device types - -## Development Guidelines - -### Component Structure -- Smart/presentational component pattern -- Reusable UI component library -- State management best practices -- Performance optimization techniques - -### Data Visualization -- Chart configuration standards -- Data refresh strategies -- Animation guidelines -- Accessibility requirements - -### User Experience -- Consistent UI/UX patterns -- Keyboard shortcuts and navigation -- Form validation approach -- Error handling and feedback - -## Future Enhancements -- Advanced customization capabilities -- User-defined dashboards and layouts -- Mobile-optimized interface -- Collaborative features (comments, annotations) -- AI-powered insights and recommendations -- Enhanced export and reporting capabilities -- Dark/light theme support