Python

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

Get Stock Order


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

Then, we will import our own url 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 getStockOrder

Inside the function, we will create the input variables.



Then we build the header and content object


Moving on, we will create the final URL, where we will use the url() function to get the API URL, and append the headerObj and contentObj 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 display the result.

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

Else, we will display the ErrorText.



Entire Code:

import requests, json

from functions import url

from getStockSymbols import getStockSymbols


def getStockOrder():

   #Header

   serviceName = 'getStockOrder'

   userID = 'KelvanTan'

   PIN = '000000'

   OTP = '000000'

   #Content

   orderID = '54'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   contentObj = {

                       'Content': {

                       'orderID': orderID

                       }

                       }

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

   response = requests.post(final_url)

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

   errorCode = serviceRespHeader['GlobalErrorID']

   

   if errorCode == '010000':

       stockOrder = response.json()['Content']['ServiceResponse']['StockOrder']['StockOrder']

       print("Buy 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 errorCode == '010041':

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

   else:

       print(serviceRespHeader['ErrorText'])


getStockOrder()



Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Benefits of a Help Authoring Tool