Accessing metadata

How to access table metadata and enumerations via nq.

For every table within the MarketGrid schema, there are associated metadata. This metadata can be accessed via the following nq commands, either inside a script or via the REPL.

Table number

The table numbers of each table in the model are generated at compile-time, and are saved as a property of each table object: <tablename>.number. This is also expressed as an enum called Table.

If the table is a Change Table, then the base table number can also be found using <tablename>.base_table_number.

N.B. The trading engine does not publish the base tables of change tables to nq. The change tables are renamed in nq for convenience, i.e. the Holding table in nq actually refers to Holding_change in the engine.

Alternatively, you could use the Table enum to find the same information.

mg_11000> Holding.number
106
mg_11000> Holding.base_table_number
43
mg_11000> Table.Holding
43
mg_11000> Table.Holding_change
106

Table length

The number of rows of a table currently in shared memory is stored in the variable <tablename>.length.

mg_11000> Instrument.length
494

Sets and Enums

Enums

There are numerous enum and set fields used throughout the MarketGrid system, all of which can be accessed via nq.

Simply type the name of the enum, followed by a . to refer to a specific entry, and it will return its respective number value. You can see all entries and values of the enum by typing the enum's name in the REPL.

mg_11000> OrderSide.Buy
0
mg_11000> OrderSide
{ Buy: 0, Sell: 1, Both: 2 }

When querying tables with a where or find function, you can use these enums to specify values to constrain your result set.

Another useful case is the TransactionType enum, which is referenced when sending a transaction to the engine with host.transact from an nq script.

For example, below is an N-server sending in a HoldingTransaction:

host.transact({
	msg_type: TransactionType.HoldingTransaction,
	body: holding_trans_body,
})

where holding_trans_body is an object in the shape of ts_holdingtransaction_t (found in models.d.ts in the SDK). The documentation for this message is also available here.

Sets

Sets (bitmask fields) are stored in the same way as enums, but the numbers represent the value of the bit as a decimal. These fields can have multiple bits set at a time, and are handled with a different set of functions (bitwise operations).

mg_11000> OrderType
{
  Limit: 1,
  Market: 2,
  Quote: 4,
  FillOrKill: 8,
  FillAndKill: 16,
  OneSideReport: 32,
  TwoSideReport: 128,
  TwoSideReportExecuted: 256,
  Rfqexecute: 512,
  Pending: 1024,
  CloseOut: 2048,
  Buy: 4096,
  Sell: 8192,
  Peg: 16384,
  PostOnly: 32768,
  Iceberg: 65536,
  Hidden: 131072,
  OneCancelsOther: 262144,
  DontPropagate: 524288,
  AllOrNone: 1048576,
  HitLift: 2097152,
  Parcel: 4194304,
  Anonymous: 8388608,
  NoPrice: 16777216,
  Value: 33554432,
  MarketMaker: 67108864,
  GoodtillCancelled: 134217728,
  GoodforSession: 268435456,
  GoodtillTime: 536870912,
  GoodtillDate: 1073741824,
  Held: 2147483648
}

Lookups

Another way to use enums in nq is to do a lookup of the number value, to return the entry string. This is useful when trying to interpret the value of an enum or set field in a record, or if a transaction sent to the engine returns an error.

All enums have a lookup Map called <enum>_Lookup, which is a Javascript Map object. The get function could then be used to find the corresponding text for a number value.

mg_11000> EngineError_Lookup.get(19004)
'InvalidInstrument'

A reverse lookup Map also exists for each enum, called <enum>_ReverseLookup, which maps the enum member string to the number value. This could be useful if the enum used is determined by a function instead of being explicitly known, or to perform other Map operations.

TableMaster

TableMaster is an array of objects that contains all the useful attributes of each table in the data model. The table numbers correspond to their index in the array.

For example, the TableMaster object for Trade would be TableMaster[Trade.number].

This object includes a number of properties, the most notable being cols, which is another array containing all the column objects for that table. Other properties include number (table number) and caption, which refers to the field of the table that will be used as the identifier on the UI.

N.B. The default caption is ShortName if not specified in TableMaster.

cols

The cols property of TableMaster is an array of column objects for a given table. Its properties include name, type, description, and enum and set (which will be populated if the column values are restricted to the domain of one of the enums in the model).

A useful example of TableMaster could be to see a list of all the field names in a table. The code below first selects the TableMaster table object for Group, then uses the map function on the cols object array to show the name of each column:

mg_11000> TableMaster[Group.number].cols.map(c => c.name)
[
  'index',           'Id',
  'Status',          'Firm',
  'Name',            'ShortName',
  'PermissionGroup', 'IndexInFirm',
  'UpdateNumber',    'OwnerUser',
  'OwnerGroup',      'OwnerFirm',
  'CreateUser',      'CreateTimestamp',
  'UpdateUser',      'UpdateTimestamp',
  'permissions'
]

Any functions that can be performed on Javascript objects could be used to manipulate either the TableMaster table object or the cols objects to retrieve the desired metadata.