UNPKG

smartapi-typescript

Version:

TypeScript library for Angel One SmartAPI broker API

158 lines 6.72 kB
"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.Brokerage = void 0; const apiUrls_1 = require("../../constants/apiUrls"); const http = __importStar(require("../../utils/http")); /** * Brokerage module for SmartAPI * Handles brokerage calculation functionality */ class Brokerage { /** * Initialize brokerage 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:Brokerage] ${message}`); if (data) { console.log(data); } } } /** * Calculate estimated brokerage charges for one or more orders * * The Brokerage Calculator API is used to calculate brokerage charges, taxes and other fees * that will be incurred by the user when placing trades. This allows users to estimate * the total transaction cost before placing an order. * * @param params Order parameters to calculate brokerage for * @param options Network configuration options * @returns Estimated brokerage charges and breakdown */ calculate(params, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } // Validate that at least one order is provided if (!params.orders || params.orders.length === 0) { return { status: false, message: 'At least one order must be provided for brokerage calculation' }; } this.log('Calculating brokerage', params); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.BROKERAGE_CALCULATOR}`, params, this.auth.getHeaders(options)); } catch (error) { this.log('Brokerage calculation failed', error); const retryOperation = () => this.calculate(params, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Calculate margin requirements for a basket of positions * * The Margin Calculator API delivers real-time margin calculations for a basket of positions. * This helps in understanding the margin requirements for various trading scenarios. * * @param params Position parameters to calculate margin for * @param options Network configuration options * @returns Calculated margin requirements */ calculateMargin(params, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } // Validate that at least one position is provided if (!params.positions || params.positions.length === 0) { return { status: false, message: 'At least one position must be provided for margin calculation' }; } // Validate that no more than 50 positions are provided (API limit) if (params.positions.length > 50) { return { status: false, message: 'Maximum 50 positions can be provided in a single request' }; } // Set default orderType as "LIMIT" if not provided for any position params.positions = params.positions.map(position => { return Object.assign(Object.assign({}, position), { orderType: position.orderType || 'LIMIT' }); }); this.log('Calculating margin', params); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.MARGIN_CALCULATOR}`, params, this.auth.getHeaders(options)); } catch (error) { this.log('Margin calculation failed', error); const retryOperation = () => this.calculateMargin(params, options); return this.auth.handleApiError(error, retryOperation); } }); } } exports.Brokerage = Brokerage; //# sourceMappingURL=index.js.map