createPricingEngine
首发版本:3.00.5
语法
createPricingEngine(name, instrument, [handler], [engineConfig])
详情
创建实时估值定价引擎。该引擎支持调用 DolphinDB 内置估值定价函数,也支持用户传入自定义函数。
定价引擎目前支持以下资产类型的内置定价函数:
| 资产类型 | 函数名 | 描述 |
|---|---|---|
| 固收 (Fixed Income) | bondPricer | 债券定价(支持贴现债、零息债、固定利率债) |
| bondFuturesPricer | 国债期货定价 | |
| 利率 (Interest Rate) | irDepositPricer | 存款定价 |
| irFixedFloatingSwapPricer | 标准利率互换定价 | |
| 外汇 (FX) | fxForwardPricer | 外汇远期定价 |
| fxSwapPricer | 外汇掉期定价 | |
| fxEuropeanOptionPricer | 外汇欧式期权定价 |
参数
name 字符串标量,表示引擎名称。该参数是引擎在一个数据节点上的唯一标识,可包含字母,数字和下划线,但必须以字母开头。
instrument INSTRUMENT 类型标量或向量,表示待定价的金融工具。定价过程中,市场数据匹配规则遵循 instrumentPricer 的规范。
handler 可选参数,一个自定义函数,用于处理定价完成后的结果输出。
- 若 engineConfig 中设置
outputTime = true,函数签名为def(eventTime, name, date, npv); - 若 engineConfig 中设置
outputTime = false(默认值),函数签名为def(name, date, npv)。
参数说明如下:
- eventTime:NANOTIMESTAMP,触发事件的时间。
- name:STRING,定价对象的 instrumentId。
- date:DATE,定价日期。
- npv:DOUBLE,定价结果的净值。
engineConfig 可选参数,一个字典,用于设置引擎运行配置。字典包含如下键值对:
-
numThreads:可选,整型标量,表示工作线程数,默认 8。设为 0 表示同步执行。
-
maxQueueDepth:可选,整型标量,表示最大队列深度,默认 10,000,000。
-
useSystemTime:可选,布尔标量,表示是否使用系统时间作为事件时间。默认为 true,使用系统时间作为事件时间。
-
timeColumn:可选,字符串标量,指定时间列作为事件时间。若指定该参数,必须将 useSystemTime 设置为 false。
-
outputTime:可选,布尔标量,表示是否输出事件时间列。默认值为 false。
返回值
返回一个定价引擎句柄。
例子
例1. 本例中定义待定价金融工具 240025.IB,根据 instrumentPricer 的默认规则,该工具将使用名为
CNY_TREASURY_BOND 的市场数据。通过调用 appendMktData 接口将相关市场数据写入定价引擎。
try{dropStreamEngine("PRICING_ENGINE")}catch(ex){}
bondDict = {
"productType": "Cash",
"assetType": "Bond",
"bondType": "FixedRateBond",
"instrumentId": "240025.IB",
"start": 2024.12.25,
"maturity": 2031.12.25,
"issuePrice": 100.0,
"coupon": 0.0149,
"frequency": "Annual",
"dayCountConvention": "ActualActualISDA"
}
instrument = parseInstrument(bondDict)
pricingDate = 2025.08.18
curveDict = {
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": pricingDate,
"currency": "CNY",
"curveName": "CNY_TREASURY_BOND",
"dayCountConvention": "ActualActualISDA",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [
2025.09.18, 2025.11.18, 2026.02.18, 2026.08.18, 2027.08.18,
2028.08.18, 2030.08.18, 2032.08.18, 2035.08.18, 2040.08.18,
2045.08.18, 2055.08.18, 2065.08.18, 2075.08.18
],
"values": [
1.3000, 1.3700, 1.3898, 1.3865, 1.4299,
1.4471, 1.6401, 1.7654, 1.7966, 1.9930,
2.1834, 2.1397, 2.1987, 2.2225
] \ 100.0
}
discountCurve = parseMktData(curveDict)
share streamTable(1:0, `name`date`price, [STRING, DATE, DOUBLE]) as st
def myHandler(name, date, price) {
tableInsert(st, name, date, price)
}
engine = createPricingEngine("PRICING_ENGINE", [instrument], myHandler)
appendMktData(engine, discountCurve)
select * from st
| name | date | price |
|---|---|---|
| 240025.IB | 2025.08.18 | 99.60 |
例2. 本例以 10 年期国债期货 T2409 为例,先清理并创建 FuturesEngine,通过回调函数将引擎返回的 npv 写入结果表;随后分别构造可交割国债和国债期货合约,配置转换因子、结算日等关键参数,并插入国债收益率曲线作为贴现曲线,最终查看期货合约的估值结果。
// 清理历史同名引擎,避免重复创建时报错
try{dropStreamEngine("FuturesEngine")}catch(ex){}
// 创建结果表,用于接收并保存期货定价引擎回调返回的估值结果
share streamTable(1000:0, `engineName`instrumentId`npv`updateTime, [STRING, STRING, DOUBLE, TIMESTAMP]) as futuresResult
// 定义回调函数:每次引擎完成计算后,将合约名称、NPV 和更新时间写入结果表
def futuresHandler(name, date, npv){
tableInsert(futuresResult, "FuturesEngine", name, npv, now())
}
// 构造可交割国债,作为国债期货 T2409 的标的券
underlyingBond = parseInstrument({
"productType": "Cash",
"assetType": "Bond",
"bondType": "FixedRateBond",
"instrumentId": "UNDERLYING_BOND",
"start": 2020.09.15,
"maturity": 2030.09.15,
"issuePrice": 100.0,
"coupon": 0.03,
"frequency": "Annual",
"dayCountConvention": "ActualActualISDA",
"currency": "CNY",
"subType": "TREASURY_BOND",
"discountCurve": "CNY_TREASURY_BOND"
})
// 构造国债期货合约,重点设置标的券、名义票息、转换因子和交割结算日
futuresIns = parseInstrument({
"productType": "Futures",
"futuresType": "BondFutures",
"instrumentId": "T2409",
"nominal": 100.0,
"maturity": 2024.09.13,
"settlement": 2024.09.17,
"underlying": underlyingBond,
"nominalCouponRate": 0.03,
"conversionFactor": 0.9812
})
// 创建期货定价引擎,并绑定待估值合约与回调函数
engine = createPricingEngine("FuturesEngine", futuresIns, futuresHandler)
// 构造国债收益率曲线;curveName 需与标的券中的 discountCurve 保持一致
discountCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2024.09.10,
"currency": "CNY",
"curveName": "CNY_TREASURY_BOND",
"dayCountConvention": "ActualActualISDA",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [2024.12.10, 2025.12.10, 2026.12.10],
"values": [0.000180, 0.000190, 0.000200]
})
// 向引擎追加市场数据,触发估值计算并通过 futuresHandler 写入结果表
appendMktData(engine, discountCurve)
// 查看 T2409 的估值结果
select * from futuresResult
| engineName | instrumentId | npv | updateTime |
|---|---|---|---|
| FuturesEngine | T2409 | 117.84882688404052 | 2026.08.27 11:45:47.979 |
例3. 本例以 3 个月人民币定期存款为例,展示定价引擎如何根据本金、合同利率和贴现曲线,计算存款现值和应计利息。
// 清理历史同名引擎,避免重复创建时报错
try{dropStreamEngine("DepositEngine")}catch(ex){}
// 创建结果表,用于接收并保存存款定价引擎回调返回的估值结果
share streamTable(1000:0, `engineName`instrumentId`npv`updateTime, [STRING, STRING, DOUBLE, TIMESTAMP]) as depositResult
// 定义回调函数:每次引擎完成计算后,将合约名称、NPV 和更新时间写入结果表
def depositHandler(name, date, npv){
tableInsert(depositResult, "DepositEngine", name, npv, now())
}
// 构造 3 个月人民币定期存款。Deposit 的本金币种字段必须使用 notionalCurrency;
// 同时本金金额使用 notionalAmount,与利率互换示例保持一致。
depositIns = parseInstrument({
"productType": "Cash",
"assetType": "Deposit",
"instrumentId": "DEP_3M",
"start": 2026.08.27,
"maturity": 2026.11.27,
"rate": 0.018,
"dayCountConvention": "Actual360",
"notionalCurrency": "CNY",
"notionalAmount": 1000000.0,
"payReceive": "Receive",
"discountCurve": "CNY_DISC_CURVE"
})
// 创建存款定价引擎,并绑定待估值合约与回调函数
engine = createPricingEngine("DepositEngine", depositIns, depositHandler)
// 构造贴现曲线市场数据;curveName 与存款合约中的 discountCurve 对应
discountCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "CNY_DISC_CURVE",
"dayCountConvention": "Actual360",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [2026.09.27, 2026.11.27, 2027.02.27, 2027.08.27],
"values": [0.0155, 0.0165, 0.0170, 0.0180]
})
// 向引擎追加市场数据,触发估值计算并通过 depositHandler 写入结果表
appendMktData(engine, discountCurve)
// 查看 DEP_3M 的估值结果
select * from depositResult
| engineName | instrumentId | npv | updateTime |
|---|---|---|---|
| DepositEngine | DEP_3M | 1,000,407.2780936392 | 2026.08.27 13:40:39.896 |
例4. 本例以一笔 5 年期固定兑浮动利率互换为例,帮助用户理解如何用贴现曲线和浮动端参考曲线估算互换价值,并查看 NPV、两端 PV 和 DV01。
// 清理历史同名引擎,避免重复创建时报错
try{dropStreamEngine("SwapEngine")}catch(ex){}
// 创建结果表,用于接收并保存利率互换定价引擎回调返回的估值结果
share streamTable(
1000:0,
`engineName`instrumentId`npv`updateTime,
[STRING, STRING, DOUBLE, TIMESTAMP]
) as swapResult
// 定义回调函数:每次引擎完成计算后,将合约名称、NPV 和更新时间写入结果表
def swapHandler(name, date, npv){
tableInsert(swapResult, "SwapEngine", name, npv, now())
}
// 构造 5 年期固定兑浮动利率互换;
// FR_007 互换除了 discountCurve / forwardCurve
// 还需要 assetPriceCurve,用于浮动端历史定盘数据
irsIns = parseInstrument({
"productType": "Swap",
"swapType": "IrSwap",
"irSwapType": "IrFixedFloatingSwap",
"instrumentId": "IRS_FR007_5Y",
"start": 2026.08.27,
"maturity": 2031.08.27,
"frequency": "Quarterly",
"fixedRate": 0.0385,
"calendar": "CFET",
"fixedDayCountConvention": "Actual365",
"floatingDayCountConvention": "Actual365",
"payReceive": "Pay",
"iborIndex": "FR_007",
"spread": 0.0,
"notionalCurrency": "CNY",
"notionalAmount": 10000000.0,
// 三条曲线名称要和后面市场数据里的 curveName 保持一致
"discountCurve": "CNY_DISC_CURVE",
"forwardCurve": "FR007_FWD_CURVE",
"assetPriceCurve": "PRICE_FR_007"
})
// 创建利率互换定价引擎,并绑定待估值合约与回调函数
engine = createPricingEngine(
"SwapEngine",
irsIns,
swapHandler
)
// 构造人民币贴现曲线;curveName 与互换合约中的 discountCurve 对应
discountCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "CNY_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [
2026.11.27,
2027.08.27,
2028.08.27,
2029.08.27,
2031.08.27
],
"values": [
0.0180,
0.0185,
0.0190,
0.0200,
0.0220
]
})
// 构造 FR007 远期曲线;curveName 与互换合约中的 forwardCurve 对应
forwardCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "FR007_FWD_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [
2026.11.27,
2027.02.27,
2027.08.27,
2028.08.27,
2031.08.27
],
"values": [
0.0195,
0.0200,
0.0210,
0.0220,
0.0240
]
})
// 构造 FR007 历史定盘数据曲线。这是 IRS 定价非常关键的一条市场数据
// curveType 必须是 AssetPriceCurve
// curveName 要和 instrument 中的 assetPriceCurve 对应
fixingDates = 2026.05.19..2026.08.26
fixingRates = take(0.0190, size(fixingDates))
assetPriceCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "AssetPriceCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "PRICE_FR_007",
// 历史 FR007 定盘利率
"dates": fixingDates,
"values": fixingRates
})
// 一次性追加三条市场数据,触发估值
appendMktData(engine, [discountCurve, forwardCurve, assetPriceCurve])
// 查看估值结果
select * from swapResult
| engineName | instrumentId | npv | updateTime |
|---|---|---|---|
| SwapEngine | IRS_FR007_5Y | -701,240.0307065442 | 2026.08.27 14:35:55.568 |
例5. 本例以 6 个月 USD/CNY 外汇远期为例,说明远期汇率如何受即期汇率和两种货币利率曲线影响,并输出远期理论汇率和合约 NPV。
// 清理历史同名引擎
try{dropStreamEngine("FxFwdEngine")}catch(ex){}
// 结果表
share streamTable(1000:0, `engineName`instrumentId`npv`updateTime, [STRING, STRING, DOUBLE, TIMESTAMP]) as fxFwdResult
// 回调函数
def fxFwdHandler(name, date, npv){
tableInsert(fxFwdResult, "FxFwdEngine", name, npv, now())
}
// 构造外汇远期合约:使用 domesticCurve / foreignCurve
fxFwdIns = parseInstrument({
"productType": "Forward",
"forwardType": "FxForward",
"instrumentId": "USDCNY_6M_FWD",
"expiry": 2027.02.27,
"delivery": 2027.03.01,
"currencyPair": "USDCNY",
"direction": "Buy",
"notionalCurrency": "USD",
"notionalAmount": 1000000.0,
"strike": 7.10,
"domesticCurve": "CNY_DISC_CURVE",
"foreignCurve": "USD_DISC_CURVE"
})
// 创建引擎
engine = createPricingEngine("FxFwdEngine", fxFwdIns, fxFwdHandler)
// FX 即期汇率市场数据
spot = parseMktData({
"mktDataType": "Price",
"priceType": "FxSpotRate",
"referenceDate": 2026.08.27,
"spotDate": 2026.08.29,
"value": 7.1200,
"unit": "USDCNY"
})
// 人民币贴现曲线
cnyCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "CNY_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [2026.09.27, 2026.11.27, 2027.02.27],
"values": [0.0155, 0.0165, 0.0170]
})
// 美元贴现曲线
usdCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "USD",
"curveName": "USD_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [2026.09.27, 2026.11.27, 2027.02.27],
"values": [0.0440, 0.0430, 0.0415]
})
// 触发定价
appendMktData(engine, spot)
appendMktData(engine, cnyCurve)
appendMktData(engine, usdCurve)
// 查看结果
select * from fxFwdResult
| engineName | instrumentId | npv | updateTime |
|---|---|---|---|
| FxFwdEngine | USDCNY_6M_FWD | -65,285.58303391563 | 2026.08.27 14:16:54.106 |
例6. 本例以一笔 USD/CNY 外汇掉期为例,展示如何评估近端和远端两笔换汇现金流的价值,并得到掉期点、分段 PV 和整体 NPV。
// 清理历史同名引擎
try{dropStreamEngine("FxSwapEngine")}catch(ex){}
// 结果表
share streamTable(1000:0, `engineName`instrumentId`npv`updateTime, [STRING, STRING, DOUBLE, TIMESTAMP]) as fxSwapResult
// 回调函数
def fxSwapHandler(name, date, npv){
tableInsert(fxSwapResult, "FxSwapEngine", name, npv, now())
}
// 构造 USD/CNY 外汇掉期;同时使用 domesticCurve / foreignCurve 指定曲线名
fxSwapIns = parseInstrument({
"productType": "Swap",
"swapType": "FxSwap",
"instrumentId": "USDCNY_SWAP",
"currencyPair": "USDCNY",
"direction": "Buy",
"notionalCurrency": "USD",
"notionalAmount": 1000000.0,
"nearStrike": 7.12,
"nearExpiry": 2026.09.27,
"nearDelivery": 2026.09.29,
"farStrike": 7.15,
"farExpiry": 2027.03.27,
"farDelivery": 2027.03.29,
"domesticCurve": "CNY_DISC_CURVE",
"foreignCurve": "USD_DISC_CURVE"
})
// 创建引擎
engine = createPricingEngine("FxSwapEngine", fxSwapIns, fxSwapHandler)
// 即期汇率
spot = parseMktData({
"mktDataType": "Price",
"priceType": "FxSpotRate",
"referenceDate": 2026.08.27,
"spotDate": 2026.08.29,
"value": 7.1200,
"unit": "USDCNY"
})
// 人民币贴现曲线
cnyCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "CNY_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [2026.09.27, 2027.03.27],
"values": [0.0155, 0.0170]
})
// 美元贴现曲线
usdCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "USD",
"curveName": "USD_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Compounded",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [2026.09.27, 2027.03.27],
"values": [0.0440, 0.0415]
})
// 触发定价
appendMktData(engine, spot)
appendMktData(engine, cnyCurve)
appendMktData(engine, usdCurve)
// 查看结果
select * from fxSwapResult
| engineName | instrumentId | npv | updateTime |
|---|---|---|---|
| FxSwapEngine | USDCNY_SWAP | 109,673.96002695739 | 2026.08.27 14:20:02.733 |
例7. 本例以 3 个月 USD/CNY 欧式看涨期权为例,帮助用户了解期权定价中即期汇率、利率曲线和隐含波动率的作用,并查看理论价值及 Delta、Gamma、Vega 等希腊值。
// 清理历史同名引擎,避免重复创建时报错
try{dropStreamEngine("FxOptEngine")}catch(ex){}
// 创建结果表
share streamTable(
1000:0,
`engineName`instrumentId`npv`updateTime,
[STRING, STRING, DOUBLE, TIMESTAMP]
) as fxOptResult
// 定义回调函数
// createPricingEngine 默认 handler 签名为 def(name, date, npv)
def fxOptHandler(name, date, npv){
tableInsert(fxOptResult, "FxOptEngine", name, npv, now())
}
// 构造 3 个月 USD/CNY 欧式看涨期权;
// 1)underlying 用货币对
// 2)domesticCurve / foreignCurve 指定曲线名
fxOptIns = parseInstrument({
"productType": "Option",
"optionType": "EuropeanOption",
"assetType": "FxEuropeanOption",
"instrumentId": "USDCNY_CALL_7.2",
"notionalCurrency": "USD",
"notionalAmount": 1000000.0,
"strike": 7.20,
"maturity": 2026.11.27,
"delivery": 2026.11.29,
"payoffType": "Call",
"direction": "Buy",
"dayCountConvention": "Actual365",
"underlying": "USDCNY",
// 自定义两条曲线名称
// 后续 IrYieldCurve 的 curveName 必须与这里一致
"domesticCurve": "CNY_DISC_CURVE",
"foreignCurve": "USD_DISC_CURVE"
})
// 创建定价引擎
engine = createPricingEngine("FxOptEngine", fxOptIns, fxOptHandler)
// 构造 USD/CNY 即期汇率。
// 对定价引擎来说,FX spot 应使用 Price / FxSpotRate
// unit 使用货币对名称,和 underlying 对应
spot = parseMktData({
"mktDataType": "Price",
"priceType": "FxSpotRate",
"referenceDate": 2026.08.27,
"spotDate": 2026.08.29,
"value": 7.1200,
"unit": "USDCNY"
})
// 构造人民币贴现曲线
// curveName 与 instrument 中 domesticCurve 对应
cnyCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "CNY",
"curveName": "CNY_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Continuous",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [
2026.09.27,
2026.11.27,
2027.02.27
],
"values": [
0.0155,
0.0165,
0.0170
]
})
// 构造美元贴现曲线
// curveName 与 instrument 中 foreignCurve 对应
usdCurve = parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"referenceDate": 2026.08.27,
"currency": "USD",
"curveName": "USD_DISC_CURVE",
"dayCountConvention": "Actual365",
"compounding": "Continuous",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"frequency": "Annual",
"dates": [
2026.09.27,
2026.11.27,
2027.02.27
],
"values": [
0.0440,
0.0430,
0.0415
]
})
// 构造 USD/CNY 外汇波动率曲面
volSurf = parseMktData({
"mktDataType": "Surface",
"surfaceType": "FxVolatilitySurface",
"referenceDate": 2026.08.27,
"surfaceName": "USDCNY",
"currencyPair": "USDCNY",
"smileMethod": "Linear",
// termDates 和 volSmiles 一一对应
"termDates": [
2026.11.27,
2027.02.27
],
"volSmiles": [
{
"strikes": [6.8, 7.2, 7.6],
"vols": [0.0550, 0.0550, 0.0550]
},
{
"strikes": [6.8, 7.2, 7.6],
"vols": [0.0560, 0.0560, 0.0560]
}
]
})
// 一次性追加全部市场数据,触发估值
appendMktData(engine, [spot, cnyCurve, usdCurve, volSurf])
// 查看估值结果
select * from fxOptResult
| engineName | instrumentId | npv | updateTime |
|---|---|---|---|
| FxOptEngine | USDCNY_CALL_7.2 | 4,306.487653692539 | 2026.08.27 14:40:36.151 |
