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.

104 lines (101 loc) 3.4 kB
import { getNakedConfig, getOptionTypeOfSpread, getSpreadConfig } from "./chunk-YUA7R3AZ.mjs"; // src/option_scrip_utils.ts import { range } from "@thoshpathi/utils-core"; 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 = 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(); } export { getSpreadScripData, getNakedScripData, generateNakedTrendOptionSymbols, findNearestPremiumData, generateOptionLabels };