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.

95 lines (92 loc) 2.9 kB
import { OrderHelper } from "./chunk-O6PJTRR5.mjs"; // src/helpers/dummy_order_helper.ts var DummyOrderHelper = class extends OrderHelper { async handleScripsTrendReversal(scripTrendMapData) { this.logger.d("run close open orders"); await this.closeTrendOpenOrders(scripTrendMapData); this.logger.d("run place new orders"); await this.placeNewTrendOrders(scripTrendMapData); } async handleDaySquareoff() { const openOrders = await this.getOpenOrders({}); if (openOrders == null) return; return await this.processOrdersSquareoff(openOrders, "squareoff"); } async processOrdersSquareoff(openOrders, status) { const scripLtpMap = await this.getOrdersLtpMap(openOrders); const squareoffOrders = await this.generateCloseOrderParams( openOrders, status, scripLtpMap ); if (squareoffOrders == null) return false; await this.updateOrdersClose(squareoffOrders); return true; } async handleOpenOrdersTracking() { const orderCloseTracks = await this.getOrderCloseTrackDatas(); if (orderCloseTracks == null) return; const closeOrders = orderCloseTracks.map( ({ order, status, ltp }) => { return { id: order.id, symbol: order.symbol, candleInterval: order.candleInterval, exitOrderid: this.generateOrderId(), exitPrice: ltp, status }; } ); await this.updateOrdersClose(closeOrders); } async closeTrendOpenOrders({ scripTrendMap }) { const names = Array.from(scripTrendMap.values()).map( (v) => v.indexScrip.name ); const openOrders = await this.getOpenOrders({ names }); if (openOrders == null) return; const scripLtpMap = await this.getOrdersLtpMap(openOrders); const closeOrders = await this.generateCloseOrderParams( openOrders, "closed", scripLtpMap ); if (closeOrders == null) return false; await this.updateOrdersClose(closeOrders); return true; } async placeNewTrendOrders(scripTrendMapData) { let newOrders = await this.getTrendNewOrders(scripTrendMapData); if (newOrders == null) return; newOrders = await this.updateEntryPriceOfOrders(newOrders); await this.createOrders(newOrders, scripTrendMapData.candleInterval); } async generateCloseOrderParams(openOrders, status, scripLtpMap) { const closeOrders = []; for (const { id, symbol, candleInterval } of openOrders) { const exitPrice = scripLtpMap.get(symbol)?.ltp; if (!exitPrice) continue; closeOrders.push({ id, symbol, candleInterval, exitOrderid: this.generateOrderId(), exitPrice, status }); } if (closeOrders.length == 0) { this.logger.w(`closeOrders empty. no ${status} orders`); return; } return closeOrders; } }; export { DummyOrderHelper };