@perpbrain/sdk 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -2
  3. package/dist/cjs/card.d.ts +2 -0
  4. package/dist/cjs/card.js +311 -0
  5. package/dist/cjs/cli.d.ts +3 -0
  6. package/dist/cjs/cli.js +33 -0
  7. package/dist/cjs/guarded.d.ts +12 -0
  8. package/dist/cjs/guarded.js +52 -0
  9. package/dist/cjs/guards.d.ts +154 -0
  10. package/dist/cjs/guards.js +855 -0
  11. package/dist/cjs/index.d.ts +16 -0
  12. package/dist/cjs/index.js +66 -0
  13. package/dist/cjs/package.json +1 -0
  14. package/dist/cjs/receipt.d.ts +45 -0
  15. package/dist/cjs/receipt.js +855 -0
  16. package/dist/cjs/study.d.ts +29 -0
  17. package/dist/cjs/study.js +259 -0
  18. package/dist/cjs/vendor/costs.d.ts +64 -0
  19. package/dist/cjs/vendor/costs.js +247 -0
  20. package/dist/cjs/vendor/execution.d.ts +44 -0
  21. package/dist/cjs/vendor/execution.js +132 -0
  22. package/dist/cjs/vendor/fees.d.ts +17 -0
  23. package/dist/cjs/vendor/fees.js +82 -0
  24. package/dist/cjs/vendor/funding_adapter.d.ts +11 -0
  25. package/dist/cjs/vendor/funding_adapter.js +33 -0
  26. package/dist/cjs/vendor/info.d.ts +24 -0
  27. package/dist/cjs/vendor/info.js +55 -0
  28. package/dist/cjs/vendor/queries.d.ts +69 -0
  29. package/dist/cjs/vendor/queries.js +210 -0
  30. package/dist/cjs/vendor/rateLimit.d.ts +14 -0
  31. package/dist/cjs/vendor/rateLimit.js +57 -0
  32. package/dist/cjs/vendor/simDefaults.d.ts +31 -0
  33. package/dist/cjs/vendor/simDefaults.js +49 -0
  34. package/dist/cjs/vendor/simulate.d.ts +130 -0
  35. package/dist/cjs/vendor/simulate.js +610 -0
  36. package/dist/esm/card.d.ts +2 -0
  37. package/dist/esm/card.js +308 -0
  38. package/dist/esm/cli.d.ts +3 -0
  39. package/dist/esm/cli.js +31 -0
  40. package/dist/esm/guarded.d.ts +12 -0
  41. package/dist/esm/guarded.js +49 -0
  42. package/dist/esm/guards.d.ts +154 -0
  43. package/dist/esm/guards.js +841 -0
  44. package/dist/esm/index.d.ts +16 -0
  45. package/dist/esm/index.js +20 -0
  46. package/dist/esm/receipt.d.ts +45 -0
  47. package/dist/esm/receipt.js +839 -0
  48. package/dist/esm/study.d.ts +29 -0
  49. package/dist/esm/study.js +249 -0
  50. package/dist/esm/vendor/costs.d.ts +64 -0
  51. package/dist/esm/vendor/costs.js +240 -0
  52. package/dist/esm/vendor/execution.d.ts +44 -0
  53. package/dist/esm/vendor/execution.js +122 -0
  54. package/dist/esm/vendor/fees.d.ts +17 -0
  55. package/dist/esm/vendor/fees.js +74 -0
  56. package/dist/esm/vendor/funding_adapter.d.ts +11 -0
  57. package/dist/esm/vendor/funding_adapter.js +30 -0
  58. package/dist/esm/vendor/info.d.ts +24 -0
  59. package/dist/esm/vendor/info.js +49 -0
  60. package/dist/esm/vendor/queries.d.ts +69 -0
  61. package/dist/esm/vendor/queries.js +196 -0
  62. package/dist/esm/vendor/rateLimit.d.ts +14 -0
  63. package/dist/esm/vendor/rateLimit.js +53 -0
  64. package/dist/esm/vendor/simDefaults.d.ts +31 -0
  65. package/dist/esm/vendor/simDefaults.js +44 -0
  66. package/dist/esm/vendor/simulate.d.ts +130 -0
  67. package/dist/esm/vendor/simulate.js +605 -0
  68. package/package.json +64 -4
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Simulator defaults pinned per receipt version. Mirror of
3
+ * python/perpbrain-read/src/perpbrain_read/sim/defaults.py; simDefaults.test.ts and
4
+ * python/perpbrain-read/tests/test_sim_defaults.py check the two tables stay identical. See that module for the history of each row.
5
+ */
6
+ export declare const FEE_VERSION_AUTO_BY_TIME = "auto-by-time";
7
+ export declare const PER_FILL_FEE_KEYS: readonly ["fee_version", "taker_bps", "maker_bps"];
8
+ export interface SimDefaults {
9
+ profile: string;
10
+ /** Stop slippage floor (bps) when execution.min_stop_slippage_bps is not given. */
11
+ min_stop_slippage_bps: string;
12
+ /** Schedule used when fee_version is null: fixed default table, or by bar time. */
13
+ fee_when_unset: "fixed" | "by_time";
14
+ /** Whether each fill carries fee_version, taker_bps and maker_bps. */
15
+ per_fill_fees: boolean;
16
+ /** Funding samples off the interval grid: ignored, or floored onto it. */
17
+ funding_off_grid: "skip" | "floor";
18
+ /** Whether the result carries cost_breakdown. */
19
+ cost_breakdown: boolean;
20
+ /** Check on a charged funding sample's published_at_ms: none, or after its own charge time (#19). */
21
+ funding_published: "none" | "charge_time";
22
+ /** Whether each fill records stop_floor_bps, the stop floor applied to it. */
23
+ record_stop_floor: boolean;
24
+ /** Arithmetic: Python Decimal (v1-v4; this TS sim was always fixed18), or the 18-decimal fixed point shared exactly with Python (v5, issue 3). */
25
+ numerics: "decimal" | "fixed18";
26
+ }
27
+ export declare const SIM_DEFAULTS: Readonly<Record<string, SimDefaults>>;
28
+ export declare const CURRENT_SIM_DEFAULTS = "5";
29
+ export declare function simDefaults(profile: string): SimDefaults;
30
+ /** Pick the defaults profile for a receipt from its version and recorded result. */
31
+ export declare function receiptProfile(version: unknown, result: unknown): string;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Simulator defaults pinned per receipt version. Mirror of
3
+ * python/perpbrain-read/src/perpbrain_read/sim/defaults.py; simDefaults.test.ts and
4
+ * python/perpbrain-read/tests/test_sim_defaults.py check the two tables stay identical. See that module for the history of each row.
5
+ */
6
+ export const FEE_VERSION_AUTO_BY_TIME = "auto-by-time";
7
+ export const PER_FILL_FEE_KEYS = ["fee_version", "taker_bps", "maker_bps"];
8
+ export const SIM_DEFAULTS = {
9
+ "1": { profile: "1", min_stop_slippage_bps: "0", fee_when_unset: "fixed", per_fill_fees: false, funding_off_grid: "skip", cost_breakdown: false, funding_published: "none", record_stop_floor: false, numerics: "decimal" },
10
+ "2": { profile: "2", min_stop_slippage_bps: "0", fee_when_unset: "fixed", per_fill_fees: false, funding_off_grid: "skip", cost_breakdown: true, funding_published: "none", record_stop_floor: false, numerics: "decimal" },
11
+ "3": { profile: "3", min_stop_slippage_bps: "0", fee_when_unset: "fixed", per_fill_fees: false, funding_off_grid: "skip", cost_breakdown: true, funding_published: "charge_time", record_stop_floor: false, numerics: "decimal" },
12
+ "3-pr18": { profile: "3-pr18", min_stop_slippage_bps: "10", fee_when_unset: "by_time", per_fill_fees: true, funding_off_grid: "floor", cost_breakdown: true, funding_published: "charge_time", record_stop_floor: false, numerics: "decimal" },
13
+ "4": { profile: "4", min_stop_slippage_bps: "10", fee_when_unset: "fixed", per_fill_fees: true, funding_off_grid: "floor", cost_breakdown: true, funding_published: "charge_time", record_stop_floor: true, numerics: "decimal" },
14
+ "5": { profile: "5", min_stop_slippage_bps: "10", fee_when_unset: "fixed", per_fill_fees: true, funding_off_grid: "floor", cost_breakdown: true, funding_published: "charge_time", record_stop_floor: true, numerics: "fixed18" },
15
+ };
16
+ export const CURRENT_SIM_DEFAULTS = "5";
17
+ export function simDefaults(profile) {
18
+ const found = Object.prototype.hasOwnProperty.call(SIM_DEFAULTS, profile) ? SIM_DEFAULTS[profile] : undefined;
19
+ if (found === undefined) {
20
+ throw new Error("unknown sim defaults profile");
21
+ }
22
+ return found;
23
+ }
24
+ /** Pick the defaults profile for a receipt from its version and recorded result. */
25
+ export function receiptProfile(version, result) {
26
+ if (typeof version !== "number" || !Number.isInteger(version)) {
27
+ return CURRENT_SIM_DEFAULTS;
28
+ }
29
+ if (version === 3 && hasFillFees(result)) {
30
+ return "3-pr18";
31
+ }
32
+ const key = String(version);
33
+ return Object.prototype.hasOwnProperty.call(SIM_DEFAULTS, key) ? key : CURRENT_SIM_DEFAULTS;
34
+ }
35
+ function hasFillFees(result) {
36
+ if (typeof result !== "object" || result === null || Array.isArray(result)) {
37
+ return false;
38
+ }
39
+ const fills = result.fills;
40
+ if (!Array.isArray(fills)) {
41
+ return false;
42
+ }
43
+ return fills.some((fill) => typeof fill === "object" && fill !== null && PER_FILL_FEE_KEYS.some((key) => key in fill));
44
+ }
@@ -0,0 +1,130 @@
1
+ /** Honest simulator. Decisions use a closed bar. Fills use the next bar and the book. */
2
+ import { type CostBreakdown } from "./costs.ts";
3
+ import { type ExecutionConfig } from "./execution.ts";
4
+ export declare function fundingGridTime(stamp: number, interval: number): number;
5
+ export declare const NOT_MODELLED: "NOT_MODELLED";
6
+ export declare const HL_FUNDING_INTERVAL_MS = 3600000;
7
+ export type OrderSide = "buy" | "sell";
8
+ export type PositionSide = "long" | "short";
9
+ export type Liquidity = "taker" | "maker";
10
+ export interface Bar {
11
+ open: string;
12
+ high: string;
13
+ low: string;
14
+ close: string;
15
+ timeMs?: number;
16
+ }
17
+ export type EntryKind = "market" | "stop";
18
+ export interface Decision {
19
+ barIndex: number;
20
+ side: PositionSide;
21
+ size: string;
22
+ stop: string | null;
23
+ target: string | null;
24
+ liquidity?: Liquidity;
25
+ entryLiquidity?: Liquidity;
26
+ exitLiquidity?: Liquidity;
27
+ entry?: EntryKind;
28
+ entryStop?: string | null;
29
+ }
30
+ export interface FundingSample {
31
+ timeMs: number;
32
+ ratePerHour: string;
33
+ oraclePxUsd: string;
34
+ publishedAtMs?: number;
35
+ }
36
+ export interface FundingPayment {
37
+ time_ms: number;
38
+ rate_per_hour: string;
39
+ oracle_px_usd: string;
40
+ payment_usd: string;
41
+ size_coin: string;
42
+ queue_position: "NOT_MODELLED";
43
+ liquidation: "NOT_MODELLED";
44
+ }
45
+ export interface Level {
46
+ px: string;
47
+ sz: string;
48
+ }
49
+ export interface Book {
50
+ bids: Level[];
51
+ asks: Level[];
52
+ timeMs?: number;
53
+ historical?: boolean;
54
+ }
55
+ export interface Leg {
56
+ size: string;
57
+ price: string;
58
+ }
59
+ export interface Fill {
60
+ kind: "entry" | "exit";
61
+ side: OrderSide;
62
+ size: string;
63
+ price: string;
64
+ fee: string;
65
+ slippage: string;
66
+ reason: string;
67
+ legs: Leg[];
68
+ liquidity: Liquidity;
69
+ fee_version?: string;
70
+ taker_bps?: string;
71
+ maker_bps?: string;
72
+ stop_floor_bps?: string;
73
+ queue_position: "NOT_MODELLED";
74
+ liquidation: "NOT_MODELLED";
75
+ }
76
+ export interface Receipt {
77
+ fills: Fill[];
78
+ funding_payments: FundingPayment[];
79
+ queue_position: "NOT_MODELLED";
80
+ liquidation: "NOT_MODELLED";
81
+ cost_breakdown: CostBreakdown;
82
+ }
83
+ export interface SimulateResult {
84
+ label: "SIMULATED";
85
+ market: string;
86
+ fills: Fill[];
87
+ fillSource: "exchange";
88
+ feeVersion: string;
89
+ takerBps: string;
90
+ makerBps: string;
91
+ fundingIntervalMs: number;
92
+ fundingPayments: FundingPayment[];
93
+ unfilledEntry: string;
94
+ openSize: string;
95
+ queue_position: "NOT_MODELLED";
96
+ liquidation: "NOT_MODELLED";
97
+ cost_breakdown: CostBreakdown;
98
+ receipt: Receipt;
99
+ guardViolations?: string[];
100
+ simLabels?: string[];
101
+ simWarnings?: string[];
102
+ }
103
+ export type { ExecutionConfig };
104
+ export interface FillGuardIntent {
105
+ coin: string;
106
+ notionalUsd: string;
107
+ leverage: string;
108
+ openPositions: number;
109
+ dailyLossUsd: string;
110
+ slippageBps: string;
111
+ ageMs: number;
112
+ opening: boolean;
113
+ orderSide: "buy" | "sell";
114
+ dailyLossPct: string;
115
+ nowMs: number;
116
+ }
117
+ export interface FillGuardVerdict {
118
+ ok: boolean;
119
+ violations: string[];
120
+ }
121
+ export type FillGuard = (intent: FillGuardIntent) => FillGuardVerdict;
122
+ export interface GuardContext {
123
+ leverage?: string;
124
+ openPositions?: number;
125
+ dailyLossUsd?: string;
126
+ dailyLossPct?: string;
127
+ ageMs?: number;
128
+ nowMs?: number;
129
+ }
130
+ export declare function simulate(market: string, decision: Decision, bars: Bar[], book: Book, feeVersion?: string | null, guard?: FillGuard | null, guardContext?: GuardContext | null, funding?: FundingSample[] | null, fundingIntervalMs?: number, execution?: ExecutionConfig | null, atrAtDecision?: string | null, minStopAtrMultiple?: string, simDefaultsProfile?: string): SimulateResult;