Spaces:
Running on Zero
Running on Zero
deveshc23
feat: Add Free Gradio Space support, Git LFS tracking for data artifacts, and Quant Allocator
3e33e2e Download frontend/src/utils/indicators.ts from deveshc23/drift-quant: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/deveshc23/drift-quant/resolve/main/frontend/src/utils/indicators.ts
- Command line
-
hf download hf://spaces/deveshc23/drift-quant/frontend/src/utils/indicators.ts
-
curl -L -o indicators.ts https://huggingface.co/spaces/deveshc23/drift-quant/resolve/main/frontend/src/utils/indicators.ts
1.5 kB
| 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; | |
| } | |