Get FX Forward Contracts
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 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 getFXForwardContracts
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 getCurrencyList import getCurrencyList
def getFXForwardContracts():
#Header
serviceName = 'getFXForwardContracts'
userID = 'alan'
PIN = '987654'
OTP = '987654'
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':
forwardcontract_list = response.json()['Content']['ServiceResponse']['FXForwardContractList']
if forwardcontract_list == {}:
print("No record found!")
else:
forwardcontract_list = forwardcontract_list['FXForwardContract']
recordCount = getRecord(forwardcontract_list)
if recordCount > 1:
for i in range(0,recordCount,1):
FXForwardContract = forwardcontract_list[i]
print("\nOpen 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: {}".format(FXForwardContract['baseCurrency']))
print("Base Currency Interest Rate: {}".format(FXForwardContract['baseCurrencyInterestRate']))
qcurrency_name = getCurrencyList(FXForwardContract['quoteCurrency'])
print("Quote Currency: {}".format(qcurrency_name[0]))
#print("Quote Currency: {}".format(FXForwardContract['quoteCurrency']))
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 recordCount == 0:
print("\nOpen Date: {}".format(forwardcontract_list['open_date']))
print("Period: {}".format(forwardcontract_list['period']))
print("Amount: {}".format(forwardcontract_list['amount']))
print("Maturity date: {}".format(forwardcontract_list['maturity_date']))
print("Spot Rate: {}".format(forwardcontract_list['spot_rate']))
print("Quote Currency Account ID: {}".format(forwardcontract_list['quoteCurrencyAccountID']))
print("Base Currency Account ID: {}".format(forwardcontract_list['baseCurrencyAccountID']))
bcurrency_name = getCurrencyList(FXForwardContract['baseCurrency'])
print("Base Currency: {}".format(bcurrency_name[0]))
#print("Base Currency: {}".format(forwardcontract_list['baseCurrency']))
print("Base Currency Interest Rate: {}".format(forwardcontract_list['baseCurrencyInterestRate']))
qcurrency_name = getCurrencyList(FXForwardContract['quoteCurrency'])
print("Quote Currency: {}".format(qcurrency_name[0]))
#print("Quote Currency: {}".format(forwardcontract_list['quoteCurrency']))
print("Reference Number: {}".format(forwardcontract_list['referenceNumber']))
print("Customer ID: {}".format(forwardcontract_list['customerID']))
print("Forward Rate: {}".format(forwardcontract_list['forward_rate']))
print("Quote Currency Interest Rate: {}".format(forwardcontract_list['quoteCurrencyInterestRate']))
print("FX Forward ID: {}".format(forwardcontract_list['FX_ForwardID']))
print("Status: {}".format(forwardcontract_list['status']))
elif errorCode == '010041':
print("OTP has expired.\nYou will receiving a SMS")
else:
print(serviceRespHeader['ErrorText'])
getFXForwardContracts()
Created with the Personal Edition of HelpNDoc: News and information about help authoring tools and software