Subscribe to realtime data
To get market data on every tick, you have to subscribe to real time data. In response it will return market data every tick. The method is:
public SubscribeRealTimeResponse getRealTimeData(String exchange,String instrumentIdentifier) {
.......
}
or
public SubscribeRealTimeResponse getRealTimeData(String exchange,String instrumentIdentifier,
Map<String,Boolean> optionalParams) {
...................
}
The necessary parameters:
-
exchange
: The value must be from the list of exchanges which you can get by calling the getSubscribedExchanges() method.
-
instrumentIdentifier
: You have to pass the identifier for the instrument as a parameter. The identifier you can find by calling the getAllInstruments(...) or getSearchedInstruments(...) methods.
In the second variant there is an optional parameter.
-
optionalParams
: The <Key,value> pair is <Constants.UNSUBSCRIBE_KEY
,value>
where value is either_true_ or false. To unsubscribe use the true value.
Example
GfeedClient gc=new GfeedClient("WEB SOCKET ENDPOINT URL","API KEY");
...................................................
SubscribeRealTimeResponse reponse = gc.getRealTimeData("NSE","SBIN");
System.out.println(response.toString);
.........................
Map<String,Boolean> op=new HashMap<String,Boolean>();
op.put(Constants.UNSUBSCRIBE_KEY,true);
gc.getRealTimeData("NSE","SBIN",op);
The getRealTimeData(...) method returns SubscribeRealTimeResponse
. It contains the following information.
Variable Name | Type |
---|---|
exchange | String |
instrumentIdentifier | String |
lastTradeTime | long |
serverTime | long |
averageTradedPrice | double |
buyprice | double |
buyQty | int |
close | double |
high | double |
low | double |
ltp | double |
lastTradedQty | int |
open | double |
openInterest | double |
quotationLot | int |
sellPrice | double |
sellQty | int |
totalQtyTraded | long |
value | double |
preOpen | boolean |
message | String |
Example
To print the last traded price of the instrument from thereponse
in the above example use the following code:
System.out.println(reponse.ltp);
[NB:]
- For a valid response the message equals to
Constants.REAL_TIME_RESULT_MESSAGE
.
- The lastTradeTime and serverTime is returned in unix timestamp.
To get the time in human readable format two methods are there.
public String getLastTradedTime() {
......
}
public String getserverTime() {
........
}
This returns the last traded time in "yyyy-MM-dd'T'HH:mm:ss" format.
Example
System.out.println(response.getLastTradedTime());
System.out.println(response.getserverTime());
Using version 1.0.1-SNAPSHOT, you have to use callback to stream real time data. Following methods are to be called for the purpose.
public void streamRealTimeData(String exchange,String instrumentIdentifier,OnRealtimeDataArrival onRealtimeDataArrival) {
..................
}
or
public void streamRealTimeData(String exchange,String instrumentIdentifier,
Map<String,Boolean> optionalParams,OnRealtimeDataArrival onRealtimeDataArrival) {
....................
}
The necessary parameters:
-
exchange
: The value must be from the list of exchanges which you can get by calling the getSubscribedExchanges() method.
-
instrumentIdentifier
: You have to pass the identifier for the instrument as a parameter. The identifier you can find by calling the getAllInstruments(...) or getSearchedInstruments(...) methods.
In the second variant there is an optional parameter.
-
optionalParams
: The <Key,value> pair is <Constants.UNSUBSCRIBE_KEY
,value>
where value is either_true_ or false. To unsubscribe use the true value.
-
onRealtimeDataArrival
: The callback for real time data message arrival.
Example
GfeedClient gc=new GfeedClient(url,
apikey);
try {
Thread.sleep(5000);
gc.streamRealTimeData("NSE","SBIN",new OnRealtimeDataArrival(){
@Override
public void onRealtimedataArrival(SubscribeRealTimeResponse response){
System.out.println("-------REALTIME DATA-------");
System.out.println(response.toString());
}
});
The successful call to the api will fire the callback OnRealtimeDataArrival()
and you will receive the response in onRealtimedataArrival(SubscribeRealTimeResponse response)
as parameter. The response structure is same as previous.
[NB: The getRealTimeData(..)
methods are not available in version 1.0.1-SNAPSHOT]
Dhelm® is a trademark of the KNC Solutions Private Limited.
For any query email at developer@kncsolutions.in
- Home
- Initialization and connection to web socket api
- Getting the list for subscribed exchanges
- Get instruments listed in an exchange
- Search instruments with keyword
- Get Last Quote
- Get snapshot data
- Get historical data
- Get required details
- Get account details
- Get market messages
- Get exchange messages
- Subscribe to realtime data
- Subscribe to realtime snapshot data
- Glossary of Methods
- Conclusion