firstock 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/Readme.md +1 -6
- package/dist/Classes/Firstock.js +2 -1
- package/dist/index.js +2 -6
- package/dist/shared/commonFunction.js +14 -1
- package/dist/test.js +2 -2
- package/package.json +1 -1
- package/Classes/AFirstock.ts +0 -371
- package/Classes/Firstock.ts +0 -1952
- package/dist/Classes/AFirstock.d.ts +0 -227
- package/dist/Classes/Firstock.d.ts +0 -918
- package/dist/index.d.ts +0 -2
- package/dist/shared/commonFunction.d.ts +0 -26
- package/dist/shared/constant.d.ts +0 -2
- package/dist/test.d.ts +0 -1
- package/index.js +0 -4
- package/index.ts +0 -4
- package/shared/commonFunction.ts +0 -99
- package/shared/constant.ts +0 -4
- package/test.ts +0 -367
- package/tsconfig.json +0 -15
package/Classes/Firstock.ts
DELETED
|
@@ -1,1952 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance, AxiosError } from "axios";
|
|
2
|
-
import * as sha256 from "sha256";
|
|
3
|
-
import AFirstock from "./AFirstock";
|
|
4
|
-
import { saveData, readData, handleError, errorMessageMapping, checkifUserLoggedIn } from "../shared/commonFunction";
|
|
5
|
-
import * as Commonfunctions from "../shared/commonFunction";
|
|
6
|
-
import { API_LINK } from "../shared/constant";
|
|
7
|
-
|
|
8
|
-
interface Response {
|
|
9
|
-
data: {
|
|
10
|
-
[key: string]: any;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
interface LoginParams {
|
|
15
|
-
userId: string;
|
|
16
|
-
password: string;
|
|
17
|
-
TOTP: string;
|
|
18
|
-
vendorCode: string;
|
|
19
|
-
apiKey: string;
|
|
20
|
-
[key: string]: any;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface ConfigData {
|
|
24
|
-
[key: string]: {
|
|
25
|
-
jKey: string;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface PlaceOrderParams {
|
|
30
|
-
userId: string;
|
|
31
|
-
exchange: string;
|
|
32
|
-
tradingSymbol: string;
|
|
33
|
-
quantity: string | number;
|
|
34
|
-
price: string | number;
|
|
35
|
-
product: string;
|
|
36
|
-
transactionType: string;
|
|
37
|
-
priceType: string;
|
|
38
|
-
retention: string;
|
|
39
|
-
remarks: string;
|
|
40
|
-
triggerPrice: string;
|
|
41
|
-
[key: string]: any;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface CancelOrderParams {
|
|
45
|
-
userId: string;
|
|
46
|
-
orderNumber: string;
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface ModifyOrderParams {
|
|
51
|
-
userId: string;
|
|
52
|
-
orderNumber: string;
|
|
53
|
-
price: string | number;
|
|
54
|
-
quantity: string | number;
|
|
55
|
-
triggerPrice: string | number;
|
|
56
|
-
tradingSymbol: string;
|
|
57
|
-
exchange: string;
|
|
58
|
-
priceType: string;
|
|
59
|
-
product: string;
|
|
60
|
-
retention: string;
|
|
61
|
-
[key: string]: any;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
interface BasketItem {
|
|
65
|
-
exchange: string;
|
|
66
|
-
transactionType: string;
|
|
67
|
-
product: string;
|
|
68
|
-
tradingSymbol: string;
|
|
69
|
-
quantity: string | number;
|
|
70
|
-
priceType: string;
|
|
71
|
-
price: string | number;
|
|
72
|
-
[key: string]: any;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
interface BasketMarginParams {
|
|
76
|
-
userId: string;
|
|
77
|
-
BasketList_Params: BasketItem[];
|
|
78
|
-
[key: string]: any;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
interface SingleOrderHistoryParams {
|
|
82
|
-
userId: string;
|
|
83
|
-
orderNumber: string;
|
|
84
|
-
[key: string]: any;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
interface UserDetailsParams {
|
|
88
|
-
userId: string;
|
|
89
|
-
[key: string]: any;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
interface OrderMarginParams {
|
|
93
|
-
userId: string;
|
|
94
|
-
exchange: string;
|
|
95
|
-
tradingSymbol: string;
|
|
96
|
-
quantity: string | number;
|
|
97
|
-
price: string | number;
|
|
98
|
-
product: string;
|
|
99
|
-
transactionType: string;
|
|
100
|
-
priceType: string;
|
|
101
|
-
[key: string]: any;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
interface ProductConversionParams {
|
|
105
|
-
userId: string;
|
|
106
|
-
exchange: string;
|
|
107
|
-
tradingSymbol: string;
|
|
108
|
-
quantity: string | number;
|
|
109
|
-
product: string;
|
|
110
|
-
previousProduct: string;
|
|
111
|
-
transactionType?: string;
|
|
112
|
-
positionType?: string;
|
|
113
|
-
[key: string]: any;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
interface OrderBookParams {
|
|
117
|
-
userId: string;
|
|
118
|
-
[key: string]: any;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
interface TradeBookParams {
|
|
122
|
-
userId: string;
|
|
123
|
-
[key: string]: any;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
interface PositionsBookParams {
|
|
127
|
-
userId: string;
|
|
128
|
-
[key: string]: any;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
interface MultiQuoteItem {
|
|
132
|
-
exchange: string;
|
|
133
|
-
tradingSymbol: string;
|
|
134
|
-
[key: string]: any;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
interface GetMultiQuotesParams {
|
|
138
|
-
userId: string;
|
|
139
|
-
data: MultiQuoteItem[];
|
|
140
|
-
[key: string]: any;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
interface OptionChainParams {
|
|
144
|
-
userId: string;
|
|
145
|
-
exchange: string;
|
|
146
|
-
symbol: string;
|
|
147
|
-
strikePrice: string | number;
|
|
148
|
-
count: string | number;
|
|
149
|
-
expiry?: string;
|
|
150
|
-
[key: string]: any;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
interface GetQuoteLTPParams {
|
|
154
|
-
userId: string;
|
|
155
|
-
exchange: string;
|
|
156
|
-
tradingSymbol: string;
|
|
157
|
-
[key: string]: any;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
interface GetMultiQuotesLTPParams {
|
|
161
|
-
userId: string;
|
|
162
|
-
data: MultiQuoteItem[];
|
|
163
|
-
[key: string]: any;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
interface SearchScriptsParams {
|
|
167
|
-
userId: string;
|
|
168
|
-
stext: string;
|
|
169
|
-
[key: string]: any;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
interface GetSecurityInfoParams {
|
|
173
|
-
userId: string;
|
|
174
|
-
exchange: string;
|
|
175
|
-
tradingSymbol: string;
|
|
176
|
-
[key: string]: any;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
interface HoldingsParams {
|
|
180
|
-
userId: string;
|
|
181
|
-
product?: string;
|
|
182
|
-
[key: string]: any;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
interface LimitsParams {
|
|
186
|
-
userId: string;
|
|
187
|
-
[key: string]: any;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
interface GetQuotesParams {
|
|
191
|
-
userId: string;
|
|
192
|
-
exchange: string;
|
|
193
|
-
tradingSymbol: string;
|
|
194
|
-
[key: string]: any;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
interface GetIndexListParams {
|
|
198
|
-
userId: string;
|
|
199
|
-
exchange: string;
|
|
200
|
-
[key: string]: any;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
interface TimePriceSeriesParams {
|
|
205
|
-
userId: string;
|
|
206
|
-
exchange: string;
|
|
207
|
-
tradingSymbol: string;
|
|
208
|
-
endTime: string;
|
|
209
|
-
startTime: string;
|
|
210
|
-
interval: string | number;
|
|
211
|
-
[key: string]: any;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
interface BrokerageCalculatorParams {
|
|
215
|
-
userId: string;
|
|
216
|
-
exchange: string;
|
|
217
|
-
tradingSymbol: string;
|
|
218
|
-
transactionType: string;
|
|
219
|
-
Product: string;
|
|
220
|
-
quantity: string | number;
|
|
221
|
-
price: string | number;
|
|
222
|
-
strike_price: string | number;
|
|
223
|
-
inst_name: string;
|
|
224
|
-
lot_size: string | number;
|
|
225
|
-
[key: string]: any;
|
|
226
|
-
}
|
|
227
|
-
interface GetExpiryParams {
|
|
228
|
-
userId: string;
|
|
229
|
-
exchange: string;
|
|
230
|
-
tradingSymbol: string;
|
|
231
|
-
[key: string]: any;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const axiosInterceptor: AxiosInstance = axios.create({
|
|
235
|
-
baseURL: API_LINK,
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
class Firstock extends AFirstock {
|
|
239
|
-
private token: string;
|
|
240
|
-
private userId: string;
|
|
241
|
-
private jsonObj: ConfigData = {};
|
|
242
|
-
|
|
243
|
-
constructor() {
|
|
244
|
-
super();
|
|
245
|
-
this.token = "";
|
|
246
|
-
this.userId = "";
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Authenticates the user with Firstock using credentials and retrieves a session token (susertoken).
|
|
252
|
-
*
|
|
253
|
-
* This method sends a secure login request to the Firstock API with SHA256-hashed password and TOTP (if applicable),
|
|
254
|
-
* and stores the received session token locally for future authenticated requests.
|
|
255
|
-
*
|
|
256
|
-
* On success, updates the instance with `token` and `userId`, and saves the token to a configuration file.
|
|
257
|
-
*
|
|
258
|
-
* @param {Object} params - Login parameters.
|
|
259
|
-
* @param {string} params.userId - Firstock user ID (e.g., AB1234).
|
|
260
|
-
* @param {string} params.password - Plain password (will be SHA256 hashed before sending).
|
|
261
|
-
* @param {string} params.TOTP - Two-factor authentication code, if enabled.
|
|
262
|
-
* @param {string} params.vendorCode - Unique vendor code provided by Firstock.
|
|
263
|
-
* @param {string} params.apiKey - API key issued by Firstock.
|
|
264
|
-
*
|
|
265
|
-
* @param {function} callBack - Callback with `(error, result)`:
|
|
266
|
-
* - `error`: Error object if login fails.
|
|
267
|
-
* - `result`: Parsed response containing login information and token if successful.
|
|
268
|
-
*/
|
|
269
|
-
|
|
270
|
-
login(
|
|
271
|
-
{ userId, password, TOTP, vendorCode, apiKey }: LoginParams,
|
|
272
|
-
callBack: (error: Error | null, result: Response | null) => void
|
|
273
|
-
): void {
|
|
274
|
-
const encryptedPassword = sha256.default(password);
|
|
275
|
-
axiosInterceptor
|
|
276
|
-
.post<Response>(`login`, {
|
|
277
|
-
userId,
|
|
278
|
-
password: encryptedPassword,
|
|
279
|
-
TOTP,
|
|
280
|
-
vendorCode,
|
|
281
|
-
apiKey,
|
|
282
|
-
})
|
|
283
|
-
.then((response: { data: Response }) => {
|
|
284
|
-
const { data } = response;
|
|
285
|
-
this.token = data.data.susertoken;
|
|
286
|
-
this.userId = data.data.actid;
|
|
287
|
-
const finished = (error: Error | null) => {
|
|
288
|
-
if (error) {
|
|
289
|
-
callBack(error, null);
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
callBack(null, data);
|
|
293
|
-
};
|
|
294
|
-
readData((err: string | Error | null, jsonData: ConfigData | null) => {
|
|
295
|
-
if (err) {
|
|
296
|
-
if (err instanceof Error && err.message === "Unexpected end of JSON input") {
|
|
297
|
-
let obj: ConfigData = {};
|
|
298
|
-
obj[data.data.actid] = {
|
|
299
|
-
jKey: data.data.susertoken,
|
|
300
|
-
};
|
|
301
|
-
saveData({ ...obj }, "config.json", finished);
|
|
302
|
-
} else {
|
|
303
|
-
callBack(err instanceof Error ? err : new Error(err as string), null);
|
|
304
|
-
}
|
|
305
|
-
} else if (jsonData) {
|
|
306
|
-
jsonData[data.data.actid] = {
|
|
307
|
-
jKey: data.data.susertoken,
|
|
308
|
-
};
|
|
309
|
-
saveData({ ...jsonData }, "config.json", finished);
|
|
310
|
-
} else {
|
|
311
|
-
callBack(new Error("No JSON data returned"), null);
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
})
|
|
315
|
-
.catch((error: AxiosError) => {
|
|
316
|
-
callBack(handleError(error), null);
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Fetches the Option Chain data from the Firstock API.
|
|
321
|
-
*
|
|
322
|
-
* This method retrieves nearby call and put option contracts for a given symbol and strike price.
|
|
323
|
-
* It's useful for constructing an option chain view or for identifying options for trading.
|
|
324
|
-
*
|
|
325
|
-
* The user must be logged in before calling this method (valid `jKey` required).
|
|
326
|
-
*
|
|
327
|
-
* @param {Object} params - Parameters required to fetch the option chain.
|
|
328
|
-
* @param {string} params.userId - Your Firstock user ID (must be same as used during login).
|
|
329
|
-
* @param {string} params.exchange - Exchange name (typically `"NFO"` for NSE options).
|
|
330
|
-
* @param {string} params.symbol - Underlying symbol or index (e.g., `"NIFTY"`).
|
|
331
|
-
* @param {string|number} params.strikePrice - Central strike price to base the option chain on.
|
|
332
|
-
* @param {string|number} params.count - Number of strike levels above and below the central price to retrieve.
|
|
333
|
-
* @param {string} [params.expiry] - (Optional) Expiry date of options in `DDMONYY` format (e.g., `"17APR25"`).
|
|
334
|
-
*
|
|
335
|
-
* @param {function} callBack - A callback function receiving `(error, result)`.
|
|
336
|
-
* On success, `result` contains an array of option contracts:
|
|
337
|
-
* - `exchange`: "NFO"
|
|
338
|
-
* - `lotSize`: Lot size of contract
|
|
339
|
-
* - `optionType`: "CE" or "PE"
|
|
340
|
-
* - `strikePrice`: Strike price of the contract
|
|
341
|
-
* - `tradingSymbol`: Usable symbol to place orders
|
|
342
|
-
* - `lastTradedPrice`: LTP of the option
|
|
343
|
-
*/
|
|
344
|
-
|
|
345
|
-
optionChain(
|
|
346
|
-
params: OptionChainParams,
|
|
347
|
-
callBack: (error: Error | string | null, result: any | null) => void
|
|
348
|
-
): void {
|
|
349
|
-
const currentUserId = params.userId;
|
|
350
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
351
|
-
if (err) {
|
|
352
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
353
|
-
} else if (data) {
|
|
354
|
-
const userId = currentUserId;
|
|
355
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
356
|
-
if (err) {
|
|
357
|
-
callBack(err, null);
|
|
358
|
-
} else if (jKey) {
|
|
359
|
-
axiosInterceptor
|
|
360
|
-
.post(`optionChain`, {
|
|
361
|
-
userId,
|
|
362
|
-
jKey,
|
|
363
|
-
exchange: params.exchange,
|
|
364
|
-
symbol: params.symbol,
|
|
365
|
-
strikePrice: params.strikePrice,
|
|
366
|
-
count: params.count,
|
|
367
|
-
...(params.expiry && { expiry: params.expiry }),
|
|
368
|
-
})
|
|
369
|
-
.then((response) => {
|
|
370
|
-
const { data } = response;
|
|
371
|
-
callBack(null, data);
|
|
372
|
-
})
|
|
373
|
-
.catch((error: AxiosError) => {
|
|
374
|
-
callBack(handleError(error), null);
|
|
375
|
-
});
|
|
376
|
-
} else {
|
|
377
|
-
callBack("No jKey found", null);
|
|
378
|
-
}
|
|
379
|
-
});
|
|
380
|
-
} else {
|
|
381
|
-
callBack("No config data found", null);
|
|
382
|
-
}
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Fetches intraday candlestick data for a given trading symbol and interval.
|
|
387
|
-
*
|
|
388
|
-
* This method provides time-series data (OHLCV) for short-term intervals like 1min, 2min, 5min, etc.
|
|
389
|
-
* It's ideal for charting, intraday analysis, and short-term strategy backtesting.
|
|
390
|
-
*
|
|
391
|
-
* ⚠️ The user must be logged in (valid jKey) before calling this method.
|
|
392
|
-
*
|
|
393
|
-
* @param {Object} params - Parameters for fetching time price series.
|
|
394
|
-
* @param {string} params.userId - Firstock user ID (same as used during login).
|
|
395
|
-
* @param {string} params.exchange - Exchange code (e.g., "NSE", "BSE", "NFO").
|
|
396
|
-
* @param {string} params.tradingSymbol - Symbol or instrument for intraday data (e.g., "NIFTY").
|
|
397
|
-
* @param {string} params.startTime - Start time in `"HH:MM:SS DD-MM-YYYY"` format (24-hour clock).
|
|
398
|
-
* @param {string} params.endTime - End time in `"HH:MM:SS DD-MM-YYYY"` format.
|
|
399
|
-
* @param {string|number} params.interval - Candle interval (e.g., `"1mi"`, `"2mi"`, `"5mi"`).
|
|
400
|
-
*
|
|
401
|
-
* @param {function} callBack - Callback function receiving `(error, result)`.
|
|
402
|
-
* On success, result includes an array of candlestick objects:
|
|
403
|
-
* - `time`: ISO timestamp
|
|
404
|
-
* - `epochTime`: UNIX time
|
|
405
|
-
* - `open`, `high`, `low`, `close`: Prices for the interval
|
|
406
|
-
* - `volume`: Volume during the interval
|
|
407
|
-
* - `oi`: Open interest (if applicable)
|
|
408
|
-
*/
|
|
409
|
-
|
|
410
|
-
timePriceSeries(
|
|
411
|
-
params: TimePriceSeriesParams,
|
|
412
|
-
callBack: (error: Error | string | null, result: any | null) => void
|
|
413
|
-
): void {
|
|
414
|
-
const currentUserId = params.userId;
|
|
415
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
416
|
-
if (err) {
|
|
417
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
418
|
-
} else if (data) {
|
|
419
|
-
const userId = currentUserId;
|
|
420
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
421
|
-
if (err) {
|
|
422
|
-
callBack(err, null);
|
|
423
|
-
} else if (jKey) {
|
|
424
|
-
axiosInterceptor
|
|
425
|
-
.post(`timePriceSeries`, {
|
|
426
|
-
userId,
|
|
427
|
-
jKey,
|
|
428
|
-
exchange: params.exchange,
|
|
429
|
-
tradingSymbol: params.tradingSymbol,
|
|
430
|
-
endTime: params.endTime,
|
|
431
|
-
startTime: params.startTime,
|
|
432
|
-
interval: params.interval,
|
|
433
|
-
})
|
|
434
|
-
.then((response) => {
|
|
435
|
-
const { data } = response;
|
|
436
|
-
callBack(null, data);
|
|
437
|
-
})
|
|
438
|
-
.catch((error: AxiosError) => {
|
|
439
|
-
callBack(handleError(error), null);
|
|
440
|
-
});
|
|
441
|
-
} else {
|
|
442
|
-
callBack("No jKey found", null);
|
|
443
|
-
}
|
|
444
|
-
});
|
|
445
|
-
} else {
|
|
446
|
-
callBack("No config data found", null);
|
|
447
|
-
}
|
|
448
|
-
});
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// Implement other abstract methods from AFirstock
|
|
452
|
-
getPosts(): Promise<any> {
|
|
453
|
-
throw new Error("Method 'getPosts()' must be implemented.");
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
getUsers(): any {
|
|
457
|
-
throw new Error("Method 'getUsers()' must be implemented.");
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
getPostByUserId(): any {
|
|
461
|
-
throw new Error("Method 'getPostByUserId()' must be implemented.");
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* Logs the user out of the Firstock platform and invalidates the session token (`jKey`).
|
|
465
|
-
*
|
|
466
|
-
* This method securely ends the authenticated session by calling the Firstock `/logout` API.
|
|
467
|
-
* It ensures that the session token is no longer valid, preventing unauthorized future requests.
|
|
468
|
-
*
|
|
469
|
-
* The user must be logged in before calling this method. Otherwise, it returns an error.
|
|
470
|
-
*
|
|
471
|
-
* Best Practice: Always log the user out after completing trading activity or on app exit.
|
|
472
|
-
*
|
|
473
|
-
* @param {Object} params - Parameters for logout.
|
|
474
|
-
* @param {string} params.userId - Firstock user ID (same as used during login).
|
|
475
|
-
*
|
|
476
|
-
* @param {function} callBack - Callback function receiving `(error, result)`:
|
|
477
|
-
* - `error`: Error message or `null` on success.
|
|
478
|
-
* - `result`: Logout response object if successful, otherwise `null`.
|
|
479
|
-
*
|
|
480
|
-
* Security:
|
|
481
|
-
* - After successful logout, the jKey/token becomes invalid.
|
|
482
|
-
* - Any further API calls with the same token will be rejected.
|
|
483
|
-
*/
|
|
484
|
-
|
|
485
|
-
logout(
|
|
486
|
-
params: { userId: string },
|
|
487
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
488
|
-
): void {
|
|
489
|
-
const currentUserId: string = params.userId;
|
|
490
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
491
|
-
if (err) {
|
|
492
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
493
|
-
} else if (data) {
|
|
494
|
-
const userId: string = currentUserId;
|
|
495
|
-
checkifUserLoggedIn({ userId, jsonData: data || {} }, (err: string | null, jKey: string | null) => { // Handle null with default empty object
|
|
496
|
-
if (err) {
|
|
497
|
-
callBack(err, null);
|
|
498
|
-
} else if (jKey) {
|
|
499
|
-
axiosInterceptor
|
|
500
|
-
.post<Response>(`logout`, {
|
|
501
|
-
userId,
|
|
502
|
-
jKey,
|
|
503
|
-
})
|
|
504
|
-
.then((response) => {
|
|
505
|
-
const { data } = response;
|
|
506
|
-
const finished = (error: Error | string | null) => {
|
|
507
|
-
if (error) {
|
|
508
|
-
callBack(error, null);
|
|
509
|
-
return;
|
|
510
|
-
} else {
|
|
511
|
-
callBack(null, data);
|
|
512
|
-
}
|
|
513
|
-
};
|
|
514
|
-
delete this.jsonObj[userId]; // Use this.jsonObj
|
|
515
|
-
saveData(
|
|
516
|
-
{ ...this.jsonObj },
|
|
517
|
-
"config.json",
|
|
518
|
-
finished
|
|
519
|
-
);
|
|
520
|
-
})
|
|
521
|
-
.catch((error: AxiosError) => {
|
|
522
|
-
callBack(handleError(error), null);
|
|
523
|
-
});
|
|
524
|
-
} else {
|
|
525
|
-
callBack("No jKey found", null);
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
} else {
|
|
529
|
-
callBack("No config data found", null);
|
|
530
|
-
}
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* Fetches detailed profile and privilege information of the logged-in Firstock user.
|
|
535
|
-
*
|
|
536
|
-
* This method sends a POST request to the `/userDetails` endpoint to retrieve user-specific
|
|
537
|
-
* account information including email, enabled exchanges, order types, and privilege level.
|
|
538
|
-
* It first checks for valid login session data (jKey) before making the API call.
|
|
539
|
-
*
|
|
540
|
-
* @param {Object} params - Parameters for the request.
|
|
541
|
-
* @param {string} params.userId - The Firstock user ID used during login.
|
|
542
|
-
*
|
|
543
|
-
* @param {function} callBack - Callback function that returns either an error or the result.
|
|
544
|
-
* @param {Error|string|null} callBack.error - An error message if the request fails.
|
|
545
|
-
* @param {UserDetailsResponse|null} callBack.result - The user details data if successful.
|
|
546
|
-
*
|
|
547
|
-
* Response includes:
|
|
548
|
-
* - actid: Account ID (same as userId)
|
|
549
|
-
* - email: Registered email address
|
|
550
|
-
* - exchange: Array of enabled exchanges (e.g., NSE, BSE)
|
|
551
|
-
* - orarr: Allowed order types (e.g., MKT, LMT)
|
|
552
|
-
* - uprev: User privilege level (e.g., INVESTOR)
|
|
553
|
-
* - userName: Full name of the user
|
|
554
|
-
* - requestTime: Timestamp of the request
|
|
555
|
-
*
|
|
556
|
-
* Notes:
|
|
557
|
-
* - If the user is not logged in or the session token is expired, an appropriate error is returned.
|
|
558
|
-
* - Always ensure the session token (jKey) used is from the latest successful login.
|
|
559
|
-
*/
|
|
560
|
-
|
|
561
|
-
userDetails(params: UserDetailsParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
562
|
-
const currentUserId = params.userId;
|
|
563
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
564
|
-
if (err) {
|
|
565
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
566
|
-
} else if (data) {
|
|
567
|
-
const userId = currentUserId;
|
|
568
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
569
|
-
if (err) {
|
|
570
|
-
callBack(err, null);
|
|
571
|
-
} else if (jKey) {
|
|
572
|
-
axiosInterceptor
|
|
573
|
-
.post<Response>(`userDetails`, {
|
|
574
|
-
userId,
|
|
575
|
-
jKey,
|
|
576
|
-
})
|
|
577
|
-
.then((response) => {
|
|
578
|
-
const { data } = response;
|
|
579
|
-
callBack(null, data);
|
|
580
|
-
})
|
|
581
|
-
.catch((error: AxiosError) => {
|
|
582
|
-
callBack(handleError(error), null);
|
|
583
|
-
});
|
|
584
|
-
} else {
|
|
585
|
-
callBack("No jKey found", null);
|
|
586
|
-
}
|
|
587
|
-
});
|
|
588
|
-
} else {
|
|
589
|
-
callBack("No config data found", null);
|
|
590
|
-
}
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* Places a new trade order on the Firstock trading platform.
|
|
595
|
-
*
|
|
596
|
-
* This method sends a POST request to the `/placeOrder` endpoint using the provided parameters.
|
|
597
|
-
* It supports market, limit, and stop-loss order types across supported exchanges like NSE, BSE, and NFO.
|
|
598
|
-
*
|
|
599
|
-
* @param {Object} params - Required order parameters.
|
|
600
|
-
* @param {string} params.userId - Firstock user ID (same as used during login).
|
|
601
|
-
* @param {string} params.exchange - Exchange to place order on (e.g., "NSE", "BSE", "NFO").
|
|
602
|
-
* @param {string} params.retention - Order validity period ("DAY", "IOC").
|
|
603
|
-
* @param {string} params.product - Product type ("C", "I", "M").
|
|
604
|
-
* @param {string} params.priceType - Type of order ("MKT", "LMT", "SL-MKT", "SL-LMT").
|
|
605
|
-
* @param {string} params.tradingSymbol - Instrument symbol (e.g., "IDEA-EQ", "NIFTY06MAR25C22500").
|
|
606
|
-
* @param {string} params.transactionType - "B" (Buy) or "S" (Sell).
|
|
607
|
-
* @param {string} params.price - Order price (0 for market orders).
|
|
608
|
-
* @param {string} params.triggerPrice - Trigger price for stop-loss orders (0 for others).
|
|
609
|
-
* @param {string} params.quantity - Quantity of shares or lots to trade.
|
|
610
|
-
* @param {string} params.remarks - Optional order remark or note.
|
|
611
|
-
*
|
|
612
|
-
* @param {function} callBack - Callback function.
|
|
613
|
-
* @param {Error|string|null} callBack.error - Error message, if any.
|
|
614
|
-
* @param {OrderResponse|null} callBack.result - Response data containing the order details.
|
|
615
|
-
*
|
|
616
|
-
* Notes:
|
|
617
|
-
* - Requires a valid session token (jKey), obtained during login.
|
|
618
|
-
* - Returns `orderNumber` and `requestTime` on success.
|
|
619
|
-
* - Handles various error conditions such as missing fields or invalid sessions.
|
|
620
|
-
* - URL-encode special characters in trading symbols (e.g., use `L%26TFH-EQ` instead of `L&TFH-EQ`).
|
|
621
|
-
*/
|
|
622
|
-
|
|
623
|
-
placeOrder(
|
|
624
|
-
params: PlaceOrderParams,
|
|
625
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
626
|
-
): void {
|
|
627
|
-
const currentUserId = params.userId;
|
|
628
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
629
|
-
if (err) {
|
|
630
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
631
|
-
} else if (data) {
|
|
632
|
-
const userId = currentUserId;
|
|
633
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
634
|
-
if (err) {
|
|
635
|
-
callBack(err, null);
|
|
636
|
-
} else if (jKey) {
|
|
637
|
-
axiosInterceptor
|
|
638
|
-
.post<Response>(`placeOrder`, {
|
|
639
|
-
userId,
|
|
640
|
-
jKey,
|
|
641
|
-
exchange: params.exchange,
|
|
642
|
-
tradingSymbol: params.tradingSymbol,
|
|
643
|
-
quantity: params.quantity,
|
|
644
|
-
price: params.price,
|
|
645
|
-
product: params.product,
|
|
646
|
-
transactionType: params.transactionType,
|
|
647
|
-
priceType: params.priceType,
|
|
648
|
-
retention: params.retention,
|
|
649
|
-
remarks: params.remarks,
|
|
650
|
-
triggerPrice: params.triggerPrice,
|
|
651
|
-
})
|
|
652
|
-
.then((response) => {
|
|
653
|
-
const { data } = response;
|
|
654
|
-
callBack(null, data);
|
|
655
|
-
})
|
|
656
|
-
.catch((error: AxiosError) => {
|
|
657
|
-
callBack(handleError(error), null);
|
|
658
|
-
});
|
|
659
|
-
} else {
|
|
660
|
-
callBack("No jKey found", null);
|
|
661
|
-
}
|
|
662
|
-
});
|
|
663
|
-
} else {
|
|
664
|
-
callBack("No config data found", null);
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
}
|
|
668
|
-
/**
|
|
669
|
-
* Fetches the margin requirements for a prospective order on the Firstock trading platform.
|
|
670
|
-
*
|
|
671
|
-
* This API helps preview the required margin, available margin, cash balance, and potential warnings (e.g., insufficient funds)
|
|
672
|
-
* before placing a live trade. It's recommended for risk checks and order feasibility validation.
|
|
673
|
-
*
|
|
674
|
-
* @param {Object} params - Required parameters for order margin calculation.
|
|
675
|
-
* @param {string} params.userId - Firstock user ID (same as used during login).
|
|
676
|
-
* @param {string} params.exchange - Exchange where the order is intended ("NSE", "BSE", "NFO", etc.).
|
|
677
|
-
* @param {string} params.transactionType - "B" for Buy or "S" for Sell.
|
|
678
|
-
* @param {string} params.product - Product type ("C", "M", "I").
|
|
679
|
-
* @param {string} params.tradingSymbol - Instrument symbol (e.g., "IDEA-EQ", "NIFTY06MAR25C22500").
|
|
680
|
-
* @param {string} params.quantity - Number of shares or lots.
|
|
681
|
-
* @param {string} params.priceType - Order type ("MKT", "LMT", "SL-LMT", "SL-MKT").
|
|
682
|
-
* @param {string} params.price - Order price (use "0" for market orders).
|
|
683
|
-
*
|
|
684
|
-
* @param {function} callBack - Callback function.
|
|
685
|
-
* @param {Error|string|null} callBack.error - Error object or message, if any.
|
|
686
|
-
* @param {OrderMarginResponse|null} callBack.result - Margin data including total cash, required margin, available margin, remarks, and timestamp.
|
|
687
|
-
*
|
|
688
|
-
* Notes:
|
|
689
|
-
* - Requires a valid `jKey` (session token) obtained during login.
|
|
690
|
-
* - The method returns key margin fields: `marginOnNewOrder`, `availableMargin`, `cash`, `remarks`, and `requestTime`.
|
|
691
|
-
* - Use this to verify sufficient margin before attempting to place the order.
|
|
692
|
-
* - `remarks` may include warnings such as "Insufficient Balance".
|
|
693
|
-
* - Ensure that the selected product type is supported on the specified exchange.
|
|
694
|
-
* - If `jKey` is missing or expired, the response will indicate session invalidation.
|
|
695
|
-
*/
|
|
696
|
-
|
|
697
|
-
orderMargin(
|
|
698
|
-
params: OrderMarginParams,
|
|
699
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
700
|
-
): void {
|
|
701
|
-
const currentUserId = params.userId;
|
|
702
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
703
|
-
if (err) {
|
|
704
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
705
|
-
} else if (data) {
|
|
706
|
-
const userId = currentUserId;
|
|
707
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
708
|
-
if (err) {
|
|
709
|
-
callBack(err, null);
|
|
710
|
-
} else if (jKey) {
|
|
711
|
-
axiosInterceptor
|
|
712
|
-
.post<Response>(`orderMargin`, {
|
|
713
|
-
userId,
|
|
714
|
-
actid: userId, // Added as per your code
|
|
715
|
-
jKey,
|
|
716
|
-
exchange: params.exchange,
|
|
717
|
-
tradingSymbol: params.tradingSymbol,
|
|
718
|
-
quantity: params.quantity,
|
|
719
|
-
price: params.price,
|
|
720
|
-
product: params.product,
|
|
721
|
-
transactionType: params.transactionType,
|
|
722
|
-
priceType: params.priceType,
|
|
723
|
-
})
|
|
724
|
-
.then((response) => {
|
|
725
|
-
const { data } = response;
|
|
726
|
-
callBack(null, data);
|
|
727
|
-
})
|
|
728
|
-
.catch((error: AxiosError) => {
|
|
729
|
-
callBack(handleError(error), null);
|
|
730
|
-
});
|
|
731
|
-
} else {
|
|
732
|
-
callBack("No jKey found", null);
|
|
733
|
-
}
|
|
734
|
-
});
|
|
735
|
-
} else {
|
|
736
|
-
callBack("No config data found", null);
|
|
737
|
-
}
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
/**
|
|
741
|
-
* Retrieves the list of all orders placed by the user, including open, filled, and rejected orders.
|
|
742
|
-
*
|
|
743
|
-
* This method sends a request to the Firstock Order Book API and returns a structured list of all orders
|
|
744
|
-
* along with their details such as exchange, transaction type, quantity, price, status, rejection reasons, etc.
|
|
745
|
-
*
|
|
746
|
-
* Useful for real-time order tracking, trade reconciliation, and identifying rejected or pending trades.
|
|
747
|
-
*
|
|
748
|
-
* @param {Object} params - Required parameters for retrieving the order book.
|
|
749
|
-
* @param {string} params.userId - Unique Firstock user ID (same as used during login).
|
|
750
|
-
*
|
|
751
|
-
* @param {function} callBack - Callback function.
|
|
752
|
-
* @param {Error|string|null} callBack.error - Error object or message, if any.
|
|
753
|
-
* @param {OrderBookResponse|null} callBack.result - Array of order details including order number, quantity, status, average price, and rejection reason.
|
|
754
|
-
*
|
|
755
|
-
* Notes:
|
|
756
|
-
* - Requires a valid `jKey` (session token) obtained at login.
|
|
757
|
-
* - Orders include both current and historical data such as `REJECTED`, `FILLED`, or `OPEN`.
|
|
758
|
-
* - Each order record contains fields like `orderNumber`, `exchange`, `transactionType`, `quantity`, `priceType`, `status`, `remarks`, and `rejectReason`.
|
|
759
|
-
* - Use this data for populating order history in user dashboards or triggering logic based on order state.
|
|
760
|
-
* - If `jKey` is invalid or expired, response will indicate session expiration.
|
|
761
|
-
* - Frequent polling should be rate-limited; consider WebSocket alternatives if available.
|
|
762
|
-
*/
|
|
763
|
-
|
|
764
|
-
orderBook(params: OrderBookParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
765
|
-
const currentUserId = params.userId;
|
|
766
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
767
|
-
if (err) {
|
|
768
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
769
|
-
} else if (data) {
|
|
770
|
-
const userId = currentUserId;
|
|
771
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
772
|
-
if (err) {
|
|
773
|
-
callBack(err, null);
|
|
774
|
-
} else if (jKey) {
|
|
775
|
-
axiosInterceptor
|
|
776
|
-
.post<Response>(`orderBook`, {
|
|
777
|
-
userId,
|
|
778
|
-
jKey,
|
|
779
|
-
})
|
|
780
|
-
.then((response) => {
|
|
781
|
-
const { data } = response;
|
|
782
|
-
callBack(null, data);
|
|
783
|
-
})
|
|
784
|
-
.catch((error: AxiosError) => {
|
|
785
|
-
callBack(handleError(error), null);
|
|
786
|
-
});
|
|
787
|
-
} else {
|
|
788
|
-
callBack("No jKey found", null);
|
|
789
|
-
}
|
|
790
|
-
});
|
|
791
|
-
} else {
|
|
792
|
-
callBack("No config data found", null);
|
|
793
|
-
}
|
|
794
|
-
});
|
|
795
|
-
}
|
|
796
|
-
/**
|
|
797
|
-
* Cancels a previously placed order that has not yet been fully executed.
|
|
798
|
-
*
|
|
799
|
-
* This method interacts with the Firstock Cancel Order API to invalidate an active order. It is useful for
|
|
800
|
-
* real-time risk management and preventing undesired executions when market conditions change or the user
|
|
801
|
-
* decides to withdraw the order.
|
|
802
|
-
*
|
|
803
|
-
* @param {Object} params - Required parameters for cancelling an order.
|
|
804
|
-
* @param {string} params.userId - Unique Firstock user ID (same as used during login).
|
|
805
|
-
* @param {string} params.orderNumber - The unique identifier of the order to be cancelled.
|
|
806
|
-
*
|
|
807
|
-
* @param {function} callBack - Callback function to handle the response.
|
|
808
|
-
* @param {Error|string|null} callBack.error - Error message or object, if the request fails.
|
|
809
|
-
* @param {OrderResponse|null} callBack.result - Response data containing order cancellation status.
|
|
810
|
-
*
|
|
811
|
-
* Notes:
|
|
812
|
-
* - Only orders in an “open” or “partially filled” state are eligible for cancellation.
|
|
813
|
-
* - The session token (`jKey`) must be valid and active; otherwise, a re-login may be required.
|
|
814
|
-
* - Responses include fields like `orderNumber`, `rejreason`, and `requestTime`.
|
|
815
|
-
* - A failed cancellation may return reasons such as "order is not open to cancel".
|
|
816
|
-
* - It's recommended to verify the final order status using the Order Book API after cancellation.
|
|
817
|
-
*/
|
|
818
|
-
|
|
819
|
-
cancelOrder(params: CancelOrderParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
820
|
-
const currentUserId = params.userId;
|
|
821
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
822
|
-
if (err) {
|
|
823
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
824
|
-
} else if (data) {
|
|
825
|
-
const userId = currentUserId;
|
|
826
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
827
|
-
if (err) {
|
|
828
|
-
callBack(err, null);
|
|
829
|
-
} else if (jKey) {
|
|
830
|
-
axiosInterceptor
|
|
831
|
-
.post<Response>(`cancelOrder`, {
|
|
832
|
-
userId,
|
|
833
|
-
jKey,
|
|
834
|
-
orderNumber: params.orderNumber,
|
|
835
|
-
})
|
|
836
|
-
.then((response) => {
|
|
837
|
-
const { data } = response;
|
|
838
|
-
callBack(null, data);
|
|
839
|
-
})
|
|
840
|
-
.catch((error: AxiosError) => {
|
|
841
|
-
callBack(handleError(error), null);
|
|
842
|
-
});
|
|
843
|
-
} else {
|
|
844
|
-
callBack("No jKey found", null);
|
|
845
|
-
}
|
|
846
|
-
});
|
|
847
|
-
} else {
|
|
848
|
-
callBack("No config data found", null);
|
|
849
|
-
}
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
/**
|
|
853
|
-
* Modifies an existing active order with updated parameters like price, quantity, or trigger price.
|
|
854
|
-
*
|
|
855
|
-
* This method interacts with the Firstock Modify Order API to alter a live order without canceling it.
|
|
856
|
-
* It's useful for adapting orders in response to changing market conditions without losing order priority.
|
|
857
|
-
*
|
|
858
|
-
* @param {Object} params - Parameters required to modify an order.
|
|
859
|
-
* @param {string} params.userId - Firstock user ID used during login.
|
|
860
|
-
* @param {string} params.orderNumber - The order identifier to be modified.
|
|
861
|
-
* @param {string} params.priceType - Order type ("MKT", "LMT", "SL-LMT", "SL-MKT").
|
|
862
|
-
* @param {string} params.tradingSymbol - Trading symbol (e.g., "IDEA-EQ").
|
|
863
|
-
* @param {string} [params.price] - Price for limit orders. Should be "0" for market orders.
|
|
864
|
-
* @param {string} [params.quantity] - Number of shares/lots to modify.
|
|
865
|
-
* @param {string} [params.triggerPrice] - Trigger price for SL orders. Optional for other types.
|
|
866
|
-
* @param {string} [params.retention] - Optional order duration (e.g., "DAY", "IOC").
|
|
867
|
-
* @param {string} [params.product] - Product type (e.g., "C", "M", "I").
|
|
868
|
-
*
|
|
869
|
-
* @param {function} callBack - Callback to handle response or error.
|
|
870
|
-
* @param {Error|string|null} callBack.error - Error object or message if request fails.
|
|
871
|
-
* @param {OrderResponse|null} callBack.result - Response data on successful order modification.
|
|
872
|
-
*
|
|
873
|
-
* Notes:
|
|
874
|
-
* - Only open or partially filled orders can be modified.
|
|
875
|
-
* - Session token (jKey) must be valid.
|
|
876
|
-
* - Use consistent combinations of `priceType`, `price`, and `triggerPrice`.
|
|
877
|
-
* - Invalid combinations or closed orders will cause the request to fail.
|
|
878
|
-
* - Always verify status using the Order Book API after calling this method.
|
|
879
|
-
*/
|
|
880
|
-
|
|
881
|
-
modifyOrder(
|
|
882
|
-
params: ModifyOrderParams,
|
|
883
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
884
|
-
): void {
|
|
885
|
-
const currentUserId = params.userId;
|
|
886
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
887
|
-
if (err) {
|
|
888
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
889
|
-
} else if (data) {
|
|
890
|
-
const userId = currentUserId;
|
|
891
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
892
|
-
if (err) {
|
|
893
|
-
callBack(err, null);
|
|
894
|
-
} else if (jKey) {
|
|
895
|
-
axiosInterceptor
|
|
896
|
-
.post<Response>(`modifyOrder`, {
|
|
897
|
-
userId,
|
|
898
|
-
jKey,
|
|
899
|
-
quantity: params.quantity,
|
|
900
|
-
price: params.price,
|
|
901
|
-
triggerPrice: params.triggerPrice,
|
|
902
|
-
orderNumber: params.orderNumber,
|
|
903
|
-
exchange: params.exchange,
|
|
904
|
-
tradingSymbol: params.tradingSymbol,
|
|
905
|
-
priceType: params.priceType,
|
|
906
|
-
})
|
|
907
|
-
.then((response) => {
|
|
908
|
-
const { data } = response;
|
|
909
|
-
callBack(null, data);
|
|
910
|
-
})
|
|
911
|
-
.catch((error: AxiosError) => {
|
|
912
|
-
callBack(handleError(error), null);
|
|
913
|
-
});
|
|
914
|
-
} else {
|
|
915
|
-
callBack("No jKey found", null);
|
|
916
|
-
}
|
|
917
|
-
});
|
|
918
|
-
} else {
|
|
919
|
-
callBack("No config data found", null);
|
|
920
|
-
}
|
|
921
|
-
});
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Fetches detailed history of a specific order including each step or fill event.
|
|
925
|
-
*
|
|
926
|
-
* This method provides a breakdown of an order's lifecycle such as status changes,
|
|
927
|
-
* partial fills, rejection reasons, and timestamps for each transition.
|
|
928
|
-
*
|
|
929
|
-
* **Endpoint:** POST `/singleOrderHistory`
|
|
930
|
-
* **URL:** https://api.firstock.in/V1/singleOrderHistory
|
|
931
|
-
*
|
|
932
|
-
* @param {SingleOrderHistoryParams} params - The order details required to fetch history.
|
|
933
|
-
* @param {string} params.userId - Unique Firstock user ID (same as login).
|
|
934
|
-
* @param {string} params.orderNumber - Unique identifier for the order to retrieve.
|
|
935
|
-
*
|
|
936
|
-
* @param {(error: Error | string | null, result: OrderResponse | null) => void} callBack - Callback function with either error or response.
|
|
937
|
-
*
|
|
938
|
-
* @returns {void}
|
|
939
|
-
*
|
|
940
|
-
* @example
|
|
941
|
-
* firstock.singleOrderHistory(
|
|
942
|
-
* { userId: "AB1234", orderNumber: "24121300003425" },
|
|
943
|
-
* (err, result) => {
|
|
944
|
-
* if (err) console.error(err);
|
|
945
|
-
* else console.log(result);
|
|
946
|
-
* }
|
|
947
|
-
* );
|
|
948
|
-
*/
|
|
949
|
-
singleOrderHistory(params: SingleOrderHistoryParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
950
|
-
const currentUserId = params.userId;
|
|
951
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
952
|
-
if (err) {
|
|
953
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
954
|
-
} else if (data) {
|
|
955
|
-
const userId = currentUserId;
|
|
956
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
957
|
-
if (err) {
|
|
958
|
-
callBack(err, null);
|
|
959
|
-
} else if (jKey) {
|
|
960
|
-
axiosInterceptor
|
|
961
|
-
.post<Response>(`singleOrderHistory`, {
|
|
962
|
-
userId,
|
|
963
|
-
jKey,
|
|
964
|
-
orderNumber: params.orderNumber,
|
|
965
|
-
})
|
|
966
|
-
.then((response) => {
|
|
967
|
-
const { data } = response;
|
|
968
|
-
callBack(null, data);
|
|
969
|
-
})
|
|
970
|
-
.catch((error: AxiosError) => {
|
|
971
|
-
callBack(handleError(error), null);
|
|
972
|
-
});
|
|
973
|
-
} else {
|
|
974
|
-
callBack("No jKey found", null);
|
|
975
|
-
}
|
|
976
|
-
});
|
|
977
|
-
} else {
|
|
978
|
-
callBack("No config data found", null);
|
|
979
|
-
}
|
|
980
|
-
});
|
|
981
|
-
}
|
|
982
|
-
/**
|
|
983
|
-
* Retrieves the trade history (executed orders) for a user account.
|
|
984
|
-
*
|
|
985
|
-
* Unlike the Order Book, which includes pending and partially filled orders, this method
|
|
986
|
-
* returns only completed or partially executed trades, including fill details such as
|
|
987
|
-
* price, quantity, and timestamps.
|
|
988
|
-
*
|
|
989
|
-
* **Endpoint:** POST `/tradeBook`
|
|
990
|
-
* **URL:** https://api.firstock.in/V1/tradeBook
|
|
991
|
-
*
|
|
992
|
-
* @param {TradeBookParams} params - Parameters for fetching the trade book.
|
|
993
|
-
* @param {string} params.userId - Unique Firstock user ID (used during login).
|
|
994
|
-
*
|
|
995
|
-
* @param {(error: Error | string | null, result: TradeBookResponse | null) => void} callBack - Callback function with error or trade data.
|
|
996
|
-
*
|
|
997
|
-
* @returns {void}
|
|
998
|
-
*
|
|
999
|
-
* @remarks
|
|
1000
|
-
* - Ensure `jKey` is valid (session token from login). An invalid or expired session will trigger an error.
|
|
1001
|
-
* - Useful for profit/loss calculations, compliance audits, and trade reconciliation.
|
|
1002
|
-
* - Typical failure reasons: invalid `userId`, expired session, or no trades found.
|
|
1003
|
-
*/
|
|
1004
|
-
tradeBook(params: TradeBookParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
1005
|
-
const currentUserId = params.userId;
|
|
1006
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1007
|
-
if (err) {
|
|
1008
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1009
|
-
} else if (data) {
|
|
1010
|
-
const userId = currentUserId;
|
|
1011
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1012
|
-
if (err) {
|
|
1013
|
-
callBack(err, null);
|
|
1014
|
-
} else if (jKey) {
|
|
1015
|
-
axiosInterceptor
|
|
1016
|
-
.post<Response>(`tradeBook`, {
|
|
1017
|
-
userId,
|
|
1018
|
-
jKey,
|
|
1019
|
-
actid: userId,
|
|
1020
|
-
})
|
|
1021
|
-
.then((response) => {
|
|
1022
|
-
const { data } = response;
|
|
1023
|
-
callBack(null, data);
|
|
1024
|
-
})
|
|
1025
|
-
.catch((error: AxiosError) => {
|
|
1026
|
-
callBack(handleError(error), null);
|
|
1027
|
-
});
|
|
1028
|
-
} else {
|
|
1029
|
-
callBack("No jKey found", null);
|
|
1030
|
-
}
|
|
1031
|
-
});
|
|
1032
|
-
} else {
|
|
1033
|
-
callBack("No config data found", null);
|
|
1034
|
-
}
|
|
1035
|
-
});
|
|
1036
|
-
}
|
|
1037
|
-
/**
|
|
1038
|
-
* Fetches the list of current open positions for the given user.
|
|
1039
|
-
*
|
|
1040
|
-
* This API provides detailed information about each open position such as:
|
|
1041
|
-
* - Buy/sell quantities and average prices for the day
|
|
1042
|
-
* - Net quantity and average price
|
|
1043
|
-
* - Unrealized mark-to-market (MTM) and realized profit/loss
|
|
1044
|
-
* - Exchange, product type, trading symbol, and more
|
|
1045
|
-
*
|
|
1046
|
-
* Common uses include:
|
|
1047
|
-
* - Real-time profit/loss tracking
|
|
1048
|
-
* - Position reconciliation with order/trade books
|
|
1049
|
-
* - Margin requirement assessment based on product type
|
|
1050
|
-
*
|
|
1051
|
-
* @param {PositionsBookParams} params - Parameters including userId (Firstock ID)
|
|
1052
|
-
* @param {(error: Error | string | null, result: PositionsBookResponse | null) => void} callBack - Callback to receive either error or response
|
|
1053
|
-
*
|
|
1054
|
-
* @returns {void}
|
|
1055
|
-
*/
|
|
1056
|
-
positionBook(params: PositionsBookParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
1057
|
-
const currentUserId = params.userId;
|
|
1058
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1059
|
-
if (err) {
|
|
1060
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1061
|
-
} else if (data) {
|
|
1062
|
-
const userId = currentUserId;
|
|
1063
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1064
|
-
if (err) {
|
|
1065
|
-
callBack(err, null);
|
|
1066
|
-
} else if (jKey) {
|
|
1067
|
-
axiosInterceptor
|
|
1068
|
-
.post<Response>(`positionBook`, {
|
|
1069
|
-
userId,
|
|
1070
|
-
jKey,
|
|
1071
|
-
actid: userId,
|
|
1072
|
-
})
|
|
1073
|
-
.then((response) => {
|
|
1074
|
-
const { data } = response;
|
|
1075
|
-
callBack(null, data);
|
|
1076
|
-
})
|
|
1077
|
-
.catch((error: AxiosError) => {
|
|
1078
|
-
callBack(handleError(error), null); // Fixed to use handleError
|
|
1079
|
-
});
|
|
1080
|
-
} else {
|
|
1081
|
-
callBack("No jKey found", null);
|
|
1082
|
-
}
|
|
1083
|
-
});
|
|
1084
|
-
} else {
|
|
1085
|
-
callBack("No config data found", null);
|
|
1086
|
-
}
|
|
1087
|
-
});
|
|
1088
|
-
}
|
|
1089
|
-
/**
|
|
1090
|
-
* Converts the product type for an existing open position.
|
|
1091
|
-
*
|
|
1092
|
-
* Enables traders to change a position's product type (e.g., from MIS to CNC) without closing and reopening it.
|
|
1093
|
-
* This is useful for margin optimization or carrying over intraday positions.
|
|
1094
|
-
*
|
|
1095
|
-
* Benefits:
|
|
1096
|
-
* - Adjust strategy without exiting trades
|
|
1097
|
-
* - Free up or reallocate margin
|
|
1098
|
-
* - Avoid unnecessary brokerage or slippage
|
|
1099
|
-
*
|
|
1100
|
-
* Restrictions and considerations:
|
|
1101
|
-
* - Ensure jKey (session token) is valid
|
|
1102
|
-
* - Conversion may not be allowed after square-off time
|
|
1103
|
-
* - Check position size before attempting partial conversions
|
|
1104
|
-
* - Only supported symbols and product types are eligible
|
|
1105
|
-
*
|
|
1106
|
-
* @param {ProductConversionParams} params - Includes userId, tradingSymbol, exchange, quantity, product, and previousProduct
|
|
1107
|
-
* @param {(error: Error | string | null, result: ProductConversionResponse | null) => void} callBack - Callback that returns the conversion result or error
|
|
1108
|
-
*
|
|
1109
|
-
* @returns {void}
|
|
1110
|
-
*/
|
|
1111
|
-
productConversion(
|
|
1112
|
-
params: ProductConversionParams,
|
|
1113
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1114
|
-
): void {
|
|
1115
|
-
const currentUserId = params.userId;
|
|
1116
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1117
|
-
if (err) {
|
|
1118
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1119
|
-
} else if (data) {
|
|
1120
|
-
const userId = currentUserId;
|
|
1121
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1122
|
-
if (err) {
|
|
1123
|
-
callBack(err, null);
|
|
1124
|
-
} else if (jKey) {
|
|
1125
|
-
axiosInterceptor
|
|
1126
|
-
.post<Response>(`productConversion`, {
|
|
1127
|
-
userId,
|
|
1128
|
-
jKey,
|
|
1129
|
-
exchange: params.exchange,
|
|
1130
|
-
tradingSymbol: params.tradingSymbol,
|
|
1131
|
-
quantity: params.quantity,
|
|
1132
|
-
product: params.product,
|
|
1133
|
-
previousProduct: params.previousProduct,
|
|
1134
|
-
})
|
|
1135
|
-
.then((response) => {
|
|
1136
|
-
const { data } = response;
|
|
1137
|
-
callBack(null, data);
|
|
1138
|
-
})
|
|
1139
|
-
.catch((error: AxiosError) => {
|
|
1140
|
-
callBack(handleError(error), null);
|
|
1141
|
-
});
|
|
1142
|
-
} else {
|
|
1143
|
-
callBack("No jKey found", null);
|
|
1144
|
-
}
|
|
1145
|
-
});
|
|
1146
|
-
} else {
|
|
1147
|
-
callBack("No config data found", null);
|
|
1148
|
-
}
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
/**
|
|
1152
|
-
* Retrieves the user's holdings (portfolio) from Firstock.
|
|
1153
|
-
*
|
|
1154
|
-
* This method fetches a summary of securities held in the user's Demat account or portfolio.
|
|
1155
|
-
* Unlike the position book (which reflects intraday/open positions), holdings represent delivery-based investments.
|
|
1156
|
-
*
|
|
1157
|
-
* Key Features:
|
|
1158
|
-
* - Provides long-term investment details.
|
|
1159
|
-
* - Shows exchange and trading symbol for each holding.
|
|
1160
|
-
* - Useful for portfolio valuation and analysis.
|
|
1161
|
-
*
|
|
1162
|
-
* @param {HoldingsParams} params - Required parameters to fetch holdings.
|
|
1163
|
-
* @param {string} params.userId - Unique identifier of the user (same as login).
|
|
1164
|
-
* @param {string} [params.product] - Optional product type (e.g., CNC, MIS).
|
|
1165
|
-
* @param {(error: Error | string | null, result: HoldingsResponse | null) => void} callBack - Callback function with error or result.
|
|
1166
|
-
*
|
|
1167
|
-
* Endpoint: POST /holdings
|
|
1168
|
-
* Headers: { "Content-Type": "application/json" }
|
|
1169
|
-
*
|
|
1170
|
-
* Response:
|
|
1171
|
-
* - On success: { status: "success", message: "Fetched holdings", data: [...] }
|
|
1172
|
-
* - On error: Error string such as "INVALID_JKEY" or "No config data found"
|
|
1173
|
-
*
|
|
1174
|
-
* Notes:
|
|
1175
|
-
* - Requires a valid session (jKey). If the jKey is invalid or expired, prompt the user to log in again.
|
|
1176
|
-
*/
|
|
1177
|
-
holdings(params: HoldingsParams, callBack: (error: Error | string | null, result: Response | null) => void): void {
|
|
1178
|
-
const currentUserId = params.userId;
|
|
1179
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1180
|
-
if (err) {
|
|
1181
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1182
|
-
} else if (data) {
|
|
1183
|
-
const userId = currentUserId;
|
|
1184
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1185
|
-
if (err) {
|
|
1186
|
-
callBack(err, null);
|
|
1187
|
-
} else if (jKey) {
|
|
1188
|
-
axiosInterceptor
|
|
1189
|
-
.post<Response>(`holdings`, {
|
|
1190
|
-
userId,
|
|
1191
|
-
jKey,
|
|
1192
|
-
...(params.product && { product: params.product }), // Include product if provided
|
|
1193
|
-
})
|
|
1194
|
-
.then((response) => {
|
|
1195
|
-
const { data } = response;
|
|
1196
|
-
callBack(null, data);
|
|
1197
|
-
})
|
|
1198
|
-
.catch((error: AxiosError) => {
|
|
1199
|
-
callBack(handleError(error), null);
|
|
1200
|
-
});
|
|
1201
|
-
} else {
|
|
1202
|
-
callBack("No jKey found", null);
|
|
1203
|
-
}
|
|
1204
|
-
});
|
|
1205
|
-
} else {
|
|
1206
|
-
callBack("No config data found", null);
|
|
1207
|
-
}
|
|
1208
|
-
});
|
|
1209
|
-
}
|
|
1210
|
-
/**
|
|
1211
|
-
* Calculates the margin required for placing a basket of orders in a single request.
|
|
1212
|
-
*
|
|
1213
|
-
* The Basket Margin API is used to compute the net margin impact of multiple orders, which is particularly useful
|
|
1214
|
-
* for complex strategies like spreads, options combinations, or simultaneous stock purchases.
|
|
1215
|
-
*
|
|
1216
|
-
* Key Features:
|
|
1217
|
-
* - Evaluates margin requirements for multiple trades together.
|
|
1218
|
-
* - Helps prevent partial order failures due to insufficient margin.
|
|
1219
|
-
* - Optimized for complex order execution.
|
|
1220
|
-
*
|
|
1221
|
-
* @param {Object} params - Basket margin request parameters.
|
|
1222
|
-
* @param {string} params.userId - Firstock user ID (same as used during login).
|
|
1223
|
-
* @param {string} params.exchange - Exchange code (e.g., "NSE", "BSE", "NFO").
|
|
1224
|
-
* @param {string} params.transactionType - "B" (Buy) or "S" (Sell) for the first order.
|
|
1225
|
-
* @param {string} params.product - Product type (e.g., "C", "M", "I").
|
|
1226
|
-
* @param {string} params.tradingSymbol - Trading symbol (e.g., "RELIANCE-EQ").
|
|
1227
|
-
* @param {string | number} params.quantity - Quantity of the first order.
|
|
1228
|
-
* @param {string} params.priceType - Price type (e.g., "MKT", "LMT").
|
|
1229
|
-
* @param {string | number} params.price - Price for the first order. Use "0" for market orders.
|
|
1230
|
-
* @param {Array<Object>} params.BasketList_Params - Additional orders in the basket.
|
|
1231
|
-
* @param {string} params.BasketList_Params[].exchange - Exchange code for each order.
|
|
1232
|
-
* @param {string} params.BasketList_Params[].transactionType - "B" or "S" for each order.
|
|
1233
|
-
* @param {string} params.BasketList_Params[].product - Product type for each order.
|
|
1234
|
-
* @param {string} params.BasketList_Params[].tradingSymbol - Symbol for each order.
|
|
1235
|
-
* @param {string | number} params.BasketList_Params[].quantity - Quantity for each order.
|
|
1236
|
-
* @param {string} params.BasketList_Params[].priceType - Price type for each order.
|
|
1237
|
-
* @param {string | number} params.BasketList_Params[].price - Price for each order.
|
|
1238
|
-
*
|
|
1239
|
-
* @param {(error: Error | string | null, result: any | null) => void} callBack - Callback with error or result.
|
|
1240
|
-
*
|
|
1241
|
-
* Response:
|
|
1242
|
-
* - status: "success" or "error"
|
|
1243
|
-
* - message: Description of result
|
|
1244
|
-
* - data: Contains fields like `BasketMargin`, `MarginOnNewOrder`, `PreviousMargin`, `TradedMargin`, and `Remarks`.
|
|
1245
|
-
*
|
|
1246
|
-
* Notes:
|
|
1247
|
-
* - Requires a valid `jKey` (session token).
|
|
1248
|
-
* - Combine this with the RMS Limits API to check available margin.
|
|
1249
|
-
* - Ensure all basket items are accurately populated to avoid miscalculations.
|
|
1250
|
-
*
|
|
1251
|
-
* Endpoint: POST /basketMargin
|
|
1252
|
-
* Content-Type: application/json
|
|
1253
|
-
*/
|
|
1254
|
-
|
|
1255
|
-
basketMargin(
|
|
1256
|
-
params: { userId: string; exchange: string; transactionType: string; product: string; tradingSymbol: string; quantity: string | number; priceType: string; price: string | number; BasketList_Params: { exchange: string; transactionType: string; product: string; tradingSymbol: string; quantity: string | number; priceType: string; price: string | number; }[] },
|
|
1257
|
-
callBack: (error: Error | string | null, result: any | null) => void
|
|
1258
|
-
): void {
|
|
1259
|
-
const currentUserId = params.userId;
|
|
1260
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1261
|
-
if (err) {
|
|
1262
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1263
|
-
} else if (data) {
|
|
1264
|
-
const userId = currentUserId;
|
|
1265
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1266
|
-
if (err) {
|
|
1267
|
-
callBack(err, null);
|
|
1268
|
-
} else if (jKey) {
|
|
1269
|
-
axiosInterceptor
|
|
1270
|
-
.post(`basketMargin`, {
|
|
1271
|
-
userId,
|
|
1272
|
-
jKey,
|
|
1273
|
-
exchange: params.exchange,
|
|
1274
|
-
transactionType: params.transactionType,
|
|
1275
|
-
product: params.product,
|
|
1276
|
-
tradingSymbol: params.tradingSymbol,
|
|
1277
|
-
quantity: params.quantity,
|
|
1278
|
-
priceType: params.priceType,
|
|
1279
|
-
price: params.price,
|
|
1280
|
-
BasketList_Params: params.BasketList_Params,
|
|
1281
|
-
})
|
|
1282
|
-
.then((response) => {
|
|
1283
|
-
const { data } = response;
|
|
1284
|
-
callBack(null, data);
|
|
1285
|
-
})
|
|
1286
|
-
.catch((error: AxiosError) => {
|
|
1287
|
-
callBack(handleError(error), null);
|
|
1288
|
-
});
|
|
1289
|
-
} else {
|
|
1290
|
-
callBack("No jKey found", null);
|
|
1291
|
-
}
|
|
1292
|
-
});
|
|
1293
|
-
} else {
|
|
1294
|
-
callBack("No config data found", null);
|
|
1295
|
-
}
|
|
1296
|
-
});
|
|
1297
|
-
}
|
|
1298
|
-
/**
|
|
1299
|
-
* Fetches the real-time RMS limits for a user account.
|
|
1300
|
-
*
|
|
1301
|
-
* This API provides detailed information about a user's trading limits and margin utilization.
|
|
1302
|
-
* It helps in assessing available margin, broker collateral, cash balance, collateral values,
|
|
1303
|
-
* exposure used, and other key risk metrics.
|
|
1304
|
-
*
|
|
1305
|
-
* Key Use Cases:
|
|
1306
|
-
* - Check if the user has sufficient buying power before placing new trades.
|
|
1307
|
-
* - Monitor exposure, margin used, collateral, and available limits.
|
|
1308
|
-
* - Ensure session is valid using the jKey.
|
|
1309
|
-
*
|
|
1310
|
-
* @param {Object} params - The input parameters.
|
|
1311
|
-
* @param {string} params.userId - The Firstock user ID (used during login).
|
|
1312
|
-
*
|
|
1313
|
-
* @param {(error: Error | string | null, result: LimitsResponse | null) => void} callBack - Callback function
|
|
1314
|
-
* to handle the response. Returns either an error or a `LimitsResponse` object.
|
|
1315
|
-
*
|
|
1316
|
-
* Response Structure:
|
|
1317
|
-
* - status: "success" or "error"
|
|
1318
|
-
* - message: Description of the result
|
|
1319
|
-
* - data: Object containing:
|
|
1320
|
-
* - availableMargin: Margin available for new orders
|
|
1321
|
-
* - brkcollamt: Broker collateral amount
|
|
1322
|
-
* - cash: Cash balance in the account
|
|
1323
|
-
* - collateral: Value of pledged holdings
|
|
1324
|
-
* - expo: Exposure margin used
|
|
1325
|
-
* - marginused: Total margin used
|
|
1326
|
-
* - payin: Incoming funds not yet cleared
|
|
1327
|
-
* - peak_mar: Peak margin used in the session
|
|
1328
|
-
* - premium: Option premium paid or received
|
|
1329
|
-
* - span: SPAN margin used (for F&O)
|
|
1330
|
-
* - requestTime: Timestamp of data retrieval
|
|
1331
|
-
*
|
|
1332
|
-
* Notes:
|
|
1333
|
-
* - Always ensure the session token (`jKey`) is valid.
|
|
1334
|
-
* - If jKey is invalid, prompt the user to re-authenticate.
|
|
1335
|
-
* - This API is essential for real-time risk and margin tracking.
|
|
1336
|
-
*
|
|
1337
|
-
* Endpoint: POST /limit
|
|
1338
|
-
* Headers: Content-Type: application/json
|
|
1339
|
-
*/
|
|
1340
|
-
|
|
1341
|
-
limit(
|
|
1342
|
-
params:LimitsParams,
|
|
1343
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1344
|
-
): void {
|
|
1345
|
-
const currentUserId: string = params.userId;
|
|
1346
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1347
|
-
if (err) {
|
|
1348
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1349
|
-
} else if (data) {
|
|
1350
|
-
const userId: string = currentUserId;
|
|
1351
|
-
checkifUserLoggedIn({ userId, jsonData: data || {} }, (err: string | null, jKey: string | null) => {
|
|
1352
|
-
if (err) {
|
|
1353
|
-
callBack(err, null);
|
|
1354
|
-
} else if (jKey) {
|
|
1355
|
-
axiosInterceptor
|
|
1356
|
-
.post<Response>(`limit`, {
|
|
1357
|
-
userId,
|
|
1358
|
-
jKey,
|
|
1359
|
-
})
|
|
1360
|
-
.then((response) => {
|
|
1361
|
-
const { data } = response;
|
|
1362
|
-
callBack(null, data);
|
|
1363
|
-
})
|
|
1364
|
-
.catch((error: AxiosError) => {
|
|
1365
|
-
callBack(handleError(error), null);
|
|
1366
|
-
});
|
|
1367
|
-
} else {
|
|
1368
|
-
callBack("No jKey found", null);
|
|
1369
|
-
}
|
|
1370
|
-
});
|
|
1371
|
-
} else {
|
|
1372
|
-
callBack("No config data found", null);
|
|
1373
|
-
}
|
|
1374
|
-
});
|
|
1375
|
-
}
|
|
1376
|
-
/**
|
|
1377
|
-
* Fetches live market quotes for a given trading symbol on a specified exchange.
|
|
1378
|
-
*
|
|
1379
|
-
* This method retrieves real-time pricing and order book data such as last traded price,
|
|
1380
|
-
* day high/low, and best bid/ask levels. It is useful for market analysis and building
|
|
1381
|
-
* live trading interfaces.
|
|
1382
|
-
*
|
|
1383
|
-
* @param {GetQuotesParams} params - Request parameters including userId, exchange, and tradingSymbol.
|
|
1384
|
-
* @param {(error: Error | string | null, result: GetQuotesResponse | null) => void} callBack -
|
|
1385
|
-
* Callback function that handles the response or error.
|
|
1386
|
-
*
|
|
1387
|
-
* @remarks
|
|
1388
|
-
* - Requires the user to be logged in with a valid session key (`jKey`).
|
|
1389
|
-
* - Calls the POST /getQuote endpoint.
|
|
1390
|
-
* - Parses and returns the latest quote data if successful.
|
|
1391
|
-
*
|
|
1392
|
-
* @example
|
|
1393
|
-
* firstock.getQuotes(
|
|
1394
|
-
* {
|
|
1395
|
-
* userId: 'AB1234',
|
|
1396
|
-
* exchange: 'NSE',
|
|
1397
|
-
* tradingSymbol: 'RELIANCE-EQ'
|
|
1398
|
-
* },
|
|
1399
|
-
* (error, result) => {
|
|
1400
|
-
* if (error) {
|
|
1401
|
-
* console.error('Quote Error:', error);
|
|
1402
|
-
* } else {
|
|
1403
|
-
* console.log('Quote Data:', result);
|
|
1404
|
-
* }
|
|
1405
|
-
* }
|
|
1406
|
-
* );
|
|
1407
|
-
*/
|
|
1408
|
-
getQuote(
|
|
1409
|
-
params: GetQuotesParams,
|
|
1410
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1411
|
-
): void {
|
|
1412
|
-
const currentUserId = params.userId;
|
|
1413
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1414
|
-
if (err) {
|
|
1415
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1416
|
-
} else if (data) {
|
|
1417
|
-
const userId = currentUserId;
|
|
1418
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1419
|
-
if (err) {
|
|
1420
|
-
callBack(err, null);
|
|
1421
|
-
} else if (jKey) {
|
|
1422
|
-
axiosInterceptor
|
|
1423
|
-
.post<Response>(`getQuote`, {
|
|
1424
|
-
userId,
|
|
1425
|
-
jKey,
|
|
1426
|
-
exchange: params.exchange,
|
|
1427
|
-
tradingSymbol: params.tradingSymbol,
|
|
1428
|
-
})
|
|
1429
|
-
.then((response) => {
|
|
1430
|
-
const { data } = response;
|
|
1431
|
-
callBack(null, data);
|
|
1432
|
-
})
|
|
1433
|
-
.catch((error: AxiosError) => {
|
|
1434
|
-
callBack(handleError(error), null);
|
|
1435
|
-
});
|
|
1436
|
-
} else {
|
|
1437
|
-
callBack("No jKey found", null);
|
|
1438
|
-
}
|
|
1439
|
-
});
|
|
1440
|
-
} else {
|
|
1441
|
-
callBack("No config data found", null);
|
|
1442
|
-
}
|
|
1443
|
-
});
|
|
1444
|
-
}
|
|
1445
|
-
/**
|
|
1446
|
-
* Fetches the Last Traded Price (LTP) for a given trading symbol on a specified exchange.
|
|
1447
|
-
*
|
|
1448
|
-
* This API provides a lightweight response containing the most recent traded price,
|
|
1449
|
-
* without full market depth. Ideal for simple UIs or price checks.
|
|
1450
|
-
*
|
|
1451
|
-
* @param {GetQuoteLTPParams} params - Request parameters including:
|
|
1452
|
-
* - `userId` (string): Unique Firstock user ID.
|
|
1453
|
-
* - `exchange` (string): Exchange name ("NSE", "BSE", "NFO", etc.).
|
|
1454
|
-
* - `tradingSymbol` (string): Trading symbol (e.g., "RELIANCE-EQ").
|
|
1455
|
-
*
|
|
1456
|
-
* @param {(error: Error | string | null, result: GetQuoteLTPResponse | null) => void} callBack -
|
|
1457
|
-
* Callback function invoked with the LTP response or error.
|
|
1458
|
-
*
|
|
1459
|
-
* @remarks
|
|
1460
|
-
* - Endpoint: `POST /getQuote/ltp`
|
|
1461
|
-
* - Requires valid session (`jKey`) from a successful login.
|
|
1462
|
-
* - Returns fields like company name, exchange, last traded price, and token.
|
|
1463
|
-
* - Useful for real-time updates, alerts, or minimal display dashboards.
|
|
1464
|
-
*
|
|
1465
|
-
* @returns {void}
|
|
1466
|
-
*
|
|
1467
|
-
*/
|
|
1468
|
-
getQuoteltp(
|
|
1469
|
-
params: GetQuoteLTPParams,
|
|
1470
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1471
|
-
): void {
|
|
1472
|
-
const currentUserId = params.userId;
|
|
1473
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1474
|
-
if (err) {
|
|
1475
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1476
|
-
} else if (data) {
|
|
1477
|
-
const userId = currentUserId;
|
|
1478
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1479
|
-
if (err) {
|
|
1480
|
-
callBack(err, null);
|
|
1481
|
-
} else if (jKey) {
|
|
1482
|
-
axiosInterceptor
|
|
1483
|
-
.post<Response>(`getQuote/ltp`, {
|
|
1484
|
-
userId,
|
|
1485
|
-
jKey,
|
|
1486
|
-
exchange: params.exchange,
|
|
1487
|
-
tradingSymbol: params.tradingSymbol,
|
|
1488
|
-
})
|
|
1489
|
-
.then((response) => {
|
|
1490
|
-
const { data } = response;
|
|
1491
|
-
callBack(null, data);
|
|
1492
|
-
})
|
|
1493
|
-
.catch((error: AxiosError) => {
|
|
1494
|
-
callBack(handleError(error), null);
|
|
1495
|
-
});
|
|
1496
|
-
} else {
|
|
1497
|
-
callBack("No jKey found", null);
|
|
1498
|
-
}
|
|
1499
|
-
});
|
|
1500
|
-
} else {
|
|
1501
|
-
callBack("No config data found", null);
|
|
1502
|
-
}
|
|
1503
|
-
});
|
|
1504
|
-
}
|
|
1505
|
-
/**
|
|
1506
|
-
* Get Last Traded Prices (LTP) for multiple instruments.
|
|
1507
|
-
*
|
|
1508
|
-
* This method is used to retrieve the latest traded prices (LTP) for multiple stocks or derivatives
|
|
1509
|
-
* from Firstock's trading API. It is efficient for watchlists or dashboards where only price is required.
|
|
1510
|
-
*
|
|
1511
|
-
* **Endpoint:** POST `/getMultiQuotes/ltp`
|
|
1512
|
-
*
|
|
1513
|
-
* **Required Fields in `params`:**
|
|
1514
|
-
* - `userId` (string): Firstock user ID.
|
|
1515
|
-
* - `data` (Array<{ exchange: string, tradingSymbol: string }>): List of instruments to fetch LTP for.
|
|
1516
|
-
*
|
|
1517
|
-
* **Usage Notes:**
|
|
1518
|
-
* - Each item in `data` must have `exchange` (e.g., NSE, BSE) and `tradingSymbol`.
|
|
1519
|
-
* - A valid `jKey` (from login) is auto-fetched from local config.
|
|
1520
|
-
* - This is a lightweight alternative to the full quote API.
|
|
1521
|
-
*
|
|
1522
|
-
* **Example Data:**
|
|
1523
|
-
* ```ts
|
|
1524
|
-
* {
|
|
1525
|
-
* userId: "AB1234",
|
|
1526
|
-
* data: [
|
|
1527
|
-
* { exchange: "NSE", tradingSymbol: "RELIANCE-EQ" },
|
|
1528
|
-
* { exchange: "NFO", tradingSymbol: "NIFTY03APR25C23500" }
|
|
1529
|
-
* ]
|
|
1530
|
-
* }
|
|
1531
|
-
* ```
|
|
1532
|
-
*
|
|
1533
|
-
* @param {GetMultiQuotesLTPParams} params - Parameters including userId and instrument list.
|
|
1534
|
-
* @param {(error: Error | string | null, result: GetMultiQuotesLTPResponse | null) => void} callBack - Callback with either error or result.
|
|
1535
|
-
*
|
|
1536
|
-
* @returns {void}
|
|
1537
|
-
*/
|
|
1538
|
-
getMultiQuotesltp(
|
|
1539
|
-
params: GetMultiQuotesLTPParams,
|
|
1540
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1541
|
-
): void {
|
|
1542
|
-
const currentUserId = params.userId;
|
|
1543
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1544
|
-
if (err) {
|
|
1545
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1546
|
-
} else if (data) {
|
|
1547
|
-
const userId = currentUserId;
|
|
1548
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1549
|
-
if (err) {
|
|
1550
|
-
callBack(err, null);
|
|
1551
|
-
} else if (jKey) {
|
|
1552
|
-
axiosInterceptor
|
|
1553
|
-
.post<Response>(`getMultiQuotes/ltp`, {
|
|
1554
|
-
userId,
|
|
1555
|
-
jKey,
|
|
1556
|
-
data: params.data,
|
|
1557
|
-
})
|
|
1558
|
-
.then((response) => {
|
|
1559
|
-
callBack(null, response.data);
|
|
1560
|
-
})
|
|
1561
|
-
.catch((error: AxiosError) => {
|
|
1562
|
-
callBack(handleError(error), null);
|
|
1563
|
-
});
|
|
1564
|
-
} else {
|
|
1565
|
-
callBack("No jKey found", null);
|
|
1566
|
-
}
|
|
1567
|
-
});
|
|
1568
|
-
} else {
|
|
1569
|
-
callBack("No config data found", null);
|
|
1570
|
-
}
|
|
1571
|
-
});
|
|
1572
|
-
}
|
|
1573
|
-
/**
|
|
1574
|
-
* Fetches detailed market quotes for multiple instruments (symbols) in a single API request.
|
|
1575
|
-
* This includes last traded price, best bid/ask levels, open interest, daily high/low/open/close prices, and more.
|
|
1576
|
-
*
|
|
1577
|
-
* @param {GetMultiQuotesParams} params - Parameters required for the API call.
|
|
1578
|
-
* @param {string} params.userId - Unique identifier for the Firstock user.
|
|
1579
|
-
* @param {Array<{ exchange: string, tradingSymbol: string }>} params.data - Array of exchange-symbol pairs for which quotes are to be retrieved.
|
|
1580
|
-
* @param {(error: Error | string | null, result: GetMultiQuotesResponse | null) => void} callBack - Callback function to handle the API response or error.
|
|
1581
|
-
*
|
|
1582
|
-
* @returns {void} - No return value; the result is returned via the callback.
|
|
1583
|
-
*
|
|
1584
|
-
* @example
|
|
1585
|
-
* getMultiQuotes(
|
|
1586
|
-
* {
|
|
1587
|
-
* userId: 'AB1234',
|
|
1588
|
-
* data: [
|
|
1589
|
-
* { exchange: 'NSE', tradingSymbol: 'RELIANCE-EQ' },
|
|
1590
|
-
* { exchange: 'NFO', tradingSymbol: 'NIFTY28JUN29P39000' }
|
|
1591
|
-
* ]
|
|
1592
|
-
* },
|
|
1593
|
-
* (err, result) => {
|
|
1594
|
-
* if (err) {
|
|
1595
|
-
* console.error(err);
|
|
1596
|
-
* } else {
|
|
1597
|
-
* console.log(result);
|
|
1598
|
-
* }
|
|
1599
|
-
* }
|
|
1600
|
-
* );
|
|
1601
|
-
*/
|
|
1602
|
-
getMultiQuotes(
|
|
1603
|
-
params: GetMultiQuotesParams,
|
|
1604
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1605
|
-
): void {
|
|
1606
|
-
const currentUserId = params.userId;
|
|
1607
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1608
|
-
if (err) {
|
|
1609
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1610
|
-
} else if (data) {
|
|
1611
|
-
const userId = currentUserId;
|
|
1612
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1613
|
-
if (err) {
|
|
1614
|
-
callBack(err, null);
|
|
1615
|
-
} else if (jKey) {
|
|
1616
|
-
axiosInterceptor
|
|
1617
|
-
.post<Response>(`getMultiQuotes`, {
|
|
1618
|
-
userId,
|
|
1619
|
-
jKey,
|
|
1620
|
-
data: params.data,
|
|
1621
|
-
})
|
|
1622
|
-
.then((response) => {
|
|
1623
|
-
callBack(null, response.data);
|
|
1624
|
-
})
|
|
1625
|
-
.catch((error: AxiosError) => {
|
|
1626
|
-
callBack(handleError(error), null);
|
|
1627
|
-
});
|
|
1628
|
-
} else {
|
|
1629
|
-
callBack("No jKey found", null);
|
|
1630
|
-
}
|
|
1631
|
-
});
|
|
1632
|
-
} else {
|
|
1633
|
-
callBack("No config data found", null);
|
|
1634
|
-
}
|
|
1635
|
-
});
|
|
1636
|
-
}
|
|
1637
|
-
/**
|
|
1638
|
-
* Searches for tradable instruments (scrips) across multiple exchanges based on partial or full input text.
|
|
1639
|
-
*
|
|
1640
|
-
* This API is useful for autocompleting trading symbols in client applications.
|
|
1641
|
-
*
|
|
1642
|
-
* @param {SearchScriptsParams} params - Parameters for the search request.
|
|
1643
|
-
* @param {string} params.userId - Unique identifier for the Firstock user (same as used during login).
|
|
1644
|
-
* @param {string} params.stext - Partial or full text to search trading symbols (e.g., "ITC").
|
|
1645
|
-
*
|
|
1646
|
-
* @param {(error: Error | string | null, result: SearchScriptsResponse | null) => void} callBack -
|
|
1647
|
-
* Callback to handle the response or error.
|
|
1648
|
-
*
|
|
1649
|
-
* @returns {void} - Result is returned via the callback function.
|
|
1650
|
-
*
|
|
1651
|
-
* @example
|
|
1652
|
-
* searchScripts(
|
|
1653
|
-
* {
|
|
1654
|
-
* userId: 'AB1234',
|
|
1655
|
-
* stext: 'ITC'
|
|
1656
|
-
* },
|
|
1657
|
-
* (err, result) => {
|
|
1658
|
-
* if (err) {
|
|
1659
|
-
* console.error('Error:', err);
|
|
1660
|
-
* } else {
|
|
1661
|
-
* console.log('Matched Scrips:', result?.data);
|
|
1662
|
-
* }
|
|
1663
|
-
* }
|
|
1664
|
-
* );
|
|
1665
|
-
*/
|
|
1666
|
-
searchScrips(
|
|
1667
|
-
params: SearchScriptsParams,
|
|
1668
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1669
|
-
): void {
|
|
1670
|
-
const currentUserId = params.userId;
|
|
1671
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1672
|
-
if (err) {
|
|
1673
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1674
|
-
} else if (data) {
|
|
1675
|
-
const userId = currentUserId;
|
|
1676
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1677
|
-
if (err) {
|
|
1678
|
-
callBack(err, null);
|
|
1679
|
-
} else if (jKey) {
|
|
1680
|
-
axiosInterceptor
|
|
1681
|
-
.post<Response>(`searchScrips`, {
|
|
1682
|
-
userId,
|
|
1683
|
-
jKey,
|
|
1684
|
-
stext: params.stext,
|
|
1685
|
-
})
|
|
1686
|
-
.then((response) => {
|
|
1687
|
-
const { data } = response;
|
|
1688
|
-
callBack(null, data);
|
|
1689
|
-
})
|
|
1690
|
-
.catch((error: AxiosError) => {
|
|
1691
|
-
callBack(handleError(error), null);
|
|
1692
|
-
});
|
|
1693
|
-
} else {
|
|
1694
|
-
callBack("No jKey found", null);
|
|
1695
|
-
}
|
|
1696
|
-
});
|
|
1697
|
-
} else {
|
|
1698
|
-
callBack("No config data found", null);
|
|
1699
|
-
}
|
|
1700
|
-
});
|
|
1701
|
-
}
|
|
1702
|
-
/**
|
|
1703
|
-
* Retrieves detailed security information about a trading symbol on a specific exchange.
|
|
1704
|
-
*
|
|
1705
|
-
* This API helps validate and fetch metadata like segment, instrument name, lot size, and token ID
|
|
1706
|
-
* before placing or managing trades (especially useful in derivatives trading).
|
|
1707
|
-
*
|
|
1708
|
-
* @param {GetSecurityInfoParams} params - Parameters for fetching security information.
|
|
1709
|
-
* @param {string} params.userId - Firstock user ID (used during login).
|
|
1710
|
-
* @param {string} params.exchange - Exchange name (e.g., "NSE", "BSE", "NFO").
|
|
1711
|
-
* @param {string} params.tradingSymbol - Symbol identifier (e.g., "IDEA-EQ", "NIFTY06MAR25C22500").
|
|
1712
|
-
*
|
|
1713
|
-
* @param {(error: Error | string | null, result: GetSecurityInfoResponse | null) => void} callBack -
|
|
1714
|
-
* Callback function to handle the response or error.
|
|
1715
|
-
*
|
|
1716
|
-
* @returns {void} - Result is returned via the callback.
|
|
1717
|
-
*
|
|
1718
|
-
* @example
|
|
1719
|
-
* getSecurityInfo(
|
|
1720
|
-
* {
|
|
1721
|
-
* userId: "AB1234",
|
|
1722
|
-
* exchange: "NSE",
|
|
1723
|
-
* tradingSymbol: "NIFTY"
|
|
1724
|
-
* },
|
|
1725
|
-
* (err, result) => {
|
|
1726
|
-
* if (err) {
|
|
1727
|
-
* console.error("Security Info Error:", err);
|
|
1728
|
-
* } else {
|
|
1729
|
-
* console.log("Security Info:", result?.data);
|
|
1730
|
-
* }
|
|
1731
|
-
* }
|
|
1732
|
-
* );
|
|
1733
|
-
*/
|
|
1734
|
-
|
|
1735
|
-
securityInfo(
|
|
1736
|
-
params: GetSecurityInfoParams,
|
|
1737
|
-
callBack: (error: Error | string | null, result: Response | null) => void
|
|
1738
|
-
): void {
|
|
1739
|
-
const currentUserId = params.userId;
|
|
1740
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1741
|
-
if (err) {
|
|
1742
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1743
|
-
} else if (data) {
|
|
1744
|
-
const userId = currentUserId;
|
|
1745
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1746
|
-
if (err) {
|
|
1747
|
-
callBack(err, null);
|
|
1748
|
-
} else if (jKey) {
|
|
1749
|
-
axiosInterceptor
|
|
1750
|
-
.post<Response>(`securityInfo`, {
|
|
1751
|
-
userId,
|
|
1752
|
-
jKey,
|
|
1753
|
-
exchange: params.exchange,
|
|
1754
|
-
tradingSymbol: params.tradingSymbol,
|
|
1755
|
-
})
|
|
1756
|
-
.then((response) => {
|
|
1757
|
-
const { data } = response;
|
|
1758
|
-
callBack(null, data);
|
|
1759
|
-
})
|
|
1760
|
-
.catch((error: AxiosError) => {
|
|
1761
|
-
callBack(handleError(error), null);
|
|
1762
|
-
});
|
|
1763
|
-
} else {
|
|
1764
|
-
callBack("No jKey found", null);
|
|
1765
|
-
}
|
|
1766
|
-
});
|
|
1767
|
-
} else {
|
|
1768
|
-
callBack("No config data found", null);
|
|
1769
|
-
}
|
|
1770
|
-
});
|
|
1771
|
-
}
|
|
1772
|
-
/**
|
|
1773
|
-
* Fetches the list of major stock market indices from the Firstock API.
|
|
1774
|
-
*
|
|
1775
|
-
* This method is useful for retrieving a consolidated list of index tokens and trading symbols
|
|
1776
|
-
* (such as NIFTY50, BANKNIFTY, SENSEX, etc.). These identifiers can then be used for quote-related APIs.
|
|
1777
|
-
*
|
|
1778
|
-
* The user must be logged in before calling this method (a valid `jKey` is required).
|
|
1779
|
-
*
|
|
1780
|
-
* @param {Object} params - Parameters required to fetch the index list.
|
|
1781
|
-
* @param {string} params.userId - Your Firstock user ID (must be the same as used during login).
|
|
1782
|
-
* @param {string} params.exchange - Exchange from which to fetch index list (e.g., "NSE" or "BSE").
|
|
1783
|
-
*
|
|
1784
|
-
* @param {function} callBack - A callback function receiving `(error, result)`.
|
|
1785
|
-
* On success, `result` contains an array of index objects:
|
|
1786
|
-
* - `exchange`: The exchange code (e.g., "NSE")
|
|
1787
|
-
* - `token`: Numeric identifier for the index
|
|
1788
|
-
* - `tradingSymbol`: Usable symbol for quote APIs
|
|
1789
|
-
* - `symbol`: Abbreviated symbol
|
|
1790
|
-
* - `idxname`: Descriptive name like "NIFTY 50", "BANKNIFTY", etc.
|
|
1791
|
-
*
|
|
1792
|
-
* @returns {void}
|
|
1793
|
-
*/
|
|
1794
|
-
indexList(
|
|
1795
|
-
params:GetIndexListParams,
|
|
1796
|
-
callBack: (error: Error | string | null, result: any | null) => void
|
|
1797
|
-
): void {
|
|
1798
|
-
const currentUserId = params.userId;
|
|
1799
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1800
|
-
if (err) {
|
|
1801
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1802
|
-
} else if (data) {
|
|
1803
|
-
const userId = currentUserId;
|
|
1804
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1805
|
-
if (err) {
|
|
1806
|
-
callBack(err, null);
|
|
1807
|
-
} else if (jKey) {
|
|
1808
|
-
axiosInterceptor
|
|
1809
|
-
.post(`indexList`, {
|
|
1810
|
-
userId,
|
|
1811
|
-
jKey,
|
|
1812
|
-
exchange: params.exchange,
|
|
1813
|
-
})
|
|
1814
|
-
.then((response) => {
|
|
1815
|
-
const { data } = response;
|
|
1816
|
-
callBack(null, data);
|
|
1817
|
-
})
|
|
1818
|
-
.catch((error: AxiosError) => {
|
|
1819
|
-
callBack(handleError(error), null);
|
|
1820
|
-
});
|
|
1821
|
-
} else {
|
|
1822
|
-
callBack("No jKey found", null);
|
|
1823
|
-
}
|
|
1824
|
-
});
|
|
1825
|
-
} else {
|
|
1826
|
-
callBack("No config data found", null);
|
|
1827
|
-
}
|
|
1828
|
-
});
|
|
1829
|
-
}
|
|
1830
|
-
/**
|
|
1831
|
-
* Fetches available expiry dates for a given trading symbol on the specified exchange.
|
|
1832
|
-
*
|
|
1833
|
-
* Useful for retrieving valid expiry dates for derivatives (Futures & Options) contracts.
|
|
1834
|
-
* A valid session (jKey) is required before calling this method.
|
|
1835
|
-
*
|
|
1836
|
-
* @param {Object} params - Parameters for the API call.
|
|
1837
|
-
* @param {string} params.userId - The Firstock user ID (must match the login session).
|
|
1838
|
-
* @param {string} params.exchange - The exchange code (e.g., "NSE", "NFO").
|
|
1839
|
-
* @param {string} params.tradingSymbol - The trading symbol (e.g., "NIFTY").
|
|
1840
|
-
*
|
|
1841
|
-
* @param {function} callBack - Callback function with `(error, result)` arguments.
|
|
1842
|
-
* On success, result contains an array of expiry dates.
|
|
1843
|
-
*/
|
|
1844
|
-
getExpiry(
|
|
1845
|
-
params: GetExpiryParams,
|
|
1846
|
-
callBack: (error: Error | string | null, result: any | null) => void
|
|
1847
|
-
): void {
|
|
1848
|
-
const currentUserId = params.userId;
|
|
1849
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1850
|
-
if (err) {
|
|
1851
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1852
|
-
} else if (data) {
|
|
1853
|
-
const userId = currentUserId;
|
|
1854
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1855
|
-
if (err) {
|
|
1856
|
-
callBack(err, null);
|
|
1857
|
-
} else if (jKey) {
|
|
1858
|
-
axiosInterceptor
|
|
1859
|
-
.post(`getExpiry`, {
|
|
1860
|
-
userId,
|
|
1861
|
-
jKey,
|
|
1862
|
-
exchange: params.exchange,
|
|
1863
|
-
tradingSymbol: params.tradingSymbol,
|
|
1864
|
-
})
|
|
1865
|
-
.then((response) => {
|
|
1866
|
-
const { data } = response;
|
|
1867
|
-
callBack(null, data);
|
|
1868
|
-
})
|
|
1869
|
-
.catch((error: AxiosError) => {
|
|
1870
|
-
callBack(handleError(error), null);
|
|
1871
|
-
});
|
|
1872
|
-
} else {
|
|
1873
|
-
callBack("No jKey found", null);
|
|
1874
|
-
}
|
|
1875
|
-
});
|
|
1876
|
-
} else {
|
|
1877
|
-
callBack("No config data found", null);
|
|
1878
|
-
}
|
|
1879
|
-
});
|
|
1880
|
-
}
|
|
1881
|
-
/**
|
|
1882
|
-
* Calculates total brokerage charges, taxes, and final costs for a given trade.
|
|
1883
|
-
*
|
|
1884
|
-
* This API helps estimate the net cost or profitability of a trade before execution.
|
|
1885
|
-
* It supports equity and derivatives markets and provides a breakdown of all applicable charges.
|
|
1886
|
-
*
|
|
1887
|
-
* Requires a valid login session (jKey). Ideal for use before placing or simulating trades.
|
|
1888
|
-
*
|
|
1889
|
-
* @param {Object} params - Parameters required for brokerage calculation.
|
|
1890
|
-
* @param {string} params.userId - The Firstock user ID (must match logged-in session).
|
|
1891
|
-
* @param {string} params.exchange - The exchange where the order is placed (e.g., "NSE", "NFO").
|
|
1892
|
-
* @param {string} params.tradingSymbol - The symbol of the instrument (e.g., "RELIANCE27FEB25F").
|
|
1893
|
-
* @param {string} params.transactionType - The order side: "B" (Buy) or "S" (Sell).
|
|
1894
|
-
* @param {string} params.Product - The product type: e.g., "M", "C", "I".
|
|
1895
|
-
* @param {string|number} params.quantity - Quantity of the instrument to trade.
|
|
1896
|
-
* @param {string|number} params.price - Price at which the order is to be placed.
|
|
1897
|
-
* @param {string|number} params.strike_price - Applicable strike price (mandatory for F&O instruments).
|
|
1898
|
-
* @param {string} params.inst_name - Instrument name, such as FUTSTK, OPTIDX, etc. (required for derivatives).
|
|
1899
|
-
* @param {string|number} params.lot_size - Lot size of the instrument (required for derivatives).
|
|
1900
|
-
*
|
|
1901
|
-
* @param {function} callBack - Callback function with signature `(error, result)`.
|
|
1902
|
-
* On success, result contains detailed brokerage information.
|
|
1903
|
-
*/
|
|
1904
|
-
|
|
1905
|
-
brokerageCalculator(
|
|
1906
|
-
params: BrokerageCalculatorParams,
|
|
1907
|
-
callBack: (error: Error | string | null, result: any | null) => void
|
|
1908
|
-
): void {
|
|
1909
|
-
const currentUserId = params.userId;
|
|
1910
|
-
readData((err: Error | string | null, data: ConfigData | null) => {
|
|
1911
|
-
if (err) {
|
|
1912
|
-
callBack(errorMessageMapping({ message: err instanceof Error ? err.message : err }), null);
|
|
1913
|
-
} else if (data) {
|
|
1914
|
-
const userId = currentUserId;
|
|
1915
|
-
checkifUserLoggedIn({ userId, jsonData: data }, (err: string | null, jKey: string | null) => {
|
|
1916
|
-
if (err) {
|
|
1917
|
-
callBack(err, null);
|
|
1918
|
-
} else if (jKey) {
|
|
1919
|
-
axiosInterceptor
|
|
1920
|
-
.post(`brokerageCalculator`, {
|
|
1921
|
-
userId,
|
|
1922
|
-
jKey,
|
|
1923
|
-
exchange: params.exchange,
|
|
1924
|
-
tradingSymbol: params.tradingSymbol,
|
|
1925
|
-
transactionType: params.transactionType,
|
|
1926
|
-
Product: params.Product,
|
|
1927
|
-
quantity: params.quantity,
|
|
1928
|
-
price: params.price,
|
|
1929
|
-
strike_price: params.strike_price,
|
|
1930
|
-
inst_name: params.inst_name,
|
|
1931
|
-
lot_size: params.lot_size,
|
|
1932
|
-
})
|
|
1933
|
-
.then((response) => {
|
|
1934
|
-
const { data } = response;
|
|
1935
|
-
callBack(null, data);
|
|
1936
|
-
})
|
|
1937
|
-
.catch((error: AxiosError) => {
|
|
1938
|
-
callBack(handleError(error), null);
|
|
1939
|
-
});
|
|
1940
|
-
} else {
|
|
1941
|
-
callBack("No jKey found", null);
|
|
1942
|
-
}
|
|
1943
|
-
});
|
|
1944
|
-
} else {
|
|
1945
|
-
callBack("No config data found", null);
|
|
1946
|
-
}
|
|
1947
|
-
});
|
|
1948
|
-
}
|
|
1949
|
-
|
|
1950
|
-
}
|
|
1951
|
-
|
|
1952
|
-
export default Firstock;
|