{"id":1807,"date":"2024-08-21T17:12:17","date_gmt":"2024-08-21T17:12:17","guid":{"rendered":"https:\/\/www.pyxll.com\/blog\/?p=1807"},"modified":"2024-11-19T16:10:15","modified_gmt":"2024-11-19T16:10:15","slug":"arcticdb","status":"publish","type":"post","link":"https:\/\/www.pyxll.com\/blog\/arcticdb\/","title":{"rendered":"23 million rows of intra-day FX tick data in Excel using ArcticDB and PyXLL"},"content":{"rendered":"\n<p>Whether you&#8217;re trading for yourself or part of a large trading team, you will more than likely work with huge data sets. Whether you&#8217;re developing and refining your own technical indicators or running fully automated algorithmic trading using machine learning, having fast and easy access to accurate data sets is key. Accurate data sets are essential to your success.<\/p>\n\n\n\n<p>Excel is still, by far, the most used and most flexible tool for working with data. But, you may well be thinking, how can you use Excel effectively when dealing with huge data sets such as intraday FX tick data? Excel is limited to just over 1 million rows, and even so, on a modern fast PC with plenty of memory it still starts to slow down way before you reach that limit.<\/p>\n\n\n\n<p>You can manage large amounts of data in Excel, you just have to be smart about it! Collecting, cleaning and organizing the data is just part of the story. You also need the tools to work with that data. Excel can be a perfect, interactive, front-end for those tools &#8211; in just the same way you might build a web application (but with much less effort)!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-in-excel\">Python in Excel<\/h2>\n\n\n\n<figure class=\"wp-block-image size-medium is-style-default\"><a href=\"https:\/\/www.pyxll.com\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"300\" height=\"110\" data-attachment-id=\"1826\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/arcticdb\/pyxll-logo-with-text\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text.png\" data-orig-size=\"3063,1119\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"pyxll-logo-with-text\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-1024x374.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-300x110.png\" alt=\"\" class=\"wp-image-1826\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-300x110.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-1024x374.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-768x281.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-1536x561.png 1536w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/pyxll-logo-with-text-2048x748.png 2048w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<p>If you&#8217;ve been working with large data sets then you will already be using Python! Python is the most widely used programming language for data science, machine learning, and industries that use those such as Finance and Engineering.<\/p>\n\n\n\n<p>The PyXLL Excel add-in bridges your Python code and Excel, enabling you to build custom tools to use directly in Excel or deploy to Excel users in your team (even if they have no knowledge of Python).<\/p>\n\n\n\n<p>With PyXLL, Excel becomes a user interface toolkit for you to build out solutions in Excel. The Python code runs locally and can import all of your Python packages. You can write Python functions to be called from the workbook, in exactly the same way as Excel&#8217;s built-in functions. Because the Python code still lives outside of Excel, you can reuse the same Python code across Excel, batch processing, web apps, and everywhere else you use that code.<\/p>\n\n\n\n<p>To get started using PyXLL, please visit <a href=\"https:\/\/www.pyxll.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pyxll.com<\/a>.<\/p>\n\n\n\n<p>If you&#8217;re new to PyXLL, this video is a great place to start <a href=\"https:\/\/www.pyxll.com\/docs\/videos\/worksheet-functions.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pyxll.com\/docs\/videos\/worksheet-functions.html<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-introducing-arcticdb\">Introducing ArcticDB<\/h2>\n\n\n\n<figure class=\"wp-block-image size-medium is-resized\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"300\" height=\"218\" data-attachment-id=\"1824\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/arcticdb\/arcticdbcropped\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped.png\" data-orig-size=\"1161,845\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"ArcticDBCropped\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped-1024x745.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped-300x218.png\" alt=\"\" class=\"wp-image-1824\" style=\"width:143px;height:auto\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped-300x218.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped-1024x745.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped-768x559.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/ArcticDBCropped.png 1161w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/figure>\n\n\n\n<p>ArcticDB is a scalable, high performance DataFrame database developed by Man Group. It powers research and trading activity not just within Man Group but across a growing number of organizations including some of the world&#8217;s largest financial firms like Bloomberg to name just one.<\/p>\n\n\n\n<p>One of the key features of ArcticDB is how easy it is to get up and running. Traditionally, setting up a large database requires a lot of effort and coordination with database teams, but with ArcticDB you choose where you want to store you data (even keeping it locally on your PC) and start saving pandas DataFrames to it.<\/p>\n\n\n\n<p>To use ArcticDB in your Python environment, pip install it using the following command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install arcticdb<\/code><\/pre>\n\n\n\n<p>Please visit <a href=\"https:\/\/arcticdb.io\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/arcticdb.io<\/a> for more details about ArcticDB and help getting started. ArcticDB is developed by Man Group, <a href=\"https:\/\/www.man.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.man.com<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-23-millions-rows-in-excel-using-pyxll-and-arcticdb\">23 Millions rows in Excel, using PyXLL and ArcticDB<\/h2>\n\n\n\n<p>To look at how we can leverage large data sets in Excel, we first need a large data set! For this example, we&#8217;re going to use a free foreign exchange intra-day tick data set from <a href=\"https:\/\/www.histdata.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.histdata.com<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-preparing-the-data\">Preparing the data<\/h3>\n\n\n\n<p>Begin by downloading the CSV files from <a href=\"https:\/\/www.histdata.com\/download-free-forex-historical-data\/?\/ascii\/tick-data-quotes\/eurusd\/2023\">https:\/\/www.histdata.com\/download-free-forex-historical-data\/?\/ascii\/tick-data-quotes\/eurusd\/2023<\/a>. Even one month of this data is too big to be loaded into Excel!<\/p>\n\n\n\n<p>Next, unzip each of the downloaded zip files so you have a folder of CSV files. Now we need some Python code to load each of these CSV files so we can save them to ArcticDB.<\/p>\n\n\n\n<p>To load the CSV files I&#8217;ve used another package, polars (<a href=\"https:\/\/pola.rs\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pola.rs<\/a>). This is the fastest way I&#8217;ve found of reading large CSV files. If you don&#8217;t already have polars installed, you can install it by running &#8220;pip install polars&#8221;. My CSV files were located in a folder named &#8220;E:\/fx-data&#8221; so you will need to change the code below to where you&#8217;ve saved the files.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport polars as pl\n\n# Load the csv data using polars\ndf = pl.scan_csv(&quot;E:\/fx-data\/DAT_ASCII_EURUSD_T_*.csv&quot;, has_header=False)\n\n# Extract time, bid, ask and mid (mid is calculated from bid and ask)\ndf = df.select(\n    pl.col(&quot;column_1&quot;).alias(&quot;timestamp&quot;).str.to_datetime(&quot;%Y%m%d %H%M%S%3f&quot;, time_unit=&quot;ns&quot;),\n    pl.col(&quot;column_2&quot;).alias(&quot;bid&quot;),\n    pl.col(&quot;column_3&quot;).alias(&quot;ask&quot;),\n    (pl.col(&quot;column_2&quot;) + ((pl.col(&quot;column_3&quot;) - pl.col(&quot;column_2&quot;)) \/ 2)).alias(&quot;mid&quot;),\n ).sort(&quot;timestamp&quot;, descending=False)\n\n# Convert to pandas and ensure index is sorted\ndfp = df.collect().to_pandas()\ndfp = dfp.set_index(&quot;timestamp&quot;)\ndfp = dfp.sort_index()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-loading-the-data-into-arcticdb\">Loading the data into ArcticDB<\/h3>\n\n\n\n<p>The code above loads all of the fx data into a pandas DataFrame, &#8220;dfp&#8221;. Next we can create an ArcticDB library and save that data. Once the data is saved in ArcticDB it can be queried and retrieved quickly.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport arcticdb as adb\n\n# Create the ArcticDB library on our local file system\nac = adb.Arctic(&quot;lmdb:\/\/E:\/fx-data\/arcticdb&quot;)\nlib = ac.get_library(&quot;fx-test&quot;, create_if_missing=True)\n\n# Write the DataFrame created above to the ArcticDB library\nlib.write(&quot;EURUSD&quot;, dfp)\n<\/pre><\/div>\n\n\n<p>That one line at the end, <code>lib.write(\"EURUSD\", dfp)<\/code>, is all that&#8217;s needed to save our DataFrame to ArcticDB! Next we will look at how to read it back out.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-querying-the-data-from-arcticdb\">Querying the data from ArcticDB<\/h3>\n\n\n\n<p>Let&#8217;s start with something simple; reading a small section of the data for a specific interval.<\/p>\n\n\n\n<p>We first need to get the ArcticDB library object, which I&#8217;m going to write as a function since we will use that quite often:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef adb_library(url, library_name):\n    ac = adb.Arctic(url)\n    return ac&#x5B;library_name]\n<\/pre><\/div>\n\n\n<p>Once we have the library we can access the data using the &#8220;read&#8221; method. We&#8217;ll do that in a second function which we&#8217;ll call &#8220;raw_tick_data&#8221;.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef raw_tick_data(lib, symbol, start, end):\n    data = lib.read(symbol, date_range=&#x5B;start, end])\n    return data.data\n<\/pre><\/div>\n\n\n<p>You&#8217;ll see why I have written the above as two functions very shortly&#8230; but for now, we can call the first to get the library, and the second to get the data for a specific interval as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime as dt\n\nlib = adb_library(&quot;lmdb:\/\/E:\/fx-data\/arcticdb&quot;, &quot;fx-data&quot;)\n\nstart = dt.datetime(2023, 8, 3, 7, 0, 0)\nend = dt.datetime(2023, 8, 3, 7, 1, 0)\n\ndata = raw_tick_data(lib, &quot;EURUSD&quot;, start, end)\n<\/pre><\/div>\n\n\n<p>And with that simple few lines of code we&#8217;ve not got a DataFrame containing the raw tick data for a specific time interval!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-bringing-arcticdb-into-excel-with-pyxll\">Bringing ArcticDB into Excel with PyXLL<\/h3>\n\n\n\n<p>We&#8217;ve seen above how simple and fast it is to query the raw tick data for a time interval using just a little bit of Python code. Let&#8217;s face it though, it would be much more convenient if you could pull the same data up in Excel! Especially if you work as part of a wider team where many people live and breath in Excel, having tools like this at their disposal is invaluable.<\/p>\n\n\n\n<p>To expose those two functions to Excel all we need is to install the PyXLL Excel add-in, configure the add-in to load the module we wrote above, and add the &#8220;@xl_func&#8221; decorator to the functions. The @xl_func decorator tells the PyXLL add-in we would like to call those functions in Excel.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport arcticdb as adb\nfrom pyxll import xl_func\n\n@xl_func(&quot;str, str: object&quot;)\ndef adb_library(url, library_name):\n    ac = adb.Arctic(url)\n    return ac&#x5B;library_name]\n\n@xl_func(&quot;object, str, datetime, datetime, int: dataframe&lt;index=True&gt;&quot;)\ndef raw_tick_data(lib, symbol, start, end):\n    data = lib.read(symbol, date_range=&#x5B;start, end])\n    return data.data\n<\/pre><\/div>\n\n\n<p>The parameter to right of each @xl_func tells the PyXLL add-in how to convert the Excel input arguments to Python, and how to convert the Python return value to an Excel type. Other than that, you can see the code is exactly the same as before.<\/p>\n\n\n\n<p>With the Python module saved, PyXLL configured to load it, and Excel started, we can call &#8220;=adb_library&#8221; from an Excel workbook and the ArcticDB Library object is returned to the sheet.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154023.png\"><img decoding=\"async\" width=\"633\" height=\"141\" data-attachment-id=\"1817\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/arcticdb\/screenshot-2024-08-19-154023\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154023.png\" data-orig-size=\"633,141\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot 2024-08-19 154023\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154023.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154023.png\" alt=\"\" class=\"wp-image-1817\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154023.png 633w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154023-300x67.png 300w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/a><\/figure>\n\n\n\n<p>We can then pass that Library object to our &#8220;raw_tick_data&#8221; function, along with the other options for the ticker, start and end date, and load the data we need directly in Excel &#8211; no Python experience required!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304.png\"><img decoding=\"async\" width=\"919\" height=\"508\" data-attachment-id=\"1818\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/arcticdb\/screenshot-2024-08-19-154304\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304.png\" data-orig-size=\"919,508\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot 2024-08-19 154304\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304.png\" alt=\"\" class=\"wp-image-1818\" style=\"width:653px;height:auto\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304.png 919w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304-300x166.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-154304-768x425.png 768w\" sizes=\"(max-width: 919px) 100vw, 919px\" \/><\/a><\/figure>\n\n\n\n<p>We now have an amazingly quick and simple way to access huge data sets directly in Excel, pulling in only the data needed so the sheet stays small and responsive.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-making-sense-of-tick-data-using-resampling\">Making sense of tick data using resampling<\/h3>\n\n\n\n<p>Querying the raw tick data is great if we&#8217;re drilling in to a very specific time period. Other times, we want to see how the market is behaving at a higher level. By &#8216;resampling&#8217; the tick data we can calculate the open, high, low and close price for regular intervals. This collapses the fine grained tick data into something much more comprehensible.<\/p>\n\n\n\n<p>To resample our tick data into 15 minute intervals, for each 15 minute interval we record the price at the start of the interval (open); the maximum price in that interval (high); lowest price in the interval (low) and the last price in the interval (close).<\/p>\n\n\n\n<p>ArcticDB makes resampling data in this way simple, and fast! Letting ArcticDB take care of the resampling is much more efficient than loading all of the data and doing it in Python. Once we expose that functionality to Excel the result is a super-fast tool for building interactive spreadsheets.<\/p>\n\n\n\n<p>We&#8217;ll write a function as before to query the ArcticDB library. This time we use the QueryBuilder API to build a query that fetches the data and does the resampling in one go:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n@xl_func(&quot;object, str, datetime, datetime, str, int: dataframe&lt;index=True&gt;&quot;)\ndef resampled_tick_data(lib, symbol, start, end, freq):\n    qb = adb.QueryBuilder()\n\n    qb = qb.resample(freq, closed=&#039;right&#039;).agg({\n        &#039;high&#039;: (&#039;mid&#039;, &#039;max&#039;),\n        &#039;low&#039;: (&#039;mid&#039;, &#039;min&#039;),\n        &#039;open&#039;: (&#039;mid&#039;, &#039;first&#039;),\n        &#039;close&#039;: (&#039;mid&#039;, &#039;last&#039;)\n    })\n\n    data = lib.read(symbol,\n                    date_range=&#x5B;start, end],\n                    query_builder=qb)\n\n    df = data.data.dropna()\n    return df\n<\/pre><\/div>\n\n\n<p>I&#8217;ve included the @xl_func decorator on this function as with the functions above. This is what makes this function callable from Excel. With this function added to the Python module, either reload the PyXLL add-in or restart Excel and the function will be available.<\/p>\n\n\n\n<p>There&#8217;s a more detailed example of using resampling in the ArcticDB examples here <a href=\"https:\/\/docs.arcticdb.io\/dev\/notebooks\/ArcticDB_demo_resample\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/docs.arcticdb.io\/dev\/notebooks\/ArcticDB_demo_resample<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204.png\"><img decoding=\"async\" width=\"892\" height=\"634\" data-attachment-id=\"1834\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/arcticdb\/screenshot-2024-08-19-165204\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204.png\" data-orig-size=\"892,634\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot 2024-08-19 165204\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204.png\" alt=\"\" class=\"wp-image-1834\" style=\"width:591px;height:auto\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204.png 892w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204-300x213.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-165204-768x546.png 768w\" sizes=\"(max-width: 892px) 100vw, 892px\" \/><\/a><\/figure>\n\n\n\n<p>Querying and resampling data using this function in Excel is incredibly fast!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-plotting-a-candlestick-chart-in-excel\">Plotting a candlestick chart in Excel<\/h3>\n\n\n\n<p>Now we&#8217;ve learned how to query and resample data we can look at ways to visualize that data. A common chart type used in finance is the &#8220;candlestick&#8221; chart. It&#8217;s exactly what we need to show the open\/high\/low\/close (OHLC) resampled data we&#8217;ve computed above.<\/p>\n\n\n\n<p>Our final function queries and resamples the data as before. But, rather than return the data to be displayed in the Excel grid, we will plot the data as a chart and display that chart in Excel.<\/p>\n\n\n\n<p>To do this we will use two more packages, &#8220;mplfinance&#8221; and &#8220;matplotlib&#8221;. Matplotlib is a well known Python plotting library and can be used to create virtually any type of plot. To make things simple, mplfinance provides some convenience functions specific to finance, such as plotting candlestick charts.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport mplfinance as mpf\nimport pyxll\n\n@xl_func(&quot;object, str, datetime, datetime, str, int&#x5B;], str: var&quot;)\ndef plot_fx_rates(lib, symbol, start, end, freq=&quot;1D&quot;, mav=&#x5B;], style=&quot;seaborn-v0_8&quot;):\n    # Build the query that will get the resampled data\n    qb = adb.QueryBuilder()\n    qb = qb.resample(freq, closed=&#039;right&#039;).agg({\n        &#039;high&#039;: (&#039;mid&#039;, &#039;max&#039;),\n        &#039;low&#039;: (&#039;mid&#039;, &#039;min&#039;),\n        &#039;open&#039;: (&#039;mid&#039;, &#039;first&#039;),\n        &#039;close&#039;: (&#039;mid&#039;, &#039;last&#039;)\n    })\n\n    # Read the data from our ArcticDB library\n    df = lib.read(symbol,\n        date_range=&#x5B;start, end],\n        query_builder=qb).data\n\n    # Plot the candlestick chart using pyplot and mplfinance\n    with plt.style.context(style):\n        # Get a matplotlib Figure and Axes object to plot the candlestick chart on\n        fig, ax = plt.subplots()\n        fig.suptitle(symbol)\n        ax.grid(True)\n\n        # Use matplotlibfinance to draw the candlestick chart\n        mpf.plot(df,\n                 type=&#039;candle&#039;,\n                 mav=&#x5B;x for x in mav if x],\n                 ax=ax)\n\n        fig.tight_layout()\n\n        # Show the chart in Excel\n        pyxll.plot(fig)\n\n    return fig\n<\/pre><\/div>\n\n\n<p>The PyXLL function &#8220;plot&#8221; takes the matplotlib figure and displays it in Excel, just below where the function is called. The PyXLL plot function also works with other Python plotting libraries so you can create any visualizations you need beyond the basic Excel charts, using Python.<\/p>\n\n\n\n<p>You might have noticed the addition of the &#8220;mav&#8221; argument to the function above. This is a list of &#8220;moving averages&#8221;. The mplfinance library can draw the moving averages for us. This extra argument allows the Excel user to pass in a list of periods so they can control which moving averages get drawn.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118.png\"><img decoding=\"async\" width=\"1024\" height=\"985\" data-attachment-id=\"1841\" data-permalink=\"https:\/\/www.pyxll.com\/blog\/arcticdb\/screenshot-2024-08-19-174118\/\" data-orig-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118.png\" data-orig-size=\"1051,1011\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Screenshot 2024-08-19 174118\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118-1024x985.png\" src=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118-1024x985.png\" alt=\"\" class=\"wp-image-1841\" style=\"width:711px;height:auto\" srcset=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118-1024x985.png 1024w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118-300x289.png 300w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118-768x739.png 768w, https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/Screenshot-2024-08-19-174118.png 1051w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>To read more about the plotting capabilities of PyXLL, see this page in the PyXLL docs <a href=\"https:\/\/www.pyxll.com\/docs\/userguide\/plotting\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pyxll.com\/docs\/userguide\/plotting\/index.html<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-wrapping-up\">Wrapping Up<\/h2>\n\n\n\n<p>In this post we&#8217;ve seen how to save and query huge data sets into the ArcticDB database. ArcticDB greatly simplifies organizing your financial tick data, or indeed any time series data you might be dealing with!<\/p>\n\n\n\n<p>Using PyXLL, we can write tools to be used in Excel, written in Python. This allows us to take advantage of packages like ArcticDB from within Excel. By using Excel as a front-end to our Python code we massively reduce the time needed to develop interactive tools. Rather than a web app with the obligatory &#8220;Export to Excel&#8221; button, or overnight reports generating CSV files, we can have the tools we need right in Excel.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-resources\">Resources<\/h2>\n\n\n\n<p>All of the code and an example workbook used in this blog post can be downloaded from here <a href=\"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/arcticdb-fx-demo.zip\">https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/arcticdb-fx-demo.zip<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For more information about <strong>ArcticDB <\/strong>please visit <a href=\"https:\/\/arcticdb.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/arcticdb.io<\/a><\/li>\n\n\n\n<li>If you would like to learn more about <strong>PyXLL<\/strong>, please go to <a href=\"https:\/\/www.pyxll.com\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pyxll.com<\/a><\/li>\n\n\n\n<li>ArcticDB is developed by <strong>Man Group<\/strong>. You can find there website here <a href=\"https:\/\/www.man.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.man.com<\/a><\/li>\n<\/ul>\n\n\n\n<p>For any questions please contact PyXLL support via <a href=\"https:\/\/www.pyxll.com\/contact.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pyxll.com\/contact.html<\/a> and we will be happy to assist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whether you&#8217;re trading for yourself or part of a large trading team, you will more than likely work with huge data sets. Whether you&#8217;re developing and refining your own technical indicators or running fully automated algorithmic trading using machine learning, having fast and easy access to accurate data sets is key. Accurate data sets are<\/p>\n","protected":false},"author":1,"featured_media":1822,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[11,12,504,272],"tags":[],"class_list":["post-1807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib","category-python","category-user-interface","category-visualization"],"acf":[],"jetpack_featured_media_url":"https:\/\/www.pyxll.com\/blog\/wp-content\/uploads\/2024\/08\/23mm-chart2.png","jetpack_shortlink":"https:\/\/wp.me\/s7l3LP-arcticdb","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/1807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/comments?post=1807"}],"version-history":[{"count":1,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/1807\/revisions"}],"predecessor-version":[{"id":1884,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/posts\/1807\/revisions\/1884"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/media\/1822"}],"wp:attachment":[{"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/media?parent=1807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/categories?post=1807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pyxll.com\/blog\/wp-json\/wp\/v2\/tags?post=1807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}