{
    "componentChunkName": "component---src-templates-post-page-js",
    "path": "/2018/cryptomate/market-port",
    "result": {"data":{"site":{"siteMetadata":{"title":"Solid Abstractions","siteUrl":"https://solidabstractions.com","twitterId":291334023,"author":{"fullName":"Julien Hartmann","profileHtml":"I am an open-source de­vel­op­er, for­mer IT con­sul­tant with a pas­sion for new tech­nol­o­gies. I be­lieve the role of an en­gi­neer is to em­pow­er peo­ple, by as­sem­bling sim­ple, re­fined de­signs.\n","links":[{"url":"https://github.com/spectras/","name":"github","title":"GitHub"},{"url":"https://stackoverflow.com/users/3212865/spectras","name":"stackoverflow","title":"StackOverflow"},{"url":"https://www.linkedin.com/in/julienhartmann/","name":"linkedin","title":"LinkedIn"}],"profilePicNode":{"original":{"src":"/static/profile-pic-301a9cbe7b572c3e7910c9717d2b3bcd.jpg"}},"url":"https://etherdream.org/about"}}},"markdownRemark":{"id":"a7fb9979-a441-5198-a965-edb025ea8cb6","excerpt":"So far, we have been talking about market events, orders and strategies, as\nabstract concepts, leaving details to a later point. Now, to design the core of\nthe strategy engine, we need the full picture.","html":"<p>So far, we have been talking about market events, orders and strategies, as\nabstract concepts, leaving details to a later point. Now, to design the core of\nthe strategy engine, we need the full picture.</p>\n<p>--- excerpt ---</p>\n<p>It is time to descend into details. We will now define the interfaces that define our\nbusiness logic's view on the world. That is, the ports it uses:</p>\n<blockquote>\n<ol>\n<li>Market port <em>(this post)</em>.</li>\n<li><a href=\"trading-port\" class=\"internal\">Trading port</a>.</li>\n<li><a href=\"other-strategy-ports\" class=\"internal\">Other strategy ports</a>.</li>\n</ol>\n</blockquote>\n<p>As we defined in the <a href=\"modules\" class=\"internal\">modules and components</a> post, the aim of the market\n<a href=\"exploring-patterns#hexagonal-architecture\" class=\"internal\">port</a> is to abstract out\nall details of connecting to market data providers to receive real-time market events.</p>\n<p>It is therefore time we detail what those events consist of and what they mean. In\nshort, we need to describe the <strong>domain model</strong> the market port represents.\nThen, we will establish the <strong>features</strong> the port has to implement to represent it in\na useful way.</p>\n<h2 id=\"domain-model\">Domain model</h2>\n<h3 id=\"what-is-a-market\">What is a market?</h3>\n<div class=\"note\"><p>If you are familiar with financial markets, simply skip down\nto <a href=\"#market-port-features\" class=\"internal\">port features</a>.</p></div>\n<p><a href=\"https://en.wikipedia.org/wiki/Financial_market\" class=\"external\" rel=\"external noopener noreferrer\">Wikipedia</a> offers the following definition:</p>\n<blockquote>\n<p>A market is one of the many varieties of systems, institutions, procedures,\nsocial relations and infrastructures whereby parties engage in exchange.\n[…]\nIt can be said that a market is the process by which the prices of goods and\nservices are established.</p>\n</blockquote>\n<p>Said otherwise, a market is where sellers and buyers meet. Organized markets,\nor exchanges, define the rules and provide tools to facilitate the transactions,\nmost notably by gathering buyers and sellers intent and matching them.</p>\n<div class=\"figure-wrapper\"><figure id=\"fig-1\"><img src=\"/files/domain-market-0569a45438243c4a89593c703e17634a.png\" alt=\"Organized market model\" srcset=\"/files/domain-market-1.5x-0aed5174c5b4c9b8020cbd51ecbed2bd.png 1.5x, /files/domain-market-2x-010115f63b2ec13c95c1f128f0a3f869.png 2x\"><figcaption><div class=\"figcaption-wrapper\">Organized market model</div></figcaption></figure></div>\n<dl><dt>Market</dt><dd><p>In its stricter acceptation in finance, a market is a notional place where\neconomical agents meet to trade two specific assets. For instance, the\n“Brent Crude market” allows trading oil from the North Sea against US dollar.\nAn exchange typically offers dozens or hundreds of markets.</p></dd><dt>Order</dt><dd><p>An order is an expression of the intent of a buyer or seller. They come in\ntwo variants:</p><ul>\n<li>a <em>limit</em> order expresses the intent to buy or sell a specific\namount at a defined price.</li>\n<li>a <em>market</em> order expresses the intent to buy or sell right now, at\nwhatever best price is currently offered.</li>\n</ul><p>On top of that, the exchange may provide additional features, such as conditional\ntriggers (known as <em>stop loss</em> orders) and follow-up orders.</p></dd><dt>Match engine</dt><dd><p>The match engine is the technical system responsible for gathering orders\nand matching them according to exchange rules. Whenever a match is found,\na transaction happens. That is, the matched orders fill each other, assets are\ntransferred between buyer and seller's accounts, and the orders are removed\nfrom the system.</p></dd><dt>Order book</dt><dd><p>The match engine organizes open orders into a list, known as the order book. It is\nsplit into a bid side (with buy orders) and an ask side (with sell orders).</p><p>The order book reflects the available liquidity on the market at any moment.\nThis information is usually public, though some exchanges offer the possibility\nof concealing orders for a fee.</p></dd><dt>Price</dt><dd><p>As there is no authority quoting the price, it fluctuates naturally according\nto orders and transactions. We typically use three different prices to fully\ndefine the concept:</p><ul>\n<li>the <strong>highest bid</strong> is the price of the order at the top of the bid side\nin the order book. That is, the best price one can sell at right now.</li>\n<li>the <strong>lowest ask</strong> is the price of the order at the bottom of the ask side\nin the order book. That is, the best price one can buy at right now.</li>\n<li>the <strong>last price</strong> is the price the last transaction happened at. That is,\nthe last time a buyer and a seller agreed on a price.</li>\n</ul></dd><dt>Tick</dt><dd><p>A tick represents the change of price from trade to trade. It is therefore a\nreal-time view of buyers and sellers consensus on price, and the most fundamental\nmovement in the market. On very active markets, there may be several thousands\nof ticks per second.</p><p>Ticks are the initial event Cryptomate has to respond to.</p></dd></dl>\n<p>With time, many people devised ways to visualize and interpret market data,\naiming at anticipating future evolution for profit.</p>\n<h3 id=\"market-analysis-and-price\">Market analysis and price</h3>\n<p>Technical analysis is a discipline that studies past market data, in an attempt\nto forecast the direction of price. Without going into the details of this field,\nwe will highlight information that matters to Cryptomate.</p>\n<p>As price moves quickly, visualization systems were developed to build a more\nusable representation of its evolution over time. The most ubiquitous is the\ncandlestick chart. It synthesizes price moves over homogeneous time slices\nof configurable duration.</p>\n<div class=\"figure-wrapper\"><figure id=\"fig-2\"><img src=\"/files/domain-candlestick-8b01defffe5d0dd1393135ddd55c8e8e.png\" alt=\"Candlestick notation\" srcset=\"/files/domain-candlestick-1.5x-fa9aadc4f4af8a54e4d691bd1ee48b41.png 1.5x, /files/domain-candlestick-2x-cfc16f097641aa2ba0fd1efadf823fcf.png 2x\"><figcaption><div class=\"figcaption-wrapper\">Candlestick notation</div></figcaption></figure></div>\n<p>An alternate representation of the same data is bar-charts, that replace the\nshadow/body visual with tiny markers.</p>\n<p>While this model is not the only one, it is the one normally used to store\nand communicate price history. Also, since it was prevalent for much of modern\nfinancial history, a lot of tools use that model to derive additional insight.\nFor instance, the vast majority of indicators (discrete mathematical formulas\nand algorithms to identify patterns) rely on this aggregated data.</p>\n<p>On the other hand, with tick data being increasingly available, and\nalgorithmic trading making it possible to interpret tick stream in a\nmeaningful way, newer indicators are now developing, that work directly\nwith raw ticks in an attempt to get a more accurate picture.</p>\n<div class=\"note\"><p>The market port must therefore support both data streams, in order to\nbuild on the large amount of work done on classical indicators, while\nleveraging the newer tick based ones.</p></div>\n<h2 id=\"market-port-features\">Market port features</h2>\n<p>Having described the events and data the market port has to make available,\nwe can design it accordingly, revolving around two major themes:</p>\n<ul>\n<li>Managing dynamic subscriptions to market event feeds.</li>\n<li>Processing events into meaningful structures.</li>\n</ul>\n<h3 id=\"workflow\">Workflow</h3>\n<p>The aim of the market port is to abstract out all details of connecting to the market\nproviders and handling the real-time feeds. Using it should thus be as simple as handing\nit a feed description and a callback, getting an active subscription in return :</p>\n<div class=\"figure-wrapper\"><figure id=\"fig-3\"><img src=\"/files/market-port-subscribe-5442ed3468a44bdc5c7cb924a2c3f8d3.png\" alt=\"Tick data subscription sequence\" srcset=\"/files/market-port-subscribe-1.5x-1b7817d2527a0e991b16b48231b415f0.png 1.5x, /files/market-port-subscribe-2x-01ce74c7d0686d9cf1ead44e9be1b732.png 2x\"><figcaption><div class=\"figcaption-wrapper\">Tick data subscription – simplified sequence. <strong>Feed</strong> is not an actual object in this diagram, but represents engine's underlying connection to a market.</div></figcaption></figure></div>\n<p><em>The actual process will be a bit more complex, as the market engine will populate\naggregators with past data from a history adapter, if it has one.</em></p>\n<p>Returned subscription should then be stored by the calling code, subsequently used to\naccess aggregated data and, eventually, cancel the subscription.</p>\n<p>While the subscription is active, every update received from the market data feed\ngenerates an event, which is delivered to the supplied callback synchronously :</p>\n<div class=\"figure-wrapper\"><figure id=\"fig-4\"><img src=\"/files/market-port-tickevent-f9cbbc49eda7dadabe42ec8fca6abc39.png\" alt=\"Tick event delivery sequence\" srcset=\"/files/market-port-tickevent-1.5x-fad1cd4db1b1b6c026b2a8f4c1e8ad34.png 1.5x, /files/market-port-tickevent-2x-261a4b132b74440d42019e115c21e019.png 2x\"><figcaption><div class=\"figcaption-wrapper\">Tick event delivery sequence.</div></figcaption></figure></div>\n<p>As event delivery is synchronous, the client object is responsible for choosing\nwhether to process events on the fly, or queue them for processing later, possibly\nin another thread.</p>\n<p>Finally, order book events shall work in the same way, except the aggregator is replaced\nwith an order book view, aggregating order updates into a full view of current order book.</p>\n<h3 id=\"interface-list\">Interface list</h3>\n<p>Assembling the features and objects described in previous sections, here is the full\nlist of interfaces that, together, constitute the market port:</p>\n<div class=\"figure-wrapper\"><figure id=\"fig-5\"><table>\n<thead>\n<tr>\n<th>Name</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code class=\"language-text\">Aggregator</code></td>\n<td>Aggregated candle data over a configurable time period.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">Candle</code></td>\n<td>Aggregated tick data, forming a single candle.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">Engine</code></td>\n<td>Main port entry point.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">FeedDescription</code></td>\n<td>Static data identifying a market on a provider and aggregation settings.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">Tick</code></td>\n<td>Single tick data.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">TickEventHandler</code></td>\n<td>Callable that responds to tick events, provided by client code.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">TickSubscription</code></td>\n<td>Represents a subscription to tick events.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">OrderBook</code></td>\n<td>Instant representation of the order book.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">OrderBookSubscription</code></td>\n<td>Represents a subscription to order book events.</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">OrderBookEventHandler</code></td>\n<td>Callable that responds to order book events, provided by client code.</td>\n</tr>\n</tbody>\n</table><figcaption><div class=\"figcaption-wrapper\">Market port interfaces</div></figcaption></figure></div>\n<p>For detailed specification of each interface, check their definition and inline documentation\non <a href=\"https://github.com/solid-abstractions/cryptomate/tree/posts/market-port/1\" class=\"external\" rel=\"external noopener noreferrer\">GitHub</a>. Note that due to the dynamic nature of python, not all interfaces need to\nappear as python types within the code.</p>\n<h2 id=\"conclusion\">Conclusion</h2>\n<p>That is it for the market port. You will find the result on GitHub under the <a href=\"https://github.com/solid-abstractions/cryptomate/tree/posts/market-port/1/cryptomate/market\" class=\"external\" rel=\"external noopener noreferrer\"><code class=\"language-text\">market</code></a>\ndirectory. In the <a href=\"trading-port\" class=\"internal\">next post</a>, we will turn to the trading port.</p>","fields":{"isPage":false,"slug":"/2018/cryptomate/market-port"},"frontmatter":{"title":"Market Port","classname":null,"date":"2018-10-09T00:00:00.000Z","formattedDate":"October 09, 2018","isoDate":"2018-10-09T00:00:00+00:00"},"headings":[{"value":"Domain model","depth":1},{"value":"What is a market?","depth":2},{"value":"Market analysis and price","depth":2},{"value":"Market port features","depth":1},{"value":"Workflow","depth":2},{"value":"Interface list","depth":2},{"value":"Conclusion","depth":1}],"image":null,"series":{"name":"cryptomate","fullName":null,"fields":{"slug":"/cryptomate"}},"tags":[{"name":"architecture","slug":"/tag/architecture"},{"name":"ports","slug":"/tag/ports"}]}},"pageContext":{"series":"cryptomate","slug":"/2018/cryptomate/market-port","previous":{"fields":{"slug":"/2018/cryptomate/groundwork"},"frontmatter":{"title":"Laying the Groundwork","series":"cryptomate"},"tags":[{"name":"code","slug":"/tag/code"}]},"next":{"fields":{"slug":"/2018/cryptomate/trading-port"},"frontmatter":{"title":"Trading Port","series":"cryptomate"},"tags":[{"name":"architecture","slug":"/tag/architecture"},{"name":"ports","slug":"/tag/ports"}]},"seriesPrevious":{"fields":{"slug":"/2018/cryptomate/groundwork"},"frontmatter":{"title":"Laying the Groundwork","series":"cryptomate"},"tags":[{"name":"code","slug":"/tag/code"}]},"seriesNext":{"fields":{"slug":"/2018/cryptomate/trading-port"},"frontmatter":{"title":"Trading Port","series":"cryptomate"},"tags":[{"name":"architecture","slug":"/tag/architecture"},{"name":"ports","slug":"/tag/ports"}]}}},
    "staticQueryHashes": ["1733002695","4006707078"]}