@livefolio/sdk 0.4.2 → 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
@@ -3528,7 +3528,10 @@ declare namespace index {
3528
3528
  * - `'close'` — removes shares from an existing position and credits cash.
3529
3529
  * - `'adjust'` — updates the position's `quantity`; only fees are debited.
3530
3530
  * - `'rebalance'` — buys or sells shares in the long position for `asset`;
3531
- * 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}).
3532
3535
  *
3533
3536
  * The returned `portfolio.t` is updated to the maximum fill timestamp.
3534
3537
  *
package/dist/index.js CHANGED
@@ -27,7 +27,7 @@ function reconcile(targets, portfolio, prices, assets) {
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;
@@ -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;