Skip to content
markpaper

src/history/stats.test.ts

v0.3.0 · 824 B

Download file
import { describe, expect, it } from 'vitest';
import { summarizePnl } from './stats.js';
import type { FillLike } from './types.js';

function f(p: Partial<FillLike> & { time: number }): FillLike {
  return { coin: 'BTC', px: '1', sz: '1', side: 'B', dir: 'Open Long', closedPnl: '0', ...p };
}

describe('summarizePnl', () => {
  it('net = closedPnl - USDC fees + funding, exact', () => {
    const s = summarizePnl(
      [
        f({ time: 1, closedPnl: '0.1', fee: '0.01', feeToken: 'USDC' }),
        f({ time: 2, closedPnl: '0.2', fee: '0.02' }),
        f({ time: 3, closedPnl: '0', fee: '5', feeToken: 'PURR' }),
      ],
      [{ delta: { usdc: '-0.07' } }, { delta: { usdc: 'bad' } }],
    );
    expect(s).toEqual({ closedPnl: '0.3', fees: '0.03', funding: '-0.07', net: '0.2', nonUsdcFeeFills: 1 });
  });
});
All files