smartapi-typescript
Version:
TypeScript library for Angel One SmartAPI broker API
236 lines • 9.61 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Instruments = void 0;
const axios_1 = __importDefault(require("axios"));
const apiUrls_1 = require("../../constants/apiUrls");
const http = __importStar(require("../../utils/http"));
/**
* Instruments module for SmartAPI
* Handles operations related to market instruments, scrips and LTP data
*/
class Instruments {
/**
* Initialize instruments module
*/
constructor(auth, httpClient, debug = false) {
this.auth = auth;
this.httpClient = httpClient;
this.debug = debug;
}
/**
* Log debug messages if debug mode is enabled
*/
log(message, data) {
if (this.debug) {
console.log(`[SmartAPI:Instruments] ${message}`);
if (data) {
console.log(data);
}
}
}
/**
* Fetch the complete instrument list with all tradable instruments
* This provides a consolidated, import-ready JSON list of instruments across all exchanges
*
* @returns Array of instrument data
*/
getInstruments() {
return __awaiter(this, void 0, void 0, function* () {
try {
this.log('Fetching instrument master list');
const response = yield axios_1.default.get(apiUrls_1.API_URLS.INSTRUMENT_MASTER);
if (Array.isArray(response.data)) {
return {
status: true,
message: 'Instruments list fetched successfully',
data: response.data
};
}
else {
return {
status: false,
message: 'Invalid response format from instrument list API'
};
}
}
catch (error) {
this.log('Fetch instruments list failed', error);
return {
status: false,
message: `Failed to fetch instruments list: ${error instanceof Error ? error.message : String(error)}`
};
}
});
}
/**
* Fetch LTP (Last Traded Price) data for a specific instrument
*
* @param exchange Exchange name (e.g., NSE, BSE, NFO)
* @param symbolToken Symbol token/ID
* @param tradingSymbol Trading symbol
* @param options Network configuration options
* @returns Last traded price data
*/
getLtp(exchange, symbolToken, tradingSymbol, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
const params = {
exchange,
symboltoken: symbolToken,
tradingsymbol: tradingSymbol
};
this.log('Fetching LTP data', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.LTP_DATA}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get LTP data failed', error);
const retryOperation = () => this.getLtp(exchange, symbolToken, tradingSymbol, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Search for scrips by name or keyword
*
* @param exchange Exchange name (e.g., NSE, BSE, NFO)
* @param searchQuery Search keyword or partial symbol name
* @param options Network configuration options
* @returns List of matching scrips with their tokens
*/
searchScrip(exchange, searchQuery, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
if (!searchQuery || !exchange) {
return {
status: false,
message: 'Exchange and search query are required'
};
}
const params = {
exchange,
searchscrip: searchQuery
};
this.log('Searching for scrips', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.SEARCH_SCRIP}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Search scrip failed', error);
const retryOperation = () => this.searchScrip(exchange, searchQuery, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get list of NSE scrips allowed for intraday trading
* This provides a list of scripts that are allowed for intraday (MIS) trading on NSE
* along with their margin multipliers
*
* @param options Network configuration options
* @returns List of NSE scrips allowed for intraday trading with their margin multipliers
*/
getNseIntradayScrips(options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
this.log('Fetching NSE intraday scrips');
try {
return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.NSE_INTRADAY_SCRIPS}`, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get NSE intraday scrips failed', error);
const retryOperation = () => this.getNseIntradayScrips(options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get list of BSE scrips allowed for intraday trading
* This provides a list of scripts that are allowed for intraday (MIS) trading on BSE
* along with their margin multipliers
*
* @param options Network configuration options
* @returns List of BSE scrips allowed for intraday trading with their margin multipliers
*/
getBseIntradayScrips(options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
this.log('Fetching BSE intraday scrips');
try {
return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.BSE_INTRADAY_SCRIPS}`, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get BSE intraday scrips failed', error);
const retryOperation = () => this.getBseIntradayScrips(options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
}
exports.Instruments = Instruments;
//# sourceMappingURL=index.js.map