主要内容

本页采用了机器翻译。点击此处可查看英文原文。

适用于 MATLAB Production Server Python 客户端的债券定价工具

R2026b

此示例显示如何创建一个通过简单公式计算债券价格的应用程序。通过在简单的图形界面中输入以下已知值来运行此示例:

  • 息票支付 - C

  • 支付次数 - N

  • 利率 - i

  • 债券或期权到期价值 - M

该应用程序根据以下公式计算价格 (P):P = C * ( (1 - (1 + i)^-N) / i ) + M * (1 + i)^-N。

债券定价工具展示了 MATLAB® Production Server™ 的以下特点:

  • 部署一个具有固定数量的输入和一个输出的简单 MATLAB 函数。

  • 部署一个带有简单 GUI 前端的 MATLAB 函数用于数据输入。

编写 MATLAB 代码

将以下代码保存为文件名 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

使用 Production Server 存档编译器创建可部署存档

要为此示例创建可部署存档:

  1. 在 MATLAB 中的 "App" 选项卡下,选择“Production Server 存档编译器”。

  2. 在 Exported Functions 字段中,添加 pricecalc.m。

  3. 在 Application Information 下,确保存档名称为 pricecalc。

  4. 点击打包。

生成的可部署存档 pricecalc.ctf 位于工程文件夹的输出目录中。

在服务器上共享可部署存档

  1. 如果需要,请从 https://www.mathworks.com/products/compiler/mcr 下载 MATLAB Runtime。有关详细信息,请参阅MATLAB Production Server 支持的 MATLAB Runtime 版本。

  2. 使用 mps-new 创建服务器。有关详细信息,请参阅使用命令行创建服务器实例。

  3. 通过编辑服务器配置文件 main_config 并指定 --mcr-root 的路径,将 MATLAB Runtime 的位置指定到服务器。有关详细信息,请参阅Server Configuration 属性。

  4. 使用命令行启动服务器实例 和 验证服务器状态。

  5. 将 pricecalc.ctf 文件复制到服务器上的 auto_deploy 文件夹进行托管。

编写 Python 客户端代码

在编写客户端应用程序之前,必须在系统上安装 MATLAB® Production Server™ Python® 客户端库。有关详细信息,请参阅安装 MATLAB Production Server Python 客户端。

复制以下 Python 代码:

from tkinter import *
import matlab
from matlab.production_server import client
window = Tk()
client_object = client.MWHttpClient('http://localhost:9910')

window.title("Bond Pricing Tool")
window.geometry('350x350')

#Face Value Scale

fv_lbl = Label(window, text="Face Value")
fv_lbl.grid(column=0, row=0)

face_val = IntVar()

fv_scl = Scale(window, variable = face_val, from_ = 0, to = 1000)
fv_scl.grid(column=1, row=0)
fv_scl['orient'] = HORIZONTAL

#Coupon Yield

cy_lbl = Label(window, text="Coupon Yield")
cy_lbl.grid(column=0, row=1)

coupon_yield = IntVar()

cy_scl = Scale(window, variable = coupon_yield, from_ = 0, to = 1000)
cy_scl.grid(column=1, row=1)
cy_scl['orient'] = HORIZONTAL

#Interest Rate

ir_lbl = Label(window, text="Interest Rate(%)")
ir_lbl.grid(column=0, row=2)

interest_rate = IntVar()

ir_scl = Scale(window, variable = interest_rate, from_ = 0, to = 100)
ir_scl.grid(column=1, row=2)
ir_scl['orient'] = HORIZONTAL

#Number of Payments

np_lbl = Label(window, text="Number of Payments")
np_lbl.grid(column=0, row=3)

num_of_payments = IntVar()

np_scl = Scale(window, variable = num_of_payments, from_ = 0, to = 100)
np_scl.grid(column=1, row=3)
np_scl['orient'] = HORIZONTAL


#Bond Price

def calculate_bond_price(val):
    bond_price = client_object.pricecalc.pricecalc(float(face_val.get()), float(coupon_yield.get()), interest_rate.get()/100, float(num_of_payments.get()))
    bp_lbl = Label(window, text="Bond Price: " + str(round(bond_price,2)))
    bp_lbl.grid(column=1, row=4)

fv_scl['command'] = calculate_bond_price
cy_scl['command'] = calculate_bond_price
np_scl['command'] = calculate_bond_price
ir_scl['command'] = calculate_bond_price

window.mainloop()

Python Client for MATLAB Production Server 包含 matlab Python 模块。使用matlab.production_server.client.MWHttpClient建立与 MATLAB Production Server 的连接。有关建立客户端连接的详细信息,请参阅 创建客户端连接。

在 calculate_bond_price Python 函数中,client_object 调用 pricecalc MATLAB 函数,该函数接受四个 float 标量并返回一个 double 标量。bond_price 变量存储函数调用的结果。每当用户移动刻度的值时,就会调用 calculate_bond_price。有关其他 Python 和 MATLAB 数据类型之间转换的信息,请参阅 在 MATLAB Production Server 和 Python 之间传递数据。有关在 Python 中使用 MATLAB 数组的信息,请参阅 在 Python 中使用 MATLAB 数组。

运行客户端代码

在尝试构建和运行客户端代码之前,请确保已完成以下操作:

运行 Python 代码。该工具将打开。

Bond pricing tool window with a scale for each input

另请参阅

主题