forked from dsuraj72/smartapi-javascript
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathapi_test.js
More file actions
253 lines (211 loc) · 6.85 KB
/
api_test.js
File metadata and controls
253 lines (211 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
let { SmartAPI, WebSocketClient, WebSocketV2 } = require("../lib");
let smart_api = new SmartAPI({
api_key: "smartapi_key", // PROVIDE YOUR API KEY HERE
// OPTIONAL : If user has valid access token and refresh token then it can be directly passed to the constructor
// access_token: "YOUR_ACCESS_TOKEN",
// refresh_token: "YOUR_REFRESH_TOKEN"
});
// // If user does not have valid access token and refresh token then use generateSession method
// }
// smart_api
// .generateSession('CLIENT_CODE', 'PASSWORD', 'TOTP')
// .then((data) => {
// console.log(data);
// return smart_api.getProfile();
// // // User Methods
// // // return smart_api.logout()
// // // return smart_api.getRMS();
// // // Order Methods
// // // return smart_api.placeOrder({
// // // "variety": "NORMAL",
// // // "tradingsymbol": "SBIN-EQ",
// // // "symboltoken": "3045",
// // // "transactiontype": "BUY",
// // // "exchange": "NSE",
// // // "ordertype": "LIMIT",
// // // "producttype": "INTRADAY",
// // // "duration": "DAY",
// // // "price": "19500",
// // // "squareoff": "0",
// // // "stoploss": "0",
// // // "quantity": "1"
// // // })
// // // return smart_api.modifyOrder({
// // // "orderid": "201130000006424",
// // // "variety": "NORMAL",
// // // "tradingsymbol": "SBIN-EQ",
// // // "symboltoken": "3045",
// // // "transactiontype": "BUY",
// // // "exchange": "NSE",
// // // "ordertype": "LIMIT",
// // // "producttype": "INTRADAY",
// // // "duration": "DAY",
// // // "price": "19500",
// // // "squareoff": "0",
// // // "stoploss": "0",
// // // "quantity": "1"
// // // });
// // // return smart_api.cancelOrder({
// // // "variety": "NORMAL",
// // // "orderid": "201130000006424"
// // // });
// // // return smart_api.getOrderBook();
// smart_api.getOrderBook().then((data)=>{
// console.log(data);
// })
// // // return smart_api.getTradeBook();
// // // Portfolio Methods
// // // return smart_api.getHolding();
// // // return smart_api.getPosition();
// // // return smart_api.convertPosition({
// // // "exchange": "NSE",
// // // "oldproducttype": "DELIVERY",
// // // "newproducttype": "MARGIN",
// // // "tradingsymbol": "SBIN-EQ",
// // // "transactiontype": "BUY",
// // // "quantity": 1,
// // // "type": "DAY"
// // // });
// // // GTT Methods
// // // return smart_api.createRule({
// // // "tradingsymbol" : "SBIN-EQ",
// // // "symboltoken" : "3045",
// // // "exchange" : "NSE",
// // // "producttype" : "MARGIN",
// // // "transactiontype" : "BUY",
// // // "price" : 100000,
// // // "qty" : 10,
// // // "disclosedqty": 10,
// // // "triggerprice" : 200000,
// // // "timeperiod" : 365
// // // })
// // // return smart_api.modifyRule({
// // // "id" : 1000014,
// // // "symboltoken" : "3045",
// // // "exchange" : "NSE",
// // // "qty" : 10
// // // })
// // // return smart_api.cancelRule({
// // // "id" : 1000014,
// // // "symboltoken" : "3045",
// // // "exchange" : "NSE"
// // // })
// // // return smart_api.ruleDetails({
// // // "id" : 25
// // // })
// // // return smart_api.ruleList({
// // // "status" : ["NEW","CANCELLED"],
// // // "page" : 1,
// // // "count" : 10
// // // })
// // // Historical Methods
// // // return smart_api.getCandleData({
// // // "exchange": "NSE",
// // // "symboltoken": "3045",
// // // "interval": "ONE_MINUTE",
// // // "fromdate": "2021-02-10 09:00",
// // // "todate": "2021-02-10 09:20"
// // // })
// Market Data Methods
// smart_api.marketData({
// "mode": "FULL",
// "exchangeTokens": {
// "NSE": [
// "3045"
// ]
// }
// }).then((data) => {
// console.log(JSON.stringify(data, null, 2));
// // console.log(JSON.stringify(data))
// });
// search Scrip Methods
smart_api
.searchScrip({
exchange: "BSE",
searchscrip: "Titan",
})
.then((data) => {
console.log(data);
});
// get all holding method
// smart_api.getAllHolding().then((data)=>{
// console.log(data);
// })
// })
// .then((data) => {
// console.log('PROFILE::', data);
// })
// .catch((ex) => {
// console.log('EX::', ex);
// });
// // // smart_api.generateToken("YOUR_REFRESH_TOKEN")
// // // .then((data) => {
// // // console.log(data)
// // // });
// smart_api.setSessionExpiryHook(customSessionHook);
// function customSessionHook() {
// // USER CAN GENERATE NEW JWT HERE
// console.log("User loggedout");
// }
// ########################### Socket Sample Code Starts Here ###########################
// Old Websocket
// let web_socket = new WebSocket({
// client_code: "CLIENT_CODE",
// feed_token: "FEED_TOKEN"
// });
// web_socket.connect()
// .then(() => {
// web_socket.runScript("SCRIPT", "TASK") // SCRIPT: nse_cm|2885, mcx_fo|222900 TASK: mw|sfi|dp
// setTimeout(function () {
// web_socket.close()
// }, 3000)
// })
// web_socket.on('tick', receiveTick)
// function receiveTick(data) {
// console.log("receiveTick:::::", data)
// }
// ########################### Socket Sample Code Ends Here ###########################
// ########################### Socket Sample Code Starts Here ###########################
// New websocket
// let web_socket = new WebSocketClient({
// clientcode: "CLIENT_CODE",
// jwttoken: 'JWT_TOKEN',
// apikey: "API_KEY",
// feedtype: "FEED_TYPE",
// });
// web_socket.connect()
// .then(() => {
// web_socket.fetchData("subscribe", "order_feed"); // ACTION_TYPE: subscribe | unsubscribe FEED_TYPE: order_feed
// setTimeout(function () {
// web_socket.close()
// }, 60000)
// });
// web_socket.on('tick', receiveTick);
// function receiveTick(data) {
// console.log("receiveTick:::::", data);
// }
// ########################### Socket V2 Sample Code Start Here ###########################
// let web_socket = new WebSocketV2({
// jwttoken: 'JWT_TOKEN',
// apikey: 'API_KEY',
// clientcode: 'Client_code',
// feedtype: 'FEED_TYPE',
// });
// web_socket.connect().then(() => {
// let json_req = {
// correlationID: 'abcde12345',
// action: 1,
// mode: 2,
// exchangeType: 1,
// tokens: ['1594'],
// };
// web_socket.fetchData(json_req);
// web_socket.on('tick', receiveTick);
// function receiveTick(data) {
// console.log('receiveTick:::::', data);
// }
// setTimeout(() => {
// web_socket.close();
// }, 10000);
// });
// ########################### Socket V2 Sample Code End Here ###########################