It looks like you're new here. If you want to get involved, click one of these buttons!
public Double getMarketCurrentPriceforNifty(KiteConnect kiteConnect) throws WebSocketException, IOException, KiteException {
final KiteTicker tickerProvider = new KiteTicker(kiteConnect.getAccessToken(), kiteConnect.getApiKey());
ArrayList<Long> tokens = new ArrayList<>();
try {
tokens.add(Long.parseLong("256265"));
tickerUsage(kiteConnect, tokens, tickerProvider);
} finally {
// Unsubscribe for a token.
tickerProvider.unsubscribe(tokens);
// After using com.zerodhatech.com.zerodhatech.ticker, close websocket connection.
tickerProvider.disconnect();
}
//setting this to test the logic
//setNiftyPrice(9199.05);
return niftyPrice;
}
public void tickerUsage(KiteConnect kiteConnect, ArrayList<Long> tokens, KiteTicker tickerProvider) throws IOException, WebSocketException, KiteException {
tickerProvider.setOnConnectedListener(new OnConnect() {
@Override
public void onConnected() {
/** Subscribe ticks for token.
* By default, all tokens are subscribed for modeQuote.
* */
tickerProvider.subscribe(tokens);
tickerProvider.setMode(tokens, KiteTicker.modeLTP);
}
});
tickerProvider.setOnDisconnectedListener(new OnDisconnect() {
@Override
public void onDisconnected() {
// your code goes here
}
});
/** Set listener to get order updates.*/
tickerProvider.setOnOrderUpdateListener(new OnOrderUpdate() {
@Override
public void onOrderUpdate(Order order) {
System.out.println("order update "+order.orderId);
}
});
/** Set error listener to listen to errors.*/
tickerProvider.setOnErrorListener(new OnError() {
@Override
public void onError(Exception exception) {
//handle here.
}
@Override
public void onError(KiteException kiteException) {
//handle here.
}
@Override
public void onError(String error) {
System.out.println(error);
}
});
tickerProvider.setOnTickerArrivalListener(new OnTicks() {
@Override
public void onTicks(ArrayList<Tick> ticks) {
NumberFormat formatter = new DecimalFormat();
System.out.println("ticks size "+ticks.size());
if(ticks.size() > 0) {
niftyPrice = Double.isNaN(ticks.get(0).getLastTradedPrice())?0.0:ticks.get(0).getLastTradedPrice();
System.out.println("LTP:"+niftyPrice);
System.out.println("open::"+ticks.get(0).getOpenPrice());
System.out.println("close::"+ticks.get(0).getClosePrice());
System.out.println("high::"+ticks.get(0).getHighPrice());
System.out.println("low::"+ticks.get(0).getLowPrice());
}
}
});
// Make sure this is called before calling connect.
tickerProvider.setTryReconnection(true);
//maximum retries and should be greater than 0
tickerProvider.setMaximumRetries(10);
//set maximum retry interval in seconds
tickerProvider.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.*/
boolean isConnected = tickerProvider.isConnectionOpen();
System.out.println(isConnected);
tickerProvider.setMode(tokens, KiteTicker.modeLTP);
}