forked from angel-one/smartapi-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamples.java
More file actions
402 lines (332 loc) · 12.7 KB
/
Examples.java
File metadata and controls
402 lines (332 loc) · 12.7 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.angelbroking.smartapi.sample;
import com.angelbroking.smartapi.SmartConnect;
import com.angelbroking.smartapi.http.exceptions.SmartAPIException;
import com.angelbroking.smartapi.models.Gtt;
import com.angelbroking.smartapi.models.GttParams;
import com.angelbroking.smartapi.models.Order;
import com.angelbroking.smartapi.models.OrderParams;
import com.angelbroking.smartapi.models.User;
import com.angelbroking.smartapi.smartTicker.SmartWSOnConnect;
import com.angelbroking.smartapi.smartTicker.SmartWSOnDisconnect;
import com.angelbroking.smartapi.smartTicker.SmartWSOnError;
import com.angelbroking.smartapi.smartTicker.SmartWSOnTicks;
import com.angelbroking.smartapi.smartTicker.SmartWebsocket;
import com.angelbroking.smartapi.ticker.OnConnect;
import com.angelbroking.smartapi.ticker.OnTicks;
import com.angelbroking.smartapi.ticker.SmartAPITicker;
import com.angelbroking.smartapi.utils.Constants;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("unused")
public class Examples {
public void getProfile(SmartConnect smartConnect) throws IOException, SmartAPIException {
User profile = smartConnect.getProfile();
}
/** CONSTANT Details */
/* VARIETY */
/*
* VARIETY_NORMAL: Normal Order (Regular)
* VARIETY_AMO: After Market Order
* VARIETY_STOPLOSS: Stop loss order
* VARIETY_ROBO: ROBO (Bracket) Order
*/
/* TRANSACTION TYPE */
/*
* TRANSACTION_TYPE_BUY: Buy TRANSACTION_TYPE_SELL: Sell
*/
/* ORDER TYPE */
/*
* ORDER_TYPE_MARKET: Market Order(MKT)
* ORDER_TYPE_LIMIT: Limit Order(L)
* ORDER_TYPE_STOPLOSS_LIMIT: Stop Loss Limit Order(SL)
* ORDER_TYPE_STOPLOSS_MARKET: Stop Loss Market Order(SL-M)
*/
/* PRODUCT TYPE */
/*
* PRODUCT_DELIVERY: Cash & Carry for equity (CNC)
* PRODUCT_CARRYFORWARD: Normal
* for futures and options (NRML)
* PRODUCT_MARGIN: Margin Delivery
* PRODUCT_INTRADAY: Margin Intraday Squareoff (MIS)
* PRODUCT_BO: Bracket Order
* (Only for ROBO)
*/
/* DURATION */
/*
* DURATION_DAY: Valid for a day
* DURATION_IOC: Immediate or Cancel
*/
/* EXCHANGE */
/*
* EXCHANGE_BSE: BSE Equity
* EXCHANGE_NSE: NSE Equity
* EXCHANGE_NFO: NSE Future and Options
* EXCHANGE_CDS: NSE Currency
* EXCHANGE_NCDEX: NCDEX Commodity
* EXCHANGE_MCX: MCX Commodity
*/
/** Place order. */
public void placeOrder(SmartConnect smartConnect) throws SmartAPIException, IOException {
OrderParams orderParams = new OrderParams();
orderParams.variety = Constants.VARIETY_STOPLOSS;
orderParams.quantity = 323;
orderParams.symboltoken = "1660";
orderParams.exchange = Constants.EXCHANGE_NSE;
orderParams.ordertype = Constants.ORDER_TYPE_STOPLOSS_LIMIT;
orderParams.tradingsymbol = "ITC-EQ";
orderParams.producttype = Constants.PRODUCT_INTRADAY;
orderParams.duration = Constants.DURATION_DAY;
orderParams.transactiontype = Constants.TRANSACTION_TYPE_BUY;
orderParams.price = 122.2;
orderParams.triggerprice = "209";
Order order = smartConnect.placeOrder(orderParams, "STOPLOSS");
System.out.print(order);
}
/** Modify order. */
public void modifyOrder(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Order modify request will return order model which will contain only
OrderParams orderParams = new OrderParams();
orderParams.quantity = 1;
orderParams.ordertype = Constants.ORDER_TYPE_LIMIT;
orderParams.tradingsymbol = "ASHOKLEY";
orderParams.symboltoken = "3045";
orderParams.producttype = Constants.PRODUCT_DELIVERY;
orderParams.exchange = Constants.EXCHANGE_NSE;
orderParams.duration = Constants.DURATION_DAY;
orderParams.price = 122.2;
String orderId = "201216000755110";
Order order = smartConnect.modifyOrder(orderId, orderParams, Constants.VARIETY_NORMAL);
}
/** Cancel an order */
public void cancelOrder(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Order modify request will return order model which will contain only
// order_id.
// Cancel order will return order model which will only have orderId.
Order order = smartConnect.cancelOrder("201009000000015", Constants.VARIETY_NORMAL);
}
/** Get order details */
public void getOrder(SmartConnect smartConnect) throws SmartAPIException, IOException {
JSONObject orders = smartConnect.getOrderHistory(smartConnect.getUserId());
System.out.print(orders);
// for (int i = 0; i < orders.size(); i++) {
// System.out.println(orders.get(i).orderId + " " + orders.get(i).status);
// }
}
/**
* Get last price for multiple instruments at once. USers can either pass
* exchange with tradingsymbol or instrument token only. For example {NSE:NIFTY
* 50, BSE:SENSEX} or {256265, 265}
*/
public void getLTP(SmartConnect smartConnect) throws SmartAPIException, IOException {
String exchange = "NSE";
String tradingSymbol = "SBIN-EQ";
String symboltoken = "3045";
JSONObject ltpData = smartConnect.getLTP(exchange, tradingSymbol, symboltoken);
}
/** Get tradebook */
public void getTrades(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Returns tradebook.
JSONObject trades = smartConnect.getTrades();
}
/** Get RMS */
public void getRMS(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Returns RMS.
JSONObject response = smartConnect.getRMS();
}
/** Get Holdings */
public void getHolding(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Returns Holding.
JSONObject response = smartConnect.getHolding();
}
/** Get Position */
public void getPosition(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Returns Position.
JSONObject response = smartConnect.getPosition();
}
/** convert Position */
public void convertPosition(SmartConnect smartConnect) throws SmartAPIException, IOException {
JSONObject requestObejct = new JSONObject();
requestObejct.put("exchange", "NSE");
requestObejct.put("oldproducttype", "DELIVERY");
requestObejct.put("newproducttype", "MARGIN");
requestObejct.put("tradingsymbol", "SBIN-EQ");
requestObejct.put("transactiontype", "BUY");
requestObejct.put("quantity", 1);
requestObejct.put("type", "DAY");
JSONObject response = smartConnect.convertPosition(requestObejct);
}
/** Create Gtt Rule */
public void createRule(SmartConnect smartConnect) throws SmartAPIException, IOException {
GttParams gttParams = new GttParams();
gttParams.tradingsymbol = "SBIN-EQ";
gttParams.symboltoken = "3045";
gttParams.exchange = "NSE";
gttParams.producttype = "MARGIN";
gttParams.transactiontype = "BUY";
gttParams.price = 100000.01;
gttParams.qty = 10;
gttParams.disclosedqty = 10;
gttParams.triggerprice = 20000.1;
gttParams.timeperiod = 300;
Gtt gtt = smartConnect.gttCreateRule(gttParams);
}
/** Modify Gtt Rule */
public void modifyRule(SmartConnect smartConnect) throws SmartAPIException, IOException {
GttParams gttParams = new GttParams();
gttParams.tradingsymbol = "SBIN-EQ";
gttParams.symboltoken = "3045";
gttParams.exchange = "NSE";
gttParams.producttype = "MARGIN";
gttParams.transactiontype = "BUY";
gttParams.price = 100000.1;
gttParams.qty = 10;
gttParams.disclosedqty = 10;
gttParams.triggerprice = 20000.1;
gttParams.timeperiod = 300;
Integer id = 1000051;
Gtt gtt = smartConnect.gttModifyRule(id, gttParams);
}
/** Cancel Gtt Rule */
public void cancelRule(SmartConnect smartConnect) throws SmartAPIException, IOException {
Integer id = 1000051;
String symboltoken = "3045";
String exchange = "NSE";
Gtt gtt = smartConnect.gttCancelRule(id, symboltoken, exchange);
}
/** Gtt Rule Details */
public void ruleDetails(SmartConnect smartConnect) throws SmartAPIException, IOException {
Integer id = 1000051;
JSONObject gtt = smartConnect.gttRuleDetails(id);
}
/** Gtt Rule Lists */
@SuppressWarnings("serial")
public void ruleList(SmartConnect smartConnect) throws SmartAPIException, IOException {
List<String> status = new ArrayList<String>() {
{
add("NEW");
add("CANCELLED");
add("ACTIVE");
add("SENTTOEXCHANGE");
add("FORALL");
}
};
Integer page = 1;
Integer count = 10;
JSONArray gtt = smartConnect.gttRuleList(status, page, count);
}
/** Historic Data */
public void getCandleData(SmartConnect smartConnect) throws SmartAPIException, IOException {
JSONObject requestObejct = new JSONObject();
requestObejct.put("exchange", "NSE");
requestObejct.put("symboltoken", "3045");
requestObejct.put("interval", "ONE_MINUTE");
requestObejct.put("fromdate", "2021-03-08 09:00");
requestObejct.put("todate", "2021-03-09 09:20");
String response = smartConnect.candleData(requestObejct);
}
/** Market Data */
/**
* @param smartConnect describes functionalities for interacting with the Angel Broking Smart API to manage trading and market-related operations.
* This describes all the modes of market data
* e.g., payload.put("mode", "FULL");
* payload.put("mode", "LTP");
* payload.put("mode", "OHLC");
*/
public void getMarketData(SmartConnect smartConnect) throws SmartAPIException, IOException {
// Create the payload object
JSONObject payload = new JSONObject();
payload.put("mode", "FULL");
JSONObject exchangeTokens = new JSONObject();
JSONArray nseTokens = new JSONArray();
nseTokens.put("3045");
exchangeTokens.put("NSE", nseTokens);
payload.put("exchangeTokens", exchangeTokens);
JSONObject response = smartConnect.marketData(payload);
}
public void tickerUsage(String clientId, String feedToken, String strWatchListScript, String task)
throws SmartAPIException {
SmartAPITicker tickerProvider = new SmartAPITicker(clientId, feedToken, strWatchListScript, task);
tickerProvider.setOnConnectedListener(new OnConnect() {
@Override
public void onConnected() {
System.out.println("subscribe() called!");
tickerProvider.subscribe();
}
});
tickerProvider.setOnTickerArrivalListener(new OnTicks() {
@Override
public void onTicks(JSONArray ticks) {
System.out.println("ticker data: " + ticks.toString());
}
});
/**
* connects to Smart API ticker server for getting live quotes
*/
tickerProvider.connect();
/**
* You can check, if websocket connection is open or not using the following
* method.
*/
boolean isConnected = tickerProvider.isConnectionOpen();
System.out.println(isConnected);
// After using SmartAPI ticker, close websocket connection.
// tickerProvider.disconnect();
}
public void smartWebSocketUsage(String clientId, String jwtToken, String apiKey, String actionType, String feedType)
throws SmartAPIException {
SmartWebsocket smartWebsocket = new SmartWebsocket(clientId, jwtToken, apiKey, actionType, feedType);
smartWebsocket.setOnConnectedListener(new SmartWSOnConnect() {
@Override
public void onConnected() {
smartWebsocket.runscript();
}
});
smartWebsocket.setOnDisconnectedListener(new SmartWSOnDisconnect() {
@Override
public void onDisconnected() {
System.out.println("onDisconnected");
}
});
/** Set error listener to listen to errors. */
smartWebsocket.setOnErrorListener(new SmartWSOnError() {
@Override
public void onError(Exception exception) {
System.out.println("onError: " + exception.getMessage());
}
@Override
public void onError(SmartAPIException smartAPIException) {
System.out.println("onError: " + smartAPIException.getMessage());
}
@Override
public void onError(String error) {
System.out.println("onError: " + error);
}
});
smartWebsocket.setOnTickerArrivalListener(new SmartWSOnTicks() {
@Override
public void onTicks(JSONArray ticks) {
System.out.println("ticker data: " + ticks.toString());
}
});
/**
* connects to Smart API ticker server for getting live quotes
*/
smartWebsocket.connect();
/**
* You can check, if websocket connection is open or not using the following
* method.
*/
boolean isConnected = smartWebsocket.isConnectionOpen();
System.out.println(isConnected);
// After using SmartAPI ticker, close websocket connection.
// smartWebsocket.disconnect();
}
/** Logout user. */
public void logout(SmartConnect smartConnect) throws SmartAPIException, IOException {
/** Logout user and kill session. */
JSONObject jsonObject = smartConnect.logout();
}
}