Skip to content

API Reference


API Reference / PositionMonitor

Class: PositionMonitor ​

Defined in: src/execution/PositionMonitor.ts:54

Turns a market feed into exit signals.

The WebSocket is the real-time truth source — polling REST for the price of an open position is both slower and rate-limited, so exit logic belongs here, driven by ticks.

This class only decides. It never places an order, so a caller can wire it to a live exit, a paper log, or an alert without changing it. See examples/ws-execution.ts for the full loop.

Extends ​

  • EventEmitter

Constructors ​

Constructor ​

new PositionMonitor(options?): PositionMonitor

Defined in: node_modules/@types/node/events.d.ts:90

Parameters ​

options? ​

EventEmitterOptions

Returns ​

PositionMonitor

Inherited from ​

EventEmitter.constructor

Methods ​

track() ​

track(position): this

Defined in: src/execution/PositionMonitor.ts:60

Starts monitoring a position. Re-adding the same key resets its trail.

Parameters ​

position ​

MonitoredPosition

Returns ​

this


untrack() ​

untrack(exchangeSegment, securityId): this

Defined in: src/execution/PositionMonitor.ts:80

Parameters ​

exchangeSegment ​

string

securityId ​

string

Returns ​

this


tracked() ​

tracked(): MonitoredPosition[]

Defined in: src/execution/PositionMonitor.ts:88

Returns ​

MonitoredPosition[]


onTick() ​

onTick(event): void

Defined in: src/execution/PositionMonitor.ts:96

Feeds one market tick in. Wire this to client.ws.market.on("tick", …). Ticks for untracked instruments are ignored.

Parameters ​

event ​

MarketFeedEvent

Returns ​

void


on() ​

on<K>(event, listener): this

Defined in: src/execution/PositionMonitor.ts:179

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

js
server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a

Type Parameters ​

K ​

K extends keyof PositionMonitorEvents

Parameters ​

event ​

K

listener ​

PositionMonitorEvents[K]

The callback function

Returns ​

this

Since ​

v0.1.101

Overrides ​

EventEmitter.on


emit() ​

emit<K>(event, ...args): boolean

Defined in: src/execution/PositionMonitor.ts:186

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

Returns true if the event had listeners, false otherwise.

js
import { EventEmitter } from 'node:events';
const myEmitter = new EventEmitter();

// First listener
myEmitter.on('event', function firstListener() {
  console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
  const parameters = args.join(', ');
  console.log(`event with parameters ${parameters} in third listener`);
});

console.log(myEmitter.listeners('event'));

myEmitter.emit('event', 1, 2, 3, 4, 5);

// Prints:
// [
//   [Function: firstListener],
//   [Function: secondListener],
//   [Function: thirdListener]
// ]
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener

Type Parameters ​

K ​

K extends keyof PositionMonitorEvents

Parameters ​

event ​

K

args ​

...Parameters<PositionMonitorEvents[K]>

Returns ​

boolean

Since ​

v0.1.26

Overrides ​

EventEmitter.emit

Community project — not affiliated with Dhan. MIT License.