smartapi-typescript
Version:
TypeScript library for Angel One SmartAPI broker API
203 lines • 8.07 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Portfolio = void 0;
const apiUrls_1 = require("../../constants/apiUrls");
const http = __importStar(require("../../utils/http"));
/**
* Portfolio module for SmartAPI
* Handles positions, holdings, funds and related functionality
*/
class Portfolio {
/**
* Initialize portfolio 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:Portfolio] ${message}`);
if (data) {
console.log(data);
}
}
}
/**
* Get user's current positions
* @param options Network configuration options
* @returns Positions data
*/
getPositions(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 positions');
try {
return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.POSITIONS}`, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get positions failed', error);
const retryOperation = () => this.getPositions(options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get user's holdings
* @param options Network configuration options
* @returns Holdings data
*/
getHoldings(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 holdings');
try {
return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.HOLDINGS}`, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get holdings failed', error);
const retryOperation = () => this.getHoldings(options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get all holdings with comprehensive portfolio summary
* This endpoint offers a more comprehensive view of the entire investments, including
* individual stock holdings and a summary of total investments in the "totalholding" section
*
* @param options Network configuration options
* @returns All holdings data with portfolio summary
*/
getAllHoldings(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 all holdings with portfolio summary');
try {
return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.ALL_HOLDINGS}`, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get all holdings failed', error);
const retryOperation = () => this.getAllHoldings(options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get funds and margin details (RMS limits)
* The GET Request to RMS returns fund, cash and margin information
* of the user for equity and commodity segments.
*
* @param options Network configuration options
* @returns Funds and RMS data
*/
getFunds(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 funds and RMS data');
try {
return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.FUNDS}`, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get funds failed', error);
const retryOperation = () => this.getFunds(options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Convert a position from one product type to another
* For example, convert from INTRADAY to DELIVERY or vice versa
*
* @param params Position conversion parameters
* @param options Network configuration options
* @returns Conversion response
*/
convertPosition(params, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
this.log('Converting position', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.CONVERT_POSITION}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Position conversion failed', error);
const retryOperation = () => this.convertPosition(params, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
}
exports.Portfolio = Portfolio;
//# sourceMappingURL=index.js.map