import type { MarketBarItem } from "../api/types"; export interface IndicatorOverlays { emaFast?: (number | null)[]; emaSlow?: (number | null)[]; vwap?: (number | null)[]; } export function calculateEma(bars: MarketBarItem[], period: number): (number | null)[] { if (bars.length < period) { return bars.map(() => null); } const result: (number | null)[] = []; const multiplier = 2 / (period + 1); // Initial SMA for first value let sum = 0; for (let i = 0; i < period; i++) { sum += bars[i].close; result.push(null); } let prevEma = sum / period; result[period - 1] = prevEma; for (let i = period; i < bars.length; i++) { const currentEma = (bars[i].close - prevEma) * multiplier + prevEma; result.push(currentEma); prevEma = currentEma; } return result; } export function calculateVwapProxy(bars: MarketBarItem[]): (number | null)[] { const result: (number | null)[] = []; let currentDate = ""; let cumTypicalVolume = 0; let cumVolume = 0; for (const bar of bars) { const barDate = bar.timestamp.slice(0, 10); if (barDate !== currentDate) { currentDate = barDate; cumTypicalVolume = 0; cumVolume = 0; } const typicalPrice = (bar.high + bar.low + bar.close) / 3; cumTypicalVolume += typicalPrice * bar.volume; cumVolume += bar.volume; if (cumVolume > 0) { result.push(cumTypicalVolume / cumVolume); } else { result.push(bar.close); } } return result; }