{"id":77,"date":"2017-02-20T20:14:29","date_gmt":"2017-02-20T20:14:29","guid":{"rendered":"http:\/\/fpganow.com\/?p=77"},"modified":"2017-02-20T21:17:31","modified_gmt":"2017-02-20T21:17:31","slug":"filter-market-data-messages-in-an-fpga-part-2","status":"publish","type":"post","link":"https:\/\/fpganow.com\/index.php\/2017\/02\/20\/filter-market-data-messages-in-an-fpga-part-2\/","title":{"rendered":"Filter Market Data Messages in an FPGA &#8211; part 2"},"content":{"rendered":"<p>Skip directly to the source code on Github.com here:<\/p>\n<p><a href=\"https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/tree\/master\/MarketData\/MarketData_01\">https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/tree\/master\/MarketData\/MarketData_01<\/a><\/p>\n<hr \/>\n<p>So what now. \u00a0We know what a NASDAQ ITCH 4.1 Market Data Message looks like. \u00a0The format is very simple, there is some &#8211; yes &#8211; ASCII data in the message format, and all messages are preceded by the message length. \u00a0Message length preceding the message makes it very easy to interpret a feed from inside an FPGA.<\/p>\n<p>What to do first? Well, what does eXtreme Programming say to do? It says keep it simple.<\/p>\n<p>So, I am working on a LabVIEW 2016 program that does the following:<\/p>\n<ol class=\"ili-indent\">\n<li>Open a NASDAQ ITCH file<\/li>\n<li>Send it one byte at a time into the FPGA over the PCIe bus<\/li>\n<li>FPGA reads the feed, skips over messages types that it does not know how to handle, and parses only messages of a specific type.<\/li>\n<li>Send back to the host computer a statistic &#8211; any statistic for now.<\/li>\n<\/ol>\n<p>If you are unfamiliar with National Instruments products and LabVIEW, go here to learn more:<\/p>\n<ul class=\"ili-indent\">\n<li><a href=\"http:\/\/www.ni.com\/labview\/\">http:\/\/www.ni.com\/labview\/<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/user\/Labview\">https:\/\/www.youtube.com\/user\/Labview<\/a><\/li>\n<\/ul>\n<p><strong>Step 1 &#8211; Open a NASDAQ ITCH file<\/strong><\/p>\n<p>The first step is to create a LabVIEW for Windows &#8211; as opposed to LabVIEW for FPGA &#8211; and to read in an entire NASDAQ ITCH 4.1 file. \u00a0This file is quite big, so I\u00a0wrote a quick Python script (used Ryan Day&#8217;s code as a guide:\u00a0<a href=\"https:\/\/github.com\/rday\/ITCH41\">https:\/\/github.com\/rday\/ITCH41<\/a>), and I generated a file with the following:<\/p>\n<ul class=\"ili-indent\">\n<li>1 Timestamp message<\/li>\n<li>5 Timestamp messages<\/li>\n<li>50 Timestamp messages<\/li>\n<\/ul>\n<p>The Python script can also output the seconds field from each message read in. \u00a0See the python script here:<\/p>\n<p><a href=\"https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/blob\/master\/MarketData\/MarketData_01\/parseItch.py\">https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/blob\/master\/MarketData\/MarketData_01\/parseItch.py<\/a><\/p>\n<p>Now I have three files, named: T.itch, T.5.itch, and T.50.itch, and I will write a LabVIEW program to send all data from one of the files above in to the FPGA.<\/p>\n<p><strong>Step 2 &#8211; Send Data From One File to FPGA via DMA FIFO<\/strong><\/p>\n<p>This requires knowledge of LabVIEW for Windows and some knowledge of LabVIEW for FPGA. \u00a0I wrote a simple User Interface that allows you to select a file. \u00a0That file is then sent to the FPGA using a Host-to-Target FIFO 1 byte at a time. \u00a0Since viewing a LabVIEW vi requires that you have LabVIEW installed on your computer, I took some screen shots of the LabVIEW code and placed them here:<\/p>\n<ul class=\"ili-indent\">\n<li><a href=\"https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/blob\/master\/MarketData\/MarketData_01\/Host-Send-Data.png\">https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/blob\/master\/MarketData\/MarketData_01\/Host-Send-Data.png<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/blob\/master\/MarketData\/MarketData_01\/Host-Top-Level_1.png\">https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/blob\/master\/MarketData\/MarketData_01\/Host-Top-Level_1.png<\/a><\/li>\n<\/ul>\n<p><strong>Step 3 &#8211; Interpret the Feed Inside the FPGA<\/strong><\/p>\n<p>Right now I have generated a file that contains, 1, 5, and 50 Timestamp messages. \u00a0So that means, for each messaged that is encountered, I will extract the timestamp, which will be in seconds, save it in to a local FPGA variable, and send the value back up to the host.<\/p>\n<p><strong>Step 4 &#8211; Send Data Back to Host<\/strong><\/p>\n<p>The statistic will be the seconds portion of each message that was passed in. \u00a0The seconds field is a 32-bit integer, so the Target-to-Host DMA FIFO will be a 32-bit integer. \u00a0Here is a quick screen shot of the FPGA top-level loop, which reads the input data stream one byte at a time:<\/p>\n<p><a href=\"http:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FPGA-Entire-Loop.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-87 size-large\" src=\"http:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FPGA-Entire-Loop-1024x547.png\" width=\"525\" height=\"280\" srcset=\"https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FPGA-Entire-Loop-1024x547.png 1024w, https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FPGA-Entire-Loop-300x160.png 300w, https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FPGA-Entire-Loop-768x410.png 768w, https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FPGA-Entire-Loop.png 1592w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/a><\/p>\n<p>The purple colored box is how the FPGA receives the data from the host. \u00a0In a live application, the purple colored box, also known as the &#8220;Read (FIFO Method)&#8221; can have this data come directly from a 10 gigabit connection, or from another loop inside the FPGA.<\/p>\n<p><a href=\"http:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FIFO-Read.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-85 size-full\" src=\"http:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FIFO-Read.png\" width=\"305\" height=\"103\" srcset=\"https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FIFO-Read.png 305w, https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/FIFO-Read-300x101.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p>(Read more about this method on National Instruments website:\u00a0<a href=\"https:\/\/zone.ni.com\/reference\/en-XX\/help\/371599H-01\/lvfpgahost\/fpga_method_fifo_read\/\">https:\/\/zone.ni.com\/reference\/en-XX\/help\/371599H-01\/lvfpgahost\/fpga_method_fifo_read\/<\/a>)<\/p>\n<p>As the data comes in, a counter is started at 0, and depending on the element count, the data is stored in different output variables. \u00a0The first 2 bytes are the message length, the 3rd is the message type, the 4th, 5th, 6th, and 7th elements are the Seconds portion of the message.<\/p>\n<p>The counter is then compared to the message length variable, and when they are equal, the output variable &#8220;Message Done&#8221; is set to true. \u00a0Here is the bottom portion of the screen shot from above:<\/p>\n<p><a href=\"http:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/Fpga-End-of-Message.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-86 size-full\" src=\"http:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/Fpga-End-of-Message.png\" width=\"417\" height=\"148\" srcset=\"https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/Fpga-End-of-Message.png 417w, https:\/\/fpganow.com\/wp-content\/uploads\/2017\/02\/Fpga-End-of-Message-300x106.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/a><\/p>\n<p>After the end of the message is read, we have a case structure, which is similar to an if statement but for FPGAs, which will read the appropriate variables and send them back up to the host via a DMA-FIFO. \u00a0Now this DMA-FIFO can be configured to send data up to the host computer, or to another DMA-FIFO inside the FPGA. \u00a0For now we are going to send this up to the host for analysis.<\/p>\n<p>Take a look at the right-half of the original FPGA vi screenshot. \u00a0This element executes once, and reads the Seconds variable and sends it up to the host.<\/p>\n<p>In part 3, I will add another feature &#8211; Add Order with MPID, so we can now know in the FPGA when a new order is entered for a particular security, what side that order is on, and how many shares\/price. \u00a0This is more meaningful information that can be used to trade the markets, especially during a Donald Trump speech!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Skip directly to the source code on Github.com here: https:\/\/github.com\/JohnStratoudakis\/LabVIEW_Fpga\/tree\/master\/MarketData\/MarketData_01 So what now. \u00a0We know what a NASDAQ ITCH 4.1 Market Data Message looks like. \u00a0The format is very simple, there is some &#8211; yes &#8211; ASCII data in the message format, and all messages are preceded by the message length. \u00a0Message length preceding the &#8230; <a title=\"Filter Market Data Messages in an FPGA &#8211; part 2\" class=\"read-more\" href=\"https:\/\/fpganow.com\/index.php\/2017\/02\/20\/filter-market-data-messages-in-an-fpga-part-2\/\" aria-label=\"Read more about Filter Market Data Messages in an FPGA &#8211; part 2\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[7,6,5],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-fpga","category-labview","category-market-data","masonry-post","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":9,"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fpganow.com\/index.php\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}