getInstrumentField
首发版本:3.00.4.1
语法
getInstrumentField(instrument, key,
[default])
详情
从金融工具中提取指定字段(key)对应的值:
- 未设置 default 时,若字段存在有效值,则返回该值,否则返回空值;
- 设置 default 时,若字段存在有效值,则将其类型转为 default 的类型返回,否则返回 default。
注:
自 3.00.6 版本起,支持通过
instrument.key 和
instrument["key"] 两种简洁语法,从金融工具中提取指定字段(key)对应的值。参数
instrument INSTRUMENT 类型标量或向量,表示金融工具。
key STRING 类型标量,表示金融工具的字段,需与 instrument 中定义的字段名一致。
default 一个标量,表示 key 的默认值。目前支持如下类型:LOGICAL, INTEGRAL(COMPRESSED 除外), TEMPORAL(DATEHOUR 除外), FLOATING, LITERAL, DECIMAL。
返回值
- 当 instrument 是标量时,返回一个标量。
- 当 instrument 是向量时,返回一个与 instrument 等长的向量。
例子
例 1:从金融工具中提取指定字段(key)对应的值。
bond = {
"productType": "Cash",
"assetType": "Bond",
"bondType": "ZeroCouponBond",
"instrumentId": "0001",
"start": 1996.03.01,
"maturity": 2032.05.15,
"dayCountConvention": "ActualActualISDA",
"coupon": 0.0276,
"issuePrice": 100.0,
"frequency": "Semiannual",
"subType":"TREASURY_BOND",
"creditRating":"B",
"settlement": 2022.05.15
}
ins = parseInstrument(bond)
// 方式一:getInstrumentField 函数
getInstrumentField(ins, "productType")
// 方式二:instrument.key 语法
ins.productType
// 方式三:instrument["key"] 语法
ins["productType"]
例 2:SQL 条件查询。
// 定义一个合约 notionalAmount 为 100
IrForwardRateAgreementDict1 = {
"productType": "Forward",
"forwardType": "IrForwardRateAgreement",
"notionalCurrency": "EUR",
"notionalAmount": 100.0,
"instrumentId": "1",
"start": 2012.01.02,
"maturity": 2022.01.02,
"fixedRate": 0.03,
"calendar": "CFET",
"payReceive": "Pay",
"dayCountConvention": "Actual365",
"iborIndex": "FR_007",
"discountCurve": "CNY_FR_007",
"forwardCurve": "CNY_FR_007"
}
IrForwardRateAgreement1 = parseInstrument(IrForwardRateAgreementDict1)
// 定义一个合约 notionalAmount 为 1000
IrForwardRateAgreementDict2 = {
"productType": "Forward",
"forwardType": "IrForwardRateAgreement",
"notionalCurrency": "CNY",
"notionalAmount": 1000.0,
"instrumentId": "2",
"start": 2012.01.03,
"maturity": 2022.01.03,
"fixedRate": 0.04,
"calendar": "CFET",
"payReceive": "Receive",
"dayCountConvention": "Actual360",
"iborIndex": "FR_007",
"discountCurve": "CNY_FR_008",
"forwardCurve": "CNY_FR_008"
}
IrForwardRateAgreement2 = parseInstrument(IrForwardRateAgreementDict2)
// 建库建表
if (existsDatabase(dbName)) {
dropDatabase(dbName)
}
tbl = table(1:0, ["id", "val1"], [INT, INSTRUMENT])
dbName = "dfs://test_IrForwardRateAgreement"
db = database("dfs://test_IrForwardRateAgreement", VALUE, 1..100, , "TSDB")
pt = createPartitionedTable(db, tbl, "pt", "id", , "id")
pt.append!(table(1..20 as id, take([IrForwardRateAgreement1, IrForwardRateAgreement2], 20) as val1))
flushTSDBCache()
// 三种方式筛选出 notionalAmount 为 100 的记录
// 方式一:getInstrumentField 函数
select getInstrumentField(val1, "notionalAmount") from pt where getInstrumentField(val1, "notionalAmount") = 100
// 方式二:instrument.key 语法
select val1.notionalAmount from pt where val1.notionalAmount = 100
// 方式三:instrument["key"] 语法
select val1["notionalAmount"] from pt where val1["notionalAmount"] = 100
