UNPKG

smartapi-typescript

Version:

TypeScript library for Angel One SmartAPI broker API

320 lines 13.2 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.Orders = void 0; const apiUrls_1 = require("../../constants/apiUrls"); const types_1 = require("../../types"); const errorCodes_1 = require("../../constants/errorCodes"); const http = __importStar(require("../../utils/http")); /** * Orders module for SmartAPI * Handles placing, modifying, cancelling orders and fetching order details */ class Orders { /** * Initialize orders 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:Orders] ${message}`); if (data) { console.log(data); } } } /** * Place a normal order * @param params Order parameters * @param options Network configuration options * @returns Order response with orderid and uniqueorderid */ placeOrder(params, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } // Check order tag length to avoid AB4008 error if (params.ordertag && params.ordertag.length > 20) { return { status: false, message: errorCodes_1.ERROR_CODES.AB4008, errorcode: 'AB4008' }; } this.log('Placing order', params); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.PLACE_ORDER}`, params, this.auth.getHeaders(options)); } catch (error) { this.log('Place order failed', error); const retryOperation = () => this.placeOrder(params, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Place a bracket order * A bracket order is a special order that includes an entry order along with target and stoploss orders * @param params Bracket order parameters * @param options Network configuration options * @returns Order response with orderid and uniqueorderid */ placeBracketOrder(params, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } // Bracket orders must use BO product type const bracketParams = Object.assign(Object.assign({}, params), { producttype: types_1.ProductType.BO, variety: types_1.Variety.ROBO // Bracket order requires ROBO variety per documentation }); // Check order tag length to avoid AB4008 error if (bracketParams.ordertag && bracketParams.ordertag.length > 20) { return { status: false, message: errorCodes_1.ERROR_CODES.AB4008, errorcode: 'AB4008' }; } this.log('Placing bracket order', bracketParams); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.PLACE_ORDER}`, bracketParams, this.auth.getHeaders(options)); } catch (error) { this.log('Place bracket order failed', error); const retryOperation = () => this.placeBracketOrder(params, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Place a cover order * A cover order is a special order that includes a stoploss order along with the main order * @param params Cover order parameters * @param options Network configuration options * @returns Order response with orderid and uniqueorderid */ placeCoverOrder(params, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } // Cover orders must use CO product type const coverParams = Object.assign(Object.assign({}, params), { producttype: types_1.ProductType.CO, variety: types_1.Variety.NORMAL // CO requires NORMAL variety }); // Check order tag length to avoid AB4008 error if (coverParams.ordertag && coverParams.ordertag.length > 20) { return { status: false, message: errorCodes_1.ERROR_CODES.AB4008, errorcode: 'AB4008' }; } this.log('Placing cover order', coverParams); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.PLACE_ORDER}`, coverParams, this.auth.getHeaders(options)); } catch (error) { this.log('Place cover order failed', error); const retryOperation = () => this.placeCoverOrder(params, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Modify an existing order * @param params Order parameters with order id * @param options Network configuration options * @returns Order modification response with orderid and uniqueorderid */ modifyOrder(params, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } // Check order tag length to avoid AB4008 error if (params.ordertag && params.ordertag.length > 20) { return { status: false, message: errorCodes_1.ERROR_CODES.AB4008, errorcode: 'AB4008' }; } this.log('Modifying order', params); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.MODIFY_ORDER}`, params, this.auth.getHeaders(options)); } catch (error) { this.log('Modify order failed', error); const retryOperation = () => this.modifyOrder(params, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Cancel an order * @param orderId Order ID to cancel * @param variety Order variety * @param options Network configuration options * @returns Order cancellation response with orderid and uniqueorderid */ cancelOrder(orderId, variety, options) { return __awaiter(this, void 0, void 0, function* () { if (!this.auth.isAuthenticated()) { return { status: false, message: 'Not authenticated. Please login first.' }; } const params = { orderid: orderId, variety }; this.log('Cancelling order', params); try { return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.CANCEL_ORDER}`, params, this.auth.getHeaders(options)); } catch (error) { this.log('Cancel order failed', error); const retryOperation = () => this.cancelOrder(orderId, variety, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Get order book (list of orders) * @param options Network configuration options * @returns Order book */ getOrderBook(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 order book'); try { return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.ORDER_BOOK}`, this.auth.getHeaders(options)); } catch (error) { this.log('Get order book failed', error); const retryOperation = () => this.getOrderBook(options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Get details of a specific order by uniqueorderid * * @param uniqueOrderId Unique order ID received in order responses * @param options Network configuration options * @returns Order details response */ getOrderDetails(uniqueOrderId, 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 order details', { uniqueOrderId }); try { return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.ORDER_DETAILS}${uniqueOrderId}`, this.auth.getHeaders(options)); } catch (error) { this.log('Get order details failed', error); const retryOperation = () => this.getOrderDetails(uniqueOrderId, options); return this.auth.handleApiError(error, retryOperation); } }); } /** * Get trade book (list of trades/executions) * @param options Network configuration options * @returns Trade book */ getTradeBook(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 trade book'); try { return yield http.get(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.TRADE_BOOK}`, this.auth.getHeaders(options)); } catch (error) { this.log('Get trade book failed', error); const retryOperation = () => this.getTradeBook(options); return this.auth.handleApiError(error, retryOperation); } }); } } exports.Orders = Orders; //# sourceMappingURL=index.js.map