Python

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

Place Limit Order


We will first import the "request" and "json" module, as we need those module to call 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.



Next, we will create a function called placeLimitOrder.

Inside the function, we will create the input variables.




Afterwards, we will build both 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


def placeLimitOrder():

   #Header

   serviceName = 'placeLimitOrder'

   userID = 'alan'

   PIN = '987654'

   OTP = '987654'

   #Content

   settlementAccount = '76'

   symbol = 'IBM'

   buyOrSell = 'sell'

   quantity = '10'

   limitPrice = '99.99'

   expirationType = 'Good-til-Date'

   maturityDate = '2019-06-30'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   contentObj = {

                       'Content': {

                       'settlementAccount': settlementAccount,

                       'symbol': symbol,

                       'buyOrSell': buyOrSell,

                       'quantity': quantity,

                       'limitPrice': limitPrice,

                       'expirationType': expirationType,

                       'maturityDate': maturityDate,

                       }

                       }

   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':

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

       print("You have successfully placed a limit order. The order ID is {}.".format(limitOrder['orderID']))


   elif errorCode == '010041':

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

   else:

       print(serviceRespHeader['ErrorText'])


placeLimitOrder()



Download


Go to Common Stuff Section

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