Main Content

Bond Pricing Tool for MATLAB Production Server Python Client

This example shows how to create an application that calculates a bond price from a simple formula. You run this example by entering the following known values into a simple graphical interface:

  • Coupon payment — C

  • Number of payments — N

  • Interest rate — i

  • Value of bond or option at maturity — M

The application calculates price (P) based on the following equation: P = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N.

The bond pricing tool demonstrates the following features of MATLAB® Production Server™:

  • Deploy a simple MATLAB function with a fixed number of inputs and a single output.

  • Deploy a MATLAB function with a simple GUI front-end for data input.

Write MATLAB Code

Save the following code under the filename pricecalc.m.

function price = pricecalc(value_at_maturity,coupon_payment, ...
                           interest_rate,num_payments)
%% Following formula has been obtained from wikipedia: http://en.wikipedia.org/wiki/Bond_valuation

% Copyright 1984-2019 The MathWorks, Inc.
% All Rights Reserved.

    C = coupon_payment;
    N = num_payments;
    i = interest_rate;
    M = value_at_maturity;
    
    price = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N;

end

Create a Deployable Archive with the Production Server Compiler App

To create the deployable archive for this example:

  1. In MATLAB, under the Apps tab, select the Production Server Compiler App.

  2. In the Application Type list, select Deployable Archive.

  3. In the Exported Functions field, add pricecalc.m.

  4. Under Application Information, ensure that the name pricecalc appears.

  5. Click Package.

The generated deployable archive, pricecalc.ctf, is located in the for_redistribution_files_only directory of the project’s folder.

Share the Deployable Archive on a Server

  1. Download the MATLAB Runtime, if needed, at https://www.mathworks.com/products/compiler/mcr. See Supported MATLAB Runtime Versions for MATLAB Production Server for more information.

  2. Create a server using mps-new. See Create Server Instance Using Command Line for more information.

  3. Specify the location of the MATLAB Runtime to the server by editing the server configuration file, main_config, and specifying a path for --mcr-root. See Server Configuration Properties for details.

  4. Start Server Instance Using Command Line and Verify Server Status.

  5. Copy the pricecalc.ctf file to the auto_deploy folder on the server for hosting.

Write Python Client Code

Before you write the client application, you must have the MATLAB® Production Server™ Python® client libraries installed on your system. For details, see Install the MATLAB Production Server Python Client.

Copy the following Python code:

 interestapp.py

The Python Client for MATLAB Production Server includes the matlab Python Module. Use the matlab.production_server.client.MWHttpClient to establish a connection to MATLAB Production Server. For more information on establishing a client connection, see Create Client Connection.

In the calculate_bond_price Python function, client_object calls the pricecalc MATLAB function, which takes four float scalars and returns a double scalar. The bond_price variable stores the result of the function call. calculate_bond_price is called whenever a user moves the value of the scale. For information on conversion between other Python and MATLAB data types, see Pass Data Between MATLAB Production Server and Python. For information on using MATLAB arrays in Python, see Use MATLAB Arrays in Python.

Run the Client Code

Before you attempt to build and run your client code, ensure that you have done the following:

Run the Python code. The tool opens.

Bond pricing tool window with a scale for each input

See Also

Related Topics