import { OpenRouterClient } from "../lib/openrouter"; import { FundamentalsAnalyst } from "./fundamentals"; import { TechnicalAnalyst } from "./technical"; import { SentimentAnalyst } from "./sentiment"; import { BullishResearcher, BearishResearcher } from "./researchers"; import { Trader } from "./trader"; import type { AnalystReport, DebateRound, TradingDecision, AgentSignal } from "../types/agents"; export class TradingGraph { private client: OpenRouterClient; private model: string; private fundamentalsAnalyst: FundamentalsAnalyst; private technicalAnalyst: TechnicalAnalyst; private sentimentAnalyst: SentimentAnalyst; private bullishResearcher: BullishResearcher; private bearishResearcher: BearishResearcher; private trader: Trader; constructor(client: OpenRouterClient, model?: string) { this.client = client; this.model = model ?? "google/gemini-2.0-flash-exp:free"; this.fundamentalsAnalyst = new FundamentalsAnalyst(client, { model: this.model }); this.technicalAnalyst = new TechnicalAnalyst(client, { model: this.model }); this.sentimentAnalyst = new SentimentAnalyst(client, { model: this.model }); this.bullishResearcher = new BullishResearcher(client, this.model); this.bearishResearcher = new BearishResearcher(client, this.model); this.trader = new Trader(client, this.model); } async propagate( ticker: string, input: { financialData: string; technicalData: { prices: number[]; sma: number; ema: number; rsi: number; macd: number }; sentimentData: { headlines: string[]; source?: "news" | "social" | "stocktwits" }; } ): Promise { const reports = await this.runAnalysts(ticker, input); const debates = await this.runDebate(ticker, reports); const decision = await this.trader.decide(ticker, reports, debates); return decision; } private async runAnalysts( ticker: string, input: { financialData: string; technicalData: { prices: number[]; sma: number; ema: number; rsi: number; macd: number }; sentimentData: { headlines: string[]; source?: "news" | "social" | "stocktwits" }; } ): Promise { const [fundamentals, technical, sentiment] = await Promise.all([ this.fundamentalsAnalyst.analyze(ticker, input.financialData), this.technicalAnalyst.analyze(ticker, input.technicalData), this.sentimentAnalyst.analyze(ticker, input.sentimentData), ]); return [fundamentals, technical, sentiment]; } private async runDebate(ticker: string, reports: AnalystReport[]): Promise { const [bullish, bearish] = await Promise.all([ this.bullishResearcher.research(ticker, reports), this.bearishResearcher.research(ticker, reports), ]); return [ { bullishView: bullish.bullishView, bearishView: bearish.bearishView, researcher: "bullish", }, ]; } }