Skip to content

Get instruments listed in an exchange

kncsolutions edited this page Aug 17, 2018 · 39 revisions

To get the list of the instruments listed in an exchange, the following methods are avalilable:

Get all instruments in an exchange.

public InstrumentResponse getAllInstruments(String exchange) {
  .......................
}

Here at the time of calling the method you just have to pass the value of the exchange. The value must be from the list of exchanges which you can get by calling the getSubscribedExchanges() method.

Example.

GfeedClient gc=new GfeedClient("WEB SOCKET ENDPOINT URL","API KEY");
...................................................
InstrumentResponse ir=gc.getAllInstruments("NSE");
if(ir.instruments!=null){
for(int i=0;i<ir.instruments.size();i++) {
    System.out.println(
	(i+1)+ "::Name : "+	ir.instruments.get(i).name+
	"  Symbol : "+	ir.instruments.get(i).tradeSymbol+
        " Identifier : "+	ir.instruments.get(i).identifier+
	"  Exchange : "+	ir.request.exchange
	);
   }
}

A word of caution is that, when calling this method it is going to dump really huge data set which might sometimes become difficult to handle. So, use this method only when necessary. In another way you can get the list of instruments in an exchange in a filtered way.

Get filtered list of instruments

public InstrumentResponse getAllInstruments(String exchange,Map<String,String> optionalParameters) {
  ..........................
}

Here along with the valid exchange value[use getSubscribedExchanges() to see the valid values], you can pass some optional parameters to filter the response using Map. Here point to be noted is that, for the map which has to be passed as argument, the Keys of the <key,value> pairs are constants and predefined. The valid values can be obtained by calling other methods.

The list of supported <key,value> pairs:

  1. <Constants.INSTRUMENT_TYPE_KEY,instrument_type_value>
    The valid values for instrument_type_value can be found in the response of getInstumentTypes(...) method.
  2. <Constants.PRODUCT_KEY,product_value>
    The valid values for product_value can be found in the response of getProducts(...) method.
  3. <Constants.OPTION_TYPE_KEY,option_type>
    The valid values for option_type can be found in the response of getOptionTypes(...) method.
  4. <Constants.EXPIRY_DATE_KEY,expiry_date>
    The valid values for expiry_date can be found in the response of getExpiryDates(...) method.
  5. <Constants.STRIKE_PRICE_KEY,strike_price>
    The valid values for strike_price can be found in the response of getStrikePrices(...) method.

    Sample optional parameters map
Map<String,String> optionalParameters = new HashMap<String,String>();
optionalParameters.put(Constants.INSTRUMENT_TYPE_KEY, "FUTIDX");
optionalParameters.put(Constants.PRODUCT_KEY, "BANKNIFTY");
optionalParameters.put(Constants.OPTION_TYPE_KEY, "CE");
optionalParameters.put(Constants.EXPIRY_DATE_KEY, "30Aug2018");
optionalParameters.put(Constants.STRIKE_PRICE_KEY, "8000");

The response structure

The return type of the getAllInstruments(...) method consists of the following fields:

  1. InstrumentRequest request
  2. List<Instrument> instruments
    This is the actual list of instruments where each element contains the following information. All are public fields.
Variable Name Type
identifier String
name String
expiry String
strikePrice double
product String
priceQuotationUnit String
optionType String
productMonth String
underlyingAsset String
underlyingAssetExpiry String
indexName String
tradeSymbol String

Example
To print the identifier for the first instrument in the instruments List you an write

InstrumentResponse ir=gc.getAllInstruments("NSE");
System.out.println(ir.instruments.get(0).identifier);
  1. String responseMessage
    The valid response equals to Constants.INSTRUMENT_RESULT_RESPONSE.