nsekit 0.3.6 → 0.3.7

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.
@@ -46,24 +46,37 @@ export declare function calculateSlippage(price: number, quantity: number, avgVo
46
46
  export declare function computeMarketFillPrice(ltp: number, side: 'BUY' | 'SELL', quantity: number, avgVolume: number, config: FillEngineConfig): number;
47
47
  /**
48
48
  * Check if a LIMIT order should fill based on the current tick.
49
- * BUY LIMIT fills when ltp <= limit price.
50
- * SELL LIMIT fills when ltp >= limit price.
49
+ *
50
+ * Marketable path (preferred): match against the opposite side of the book.
51
+ * BUY fills when ask <= limit (a seller is offering at or below limit)
52
+ * SELL fills when bid >= limit (a buyer is bidding at or above limit)
53
+ * This mirrors a real exchange — a marketable limit crosses the spread the
54
+ * instant a counterparty is willing to take it. For illiquid options where
55
+ * LTP can lag bid/ask by minutes, the pure-LTP path leaves obviously
56
+ * fillable orders pending until a trade prints, and the smart-pricer's
57
+ * walker exhausts.
58
+ *
59
+ * Passive path (fallback): when bid/ask is unavailable (0), fall back to
60
+ * LTP — covers ticks that omit quote data and existing test fixtures that
61
+ * only supply LTP.
62
+ * BUY fills when ltp <= limit
63
+ * SELL fills when ltp >= limit
51
64
  */
52
- export declare function shouldLimitFill(side: 'BUY' | 'SELL', limitPrice: number, ltp: number): boolean;
65
+ export declare function shouldLimitFill(side: 'BUY' | 'SELL', limitPrice: number, ltp: number, bid?: number, ask?: number): boolean;
53
66
  /**
54
67
  * Compute the fill price for a LIMIT order whose `shouldLimitFill` condition
55
68
  * just became true. Real exchanges match limits at the prevailing market
56
- * price (LTP), not at the user's limit price — the user's limit is a
57
- * max/min boundary, better is allowed. Honoring the limit price would
58
- * charge the full slippage of an aggressive limit (e.g. 5% above ask),
59
- * which trips tight SL immediately on entry and breaks any strategy that
60
- * relies on aggressive-limit fallback for fill guarantees.
69
+ * price, not at the user's limit price — the user's limit is a max/min
70
+ * boundary, better is allowed. Honoring the limit price would charge the
71
+ * full slippage of an aggressive limit (e.g. 5% above ask), which trips
72
+ * tight SL immediately on entry and breaks any strategy that relies on
73
+ * aggressive-limit fallback for fill guarantees.
61
74
  *
62
- * `side` and `limitPrice` are accepted for symmetry with shouldLimitFill
63
- * and to keep the signature self-documenting at call sites; the function
64
- * always returns ltp.
75
+ * Use the marketable side of the book (ask for BUY, bid for SELL) when
76
+ * available — that's the actual transaction price. Fall back to LTP when
77
+ * bid/ask are missing.
65
78
  */
66
- export declare function computeLimitFillPrice(_side: 'BUY' | 'SELL', _limitPrice: number, ltp: number): number;
79
+ export declare function computeLimitFillPrice(side: 'BUY' | 'SELL', _limitPrice: number, ltp: number, bid?: number, ask?: number): number;
67
80
  /**
68
81
  * Check if a stop-loss trigger has been hit.
69
82
  * BUY SL triggers when ltp >= trigger price.
@@ -1 +1 @@
1
- {"version":3,"file":"paper-fill-engine.d.ts","sourceRoot":"","sources":["../../../src/brokers/paper/paper-fill-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAIpG,MAAM,WAAW,gBAAgB;IAC/B,6EAA6E;IAC7E,eAAe,EAAE,MAAM,CAAC;IACxB,kFAAkF;IAClF,YAAY,EAAE,MAAM,CAAC;IACrB,mFAAmF;IACnF,gBAAgB,EAAE,MAAM,CAAC;IACzB,uEAAuE;IACvE,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,mBAAmB,EAAE,gBAKjC,CAAC;AAIF,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,kBAAkB,CAAC;IAC3B,MAAM,EAAE,WAAW,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CA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AAC;QAClB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,IAAI,CAAC,EAAE,SAAS,CAAC;KAClB,GAAG,OAAO;IAcX;;;OAGG;IACH,WAAW,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI;IAkBpC,8BAA8B;IAC9B,gBAAgB,IAAI,YAAY,EAAE;IAMlC,kCAAkC;IAClC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;IAInD,mDAAmD;IACnD,YAAY,IAAI,YAAY,EAAE;IAI9B,6CAA6C;IAC7C,KAAK,IAAI,IAAI;IAOb,OAAO,CAAC,YAAY;IAmEpB,OAAO,CAAC,YAAY;IAYpB,OAAO,CAAC,SAAS;IAmCjB,OAAO,CAAC,UAAU;CASnB"}
package/dist/index.js CHANGED
@@ -23938,12 +23938,21 @@ function computeMarketFillPrice(ltp, side, quantity, avgVolume, config) {
23938
23938
  const raw = side === "BUY" ? ltp + slippage : ltp - slippage;
23939
23939
  return Math.round(raw * 100) / 100;
23940
23940
  }
23941
- function shouldLimitFill(side, limitPrice, ltp) {
23942
- if (side === "BUY")
23941
+ function shouldLimitFill(side, limitPrice, ltp, bid = 0, ask = 0) {
23942
+ if (side === "BUY") {
23943
+ if (ask > 0)
23944
+ return ask <= limitPrice;
23943
23945
  return ltp <= limitPrice;
23946
+ }
23947
+ if (bid > 0)
23948
+ return bid >= limitPrice;
23944
23949
  return ltp >= limitPrice;
23945
23950
  }
23946
- function computeLimitFillPrice(_side, _limitPrice, ltp) {
23951
+ function computeLimitFillPrice(side, _limitPrice, ltp, bid = 0, ask = 0) {
23952
+ if (side === "BUY" && ask > 0)
23953
+ return ask;
23954
+ if (side === "SELL" && bid > 0)
23955
+ return bid;
23947
23956
  return ltp;
23948
23957
  }
23949
23958
  function shouldTrigger(side, triggerPrice, ltp) {
@@ -24087,7 +24096,7 @@ class PaperFillEngine {
24087
24096
  if (pending.params.tradingSymbol !== tick.tradingSymbol && pending.params.tradingSymbol !== tick.token) {
24088
24097
  continue;
24089
24098
  }
24090
- this.checkAndFill(orderId, pending, ltp);
24099
+ this.checkAndFill(orderId, pending, ltp, tick.bid ?? 0, tick.ask ?? 0);
24091
24100
  }
24092
24101
  }
24093
24102
  getPendingOrders() {
@@ -24103,7 +24112,7 @@ class PaperFillEngine {
24103
24112
  this.pendingOrders.clear();
24104
24113
  this.avgVolumes.clear();
24105
24114
  }
24106
- checkAndFill(orderId, pending, ltp) {
24115
+ checkAndFill(orderId, pending, ltp, bid = 0, ask = 0) {
24107
24116
  const { params } = pending;
24108
24117
  const avgVolume = this.avgVolumes.get(params.tradingSymbol) ?? this.config.defaultAvgVolume;
24109
24118
  const remainingQty = params.quantity - pending.filledQty;
@@ -24118,9 +24127,9 @@ class PaperFillEngine {
24118
24127
  break;
24119
24128
  }
24120
24129
  case "LIMIT": {
24121
- if (shouldLimitFill(params.side, params.price ?? 0, ltp)) {
24130
+ if (shouldLimitFill(params.side, params.price ?? 0, ltp, bid, ask)) {
24122
24131
  const fillQty = determineFillQty(remainingQty, this.config.partialFillProb);
24123
- const fillPrice = computeLimitFillPrice(params.side, params.price ?? 0, ltp);
24132
+ const fillPrice = computeLimitFillPrice(params.side, params.price ?? 0, ltp, bid, ask);
24124
24133
  this.applyFill(orderId, pending, fillQty, fillPrice);
24125
24134
  }
24126
24135
  break;
@@ -24135,9 +24144,9 @@ class PaperFillEngine {
24135
24144
  }
24136
24145
  }
24137
24146
  if (pending.triggered) {
24138
- if (shouldLimitFill(params.side, params.price ?? 0, ltp)) {
24147
+ if (shouldLimitFill(params.side, params.price ?? 0, ltp, bid, ask)) {
24139
24148
  const fillQty = determineFillQty(remainingQty, this.config.partialFillProb);
24140
- const fillPrice = computeLimitFillPrice(params.side, params.price ?? 0, ltp);
24149
+ const fillPrice = computeLimitFillPrice(params.side, params.price ?? 0, ltp, bid, ask);
24141
24150
  this.applyFill(orderId, pending, fillQty, fillPrice);
24142
24151
  }
24143
24152
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nsekit",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Unified broker abstraction for Indian stock markets - CCXT-like interface for NSE/BSE brokers",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",