UNPKG

@thoshpathi/utils-smartapi

Version:

Extended utilities for Angel One's smartapi-javascript SDK, including custom methods and helpers for market data like candles, P&L, and more.

149 lines (146 loc) 4.57 kB
import { OHLCData } from "./chunk-KND42SB5.mjs"; // src/smartapi/smartapi_enhanced.ts import { SmartAPI } from "@thoshpathi/smartapi-javascript"; import { format } from "date-fns"; var SmartApiError = class extends Error { constructor(message, errorCode) { super(message); this.errorCode = errorCode; } }; var SmartApiEnhanced = class extends SmartAPI { constructor(params, apiRetryDelayMs, logger) { super(params); this.apiRetryDelayMs = apiRetryDelayMs; this.logger = logger; this.apiRetryDelayMs = apiRetryDelayMs; this.logger = logger; } getData(response) { if (response.status && response.errorcode === "") return response.data; this.logger?.e("smart api error response", response); throw new SmartApiError(response.message, response.errorcode); } async retryCallback(callback) { let response = await callback(); if ("status" in response && response.status === 500) { this.logger?.w("smart api 500 error. retry once"); await new Promise((resolve) => setTimeout(resolve, this.apiRetryDelayMs)); response = await callback(); } return this.getData(response); } async generateSession_e(clientCode, clientMpin, totp) { const response = await this.generateSession( clientCode, clientMpin, totp ); return this.getData(response); } async logout_e(clientCode) { const response = await this.logout(clientCode); return this.getData(response); } fetchProfile() { return this.retryCallback(() => this.getProfile()); } fetchRMS() { return this.retryCallback(() => this.getRMS()); } fetchOrderBook() { return this.retryCallback(() => this.getOrderBook()); } async fetchMarketData(params) { const { mode = "LTP", exchangeTokens } = params; for (const prop in exchangeTokens) { exchangeTokens[prop] = [...new Set(exchangeTokens[prop])]; } const responseData = await this.retryCallback( () => this.marketData({ mode, exchangeTokens }) ); return responseData.fetched; } fetchTokensLtp(exchangeTokens) { return this.fetchMarketData({ mode: "LTP", exchangeTokens }); } fetchTokensOhlc(exchangeTokens) { return this.fetchMarketData({ mode: "OHLC", exchangeTokens }); } fetchTokensQuote(exchangeTokens) { return this.fetchMarketData({ mode: "FULL", exchangeTokens }); } placeOrder_e(params) { return this.retryCallback(() => this.placeOrder(params)); } fetchCandlesDatetimeString(date) { return format(date, "yyyy-MM-dd HH:mm"); } async fetchCandleData(params) { const { symboltoken, fromdate, todate, exchange = "NSE", interval = "FIFTEEN_MINUTE" } = params; const responseData = await this.retryCallback( () => this.getCandleData({ symboltoken, fromdate: this.fetchCandlesDatetimeString(fromdate), todate: this.fetchCandlesDatetimeString(todate), exchange, interval }) ); return responseData.map((data) => { const date = new Date(data[0]), open = data[1], high = data[2], low = data[3], close = data[4]; return new OHLCData(date, +open, +high, +low, +close); }); } async fetchCompletedOrders(orderResponses) { const ordersBook = await this.fetchOrderBook(); if (ordersBook == null || ordersBook.length <= 0) { this.logger?.c("failed to get orders book"); return null; } const orderIds = orderResponses.map((v) => v.orderid); const filteredOrders = ordersBook.filter( (v) => orderIds.includes(v.orderid) ); if (filteredOrders == null || filteredOrders.length <= 0) { this.logger?.w("specific orders not exist in order book", orderIds); return null; } const completedOrders = filteredOrders.filter( (v) => v.status === "complete" ); if (completedOrders.length <= 0) { this.logger?.c("all orders are not completed", orderIds); return null; } return completedOrders; } async fetchOptionGreeks(params) { const response = await this.optionGreek(params); return this.getData(response); } async fetchGainersLosers(params) { const response = await this.gainersLosers(params); return this.getData(response); } async fetchPutCallRatio() { const response = await this.putCallRatio(); return this.getData(response); } async fetchOIBuildup(params) { const response = await this.oIBuildup(params); return this.getData(response); } }; export { SmartApiError, SmartApiEnhanced };