positionManager.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type { PositionSnapshot } from "../../domain/src/types";
  2. export interface AccountPosition extends PositionSnapshot {
  3. accountId: string;
  4. fundingRate?: number;
  5. fundingCorrelation?: number;
  6. }
  7. export interface AggregatedPosition {
  8. symbol: string;
  9. base: number;
  10. quote: number;
  11. ts: number;
  12. accounts: AccountPosition[];
  13. }
  14. export class PositionManager {
  15. async snapshot(symbol: string, snapshots: Array<PositionSnapshot & { accountId?: string }>): Promise<AggregatedPosition> {
  16. const accounts: AccountPosition[] = snapshots.map((snap, idx) => ({
  17. accountId: snap.accountId ?? `account-${idx}`,
  18. symbol: snap.symbol,
  19. base: snap.base,
  20. quote: snap.quote,
  21. entryPx: snap.entryPx,
  22. ts: snap.ts,
  23. fundingRate: (snap as any).fundingRate,
  24. fundingCorrelation: (snap as any).fundingCorrelation
  25. }));
  26. const base = accounts.reduce((sum, snap) => sum + snap.base, 0);
  27. const quote = accounts.reduce((sum, snap) => sum + snap.quote, 0);
  28. const ts = Date.now();
  29. return { symbol, base, quote, ts, accounts };
  30. }
  31. }