|
| 1 | +# The Kite Connect API Java client |
| 2 | +The official Java client for communicating with [Kite Connect API](https://kite.trade). |
| 3 | + |
| 4 | +Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio, stream live market data (WebSockets), and more, with the simple HTTP API collection. |
| 5 | + |
| 6 | +[Rainmatter](http://rainmatter.com) (c) 2016. Licensed under the MIT License. |
| 7 | + |
| 8 | +##Documentation |
| 9 | +- [Kite Connect HTTP API documentation](https://kite.trade/docs/connect/v1) |
| 10 | +- [Java library documentation](https://kite.trade/docs/javakiteconnect) |
| 11 | + |
| 12 | +##Usage |
| 13 | +- [Download jar file](https://github.com/rainmattertech/kiteconnectjava/raw/master/dist/kiteconnectjava.jar) and include it in your build path. |
| 14 | + |
| 15 | +- Include com.rainmatter.kiteconnect into build path from maven. Use version 1.4.4 |
| 16 | + |
| 17 | +## API usage |
| 18 | +```java |
| 19 | +// Initialize Kiteconnect using apiKey. |
| 20 | +Kiteconnect kiteSdk = new Kiteconnect("your_apiKey"); |
| 21 | + |
| 22 | +// Set userId. |
| 23 | +kiteSdk.setUserId("your_userId"); |
| 24 | + |
| 25 | +/* First you should get request_token, public_token using kitconnect login and then use request_token, public_token, api_secret to make any kiteconnect api call. |
| 26 | +Get login url. Use this url in webview to login user, after authenticating user you will get requestToken. Use the same to get accessToken. */ |
| 27 | +String url = kiteSdk.getLoginUrl(); |
| 28 | + |
| 29 | +// Get accessToken as follows, |
| 30 | +UserModel userModel = kiteSdk.requestAccessToken("request_token", "your_apiSecret"); |
| 31 | + |
| 32 | +// Set request token and public token which are obtained from login process. |
| 33 | +kiteSdk.setAccessToken(userModel.accessToken); |
| 34 | +kiteSdk.setPublicToken(userModel.publicToken); |
| 35 | + |
| 36 | +// Set session expiry callback. |
| 37 | +kiteSdk.registerHook(new SessionExpiryHook() { |
| 38 | + @Override |
| 39 | + public void sessionExpired() { |
| 40 | + System.out.println("session expired"); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +// Get margins returns margin model, you can pass equity or commodity as arguments to get margins of respective segments. |
| 45 | +Margins margins = kiteSdk.getMargins("equity"); |
| 46 | +System.out.println(margins.available.cash); |
| 47 | +System.out.println(margins.utilised.debits); |
| 48 | + |
| 49 | +/* Place order method requires a map argument which contains, |
| 50 | +tradingsymbol, exchange, transaction_type, order_type, quantity, product, price, trigger_price, disclosed_quantity, validity |
| 51 | +squareoff_value, stoploss_value, trailing_stoploss |
| 52 | +and variety which can be value can be regular, bo, co, amo. |
| 53 | +place order which will return order model which will have only orderId in the order model. |
| 54 | +
|
| 55 | +Following is an example param for SL order, |
| 56 | +if a call fails then KiteException will have error message in it |
| 57 | +Success of this call implies only order has been placed successfully, not order execution.*/ |
| 58 | +Map<String, Object> param = new HashMap<String, Object>(){ |
| 59 | + { |
| 60 | + put("quantity", "1"); |
| 61 | + put("order_type", "SL"); |
| 62 | + put("tradingsymbol", "HINDALCO"); |
| 63 | + put("product", "CNC"); |
| 64 | + put("exchange", "NSE"); |
| 65 | + put("transaction_type", "BUY"); |
| 66 | + put("validity", "DAY"); |
| 67 | + put("price", "158.0"); |
| 68 | + put("trigger_price", "157.5"); |
| 69 | + }}; |
| 70 | +Order order = kiteconnect.placeOrder(param, "regular"); |
| 71 | +System.out.println(order.orderId); |
| 72 | +``` |
| 73 | +For more details, take a look at Examples.java in sample directory. |
| 74 | + |
| 75 | +##WebSocket live streaming data |
| 76 | +```java |
| 77 | + |
| 78 | +/** To get live price use KiteTicker websocket connection. |
| 79 | +It is recommended to use only one websocket connection at any point of time and make sure you stop connection, once user goes out of app.*/ |
| 80 | +ArrayList tokens = new ArrayList<>(); |
| 81 | +tokens.add(53287175); |
| 82 | +KiteTicker tickerProvider = new KiteTicker(kiteconnect); |
| 83 | +tickerProvider.setOnConnectedListener(new OnConnect() { |
| 84 | + @Override |
| 85 | + public void onConnected() { |
| 86 | + try { |
| 87 | + /** Subscribe ticks for token. |
| 88 | + * By default, all tokens are subscribed for modeQuote.*/ |
| 89 | + tickerProvider.subscribe(tokens); |
| 90 | + } catch (IOException e) { |
| 91 | + e.printStackTrace(); |
| 92 | + } catch (WebSocketException e) { |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +tickerProvider.setOnDisconnectedListener(new OnDisconnect() { |
| 99 | + @Override |
| 100 | + public void onDisconnected() { |
| 101 | + // your code goes here |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +tickerProvider.setOnTickerArrivalListener(new OnTick() { |
| 106 | + @Override |
| 107 | + public void onTick(ArrayList<Tick> ticks) { |
| 108 | + System.out.println(ticks.size()); |
| 109 | + } |
| 110 | +}); |
| 111 | + |
| 112 | +// Connects to ticker server for getting live quotes. |
| 113 | +tickerProvider.connect(); |
| 114 | + |
| 115 | +// Disconnect from ticker server. |
| 116 | +tickerProvider.disconnect(); |
| 117 | + |
| 118 | +``` |
| 119 | +For more details about different mode of quotes and subscribing for them, take a look at Examples in sample directory. |
0 commit comments