Python

Features ›› Wealth ›› getStockOrders ›› Sample Code ››
Parent Previous Next

Get Stock Orders


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

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

We will also import a reference data API getStockSymbols as one of the output is symbol, and we want to show symbol company name.

The Reference Data API can be referred under Reference Data Sub-Menu



Next, we create a function getStockOrders

Inside the function, we will create the input variables.



Then we build the header object


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".

The GlobalErrorID is then retrieved and stored in variable "errorCode".


If the errorCode is '010000', it means that there is no error, and we will proceed to check whether there are any records. If there are no records, we will display 'No record Found'. If there are records, we will use the getRecord() function to check whether there is only 1 record, or more than 1 records, and display the results accordingly.


**NOTE: JSON output for 1 record and more than 1 record is different due to JSON structure. Hence, we use getRecord() function to retrieve the number of records.


If the errorCode is '010041', it means that the OTP provided has expired. We will then display 'OTP has expired. You will be receiving a SMS'.

Else, we will display the ErrorText.





Entire Code:

import requests, json

from functions import url,getRecord

from getStockSymbols import getStockSymbols


def getStockOrders():

   #Header

   serviceName = 'getStockOrders'

   userID = 'KelvanTan'

   PIN = '000000'

   OTP = '000000'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP':OTP

                       }

                       }

   

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

   response = requests.post(final_url)

   serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']

   errorCode = serviceRespHeader['GlobalErrorID']


   if errorCode == '010000':

       stock_order_list = response.json()['Content']['ServiceResponse']['StockOrderList']

       if stock_order_list == {}:

           print("No record found!")

       else:

           stock_order_list = stock_order_list['StockOrder']

           recordCount = getRecord(stock_order_list)

           if recordCount > 1:

               for i in range(0,recordCount,1):

                   stockOrder = stock_order_list[i]

                   print("\nBuy Or Sell: {}".format(stockOrder['buy_or_sell']))

                   print("Customer ID: {}".format(stockOrder['customerID']))

                   print("Expiration Type: {}".format(stockOrder['expiration_type']))

                   print("Intial Spot Price: {}".format(stockOrder['initial_spot_price']))

                   print("Maturity Date: {}".format(stockOrder['maturity_date']))

                   print("Order ID: {}".format(stockOrder['orderID']))

                   print("Order Type: {}".format(stockOrder['orderType']))

                   print("Order Date: {}".format(stockOrder['order_date']))

                   print("Order Status: {}".format(stockOrder['order_status']))

                   print("Price At Execution: {}".format(stockOrder['price_at_execution']))

                   print("Price At Order : {}".format(stockOrder['price_at_order']))

                   print("Quantity: {}".format(stockOrder['quantity']))

                   print("Settlement Account ID: {}".format(stockOrder['settlementAccountID']))

                   symbol_company = getStockSymbols(stockOrder['stockSymbol'])

                   print("Stock Symbol Name: {}".format(symbol_company))


                   

           elif recordCount == 0:

                   print("\nBuy Or Sell: {}".format(stock_order_list['buy_or_sell']))

                   print("Customer ID: {}".format(stock_order_list['customerID']))

                   print("Expiration Type: {}".format(stock_order_list['expiration_type']))

                   print("Intial Spot Price: {}".format(stock_order_list['initial_spot_price']))

                   print("Maturity Date: {}".format(stock_order_list['maturity_date']))

                   print("Order ID: {}".format(stock_order_list['orderID']))

                   print("Order Type: {}".format(stock_order_list['orderType']))

                   print("Order Date: {}".format(stock_order_list['order_date']))

                   print("Order Status: {}".format(stock_order_list['order_status']))

                   print("Price At Execution: {}".format(stock_order_list['price_at_execution']))

                   print("Price At Order : {}".format(stock_order_list['price_at_order']))

                   print("Quantity: {}".format(stock_order_list['quantity']))

                   print("Settlement Account ID: {}".format(stock_order_list['settlementAccountID']))

                   symbol_company = getStockSymbols(stock_order_list['stockSymbol'])

                   print("Stock Symbol Name: {}".format(symbol_company))

                   

   elif errorCode == '010041':

       print("OTP has expired.\nYou will receiving a SMS")

   else:

       print(serviceRespHeader['ErrorText'])


getStockOrders()



Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Easy EBook and documentation generator