You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The official Java client for communicating with Kite Connect API.
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.
Include com.zerodhatech.kiteconnect into build path from maven. Use version 3.4.0
To use javakiteconnect in Android, you need to include jar file in the libs directory and add the following line in you module's gradle file compile files('libs/kiteconnect.jar')
API usage
// Initialize Kiteconnect using apiKey.KiteConnectkiteSdk = newKiteConnect("your_apiKey");
// Set userId.kiteSdk.setUserId("your_userId");
/* 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.Get login url. Use this url in webview to login user, after authenticating user you will get requestToken. Use the same to get accessToken. */Stringurl = kiteSdk.getLoginUrl();
// Get accessToken as follows,Useruser = kiteSdk.generateSession("request_token", "your_apiSecret");
// Set request token and public token which are obtained from login process.kiteSdk.setAccessToken(userModel.accessToken);
kiteSdk.setPublicToken(userModel.publicToken);
// Set session expiry callback.kiteSdk.setSessionExpiryHook(newSessionExpiryHook() {
@OverridepublicvoidsessionExpired() {
System.out.println("session expired");
}
});
// Get margins returns margin model, you can pass equity or commodity as arguments to get margins of respective segments.Marginmargins = kiteSdk.getMargins("equity");
System.out.println(margins.available.cash);
System.out.println(margins.utilised.debits);
/** Place order method requires a orderParams argument which contains, * tradingsymbol, exchange, transaction_type, order_type, quantity, product, price, trigger_price, disclosed_quantity, validity * squareoff_value, stoploss_value, trailing_stoploss * and variety (value can be regular, bo, co, amo) * place order will return order model which will have only orderId in the order model * Following is an example param for LIMIT order, * if a call fails then KiteException will have error message in it * Success of this call implies only order has been placed successfully, not order execution. */OrderParamsorderParams = newOrderParams();
orderParams.quantity = 1;
orderParams.orderType = Constants.ORDER_TYPE_LIMIT;
orderParams.tradingsymbol = "ASHOKLEY";
orderParams.product = Constants.PRODUCT_CNC;
orderParams.exchange = Constants.EXCHANGE_NSE;
orderParams.transactionType = Constants.TRANSACTION_TYPE_BUY;
orderParams.validity = Constants.VALIDITY_DAY;
orderParams.price = 122.2;
orderParams.triggerPrice = 0.0;
orderParams.tag = "myTag"; //tag is optional and it cannot be more than 8 characters and only alphanumeric is allowedOrderorder = kiteConnect.placeOrder(orderParams, Constants.VARIETY_REGULAR);
System.out.println(order.orderId);
For more details, take a look at Examples.java in sample directory.
WebSocket live streaming data
/** To get live price use websocket connection. * 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. * custom url points to new endpoint which can be used till complete Kite Connect 3 migration is done. */KiteTickertickerProvider = newKiteTicker(kiteConnect.getAccessToken(), kiteConnect.getApiKey());
tickerProvider.setOnConnectedListener(newOnConnect() {
@OverridepublicvoidonConnected() {
/** Subscribe ticks for token. * By default, all tokens are subscribed for modeQuote. * */tickerProvider.subscribe(tokens);
tickerProvider.setMode(tokens, KiteTicker.modeFull);
}
});
tickerProvider.setOnDisconnectedListener(newOnDisconnect() {
@OverridepublicvoidonDisconnected() {
// your code goes here
}
});
/** Set listener to get order updates.*/tickerProvider.setOnOrderUpdateListener(newOnOrderUpdate() {
@OverridepublicvoidonOrderUpdate(Orderorder) {
System.out.println("order update "+order.orderId);
}
});
tickerProvider.setOnTickerArrivalListener(newOnTicks() {
@OverridepublicvoidonTicks(ArrayList<Tick> ticks) {
NumberFormatformatter = newDecimalFormat();
System.out.println("ticks size "+ticks.size());
if(ticks.size() > 0) {
System.out.println("last price "+ticks.get(0).getLastTradedPrice());
System.out.println("open interest "+formatter.format(ticks.get(0).getOi()));
System.out.println("day high OI "+formatter.format(ticks.get(0).getOpenInterestDayHigh()));
System.out.println("day low OI "+formatter.format(ticks.get(0).getOpenInterestDayLow()));
System.out.println("change "+formatter.format(ticks.get(0).getChange()));
System.out.println("tick timestamp "+ticks.get(0).getTickTimestamp());
System.out.println("tick timestamp date "+ticks.get(0).getTickTimestamp());
System.out.println("last traded time "+ticks.get(0).getLastTradedTime());
System.out.println(ticks.get(0).getMarketDepth().get("buy").size());
}
}
});
tickerProvider.setTryReconnection(true);
//maximum retries and should be greater than 0tickerProvider.setMaximumRetries(10);
//set maximum retry interval in secondstickerProvider.setMaximumRetryInterval(30);
/** connects to com.zerodhatech.com.zerodhatech.ticker server for getting live quotes*/tickerProvider.connect();
/** You can check, if websocket connection is open or not using the following method.*/booleanisConnected = tickerProvider.isConnectionOpen();
System.out.println(isConnected);
/** set mode is used to set mode in which you need tick for list of tokens. * Ticker allows three modes, modeFull, modeQuote, modeLTP. * For getting only last traded price, use modeLTP * For getting last traded price, last traded quantity, average price, volume traded today, total sell quantity and total buy quantity, open, high, low, close, change, use modeQuote * For getting all data with depth, use modeFull*/tickerProvider.setMode(tokens, KiteTicker.modeLTP);
// Unsubscribe for a token.tickerProvider.unsubscribe(tokens);
// After using com.zerodhatech.com.zerodhatech.ticker, close websocket connection.tickerProvider.disconnect();
For more details about the different mode of quotes and subscribing for them, take a look at Examples in sample directory.
Breaking changes from 3.2.1 to 3.3.1
Margin calculation data
version 3.3.1
version 3.2.1
option_premium(double)
optionPremium(double)
Breaking changes from 3.1.14 to 3.2.1
Holding (model)
version 3.1.14
version 3.2.1
lastPrice(String)
lastPrice(Double)
t1Quantity(String)
t1Quantity(int)
pnl(String)
pnl(Double)
quantity(String)
quantity(int)
averagePrice(String)
averagePrice(Double)
Removed:
version 3.1.14
version 3.2.1
accountId
NA
Tick (model)
version 3.1.14
version 3.2.1
volumeTradedToday(double)
volumeTradedToday(long)
Change attribute for indices tick will have change percent value against
the previously sent absolute change value.