smartapi-typescript
Version:
TypeScript library for Angel One SmartAPI broker API
233 lines • 9.49 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.GTT = void 0;
const apiUrls_1 = require("../../constants/apiUrls");
const types_1 = require("../../types");
const http = __importStar(require("../../utils/http"));
/**
* GTT (Good Till Triggered) module for SmartAPI
* Handles creating, modifying, and cancelling GTT rules
*/
class GTT {
/**
* Initialize GTT 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:GTT] ${message}`);
if (data) {
console.log(data);
}
}
}
/**
* Create GTT (Good Till Triggered) order rule
*
* GTT orders allow you to set trigger conditions based on price movements.
* When a trigger price is hit, your order will be automatically placed.
* Currently supported only for NSE and BSE exchanges, with DELIVERY and MARGIN product types.
*
* @param params GTT order creation parameters
* @param options Network configuration options
* @returns GTT rule creation response with rule ID
*/
createRule(params, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
// Validate exchange - only NSE and BSE supported
if (params.exchange !== types_1.Exchange.NSE && params.exchange !== types_1.Exchange.BSE) {
return {
status: false,
message: types_1.GTTErrorCodes.AB9010,
errorcode: 'AB9010'
};
}
// Validate product type - only DELIVERY and MARGIN supported
if (params.producttype !== types_1.ProductType.DELIVERY && params.producttype !== types_1.ProductType.MARGIN) {
return {
status: false,
message: types_1.GTTErrorCodes.AB9015,
errorcode: 'AB9015'
};
}
this.log('Creating GTT rule', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.GTT_CREATE_RULE}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Create GTT rule failed', error);
const retryOperation = () => this.createRule(params, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Modify an existing GTT rule
*
* @param params GTT rule modification parameters
* @param options Network configuration options
* @returns GTT rule modification response with rule ID
*/
modifyRule(params, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.auth.isAuthenticated()) {
return {
status: false,
message: 'Not authenticated. Please login first.'
};
}
// Validate exchange - only NSE and BSE supported
if (params.exchange !== types_1.Exchange.NSE && params.exchange !== types_1.Exchange.BSE) {
return {
status: false,
message: types_1.GTTErrorCodes.AB9010,
errorcode: 'AB9010'
};
}
this.log('Modifying GTT rule', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.GTT_MODIFY_RULE}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Modify GTT rule failed', error);
const retryOperation = () => this.modifyRule(params, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Cancel/Delete a GTT rule
*
* @param params GTT rule cancellation parameters
* @param options Network configuration options
* @returns GTT rule cancellation response with rule ID
*/
cancelRule(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('Cancelling GTT rule', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.GTT_CANCEL_RULE}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Cancel GTT rule failed', error);
const retryOperation = () => this.cancelRule(params, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get details of a specific GTT rule
*
* @param id GTT rule ID
* @param options Network configuration options
* @returns GTT rule details
*/
getRuleDetails(id, 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 GTT rule details', { id });
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.GTT_RULE_DETAILS}`, { id }, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get GTT rule details failed', error);
const retryOperation = () => this.getRuleDetails(id, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
/**
* Get list of GTT rules with pagination and status filtering options
*
* @param params GTT rule list parameters with status filters and pagination
* @param options Network configuration options
* @returns List of GTT rules
*/
getRuleList(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('Fetching GTT rules list', params);
try {
return yield http.post(`${apiUrls_1.API_URLS.BASE_URL}${apiUrls_1.API_URLS.GTT_RULE_LIST}`, params, this.auth.getHeaders(options));
}
catch (error) {
this.log('Get GTT rules list failed', error);
const retryOperation = () => this.getRuleList(params, options);
return this.auth.handleApiError(error, retryOperation);
}
});
}
}
exports.GTT = GTT;
//# sourceMappingURL=index.js.map