{"id":4463,"date":"2021-08-28T21:33:39","date_gmt":"2021-08-28T21:33:39","guid":{"rendered":"https:\/\/dalelane.co.uk\/blog\/?p=4463"},"modified":"2021-09-01T09:43:05","modified_gmt":"2021-09-01T09:43:05","slug":"stock-price-events-for-kafka","status":"publish","type":"post","link":"https:\/\/dalelane.co.uk\/blog\/?p=4463","title":{"rendered":"Stock price events for Kafka"},"content":{"rendered":"<p><strong>I&#8217;ve made a <a href=\"https:\/\/github.com\/dalelane\/kafka-connect-stockprice-source\">Kafka Connect source connector<\/a> for sending &#8220;real-time&#8221; (<em>not really<\/em>) stock price events to a Kafka topic.<\/strong><\/p>\n<p>Choose your stock (e.g. &#8220;<a href=\"https:\/\/www.google.com\/finance\/quote\/IBM:NYSE\"><code>IBM<\/code><\/a>&#8220;) and <a href=\"https:\/\/kafka.apache.org\/documentation\/#connect_user\">run the Connector<\/a> pointed at your topic, and every minute a JSON event will be produced like:<\/p>\n<pre style=\"background-color: #eeeeee; padding: 10px; overflow: scroll; border: thin black solid; font-size: smaller;\">{\"open\":137.9,\"high\":137.9,\"low\":137.9,\"close\":137.9,\"volume\":500,\"timestamp\":1629421200,\"datetime\":\"2021-08-19 20:00:00\"}<\/pre>\n<p><!--more-->It&#8217;s a quick and simple hack I needed for a proof-of-concept based on processing live stock price events. But I wanted something I could run with a free API key so it couldn&#8217;t use anything really &#8220;live&#8221; live.<\/p>\n<p>I found <a href=\"https:\/\/www.alphavantage.co\">Alpha Vantage<\/a> who have a few simple APIs, including a <a href=\"https:\/\/www.alphavantage.co\/documentation\/#intraday\">time-series API that returns historical stock price data in one-minute intervals<\/a>. They offer <a href=\"https:\/\/www.alphavantage.co\/support\/#api-key\">free API keys that allow up to 500 requests a day<\/a>.<\/p>\n<p>My Connector is using this to&#8230; cheat a little.<\/p>\n<p>The Connector uses Alpha Vantage&#8217;s time-series API to download a history of one-minute interval time-series records. It caches them and then gradually produces them to the Kafka topic &#8211; each event time-shifted by a configurable number of hours.<\/p>\n<p>For example, if you set the delay to 24, it downloads the last day of stock price records, then sends each stock price event to Kafka twenty-four-hours after the timestamp in the price record.<\/p>\n<p>This means the Connector only needs to make one API call a day, which is easily within the free API tier limits. But it&#8217;s still able to produce an event every minute, so it looks enough like real-time stock price events for my proof-of-concept project.<\/p>\n<p>By default, I set the delay to 168 hours (one week) to make it look even more realistic (for example, it means events are correctly only published on weekdays and not weekends).<\/p>\n<p>Just a quick hack, but I thought I&#8217;d share it. My use case is a little unusual, but if you also need (something that looks a little like a realistic) real-time stock-price events in Kafka, then you can find it on <a href=\"https:\/\/github.com\/dalelane\/kafka-connect-stockprice-source\"><strong>github<\/strong><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve made a Kafka Connect source connector for sending &#8220;real-time&#8221; (not really) stock price events to a Kafka topic. Choose your stock (e.g. &#8220;IBM&#8220;) and run the Connector pointed at your topic, and every minute a JSON event will be produced like: {&#8220;open&#8221;:137.9,&#8221;high&#8221;:137.9,&#8221;low&#8221;:137.9,&#8221;close&#8221;:137.9,&#8221;volume&#8221;:500,&#8221;timestamp&#8221;:1629421200,&#8221;datetime&#8221;:&#8221;2021-08-19 20:00:00&#8243;}<\/p>\n","protected":false},"author":1,"featured_media":4479,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[593,584],"class_list":["post-4463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","tag-apachekafka","tag-kafka"],"_links":{"self":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4463"}],"version-history":[{"count":0,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4463\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/media\/4479"}],"wp:attachment":[{"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalelane.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}