Python

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

Get FX Forward Contract


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 getCurrencyList as one of the output is currency, and we want to show the currency's country name.

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



Next, we create a function getFXForwardContract

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 getCurrencyList import getCurrencyList


def getFXForwardContract():

   #Header

   serviceName = 'getFXForwardContract'

   userID = 'alan'

   PIN = '987654'

   OTP = '987654'

   #Content

   fxForwardID = '5'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': userID,

                       'PIN': PIN,

                       'OTP': OTP

                       }

                       }

   contentObj = {

                       'Content': {

                       'FX_ForwardID': fxForwardID

                       }

                       }

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

       FXForwardContract = response.json()['Content']['ServiceResponse']['FXForwardContract']['FXForwardContract']

       print("Open Date: {}".format(FXForwardContract['open_date']))

       print("Period: {}".format(FXForwardContract['period']))

       print("Amount: {}".format(FXForwardContract['amount']))

       print("Maturity date: {}".format(FXForwardContract['maturity_date']))

       print("Spot Rate: {}".format(FXForwardContract['spot_rate']))

       print("Quote Currency Account ID: {}".format(FXForwardContract['quoteCurrencyAccountID']))

       print("Base Currency Account ID: {}".format(FXForwardContract['baseCurrencyAccountID']))

       bcurrency_name = getCurrencyList(FXForwardContract['baseCurrency'])

       print("Base Currency: {}".format(bcurrency_name[0]))

       print("Base Currency Interest Rate: {}".format(FXForwardContract['baseCurrencyInterestRate']))

       qcurrency_name = getCurrencyList(FXForwardContract['quoteCurrency'])

       print("Quote Currency: {}".format(qcurrency_name[0]))

       print("Reference Number: {}".format(FXForwardContract['referenceNumber']))

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

       print("Forward Rate: {}".format(FXForwardContract['forward_rate']))

       print("Quote Currency Interest Rate: {}".format(FXForwardContract['quoteCurrencyInterestRate']))

       print("FX Forward ID: {}".format(FXForwardContract['FX_ForwardID']))

       print("Status: {}".format(FXForwardContract['status']))


   elif errorCode == '010041':

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

   else:

       print(serviceRespHeader['ErrorText'])


getFXForwardContract()



Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Free PDF documentation generator