@livefolio/sdk 0.4.1 → 0.4.3

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.
package/dist/index.d.ts CHANGED
@@ -577,6 +577,11 @@ type PriceMap = ReadonlyMap<AssetId, number>;
577
577
  * value and existing share counts.
578
578
  * @param prices - Current prices for all assets that appear in `targets` or are
579
579
  * currently held. Throws `Error` if a target asset is missing from this map.
580
+ * @param assets - Optional canonical {@link Asset} metadata keyed by id. When
581
+ * `reconcile` needs to emit an order for an asset that is not yet held in the
582
+ * portfolio, it consults this map for the proper `symbol`/`kind`. If the
583
+ * asset is missing here too, the order falls back to a synthesized
584
+ * `{ kind: 'equity', id, symbol: id }` — lossless but display-unfriendly.
580
585
  * @returns A readonly array of `RebalanceOrder` objects. The array may be empty
581
586
  * if the portfolio is already at the target allocation. Order IDs are
582
587
  * deterministic within a single call (`rebal_<assetId>_<counter>`).
@@ -603,7 +608,7 @@ type PriceMap = ReadonlyMap<AssetId, number>;
603
608
  * // ]
604
609
  * ```
605
610
  */
606
- declare function reconcile(targets: TargetWeights, portfolio: Portfolio, prices: PriceMap): ReadonlyArray<RebalanceOrder>;
611
+ declare function reconcile(targets: TargetWeights, portfolio: Portfolio, prices: PriceMap, assets?: ReadonlyMap<AssetId, Asset>): ReadonlyArray<RebalanceOrder>;
607
612
 
608
613
  /**
609
614
  * A flat record of fundamental data points for an asset at a point in time.
@@ -3523,7 +3528,10 @@ declare namespace index {
3523
3528
  * - `'close'` — removes shares from an existing position and credits cash.
3524
3529
  * - `'adjust'` — updates the position's `quantity`; only fees are debited.
3525
3530
  * - `'rebalance'` — buys or sells shares in the long position for `asset`;
3526
- * creates or removes the position as needed.
3531
+ * creates the position on a positive `delta`, removes it
3532
+ * when fully reduced. A reduce against a non-existent
3533
+ * long position is silently ignored (matching the same
3534
+ * case in {@link applyOrders}).
3527
3535
  *
3528
3536
  * The returned `portfolio.t` is updated to the maximum fill timestamp.
3529
3537
  *
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/strategy/reconcile.ts
8
- function reconcile(targets, portfolio, prices) {
8
+ function reconcile(targets, portfolio, prices, assets) {
9
9
  const longByAsset = /* @__PURE__ */ new Map();
10
10
  for (const p of portfolio.positions) {
11
11
  if (p.side !== "long") continue;
@@ -27,16 +27,16 @@ function reconcile(targets, portfolio, prices) {
27
27
  if (price === void 0) {
28
28
  throw new Error(`reconcile: missing price for target asset ${assetId}`);
29
29
  }
30
- const targetShares = Math.floor(totalValue * weight / price);
30
+ const targetShares = Math.max(0, Math.floor(totalValue * weight / price));
31
31
  const held = longByAsset.get(assetId);
32
32
  const currentShares = held?.quantity ?? 0;
33
33
  const delta = targetShares - currentShares;
34
34
  seen.add(assetId);
35
35
  if (delta !== 0) {
36
- const asset = held?.asset ?? {
36
+ const asset = held?.asset ?? assets?.get(assetId) ?? {
37
37
  kind: "equity",
38
38
  id: assetId,
39
- symbol: assetId.split(":").pop() ?? assetId
39
+ symbol: assetId
40
40
  };
41
41
  orders.push({ id: nextId(assetId), kind: "rebalance", asset, delta });
42
42
  }
@@ -125,10 +125,7 @@ function applyFills(portfolio, fills, orders) {
125
125
  basis: prev.basis + cost
126
126
  };
127
127
  }
128
- } else {
129
- if (idx < 0) {
130
- throw new Error(`applyFills: rebalance reduce on ${order.asset.id} but no long position exists`);
131
- }
128
+ } else if (idx >= 0) {
132
129
  const prev = positions[idx];
133
130
  cash += fill.quantity * fill.price - fill.fees;
134
131
  const remaining = prev.quantity - fill.quantity;
@@ -2890,6 +2887,13 @@ function fromSpec(spec, opts) {
2890
2887
  }
2891
2888
  validateSynthetics(spec);
2892
2889
  const universe = spec.universe.map(resolveAssetRef);
2890
+ const assetsById = /* @__PURE__ */ new Map();
2891
+ for (const a of universe) assetsById.set(a.id, a);
2892
+ for (const s of spec.synthetics ?? []) {
2893
+ if (!assetsById.has(s.id)) {
2894
+ assetsById.set(s.id, { kind: "equity", id: s.id, symbol: s.symbol });
2895
+ }
2896
+ }
2893
2897
  const { runtime, calendar } = opts;
2894
2898
  const cadence = spec.rebalance?.frequency ?? "Daily";
2895
2899
  return {
@@ -2934,7 +2938,7 @@ function fromSpec(spec, opts) {
2934
2938
  }
2935
2939
  }
2936
2940
  return {
2937
- orders: reconcile(evaluated.weights, portfolio, features.prices),
2941
+ orders: reconcile(evaluated.weights, portfolio, features.prices, assetsById),
2938
2942
  state: evaluated.state
2939
2943
  };
2940
2944
  }