1 /* packet-nasdaq-itch.c
2 * Routines for NASDAQ TotalView-ITCH version 2.00/3.00 (with Chi-X extension) Protocol dissection
3 * Copyright 2007,2008 Didier Gautheron <dgautheron@magic.fr>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * http://www.nasdaqtrader.com/Trader.aspx?id=DPSpecs
28 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/tv-itch2a.pdf
29 * http://www.nasdaqtrader.com/content/technicalsupport/specifications/dataproducts/tvitch-v3.pdf
32 * http://www.chi-x.com/docs/Chi-X%20CHIXMD.pdf
43 #include <epan/packet.h>
44 #include <epan/prefs.h>
45 #include <epan/wmem/wmem.h>
46 #include <wsutil/type_util.h>
49 static gboolean nasdaq_itch_chi_x
= TRUE
;
51 static const value_string message_types_val
[] = {
52 { 'A', "Add Order " },
53 { 'X', "Order Cancel " },
54 { 'M', "Milliseconds " },
55 { 'E', "Order Executed " },
57 { 'P', "Trade Message Identifier " },
58 { 'C', "Order Executed With Price " },
59 { 'D', "Order Delete " },
60 { 'Q', "Cross Trade " },
61 { 'S', "System Event " },
62 { 'R' , "Stock Directory " },
63 { 'H', "Stock Trading Action " },
64 { 'F', "Add Order (MPID) " },
65 { 'I', "Net Order Imbalance Indicator (NOII) " },
66 { 'B', "Broken Trade " },
67 /* Chi-X msg with big size,price */
68 { 'a', "Add Order (big)" },
69 { 'p', "Trade Message Identifier (big)" },
70 { 'e', "Order Executed (big)" },
71 { 'x', "Order Cancel (big)" },
75 static char chix_msg
[] = "apex";
77 static const value_string system_event_val
[] = {
78 { 'O', "Start of Messages" },
79 { 'S', "Start of System hours" },
80 { 'Q', "Start of Market hours" },
81 { 'M', "End of Market hours" },
82 { 'E', "End of System hours" },
83 { 'C', "End of Messages" },
87 static const value_string market_category_val
[] = {
88 { 'T', "CQS (NYSE, Amex or regional exchange)" },
89 { 'Q', "NASDAQ Global Select MarketSM" },
90 { 'G', "NASDAQ Global MarketSM" },
91 { 'S', "NASDAQ Capital Market" },
92 { ' ', "Not available" },
96 static const value_string financial_status_val
[] = {
98 { 'E', "Delinquent" },
100 { 'S', "Suspended" },
101 { 'G', "Deficient and Bankrupt" },
102 { 'H', "Deficient and Delinquent" },
103 { 'J', "Delinquent and Bankrupt" },
104 { 'K', "Deficient, Delinquent and Bankrupt" },
105 { ' ', "Company is in compliance" },
109 static const value_string round_lots_only_val
[] = {
110 { 'Y', "only round lots are accepted in this stock" },
111 { 'N', "odd/mixed lots are allowed" },
115 /* Initialize the protocol and registered fields */
116 static int proto_nasdaq_itch
= -1;
118 /* Initialize the subtree pointers */
119 static gint ett_nasdaq_itch
= -1;
121 static int hf_nasdaq_itch_version
= -1;
123 static int hf_nasdaq_itch_message_type
= -1;
124 static int hf_nasdaq_itch_market_category
= -1;
125 static int hf_nasdaq_itch_financial_status
= -1;
126 static int hf_nasdaq_itch_stock
= -1;
127 static int hf_nasdaq_itch_round_lot_size
= -1;
128 static int hf_nasdaq_itch_round_lots_only
= -1;
130 static int hf_nasdaq_itch_system_event
= -1;
131 static int hf_nasdaq_itch_second
= -1;
132 static int hf_nasdaq_itch_millisecond
= -1;
134 static int hf_nasdaq_itch_message
= -1;
136 static int hf_nasdaq_itch_trading_state
= -1;
137 static int hf_nasdaq_itch_reserved
= -1;
138 static int hf_nasdaq_itch_reason
= -1;
139 static int hf_nasdaq_itch_order_reference
= -1;
140 static int hf_nasdaq_itch_buy_sell
= -1;
141 static int hf_nasdaq_itch_shares
= -1;
142 static int hf_nasdaq_itch_price
= -1;
143 static int hf_nasdaq_itch_attribution
= -1;
144 static int hf_nasdaq_itch_executed
= -1;
145 static int hf_nasdaq_itch_match
= -1;
146 static int hf_nasdaq_itch_printable
= -1;
147 static int hf_nasdaq_itch_execution_price
= -1;
148 static int hf_nasdaq_itch_canceled
= -1;
149 static int hf_nasdaq_itch_cross
= -1;
151 /* ---------------------- */
153 order_ref_number(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
)
155 const char *str_value
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, 9);
156 guint32 value
= (guint32
)strtoul(str_value
, NULL
, 10);
158 proto_tree_add_uint(nasdaq_itch_tree
, hf_nasdaq_itch_order_reference
, tvb
, offset
, 9, value
);
159 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "%u ", value
);
164 /* -------------------------- */
166 time_stamp(tvbuff_t
*tvb
, proto_tree
*nasdaq_itch_tree
, int id
, int offset
, int size
)
169 if (nasdaq_itch_tree
) {
171 const char *display
= "";
172 const char *str_value
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, size
);
174 ms
= val
= (guint32
)strtoul(str_value
, NULL
, 10);
177 display
= wmem_strdup_printf(wmem_packet_scope(), " %03u" , val
);
181 case 8: /* 0 86 400 000 */
182 display
= wmem_strdup_printf(wmem_packet_scope(), " %u (%02u:%02u:%02u.%03u)", val
,
183 ms
/3600000, (ms
% 3600000)/60000, (ms
% 60000)/1000, ms
%1000);
186 proto_tree_add_uint_format_value(nasdaq_itch_tree
, id
, tvb
, offset
, size
, val
, "%s", display
);
191 /* -------------------------- */
193 number_of_shares(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int id
, int offset
, int big
)
195 gint size
= (big
)?10:6;
196 const char *str_value
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, size
);
198 guint32 value
= (guint32
)strtoul(str_value
, NULL
, 10);
200 proto_tree_add_uint(nasdaq_itch_tree
, id
, tvb
, offset
, size
, value
);
201 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "qty %u ", value
);
206 /* -------------------------- */
208 price(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int id
, int offset
, int big
)
210 gint size
= (big
)?19:10;
212 const char *str_value
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, size
);
213 gdouble value
= guint64_to_gdouble(g_ascii_strtoull(str_value
, NULL
, 10))/((big
)?1000000.0:10000.0);
215 proto_tree_add_double(nasdaq_itch_tree
, id
, tvb
, offset
, size
, value
);
216 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "price %g ", value
);
221 /* -------------------------- */
223 stock(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
)
225 char *stock_p
= tvb_get_string(wmem_packet_scope(), tvb
, offset
, 6);
227 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_stock
, tvb
, offset
, 6, ENC_ASCII
|ENC_NA
);
228 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "<%s> ", stock_p
);
233 /* -------------------------- */
235 order(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
, int big
)
239 offset
= order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
241 value
= tvb_get_guint8(tvb
, offset
);
243 col_append_fstr(pinfo
->cinfo
, COL_INFO
, "%c ", value
);
244 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_buy_sell
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
247 offset
= number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_shares
, offset
, big
);
249 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
251 offset
= price(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_price
, offset
, big
);
255 /* -------------------------- */
257 executed(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*nasdaq_itch_tree
, int offset
, int big
)
259 offset
= order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
261 offset
= number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_executed
, offset
, big
);
263 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
|ENC_NA
);
268 /* ---------------------------- */
270 dissect_nasdaq_itch(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
)
273 proto_tree
*nasdaq_itch_tree
= NULL
;
274 guint8 nasdaq_itch_type
;
280 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "Nasdaq-ITCH");
282 nasdaq_itch_type
= tvb_get_guint8(tvb
, offset
);
283 if (nasdaq_itch_type
>= '0' && nasdaq_itch_type
<= '9') {
285 nasdaq_itch_type
= tvb_get_guint8(tvb
, offset
+8);
288 if ((!nasdaq_itch_chi_x
|| version
== 3) && strchr(chix_msg
, nasdaq_itch_type
)) {
289 nasdaq_itch_type
= 0; /* unknown */
292 rep
= val_to_str(nasdaq_itch_type
, message_types_val
, "Unknown packet type (0x%02x) ");
293 col_add_str(pinfo
->cinfo
, COL_INFO
, rep
);
298 ti
= proto_tree_add_protocol_format(tree
, proto_nasdaq_itch
, tvb
, offset
, -1, "Nasdaq TotalView-ITCH %s, %s",
299 version
== 2?"2.0":"3.0", rep
);
301 nasdaq_itch_tree
= proto_item_add_subtree(ti
, ett_nasdaq_itch
);
303 item
=proto_tree_add_uint(nasdaq_itch_tree
, hf_nasdaq_itch_version
, tvb
, 0, 0, version
);
304 PROTO_ITEM_SET_GENERATED(item
);
308 offset
= time_stamp (tvb
, nasdaq_itch_tree
, hf_nasdaq_itch_millisecond
, offset
, 8);
311 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_message_type
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
315 switch (nasdaq_itch_type
) {
316 case 'T': /* seconds */
317 /*offset =*/ time_stamp (tvb
, nasdaq_itch_tree
, hf_nasdaq_itch_second
, offset
, 5);
320 case 'M': /* milliseconds */
321 /*offset =*/ time_stamp (tvb
, nasdaq_itch_tree
, hf_nasdaq_itch_millisecond
, offset
, 3);
326 switch (nasdaq_itch_type
) {
327 case 'S': /* system event */
328 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_system_event
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
332 case 'R': /* Stock Directory */
333 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
335 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_market_category
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
337 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_financial_status
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
339 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_round_lot_size
, tvb
, offset
, 6, ENC_ASCII
|ENC_NA
);
341 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_round_lots_only
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
345 case 'H': /* Stock trading action */
346 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
348 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_trading_state
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
350 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_reserved
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
352 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_reason
, tvb
, offset
, 4, ENC_ASCII
|ENC_NA
);
358 case 'A': /* Add order, no MPID */
359 offset
= order(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
361 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_printable
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
366 case 'F': /* Add order, MPID */
367 offset
= order(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
368 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_attribution
, tvb
, offset
, 4, ENC_ASCII
|ENC_NA
);
374 case 'E' : /* Order executed */
375 /*offset =*/ executed(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
378 case 'C' : /* Order executed with price */
379 offset
= executed(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
380 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_printable
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
383 /*offset = */price(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_execution_price
, offset
, big
);
388 case 'X' : /* Order cancel */
389 offset
= order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
390 /*offset = */number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_canceled
, offset
, big
);
393 case 'D' : /* Order delete */
394 /*offset = */order_ref_number(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
400 case 'P' : /* Trade identifier */
401 offset
= order(tvb
, pinfo
, nasdaq_itch_tree
, offset
, big
);
402 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
|ENC_NA
);
406 case 'Q' : /* Cross Trade */
407 offset
= number_of_shares(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_shares
, offset
, big
);
409 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
411 offset
= price(tvb
, pinfo
, nasdaq_itch_tree
, hf_nasdaq_itch_price
, offset
, big
);
413 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
|ENC_NA
);
415 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_cross
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
419 case 'B' : /* Broken Trade */
420 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_match
, tvb
, offset
, 9, ENC_ASCII
|ENC_NA
);
424 case 'I': /* NOII, FIXME */
425 offset
= stock(tvb
, pinfo
, nasdaq_itch_tree
, offset
);
427 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_cross
, tvb
, offset
, 1, ENC_ASCII
|ENC_NA
);
433 proto_tree_add_item(nasdaq_itch_tree
, hf_nasdaq_itch_message
, tvb
, offset
, -1, ENC_ASCII
|ENC_NA
);
439 /* Register the protocol with Wireshark */
442 proto_register_nasdaq_itch(void)
445 /* Setup list of header fields See Section 1.6.1 for details*/
446 static hf_register_info hf
[] = {
447 { &hf_nasdaq_itch_version
,
448 { "Version", "nasdaq-itch.version",
449 FT_UINT8
, BASE_DEC
, NULL
, 0x0,
452 { &hf_nasdaq_itch_message_type
,
453 { "Message Type", "nasdaq-itch.message_type",
454 FT_UINT8
, BASE_DEC
, VALS(message_types_val
), 0x0,
457 { &hf_nasdaq_itch_second
,
458 { "Second", "nasdaq-itch.second",
459 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
462 { &hf_nasdaq_itch_millisecond
,
463 { "Millisecond", "nasdaq-itch.millisecond",
464 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
467 { &hf_nasdaq_itch_system_event
,
468 { "System Event", "nasdaq-itch.system_event",
469 FT_UINT8
, BASE_DEC
, VALS(system_event_val
), 0x0,
472 { &hf_nasdaq_itch_market_category
,
473 { "Market Category", "nasdaq-itch.market_category",
474 FT_UINT8
, BASE_DEC
, VALS(market_category_val
), 0x0,
477 { &hf_nasdaq_itch_financial_status
,
478 { "Financial Status Indicator", "nasdaq-itch.financial_status",
479 FT_UINT8
, BASE_DEC
, VALS(financial_status_val
), 0x0,
482 { &hf_nasdaq_itch_stock
,
483 { "Stock", "nasdaq-itch.stock",
484 FT_STRING
, BASE_NONE
, NULL
, 0x0,
487 { &hf_nasdaq_itch_round_lot_size
,
488 { "Round Lot Size", "nasdaq-itch.round_lot_size",
489 FT_STRING
, BASE_NONE
, NULL
, 0x0,
492 { &hf_nasdaq_itch_round_lots_only
,
493 { "Round Lots Only", "nasdaq-itch.round_lots_only",
494 FT_UINT8
, BASE_DEC
, VALS(round_lots_only_val
), 0x0,
497 { &hf_nasdaq_itch_trading_state
,
498 { "Trading State", "nasdaq-itch.trading_state",
499 FT_STRING
, BASE_NONE
, NULL
, 0x0,
502 { &hf_nasdaq_itch_reserved
,
503 { "Reserved", "nasdaq-itch.reserved",
504 FT_STRING
, BASE_NONE
, NULL
, 0x0,
507 { &hf_nasdaq_itch_reason
,
508 { "Reason", "nasdaq-itch.reason",
509 FT_STRING
, BASE_NONE
, NULL
, 0x0,
512 { &hf_nasdaq_itch_order_reference
,
513 { "Order Reference", "nasdaq-itch.order_reference",
514 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
515 "Order reference number", HFILL
}},
517 { &hf_nasdaq_itch_buy_sell
,
518 { "Buy/Sell", "nasdaq-itch.buy_sell",
519 FT_STRING
, BASE_NONE
, NULL
, 0x0,
520 "Buy/Sell indicator", HFILL
}},
522 { &hf_nasdaq_itch_shares
,
523 { "Shares", "nasdaq-itch.shares",
524 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
525 "Number of shares", HFILL
}},
527 { &hf_nasdaq_itch_price
,
528 { "Price", "nasdaq-itch.price",
529 FT_DOUBLE
, BASE_NONE
, NULL
, 0x0,
532 { &hf_nasdaq_itch_attribution
,
533 { "Attribution", "nasdaq-itch.attribution",
534 FT_STRING
, BASE_NONE
, NULL
, 0x0,
535 "Market participant identifier", HFILL
}},
537 { &hf_nasdaq_itch_executed
,
538 { "Executed Shares", "nasdaq-itch.executed",
539 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
540 "Number of shares executed", HFILL
}},
542 { &hf_nasdaq_itch_match
,
543 { "Matched", "nasdaq-itch.match",
544 FT_STRING
, BASE_NONE
, NULL
, 0x0,
545 "Match number", HFILL
}},
547 { &hf_nasdaq_itch_printable
,
548 { "Printable", "nasdaq-itch.printable",
549 FT_STRING
, BASE_NONE
, NULL
, 0x0,
552 { &hf_nasdaq_itch_execution_price
,
553 { "Execution Price", "nasdaq-itch.execution_price",
554 FT_DOUBLE
, BASE_NONE
, NULL
, 0x0,
557 { &hf_nasdaq_itch_canceled
,
558 { "Canceled Shares", "nasdaq-itch.canceled",
559 FT_UINT32
, BASE_DEC
, NULL
, 0x0,
560 "Number of shares to be removed", HFILL
}},
562 { &hf_nasdaq_itch_cross
,
563 { "Cross Type", "nasdaq-itch.cross",
564 FT_STRING
, BASE_NONE
, NULL
, 0x0,
565 "Cross trade type", HFILL
}},
567 { &hf_nasdaq_itch_message
,
568 { "Message", "nasdaq-itch.message",
569 FT_STRING
, BASE_NONE
, NULL
, 0x0,
573 /* Setup protocol subtree array */
574 static gint
*ett
[] = {
578 module_t
*nasdaq_itch_module
;
580 /* Register the protocol name and description */
581 proto_nasdaq_itch
= proto_register_protocol("Nasdaq TotalView-ITCH", "NASDAQ-ITCH", "nasdaq_itch");
583 /* Required function calls to register the header fields and subtrees used */
584 proto_register_field_array(proto_nasdaq_itch
, hf
, array_length(hf
));
585 proto_register_subtree_array(ett
, array_length(ett
));
587 nasdaq_itch_module
= prefs_register_protocol(proto_nasdaq_itch
, NULL
);
588 prefs_register_bool_preference(nasdaq_itch_module
, "chi_x", "Decode Chi X extensions",
589 "Whether the Nasdaq ITCH dissector should decode Chi X extensions.",
592 register_dissector("nasdaq-itch", dissect_nasdaq_itch
, proto_nasdaq_itch
);
596 /* If this dissector uses sub-dissector registration add a registration routine.
597 This format is required because a script is used to find these routines and
598 create the code that calls these routines.
601 proto_reg_handoff_nasdaq_itch(void)