shoonya-sdk 0.3.4 → 0.3.6

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.
@@ -311,13 +311,14 @@ declare class Shoonya extends EventEmitter {
311
311
  * @default 5
312
312
  */
313
313
  count?: string;
314
+ optionExpiry?: "current" | "next" | "next-next" | "more-than-1-day";
314
315
  }): Promise<{
315
- token: string;
316
+ token: number;
316
317
  exchange: "NFO" | "MCX";
317
318
  tsym: string;
318
319
  }>;
319
320
  private getFormattedExpiry;
320
- private getNext7FormattedDate;
321
+ private getNextFormattedDates;
321
322
  connectWS(): void;
322
323
  /**
323
324
  *
@@ -311,13 +311,14 @@ declare class Shoonya extends EventEmitter {
311
311
  * @default 5
312
312
  */
313
313
  count?: string;
314
+ optionExpiry?: "current" | "next" | "next-next" | "more-than-1-day";
314
315
  }): Promise<{
315
- token: string;
316
+ token: number;
316
317
  exchange: "NFO" | "MCX";
317
318
  tsym: string;
318
319
  }>;
319
320
  private getFormattedExpiry;
320
- private getNext7FormattedDate;
321
+ private getNextFormattedDates;
321
322
  connectWS(): void;
322
323
  /**
323
324
  *
@@ -424,10 +424,18 @@ var Shoonya = class extends import_events.EventEmitter {
424
424
  * @returns
425
425
  */
426
426
  async getOptionToken(params) {
427
- const { strikePrice, exchange = "NFO", count = "5" } = params;
427
+ const {
428
+ strikePrice,
429
+ exchange = "NFO",
430
+ count = "5",
431
+ optionExpiry = "current"
432
+ } = params;
428
433
  const optionType = params.optionType.at(0);
429
434
  const symbol = params.symbol.toUpperCase();
430
- for await (const expiry of this.getNext7FormattedDate()) {
435
+ let c = 0;
436
+ for await (const [expiry, unformattedDate] of this.getNextFormattedDates(
437
+ optionExpiry
438
+ )) {
431
439
  const optionToPick = `${symbol}${expiry}${optionType}${strikePrice}`;
432
440
  const optionChain = await this.getOptionChain({
433
441
  count,
@@ -437,9 +445,20 @@ var Shoonya = class extends import_events.EventEmitter {
437
445
  });
438
446
  if (optionChain.stat === "Not_Ok")
439
447
  continue;
448
+ c++;
449
+ if (optionExpiry === "next" && c < 2)
450
+ continue;
451
+ if (optionExpiry === "next-next" && c < 3)
452
+ continue;
453
+ if (optionExpiry === "more-than-1-day") {
454
+ const dateNow = (/* @__PURE__ */ new Date()).getDate();
455
+ const optionExpiryDate = unformattedDate.getDate();
456
+ if (optionExpiryDate - dateNow === 0)
457
+ continue;
458
+ }
440
459
  for (const { tsym, token } of optionChain.values) {
441
460
  if (tsym === optionToPick) {
442
- return { token, exchange, tsym };
461
+ return { token: Number(token), exchange, tsym };
443
462
  }
444
463
  }
445
464
  }
@@ -452,12 +471,17 @@ var Shoonya = class extends import_events.EventEmitter {
452
471
  const newYear = year.slice(2);
453
472
  return `${day}${newMonth}${newYear}`;
454
473
  }
455
- getNext7FormattedDate() {
474
+ getNextFormattedDates(expiry) {
475
+ let multipler = 1;
476
+ if (expiry === "next" || expiry === "more-than-1-day")
477
+ multipler = 2;
478
+ if (expiry === "next-next")
479
+ multipler = 3;
456
480
  const dates = [];
457
481
  let currentDate = /* @__PURE__ */ new Date();
458
- for (let i = 0; i < 7; i++) {
482
+ for (let i = 0; i <= 7 * multipler; i++) {
459
483
  const formattedDate = this.getFormattedExpiry(currentDate);
460
- dates.push(formattedDate);
484
+ dates.push([formattedDate, currentDate]);
461
485
  currentDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
462
486
  }
463
487
  return dates;
@@ -398,10 +398,18 @@ var Shoonya = class extends EventEmitter {
398
398
  * @returns
399
399
  */
400
400
  async getOptionToken(params) {
401
- const { strikePrice, exchange = "NFO", count = "5" } = params;
401
+ const {
402
+ strikePrice,
403
+ exchange = "NFO",
404
+ count = "5",
405
+ optionExpiry = "current"
406
+ } = params;
402
407
  const optionType = params.optionType.at(0);
403
408
  const symbol = params.symbol.toUpperCase();
404
- for await (const expiry of this.getNext7FormattedDate()) {
409
+ let c = 0;
410
+ for await (const [expiry, unformattedDate] of this.getNextFormattedDates(
411
+ optionExpiry
412
+ )) {
405
413
  const optionToPick = `${symbol}${expiry}${optionType}${strikePrice}`;
406
414
  const optionChain = await this.getOptionChain({
407
415
  count,
@@ -411,9 +419,20 @@ var Shoonya = class extends EventEmitter {
411
419
  });
412
420
  if (optionChain.stat === "Not_Ok")
413
421
  continue;
422
+ c++;
423
+ if (optionExpiry === "next" && c < 2)
424
+ continue;
425
+ if (optionExpiry === "next-next" && c < 3)
426
+ continue;
427
+ if (optionExpiry === "more-than-1-day") {
428
+ const dateNow = (/* @__PURE__ */ new Date()).getDate();
429
+ const optionExpiryDate = unformattedDate.getDate();
430
+ if (optionExpiryDate - dateNow === 0)
431
+ continue;
432
+ }
414
433
  for (const { tsym, token } of optionChain.values) {
415
434
  if (tsym === optionToPick) {
416
- return { token, exchange, tsym };
435
+ return { token: Number(token), exchange, tsym };
417
436
  }
418
437
  }
419
438
  }
@@ -426,12 +445,17 @@ var Shoonya = class extends EventEmitter {
426
445
  const newYear = year.slice(2);
427
446
  return `${day}${newMonth}${newYear}`;
428
447
  }
429
- getNext7FormattedDate() {
448
+ getNextFormattedDates(expiry) {
449
+ let multipler = 1;
450
+ if (expiry === "next" || expiry === "more-than-1-day")
451
+ multipler = 2;
452
+ if (expiry === "next-next")
453
+ multipler = 3;
430
454
  const dates = [];
431
455
  let currentDate = /* @__PURE__ */ new Date();
432
- for (let i = 0; i < 7; i++) {
456
+ for (let i = 0; i <= 7 * multipler; i++) {
433
457
  const formattedDate = this.getFormattedExpiry(currentDate);
434
- dates.push(formattedDate);
458
+ dates.push([formattedDate, currentDate]);
435
459
  currentDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
436
460
  }
437
461
  return dates;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shoonya-sdk",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Wrapper around Shoonya API",
5
5
  "main": "dist/shoonya-sdk.js",
6
6
  "module": "dist/shoonya-sdk.mjs",