(config: TickSimulatorConfig)
| 20 | } |
| 21 | |
| 22 | export function createTickSimulator(config: TickSimulatorConfig): TickSimulator { |
| 23 | const { initialPrice, ticksPerSecond, volatility, onTick } = config; |
| 24 | |
| 25 | let price = initialPrice; |
| 26 | let running = false; |
| 27 | let tickCount = 0; |
| 28 | let intervalId: number | null = null; |
| 29 | |
| 30 | // Use high-resolution timer for smooth ticks |
| 31 | const tickIntervalMs = 1000 / ticksPerSecond; |
| 32 | |
| 33 | function generateTick() { |
| 34 | const change = (Math.random() - 0.5) * 2 * volatility * price; |
| 35 | price += change; |
| 36 | price = Math.max(price, initialPrice * 0.01); // Floor |
| 37 | |
| 38 | const tick: Tick = { |
| 39 | timestamp: Date.now(), |
| 40 | price, |
| 41 | volume: Math.random() * 1000 + 100, |
| 42 | }; |
| 43 | |
| 44 | tickCount++; |
| 45 | onTick(tick); |
| 46 | } |
| 47 | |
| 48 | return { |
| 49 | start() { |
| 50 | if (running) return; |
| 51 | running = true; |
| 52 | intervalId = window.setInterval(generateTick, tickIntervalMs); |
| 53 | }, |
| 54 | |
| 55 | stop() { |
| 56 | if (!running) return; |
| 57 | running = false; |
| 58 | if (intervalId !== null) { |
| 59 | clearInterval(intervalId); |
| 60 | intervalId = null; |
| 61 | } |
| 62 | }, |
| 63 | |
| 64 | getTickCount() { |
| 65 | return tickCount; |
| 66 | }, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | // Aggregates ticks into candles |
| 71 | export interface CandleAggregator { |
no outgoing calls
no test coverage detected