UNPKG

@thoshpathi/utils-smartapi-order

Version:

Utility functions for placing live and dummy orders using Angel One's SmartAPI, with helper methods for streamlined trading workflows.

158 lines (154 loc) 5.43 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/option_scrip_utils.ts var option_scrip_utils_exports = {}; __export(option_scrip_utils_exports, { default: () => getSpreadScripData, findNearestPremiumData: () => findNearestPremiumData, generateNakedTrendOptionSymbols: () => generateNakedTrendOptionSymbols, generateOptionLabels: () => generateOptionLabels, getNakedScripData: () => getNakedScripData, getSpreadScripData: () => getSpreadScripData }); module.exports = __toCommonJS(option_scrip_utils_exports); var import_utils_core = require("@thoshpathi/utils-core"); // src/config_utils.ts var spreadConfig = { BUY_call_spread: ["BUY", "SELL"], BUY_put_spread: ["SELL", "BUY"], SELL_call_spread: ["SELL", "BUY"], SELL_put_spread: ["BUY", "SELL"] }; function getSpreadConfig(params) { const { trend, spreadType } = params; return spreadConfig[`${trend}_${spreadType}`]; } var nakedConfig = { BUY_BUY: ["BUY", "CE"], BUY_SELL: ["BUY", "PE"], SELL_BUY: ["SELL", "PE"], SELL_SELL: ["SELL", "CE"] }; function getNakedConfig(params) { const { trend, transaction } = params; return nakedConfig[`${transaction}_${trend}`]; } var spreadOptionConfig = { call_spread: "CE", put_spread: "PE" }; function getOptionTypeOfSpread(spreadType) { return spreadOptionConfig[spreadType]; } // src/option_scrip_utils.ts function getSpreadScripData(params) { const { strike, trend, spreadType, contractPrefix, leg1Gap, leg2Gap } = params; const spreadScripMap = /* @__PURE__ */ new Map(); const [leg1Trend, leg2Trend] = getSpreadConfig({ trend, spreadType }); if (leg1Trend && leg2Trend) { const optionType = getOptionTypeOfSpread(spreadType); const symbol1 = getOptionScripOfLabel( contractPrefix, strike, leg1Gap, optionType ); const symbol2 = getOptionScripOfLabel( contractPrefix, strike, leg2Gap, optionType ); spreadScripMap.set(symbol1, { symbol: symbol1, trend: leg1Trend }).set(symbol2, { symbol: symbol2, trend: leg2Trend }); } return spreadScripMap; } function getNakedScripData(params) { const { strike, trend, transaction, contractPrefix, legGap } = params; const nakedScripMap = /* @__PURE__ */ new Map(); const tradeConfig = getNakedConfig({ transaction, trend }); if (Array.isArray(tradeConfig)) { const symbol = getOptionScripOfLabel( contractPrefix, strike, legGap, tradeConfig[1] ); nakedScripMap.set(symbol, { symbol, trend: tradeConfig[0] }); } return nakedScripMap; } function generateNakedTrendOptionSymbols(params) { const { strike: atmStrike, trend, transaction, contractPrefix, roundTo = 100, length = 8 } = params; const tradeConfig = getNakedConfig({ transaction, trend }); const scripSymbols = (0, import_utils_core.range)(-length, length + 1, 1).map((n) => { const strike = atmStrike + n * roundTo; return getOptionSymbol(contractPrefix, strike, tradeConfig[1]); }); return { trend: tradeConfig[0], symbols: scripSymbols }; } function findNearestPremiumData(scripLtpMap, premium) { const scripLtpArr = Array.from(scripLtpMap.values()); return scripLtpArr.reduce((nearest, current) => { const currentDiff = Math.abs(current.ltp - premium); const nearestDiff = Math.abs(nearest.ltp - premium); return currentDiff < nearestDiff ? current : nearest; }, scripLtpArr[0]); } function generateOptionLabels(gap = 10, step = 100) { const numbers = []; for (let n = 1; n <= gap; n++) numbers.push(n * step); const otmLabels = numbers.map((n) => `${n}-OTM`); const itmLabels = numbers.reverse().map((n) => `${n}-ITM`); return [...itmLabels, "ATM", ...otmLabels]; } function getOptionScripOfLabel(prefix, strike, optionLabel, optionType) { if (optionLabel === "ATM") return `${prefix}${strike}${optionType}`; const match = optionLabel.match(/^(\d+)-(ITM|OTM)$/); if (!match) throw new Error(`Invalid optionLabel format: ${optionLabel}`); const offset = parseInt(match[1], 10); const labelType = match[2]; const isCall = optionType === "CE"; const isItm = labelType === "ITM"; const isOtm = labelType === "OTM"; if (isCall) { strike += isOtm ? offset : -offset; } else { strike += isOtm ? -offset : offset; } return getOptionSymbol(prefix, strike, optionType); } function getOptionSymbol(prefix, strike, optionType) { return `${prefix}${strike}${optionType}`.toUpperCase(); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { findNearestPremiumData, generateNakedTrendOptionSymbols, generateOptionLabels, getNakedScripData, getSpreadScripData });