getInstrumentField
First introduced in version: 3.00.4.1
Syntax
getInstrumentField(instrument, key, [default])
Details
Get the value of the specified field (key) from one or more instruments:
- If default is not set, the function returns the field value if it exists; otherwise, it returns a null value.
- If default is set, the function returns the field value converted to the type specified by default if the field has a valid value; otherwise, it returns default.
instrument.key and instrument["key"] are
supported to get the value of the specified field (key) from one or more
instruments.Parameters
instrument is a scalar or vector of the INSTRUMENT type, indicating one or more instruments.
key is a scalar of the STRING type, indicating a field from the instrument. The value must match a field name defined in instrument.
default is a scalar, indicating the default value for key. The following types are supported: LOGICAL, INTEGRAL (except COMPRESSED), TEMPORAL (except DATEHOUR), FLOATING, LITERAL, and DECIMAL.
Returns
- If instrument is a scalar, the function returns a scalar.
- If instrument is a vector, the function returns a vector of the same length as instrument.
Examples
Example 1: Get the value of the specified field from one instrument.
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)
// Method 1: getInstrumentField function
getInstrumentField(ins, "productType")
// Method 2: instrument.key syntax
ins.productType
// Method 3: instrument["key"] syntax
ins["productType"]
Example 2: Conditional query in SQL.
// Define a contract with a notionalAmount of 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)
// Define a contract with a notionalAmount of 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)
// Create database and table
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()
// Filter out records where the notionalAmount is 100 in three methods
// Method 1: getInstrumentField function
select getInstrumentField(val1, "notionalAmount") from pt where getInstrumentField(val1, "notionalAmount") = 100
// Method 2: instrument.key syntax
select val1.notionalAmount from pt where val1.notionalAmount = 100
// Method 3: instrument["key"] syntax
select val1["notionalAmount"] from pt where val1["notionalAmount"] = 100
Related functions: parseInstrument, getInstrumentKeys
