Skip to content

Subscribe to realtime snapshot data

kncsolutions edited this page Nov 5, 2018 · 6 revisions

To get market data on every predefined interval, you have to subscribe to real time snapshot data. In response it will return market data on every specified period. The method is:

public SubscribeSnapshotResponse getRealTimeSnapshotData(String exchange,String instrumentIdentifier,
		String periodicity) {
	......................
}

or

public SubscribeSnapshotResponse getRealTimeSnapshotData(String exchange,String instrumentIdentifier,
		 String periodicity,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.
  • periodicity : The valid value is from the set {Constants.PERIODICITY_MINUTE,Constants.PERIODICITY_HOUR}.
    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");
...................................................
SubscribeSnapshotResponse  reponse = gc.getRealTimeSnapshotData("NSE","SBIN",Constants.PERIODICITY_MINUTE);
System.out.println(response.toString);
.........................
Map<String,Boolean> op=new HashMap<String,Boolean>();
op.put(Constants.UNSUBSCRIBE_KEY,true);
gc.getRealTimeSnapshotData("NSE","SBIN",Constants.PERIODICITY_MINUTE,op);

The response structure

The getRealTimeSnapshotData(...) method returns SubscribeSnapshotResponse. It contains the following information.

Variable Name Type
exchange String
instrumentIdentifier String
periodicity String
lastTradeTime long
tradedQty int
close double
high double
low double
open double
openInterest double
message String

Example
To print the last traded quantity of the instrument from the reponse in the above example use the following code:

System.out.println(reponse.tradedQty);

[NB:]

  • For a valid response the message equals to Constants.REAL_TIME_SNAPSHOT_RESULT_MESSAGE.
  • The lastTradeTime is returned in unix timestamp.
    To get the time in human readable format use the following method.
public String getLastTradedTime() {
......
}

This returns the last traded time in "yyyy-MM-dd'T'HH:mm:ss" format.

Example

System.out.println(response.getLastTradedTime());

Changelog

Using version 1.0.1-SNAPSHOT, you have to use callback to stream real time snapshot data. Following methods are to be called for the purpose.

public void streamRealTimeSnapshotData(String exchange,String instrumentIdentifier,
		String periodicity,OnSnapshotDataArrival onSnapshotDataArrival) {{
..................
}

or

public void streamRealTimeSnapshotData(String exchange,String instrumentIdentifier,
		String periodicity,Map<String,Boolean> optionalParams,OnSnapshotDataArrival onSnapshotDataArrival) {

....................
}

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.
  • periodicity : The valid value is from the set {Constants.PERIODICITY_MINUTE,Constants.PERIODICITY_HOUR}.
    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.streamRealTimeSnapshotData("NSE","SBIN",Constants.PERIODICITY_MINUTE,new OnSnapshotDataArrival(){
         @Override
         public void onSnapshotdataArrival(SubscribeSnapshotResponse response){
           System.out.println("-------SNAPSHOT DATA-------");
           System.out.println(response.toString());
         }
     });

The successful call to the api will fire the callback OnSnapshotDataArrival() and you will receive the response in onSnapshotdataArrival(SubscribeSnapshotResponse response) as parameter. The response structure is same as previous.
[NB: The getRealTimeSnapshotData(..) methods are not available in version 1.0.1-SNAPSHOT]