Python

Features ›› Reference Data ›› getStockSymbols ›› Sample Code ››
Parent Previous Next

Get Stock Symbols

We will first import "request" and "json" module, as we need those module for calling the API

Then, we will import our own url function from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.



Next, we create a function getStockSymbols, which require symbol to be passed in.

Inside the function, we will declare the serviceName and build the headerObj straight.




Then, we will create the final url, where we will use the url() function to get the API url, and append the headerObj to the API url.

We will then post the final_url and store the server response in the variable "response".



We will then retrieve a list of Currency from the 'response' variable and store in 'stockSymbols' variable.

After that, we create 1 empty list, Symbol_List and Company_List.



We will create a for-loop where we will loop through all the items in 'stockSymbols" variable.

As we loop through the items in ''stockSymbols', we will append the symbol and company to Symbol_List and Company_List respectively.



Then we create a 'if' condition, where we will check if the symbol passed in, is in the Symbol_List.

If the symbol passed in, is in the Symbol_List, we will retrieve the index of the symbol in the Symbol_List, and proceed to return the company in Company_List.

Else, we return "Not Found".



Entire Code:


import requests, json

from functions import url


def getStockSymbols(symbol):

   serviceName = 'getStockSymbols'

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': '',

                       'PIN': '',

                       'OTP': ''

                       }

                       }

   final_url="{0}?Header={1}".format(url(),json.dumps(headerObj))

   response = requests.post(final_url)


   stockSymbols = response.json()['Content']['ServiceResponse']['StockSymbolList']['StockSymbol']

   Symbol_List= []

   Company_List = []

   

   for i in range(len(stockSymbols)):

       symbol = stockSymbols[i]

       Symbol_List.append(symbol['symbol'])

       Company_List.append(symbol['company'])


   if symbols in Symbol_List:

       index = Symbol_List.index(symbols)

       return Company_List[index]

   else:

       return 'Record not found'

       


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Free EPub and documentation generator