{"id":16510,"date":"2023-05-02T12:49:56","date_gmt":"2023-05-02T12:49:56","guid":{"rendered":"https:\/\/www.anychart.com\/blog\/?p=16510"},"modified":"2023-05-18T19:41:17","modified_gmt":"2023-05-18T19:41:17","slug":"candlestick-chart-stock-analysis","status":"publish","type":"post","link":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/","title":{"rendered":"How to Create Candlestick Chart with JavaScript for Stock Analysis"},"content":{"rendered":"<p><a href=\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\"><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\" alt=\"Candlestick Chart Built with JavaScript for Stock Analysis\" width=\"100%\" class=\"alignnone size-full wp-image-16518\" srcset=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png 1366w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide-300x169.png 300w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide-768x432.png 768w, https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide-1024x576.png 1024w\" sizes=\"(max-width: 1366px) 100vw, 1366px\" \/><\/a>Candlestick charts are a fantastic data visualization tool for tracking the price movements of stocks over a period of time. In this tutorial, I&#39;ll show you how to create your own candlestick chart using JavaScript.<\/p>\n<p>We\u2019ll be using TSMC (Taiwan Semiconductor Manufacturing Company) as an example, as they&#39;re the largest contract chip maker in the world and have been making waves in the stock market recently.<\/p>\n<p>By the end of this tutorial, you&#39;ll be able to create your own JS candlestick charts and analyze stock trends like a pro. So, let&#39;s get started and dive into the exciting world of data visualization with JavaScript!<\/p>\n<p><!--more Read the JS charting tutorial \u00bb--><\/p>\n<h2 id=\"what-is-a-candlestick-chart-\">What Is Candlestick Chart?<\/h2>\n<p>You may already be familiar with candlestick charts, but just in case you need a quick refresher, here&#39;s the gist: A candlestick chart, or a Japanese candlestick chart, is a type of chart used to <a href=\"https:\/\/www.anychart.com\/chartopedia\/chart-type\/japanese-candlestick-chart\/\">visualize the price movement<\/a> of a stock over time. Each point on the graph represents a specific time period, and the &quot;candle&quot; shape is made up of a rectangular body and two wicks that extend from either end.<\/p>\n<p>The body of the candle represents the difference between the opening and closing prices, while the wicks represent the highest and lowest prices that occurred during that time period. The color of the candle can indicate whether the stock price went up or down during that period, with green or blue usually indicating a rise and red indicating a decline.<\/p>\n<p>Candlestick charts are a popular tool used by traders and analysts to identify trends and patterns in stock prices. And now, with the power of JavaScript, you can create your very own candlesticks and impress all your finance friends with your data visualization skills!<\/p>\n<h2 id=\"candlestick-chart-being-built\">Candlestick Chart Being Built<\/h2>\n<p>Are you ready to create your very own candlestick chart with JavaScript? I sure am! By the end of this tutorial, you&#39;ll have a beautiful graph that looks something like the one below:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-js-final.png\" width=\"100%\" alt=\"Final interactive stock candlestick chart on a web page, built with JavaScript (HTML5)\"><\/p>\n<p>Exciting, right? So let&#39;s not waste any time, and get right into the fun part: building our candlestick chart!<\/p>\n<h2 id=\"steps-to-build-a-basic-js-candlestick-chart\">Steps to Build a Basic JS Candlestick Chart<\/h2>\n<p>Okay, let&#39;s get started with building our very own candlestick chart using JavaScript! Don&#39;t worry if you&#39;re new to this \u2014 I&#39;ll walk you through each step, and before you know it, you&#39;ll be a pro at it!<\/p>\n<p>Here&#39;s a quick list of what we&#39;ll be doing:<\/p>\n<ol>\n<li>Create a basic HTML page.<\/li>\n<li>Insert the required JS files.<\/li>\n<li>Set the data.<\/li>\n<li>Write some JS charting code.<\/li>\n<\/ol>\n<p>Let&#39;s get started!<\/p>\n<h3 id=\"1-create-a-basic-html-page\">1. Create a Basic HTML Page<\/h3>\n<p>To start things off, let&#39;s create a basic HTML page and give it the snazzy title &quot;JavaScript Candlestick Chart.&quot; We&#39;ll add an HTML block element called <code>&lt;div&gt;<\/code> to contain our masterpiece, and give it a special ID attribute of &quot;container&quot; so we can refer to it later in the code.<\/p>\n<p>To make our chart look extra slick, we&#39;ll throw in some CSS rules in the <code>&lt;style&gt;<\/code> block. We can set the width and height properties to 100% and the margin and padding to 0. Of course, you can always change these to suit your personal style.<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-meta\">&lt;!DOCTYPE html&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"utf-8\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>JavaScript Candlestick Chart<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span><span class=\"css\">\r\n      <span class=\"hljs-selector-tag\">html<\/span>,\r\n      <span class=\"hljs-selector-tag\">body<\/span>,\r\n      <span class=\"hljs-selector-id\">#container<\/span> {\r\n        <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n        <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n      }\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<h3 id=\"2-insert-the-required-js-files\">2. Insert the Required JS files<\/h3>\n<p>Using <a href=\"https:\/\/en.wikipedia.org\/wiki\/Wikipedia:Articles_for_deletion\/Comparison_of_JavaScript_charting_libraries\" target=\"_blank\" rel=\"nofollow\">JavaScript charting libraries<\/a> is an absolute game-changer when it comes to creating stunning interactive visualizations. Luckily, you don&#39;t need to be a technical wizard to use them. For this tutorial, we&#39;ll be working with <a href=\"https:\/\/www.anychart.com\/\">AnyChart<\/a>, which has detailed <a href=\"https:\/\/docs.anychart.com\/\" target=\"_blank\" rel=\"nofollow\">documentation<\/a> (e.g., candlestick <a href=\"https:\/\/docs.anychart.com\/Basic_Charts\/Japanese_Candlestick_Chart\" target=\"_blank\" rel=\"nofollow\">here<\/a> and stock candlestick <a href=\"https:\/\/docs.anychart.com\/Stock_Charts\/Series\/Japanese_Candlestick\" target=\"_blank\" rel=\"nofollow\">here<\/a>), and does not require a fee for non-commercial use.<\/p>\n<p>Next up, we need to insert the required JS files. This is where the beauty of CDNs (Content Delivery Networks) comes in handy. We can simply add the links to the Core, Stock, and Data Adapter <a href=\"https:\/\/docs.anychart.com\/Quick_Start\/Modules\" target=\"_blank\" rel=\"nofollow\">modules<\/a> in the <code>&lt;head&gt;<\/code> section of our HTML page. We&#39;ll also need to add the CSS links for UI and fonts to ensure our candlestick chart looks top-notch.<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-meta\">&lt;!DOCTYPE html&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"utf-8\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>JavaScript Candlestick Chart<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/css\/anychart-ui.min.css\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/fonts\/css\/anychart-font.min.css\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span>  \r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-stock.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-data-adapter.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span><span class=\"css\">\r\n      <span class=\"hljs-selector-tag\">html<\/span>,\r\n      <span class=\"hljs-selector-tag\">body<\/span>,\r\n      <span class=\"hljs-selector-id\">#container<\/span> {\r\n        <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n        <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n      }\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span>&gt;<\/span><span class=\"actionscript\">\r\n      <span class=\"hljs-comment\">\/\/ All the code for the JS Candlestick chart will come here<\/span>\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>Later on, we might need several other modules for customization.<\/p>\n<h3 id=\"3-set-the-data\">3. Set the Data<\/h3>\n<p>To set up the data for the candlestick chart, we will use the <a href=\"https:\/\/finance.yahoo.com\/quote\/TSM\/history?p=TSM\" target=\"_blank\" rel=\"nofollow\">stock data of TSMC<\/a>, which I have compiled into a CSV file available at <a href=\"https:\/\/gist.githubusercontent.com\/awanshrestha\/6481638c175e82dc6a91a499a8730057\/raw\/1791ef1f55f0be268479286284a0452129371003\/TSMC.csv\" target=\"_blank\" rel=\"nofollow\">this link<\/a>. Loading the data is easy with the <code>anychart.data.loadCsvFile()<\/code> function, which we can use to load the data from the CSV file into our chart. This function will parse the CSV file and create a data table, which will be used to plot the chart.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-title\">anychart<\/span>.<span class=\"hljs-class\"><span class=\"hljs-keyword\">data<\/span>.loadCsvFile('<span class=\"hljs-title\">https<\/span>:\/\/<span class=\"hljs-title\">gist<\/span>.<span class=\"hljs-title\">githubusercontent<\/span>.<span class=\"hljs-title\">com<\/span>\/<span class=\"hljs-title\">awanshrestha<\/span>\/6481638<span class=\"hljs-title\">c175e82dc6a91a499a8730057<\/span>\/<span class=\"hljs-title\">raw<\/span>\/1791<span class=\"hljs-title\">ef1f55f0be268479286284a0452129371003<\/span>\/<span class=\"hljs-type\">TSMC<\/span>.<span class=\"hljs-title\">csv'<\/span>, <span class=\"hljs-title\">function<\/span> (<span class=\"hljs-title\">data<\/span>) {});<\/span>\r\n<\/code><\/pre>\n<h3 id=\"4-write-some-js-charting-code\">4. Write Some JS Charting Code<\/h3>\n<p>Alright, it&#39;s time to put all the pieces together and create our awesome candlestick chart for TSMC! Let&#39;s start by writing some JS code to make our chart functional.<\/p>\n<p>First off, we need to wrap our code in the <code>anychart.onDocumentReady()<\/code> function. This ensures that the code is executed only after the document has finished loading.<\/p>\n<pre><code class=\"lang-javascript\">anychart.onDocumentReady(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <\/span>{\r\n  anychart.data.loadCsvFile(\r\n<span class=\"hljs-string\">'https:\/\/gist.githubusercontent.com\/awanshrestha\/6481638c175e82dc6a91a499a8730057\/raw\/1791ef1f55f0be268479286284a0452129371003\/TSMC.csv'<\/span>,\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">(data)<\/span> <\/span>{});\r\n});\r\n<\/code><\/pre>\n<p>Since candlestick charts can process table-formatted data, we create a table using the <code>table()<\/code> method and add our data to it with the <code>addData()<\/code> method.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-title\">var<\/span> dataTable = anychart.<span class=\"hljs-class\"><span class=\"hljs-keyword\">data<\/span>.table();<\/span>\r\n<span class=\"hljs-title\">dataTable<\/span>.addData(<span class=\"hljs-class\"><span class=\"hljs-keyword\">data<\/span>);<\/span>\r\n<\/code><\/pre>\n<p>But hold up, we&#39;re not done yet! We need to map our data properly to make it visually appealing. So, we use the <code>mapAs()<\/code> function to create a new mapping object responsible for mapping the data.<\/p>\n<pre><code class=\"lang-javascript\">var mapping = dataTable.mapAs({\r\n<span class=\"hljs-symbol\">  open:<\/span> <span class=\"hljs-number\">1<\/span>,\r\n<span class=\"hljs-symbol\">  high:<\/span> <span class=\"hljs-number\">2<\/span>,\r\n<span class=\"hljs-symbol\">  low:<\/span> <span class=\"hljs-number\">3<\/span>,\r\n<span class=\"hljs-symbol\">  close:<\/span> <span class=\"hljs-number\">4<\/span>\r\n});\r\n<\/code><\/pre>\n<p>Next, we create a stock chart and a plot to represent our data.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-comment\">\/\/ create a stock chart<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> chart = anychart.stock();\r\n<span class=\"hljs-comment\">\/\/ create the plot<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> <span class=\"hljs-keyword\">plot<\/span> = chart.<span class=\"hljs-keyword\">plot<\/span>(0);\r\n<\/code><\/pre>\n<p>Moving on, we work on the grid settings and create a series for our data. Using the <code>addSeries()<\/code> method, we configure our series name to be TSMC using the <code>name()<\/code> function. And to add a playful touch, we use the <code>iconType()<\/code> function to show the rise and fall of the stock markets with arrowheads pointing up and down, respectively.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-comment\">\/\/ set the grid<\/span>\r\nplot.yGrid(<span class=\"hljs-literal\">true<\/span>).xGrid(<span class=\"hljs-literal\">true<\/span>).yMinorGrid(<span class=\"hljs-literal\">true<\/span>).xMinorGrid(<span class=\"hljs-literal\">true<\/span>);\r\n<span class=\"hljs-comment\">\/\/ create the candlestick series<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> series = plot.candlestick(mapping);\r\nseries.name(<span class=\"hljs-string\">'TSMC'<\/span>);\r\nseries.legendItem().iconType(<span class=\"hljs-string\">'rising-falling'<\/span>);\r\n<\/code><\/pre>\n<p>To give our chart a title, we use the <code>title()<\/code> function. And finally, we specify the container of our chart as the HTML div with the id &quot;container&quot;. And voila! Our JavaScript-based candlestick chart for TSMC is ready to be rendered with the <code>draw()<\/code> function.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-comment\">\/\/ set the title of the chart<\/span>\r\n<span class=\"hljs-selector-tag\">chart<\/span><span class=\"hljs-selector-class\">.title<\/span>(<span class=\"hljs-string\">'TSMC Stock Chart'<\/span>);\r\n<span class=\"hljs-comment\">\/\/ set the container id for the chart<\/span>\r\n<span class=\"hljs-selector-tag\">chart<\/span><span class=\"hljs-selector-class\">.container<\/span>(<span class=\"hljs-string\">'container'<\/span>);\r\n<span class=\"hljs-comment\">\/\/ initiate the chart drawing<\/span>\r\n<span class=\"hljs-selector-tag\">chart<\/span><span class=\"hljs-selector-class\">.draw<\/span>();\r\n<\/code><\/pre>\n<p>And here it is. Our (first) candlestick chart for the TSMC:<\/p>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-PGCK5IPH\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/PGCK5IPH\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-PGCK5IPH{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<p>But wait, there&#39;s more! In the next section, we&#39;ll learn how to customize our chart to make it even more visually appealing and informative. So keep reading!<\/p>\n<p>The <a href=\"https:\/\/playground.anychart.com\/PGCK5IPH\" target=\"_blank\" rel=\"nofollow\">Playground Link<\/a> is also available for you to play with the interactive version of this visualization. And the entire code for the basic JS candlestick chart has been placed below for your convenience.<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-meta\">&lt;!DOCTYPE html&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"utf-8\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>JavaScript Candlestick Chart<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/css\/anychart-ui.min.css\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/fonts\/css\/anychart-font.min.css\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span>  \r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-stock.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-data-adapter.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span><span class=\"css\">\r\n      <span class=\"hljs-selector-tag\">html<\/span>,\r\n      <span class=\"hljs-selector-tag\">body<\/span>,\r\n      <span class=\"hljs-selector-id\">#container<\/span> {\r\n        <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n        <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n      }\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span>&gt;<\/span><span class=\"actionscript\">\r\nanychart.onDocumentReady(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <\/span>{\r\n  <span class=\"hljs-comment\">\/\/ load data<\/span>\r\n  anychart.data.loadCsvFile(\r\n<span class=\"hljs-string\">'https:\/\/gist.githubusercontent.com\/awanshrestha\/6481638c175e82dc6a91a499a8730057\/raw\/1791ef1f55f0be268479286284a0452129371003\/TSMC.csv'<\/span>,\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">(data)<\/span> <\/span>{\r\n      <span class=\"hljs-comment\">\/\/ create a data table with the loaded data<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> dataTable = anychart.data.table();\r\n      dataTable.addData(data);\r\n      <span class=\"hljs-comment\">\/\/ map the loaded data for the candlestick series<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> mapping = dataTable.mapAs({\r\n        open: <span class=\"hljs-number\">1<\/span>,\r\n        high: <span class=\"hljs-number\">2<\/span>,\r\n        low: <span class=\"hljs-number\">3<\/span>,\r\n        close: <span class=\"hljs-number\">4<\/span>\r\n      });\r\n      <span class=\"hljs-comment\">\/\/ create a stock chart<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> chart = anychart.stock();\r\n      <span class=\"hljs-comment\">\/\/ create the chart plot<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> plot = chart.plot(<span class=\"hljs-number\">0<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the grid settings<\/span>\r\n      plot\r\n        .yGrid(<span class=\"hljs-literal\">true<\/span>)\r\n        .xGrid(<span class=\"hljs-literal\">true<\/span>)\r\n        .yMinorGrid(<span class=\"hljs-literal\">true<\/span>)\r\n        .xMinorGrid(<span class=\"hljs-literal\">true<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ create the candlestick series<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> series = plot.candlestick(mapping);\r\n      series.name(<span class=\"hljs-string\">'TSMC'<\/span>);\r\n      series.legendItem().iconType(<span class=\"hljs-string\">'rising-falling'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the title of the chart<\/span>\r\n      chart.title(<span class=\"hljs-string\">'TSMC Stock Chart'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the container id for the chart<\/span>\r\n      chart.container(<span class=\"hljs-string\">'container'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ initiate the chart drawing<\/span>\r\n      chart.draw();\r\n    }\r\n  );\r\n});\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<h2 id=\"options-to-customize-a-js-candlestick-chart\">Options to Customize a JS Candlestick Chart<\/h2>\n<p>Alright, now that we&#39;ve got our basic JavaScript-based candlestick chart up and running, it&#39;s time to put our creative hats on and add some customizations to make it even more awesome! Let&#39;s explore some fun ways to jazz up our chart and make it stand out from the rest. <\/p>\n<h3 id=\"a-get-a-custom-date-range-picker-and-selector\">A. Get a Custom Date Range Picker and Selector<\/h3>\n<p>Buckle up, it&#39;s time to play with some dates! Let&#39;s add some customization to our candlestick chart by setting up a custom <a href=\"https:\/\/docs.anychart.com\/Stock_Charts\/Range_Selection\" target=\"_blank\" rel=\"nofollow\">date range picker and selector<\/a>. With this feature, users can easily navigate date\/time-based charts and select specific periods for further analysis.<\/p>\n<p>To get started, we need to add the Common UI module by including the following CDN link in the head section of our HTML page.<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-ui.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>Once added, we can use the range picker to facilitate the use of the scroller by providing two input fields that allow users to customize the start and end dates on the display.<\/p>\n<pre><code class=\"lang-javascript\">var rangePicker = anychart.ui.rangePicker()<span class=\"hljs-comment\">;<\/span>\r\nrangePicker.render(chart)<span class=\"hljs-comment\">;<\/span>\r\n<\/code><\/pre>\n<p>Additionally, we can use the range selector, which provides a set of buttons for selecting certain time periods. <\/p>\n<pre><code class=\"lang-javascript\">var rangeSelector = anychart.ui.rangeSelector()<span class=\"hljs-comment\">;<\/span>\r\nrangeSelector.render(chart)<span class=\"hljs-comment\">;<\/span>\r\n<\/code><\/pre>\n<p>For our chart, we&#39;ve chosen a custom date range of 2020 to 2022 to represent the post-COVID scenario.<\/p>\n<pre><code class=\"lang-javascript\">chart.selectRange('<span class=\"hljs-number\">2020<\/span><span class=\"hljs-number\">-01<\/span><span class=\"hljs-number\">-01<\/span>', '<span class=\"hljs-number\">2022<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-31<\/span>');\r\n<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-nKoPzl6N\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/E7E28qyb\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-nKoPzl6N{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3 id=\"b-configure-the-visual-appearance\">B. Configure the Visual Appearance<\/h3>\n<p>Who said charts have to be boring? Let&#39;s make our JS candlestick chart stand out with some visual enhancements! In this section, we will explore how to configure the <a href=\"https:\/\/docs.anychart.com\/Appearance_Settings\/Themes\" target=\"_blank\" rel=\"nofollow\">theme<\/a> of the chart.<\/p>\n<p>The theme changes the face, feel, and layout of the candlestick chart. I want to go with something glamorous. To get started, we need to add the following CDN link in the head section of our HTML page.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/themes\/dark_glamour.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>Once added, we can use the function <code>theme()<\/code> to change the theme or background color of the chart. <\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-selector-tag\">anychart<\/span><span class=\"hljs-selector-class\">.theme<\/span>(<span class=\"hljs-string\">'darkGlamour'<\/span>);\r\n<\/code><\/pre>\n<p>With the code provided below, we can change different parts of the candlesticks to create a unique look and feel. And don&#39;t forget the conditional coloring of the chart: green for positive growth and red for negative.<\/p>\n<pre><code class=\"lang-javascript\">series.fallingFill(<span class=\"hljs-string\">\"#FF0D0D\"<\/span>)<span class=\"hljs-comment\">;<\/span>\r\nseries.fallingStroke(<span class=\"hljs-string\">\"#FF0D0D\"<\/span>)<span class=\"hljs-comment\">;<\/span>\r\nseries.risingFill(<span class=\"hljs-string\">\"#43FF43\"<\/span>)<span class=\"hljs-comment\">;<\/span>\r\nseries.risingStroke(<span class=\"hljs-string\">\"#43FF43\"<\/span>)<span class=\"hljs-comment\">;<\/span>\r\n<\/code><\/pre>\n<p>Ta-da! Feast your eyes on the beautiful transformation of our candlestick chart!<\/p>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-nKoPzl6N\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/3YQ8GiRm\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-nKoPzl6N{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3 id=\"c-add-event-markers\">C. Add Event Markers<\/h3>\n<p>Let&#39;s make our candlestick chart even more informative by adding <a href=\"https:\/\/docs.anychart.com\/Stock_Charts\/Event_Markers\/Overview\" target=\"_blank\" rel=\"nofollow\">event markers<\/a>. This will provide a better understanding of key developments and dividends that occurred during the selected time period.<\/p>\n<p>To begin with, we need to establish the settings for the event markers in our graph.<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-attribute\">var eventMarkers<\/span> = plot.eventMarkers();\r\n<\/code><\/pre>\n<p>We can also customize the appearance of our event markers.<br \/>\nset the symbol of event markers<\/p>\n<pre><code class=\"lang-javascript\">plot.eventMarkers().format(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>.getData(<span class=\"hljs-string\">\"symbol\"<\/span>);\r\n});\r\n<\/code><\/pre>\n<p>Next, we add the data for the event markers. I have added some significant events that occurred between 2020 and 2022 based on information from the <a href=\"https:\/\/en.wikipedia.org\/wiki\/TSMC\" target=\"_blank\" rel=\"nofollow\">TSMC Wikipedia page<\/a> and various news portals.<\/p>\n<pre><code class=\"lang-javascript\">eventMarkers.data([\r\n  { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">date:<\/span> <span class=\"hljs-string\">'2020-03-11'<\/span>, <span class=\"hljs-string\">description:<\/span> <span class=\"hljs-string\">'WHO declares COVID-19 a global pandemic'<\/span> },\r\n  { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">date:<\/span> <span class=\"hljs-string\">'2020-11-20'<\/span>, <span class=\"hljs-string\">description:<\/span> <span class=\"hljs-string\">'TSMC wins approval from Arizona for $12 billion chip plant'<\/span> },\r\n  { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">date:<\/span> <span class=\"hljs-string\">'2021-07-12'<\/span>, <span class=\"hljs-string\">description:<\/span> <span class=\"hljs-string\">'TSMC and Foxconn reach deals to buy COVID-19 vaccines for Taiwan'<\/span> },\r\n  { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-string\">date:<\/span> <span class=\"hljs-string\">'2021-11-09'<\/span>, <span class=\"hljs-string\">description:<\/span> <span class=\"hljs-string\">'TSMC announces to build a specialty technology fab in Japan with Sony'<\/span> },\r\n  { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">date:<\/span> <span class=\"hljs-string\">'2022-02-24'<\/span>, <span class=\"hljs-string\">description:<\/span> <span class=\"hljs-string\">'Russia-Ukraine war begins'<\/span> },\r\n  { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-string\">date:<\/span> <span class=\"hljs-string\">'2022-11-15'<\/span>, <span class=\"hljs-string\">description:<\/span> <span class=\"hljs-string\">'Berkshire Hathaway discloses a $4.1 billion stake in TSMC'<\/span> },\r\n]);\r\n<\/code><\/pre>\n<p>The result of implementing all of the above code will be a candlestick chart that includes event markers, making it easier to understand the key events that took place during the selected time period.<\/p>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-nKoPzl6N\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/nKoPzl6N\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-nKoPzl6N{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3 id=\"d-add-annotations\">D. Add Annotations<\/h3>\n<p>Let&#39;s add some helpful fun to the JS candlestick chart by including <a href=\"https:\/\/docs.anychart.com\/Stock_Charts\/Drawing_Tools_and_Annotations\/Overview\" target=\"_blank\" rel=\"nofollow\">annotations<\/a>!<\/p>\n<p>First, we need to add the Annotations module by including the following script in the HTML code:<\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-annotations.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>To add an annotation to the candlestick chart, we&#39;ll use the <code>annotations()<\/code> object and one of its methods, such as <code>ellipse()<\/code>, <code>rectangle()<\/code>, or more. Let&#39;s start by creating the annotation object: <\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-attribute\">var annotation<\/span> = plot.annotations();\r\n<\/code><\/pre>\n<p>For this article, we&#39;ll use the <code>rectangle()<\/code> method, which requires four points to define its location. You&#39;ll need to insert these four points in YYYY-MM-DD format. Since the exact start and end dates of the &quot;work from home&quot; period may vary, we&#39;ll use the day the pandemic was declared as the first date, and the day the first vaccine was approved by the WHO as the second date. <\/p>\n<pre><code class=\"lang-javascript\">annotation.rectangle({\r\n  \/\/ X - part of the first <span class=\"hljs-built_in\">anchor<\/span>\r\n  xAnchor: '<span class=\"hljs-number\">2020<\/span>-<span class=\"hljs-number\">03<\/span>-<span class=\"hljs-number\">11<\/span>',\r\n  \/\/ Y - part of the first <span class=\"hljs-built_in\">anchor<\/span>\r\n  valueAnchor: <span class=\"hljs-number\">45<\/span>,\r\n  \/\/ X - part of the second <span class=\"hljs-built_in\">anchor<\/span>\r\n  secondXAnchor: '<span class=\"hljs-number\">2020<\/span>-<span class=\"hljs-number\">12<\/span>-<span class=\"hljs-number\">31<\/span>',\r\n  \/\/ Y - part of the second <span class=\"hljs-built_in\">anchor<\/span>\r\n  secondValueAnchor: <span class=\"hljs-number\">125<\/span>,\r\n  \/\/ <span class=\"hljs-built_in\">set<\/span> the stroke settings\r\n  stroke: '<span class=\"hljs-number\">3<\/span> <span class=\"hljs-comment\">#098209',<\/span>\r\n  \/\/ <span class=\"hljs-built_in\">set<\/span> the fill settings\r\n  fill: '<span class=\"hljs-comment\">#43FF43 0.25'<\/span>\r\n});\r\n<\/code><\/pre>\n<p>We can also add a text label annotation:<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-selector-tag\">annotation<\/span>\r\n  <span class=\"hljs-selector-class\">.label<\/span>()\r\n  <span class=\"hljs-selector-class\">.xAnchor<\/span>(<span class=\"hljs-string\">'2020-03-11'<\/span>)\r\n  <span class=\"hljs-selector-class\">.valueAnchor<\/span>(<span class=\"hljs-number\">75<\/span>)\r\n  <span class=\"hljs-selector-class\">.anchor<\/span>(<span class=\"hljs-string\">'left-top'<\/span>)\r\n  <span class=\"hljs-selector-class\">.offsetY<\/span>(<span class=\"hljs-number\">5<\/span>)\r\n  <span class=\"hljs-selector-class\">.padding<\/span>(<span class=\"hljs-number\">6<\/span>)\r\n  <span class=\"hljs-selector-class\">.text<\/span>(<span class=\"hljs-string\">'Global Lockdowns \u2014 Rise in demand for semiconductor chips'<\/span>)\r\n  <span class=\"hljs-selector-class\">.fontColor<\/span>(<span class=\"hljs-string\">'#fff'<\/span>)\r\n  <span class=\"hljs-selector-class\">.background<\/span>({\r\n    <span class=\"hljs-attribute\">fill<\/span>: <span class=\"hljs-string\">'#098209 0.75'<\/span>,\r\n    <span class=\"hljs-attribute\">stroke<\/span>: <span class=\"hljs-string\">'0.5 #098209'<\/span>,\r\n    <span class=\"hljs-attribute\">corners<\/span>: <span class=\"hljs-number\">2<\/span>\r\n  });\r\n<\/code><\/pre>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-s3jZnoYL\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/JyjXDTSl\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-s3jZnoYL{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<h3 id=\"e-mdd-the-macd-indicator-for-technical-analysis\">E. Add the MACD Indicator for Technical Analysis<\/h3>\n<p><a href=\"https:\/\/docs.anychart.com\/Stock_Charts\/Technical_Indicators\/Moving_Average_Convergence_Divergence_(MACD)\" target=\"_blank\" rel=\"nofollow\">MACD<\/a> (Moving Average Convergence \/ Divergence) is like a superhero of <a href=\"https:\/\/docs.anychart.com\/Stock_Charts\/Technical_Indicators\/Overview\" target=\"_blank\" rel=\"nofollow\">technical analysis indicators<\/a>! It shows the difference between a fast and slow exponential moving average (EMA) of closing prices. To add this indicator to the candlestick chart, we&#39;ll create a separate plot using the <code>macd()<\/code> method, which requires a mapping with the value field in it:<\/p>\n<pre><code class=\"lang-javascript\">\/\/ add a <span class=\"hljs-built_in\">second<\/span> plot to <span class=\"hljs-built_in\">show<\/span> macd <span class=\"hljs-built_in\">values<\/span>\r\n<span class=\"hljs-built_in\">var<\/span> indicatorPlot = chart.plot(<span class=\"hljs-number\">1<\/span>);\r\n\/\/ <span class=\"hljs-built_in\">map<\/span> the macd <span class=\"hljs-built_in\">values<\/span>\r\n<span class=\"hljs-built_in\">var<\/span> macdIndicator = indicatorPlot.macd(mapping);\r\n<\/code><\/pre>\n<p>Don&#39;t forget to set the series type for the histogram series and adjust the plot height if needed:<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-comment\">\/\/ set the histogram series type<\/span>\r\n<span class=\"hljs-selector-tag\">macdIndicator<\/span><span class=\"hljs-selector-class\">.histogramSeries<\/span>(<span class=\"hljs-string\">'area'<\/span>);\r\n<span class=\"hljs-selector-tag\">macdIndicator<\/span>\r\n  <span class=\"hljs-selector-class\">.histogramSeries<\/span>()<span class=\"hljs-selector-class\">.normal<\/span>()<span class=\"hljs-selector-class\">.fill<\/span>(<span class=\"hljs-string\">'green .3'<\/span>)<span class=\"hljs-selector-class\">.stroke<\/span>(<span class=\"hljs-string\">'green'<\/span>);\r\n<span class=\"hljs-selector-tag\">macdIndicator<\/span>\r\n  <span class=\"hljs-selector-class\">.histogramSeries<\/span>()<span class=\"hljs-selector-class\">.normal<\/span>()<span class=\"hljs-selector-class\">.negativeFill<\/span>(<span class=\"hljs-string\">'red .3'<\/span>)<span class=\"hljs-selector-class\">.negativeStroke<\/span>(<span class=\"hljs-string\">'red'<\/span>);\r\n<span class=\"hljs-comment\">\/\/ set the second plot's height<\/span>\r\n<span class=\"hljs-selector-tag\">indicatorPlot<\/span><span class=\"hljs-selector-class\">.height<\/span>(<span class=\"hljs-string\">'30%'<\/span>);\r\n<\/code><\/pre>\n<p>Note that the MACD indicator must be placed before the date range configuration:<\/p>\n<pre><code class=\"lang-javascript\"><span class=\"hljs-comment\">\/\/ add a second plot to show macd values<\/span>\r\nvar indicatorPlot = chart.plot(<span class=\"hljs-number\">1<\/span>);\r\n<span class=\"hljs-comment\">\/\/ map the macd values<\/span>\r\nvar macdIndicator = indicatorPlot.macd(mapping);\r\n<span class=\"hljs-comment\">\/\/ set the histogram series<\/span>\r\nmacdIndicator.histogramSeries(<span class=\"hljs-string\">'area'<\/span>);\r\nmacdIndicator\r\n  .histogramSeries().<span class=\"hljs-built_in\">normal<\/span>().<span class=\"hljs-built_in\">fill<\/span>(<span class=\"hljs-string\">'green .3'<\/span>).<span class=\"hljs-built_in\">stroke<\/span>(<span class=\"hljs-string\">'green'<\/span>);\r\nmacdIndicator\r\n  .histogramSeries().<span class=\"hljs-built_in\">normal<\/span>().negativeFill(<span class=\"hljs-string\">'red .3'<\/span>).negativeStroke(<span class=\"hljs-string\">'red'<\/span>);\r\n<span class=\"hljs-comment\">\/\/ set the second plot's height<\/span>\r\nindicatorPlot.<span class=\"hljs-built_in\">height<\/span>(<span class=\"hljs-string\">'30%'<\/span>);\r\n<span class=\"hljs-comment\">\/\/ set the pre-selected range<\/span>\r\nchart.selectRange(<span class=\"hljs-string\">'2020-01-01'<\/span>, <span class=\"hljs-string\">'2022-12-31'<\/span>);\r\n<\/code><\/pre>\n<p>Great job! You&#39;re now ready to view the final version of the JavaScript candlestick chart on the <a href=\"https:\/\/playground.anychart.com\/s3jZnoYL\" target=\"_blank\" rel=\"nofollow\">Playground<\/a> and make any additional customizations you desire. The entire code, with all the additions made, is listed below. So feel free to experiment and make any further changes you see fit!<\/p>\n<p><iframe sandbox=\"allow-scripts allow-pointer-lock allow-same-origin\n                 allow-popups allow-modals allow-forms\" frameBorder=\"0\" class=\"anychart-embed anychart-embed-s3jZnoYL\"\n        allowtransparency=\"true\" allowfullscreen=\"true\"\n        src=\"https:\/\/playground.anychart.com\/s3jZnoYL\/iframe\"><br \/>\n<\/iframe><br \/>\n<script type=\"text\/javascript\">(function(){\nfunction ac_add_to_head(el){\n\tvar head = document.getElementsByTagName('head')[0];\n\thead.insertBefore(el,head.firstChild);\n}\nfunction ac_add_style(css){\n\tvar ac_style = document.createElement('style');\n\tif (ac_style.styleSheet) ac_style.styleSheet.cssText = css;\n\telse ac_style.appendChild(document.createTextNode(css));\n\tac_add_to_head(ac_style);\n}\nac_add_style(\".anychart-embed-s3jZnoYL{width:100%;height:600px;}\");\n})();<\/script><\/p>\n<pre><code class=\"lang-html\"><span class=\"hljs-meta\">&lt;!DOCTYPE html&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"utf-8\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span>&gt;<\/span>JavaScript Candlestick Chart<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/css\/anychart-ui.min.css\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">link<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/fonts\/css\/anychart-font.min.css\"<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-core.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-stock.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/themes\/dark_glamour.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-ui.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-data-adapter.min.js\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"https:\/\/cdn.anychart.com\/releases\/8.11.0\/js\/anychart-annotations.min.js\r\n\"<\/span>&gt;<\/span><span class=\"undefined\"><\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/css\"<\/span>&gt;<\/span><span class=\"css\">\r\n      <span class=\"hljs-selector-tag\">html<\/span>,\r\n      <span class=\"hljs-selector-tag\">body<\/span>,\r\n      <span class=\"hljs-selector-id\">#container<\/span> {\r\n        <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\r\n        <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n        <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;\r\n      }\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span>&gt;<\/span><span class=\"actionscript\">\r\nanychart.onDocumentReady(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">()<\/span> <\/span>{\r\n  <span class=\"hljs-comment\">\/\/ load data<\/span>\r\n  anychart.data.loadCsvFile(\r\n<span class=\"hljs-string\">'https:\/\/gist.githubusercontent.com\/awanshrestha\/6481638c175e82dc6a91a499a8730057\/raw\/1791ef1f55f0be268479286284a0452129371003\/TSMC.csv'<\/span>,\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">(data)<\/span> <\/span>{\r\n      <span class=\"hljs-comment\">\/\/ create a data table on the loaded data<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> dataTable = anychart.data.table();\r\n      dataTable.addData(data);\r\n      <span class=\"hljs-comment\">\/\/ map the loaded data for the candlestick series<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> mapping = dataTable.mapAs({\r\n        open: <span class=\"hljs-number\">1<\/span>,\r\n        high: <span class=\"hljs-number\">2<\/span>,\r\n        low: <span class=\"hljs-number\">3<\/span>,\r\n        close: <span class=\"hljs-number\">4<\/span>\r\n      });\r\n      <span class=\"hljs-comment\">\/\/ create a stock chart<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> chart = anychart.stock();\r\n      <span class=\"hljs-comment\">\/\/ change the color theme<\/span>\r\n      anychart.theme(<span class=\"hljs-string\">'darkGlamour'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ create the chart plot<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> plot = chart.plot(<span class=\"hljs-number\">0<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the grid settings<\/span>\r\n      plot\r\n        .yGrid(<span class=\"hljs-literal\">true<\/span>)\r\n        .xGrid(<span class=\"hljs-literal\">true<\/span>)\r\n        .yMinorGrid(<span class=\"hljs-literal\">true<\/span>)\r\n        .xMinorGrid(<span class=\"hljs-literal\">true<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ create the candlestick series<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> series = plot.candlestick(mapping);\r\n      series.name(<span class=\"hljs-string\">'TSMC'<\/span>);\r\n      series.legendItem().iconType(<span class=\"hljs-string\">'rising-falling'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ create a range picker<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> rangePicker = anychart.ui.rangePicker();\r\n      rangePicker.render(chart);\r\n      <span class=\"hljs-comment\">\/\/ create a range selector<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> rangeSelector = anychart.ui.rangeSelector();\r\n      rangeSelector.render(chart);\r\n      <span class=\"hljs-comment\">\/\/ modify the color of the candlesticks <\/span>\r\n      series.fallingFill(<span class=\"hljs-string\">\"#FF0D0D\"<\/span>);\r\n      series.fallingStroke(<span class=\"hljs-string\">\"#FF0D0D\"<\/span>);\r\n      series.risingFill(<span class=\"hljs-string\">\"#43FF43\"<\/span>);\r\n      series.risingStroke(<span class=\"hljs-string\">\"#43FF43\"<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the event markers<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> eventMarkers = plot.eventMarkers();\r\n      <span class=\"hljs-comment\">\/\/ set the symbol of the event markers<\/span>\r\n      plot.eventMarkers().format(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">this<\/span>.getData(<span class=\"hljs-string\">\"symbol\"<\/span>);\r\n      });\r\n      <span class=\"hljs-comment\">\/\/ set the event markers data<\/span>\r\n      eventMarkers.data([\r\n           { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">1<\/span>, date: <span class=\"hljs-string\">'2020-03-11'<\/span>, description: <span class=\"hljs-string\">'WHO declares COVID-19 a global pandemic'<\/span> },\r\n        { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">2<\/span>, date: <span class=\"hljs-string\">'2020-11-20'<\/span>, description: <span class=\"hljs-string\">'TSMC wins approval from Arizona for $12 billion chip plant'<\/span> },\r\n        { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">3<\/span>, date: <span class=\"hljs-string\">'2021-07-12'<\/span>, description: <span class=\"hljs-string\">'TSMC and Foxconn reach deals to buy COVID-19 vaccines for Taiwan'<\/span> },\r\n        { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">4<\/span>, date: <span class=\"hljs-string\">'2021-11-09'<\/span>, description: <span class=\"hljs-string\">'TSMC announces to build a specialty technology fab in Japan with Sony'<\/span> },\r\n        { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">5<\/span>, date: <span class=\"hljs-string\">'2022-02-24'<\/span>, description: <span class=\"hljs-string\">'Russia-Ukraine war begins'<\/span> },\r\n        { <span class=\"hljs-string\">\"symbol\"<\/span>: <span class=\"hljs-number\">6<\/span>, date: <span class=\"hljs-string\">'2022-11-15'<\/span>, description: <span class=\"hljs-string\">'Berkshire Hathaway discloses a $4.1 billion stake in TSMC'<\/span> },\r\n      ]);\r\n      <span class=\"hljs-comment\">\/\/ create an annotation<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> annotation = plot.annotations();\r\n      <span class=\"hljs-comment\">\/\/ create a rectangle<\/span>\r\n      annotation.rectangle({\r\n        <span class=\"hljs-comment\">\/\/ X - part of the first anchor<\/span>\r\n        xAnchor: <span class=\"hljs-string\">'2020-03-11'<\/span>,\r\n        <span class=\"hljs-comment\">\/\/ Y - part of the first anchor<\/span>\r\n        valueAnchor: <span class=\"hljs-number\">45<\/span>,\r\n        <span class=\"hljs-comment\">\/\/ X - part of the second anchor<\/span>\r\n        secondXAnchor: <span class=\"hljs-string\">'2020-12-31'<\/span>,\r\n        <span class=\"hljs-comment\">\/\/ Y - part of the second anchor<\/span>\r\n        secondValueAnchor: <span class=\"hljs-number\">125<\/span>,\r\n        <span class=\"hljs-comment\">\/\/ set the stroke settings<\/span>\r\n        stroke: <span class=\"hljs-string\">'3 #098209'<\/span>,\r\n        <span class=\"hljs-comment\">\/\/ set the fill settings<\/span>\r\n        fill: <span class=\"hljs-string\">'#43FF43 0.25'<\/span>\r\n      });\r\n      <span class=\"hljs-comment\">\/\/ create a text label<\/span>\r\n      annotation\r\n        .label()\r\n        .xAnchor(<span class=\"hljs-string\">'2020-03-11'<\/span>)\r\n        .valueAnchor(<span class=\"hljs-number\">75<\/span>)\r\n        .anchor(<span class=\"hljs-string\">'left-top'<\/span>)\r\n        .offsetY(<span class=\"hljs-number\">5<\/span>)\r\n        .padding(<span class=\"hljs-number\">6<\/span>)\r\n        .text(<span class=\"hljs-string\">'Global Lockdowns \u2014 Rise in demand for semiconductor chips'<\/span>)\r\n        .fontColor(<span class=\"hljs-string\">'#fff'<\/span>)\r\n        .background({\r\n          fill: <span class=\"hljs-string\">'#098209 0.75'<\/span>,\r\n          stroke: <span class=\"hljs-string\">'0.5 #098209'<\/span>,\r\n          corners: <span class=\"hljs-number\">2<\/span>\r\n        });\r\n      <span class=\"hljs-comment\">\/\/ add a second plot to show macd values<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> indicatorPlot = chart.plot(<span class=\"hljs-number\">1<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ map the macd values<\/span>\r\n      <span class=\"hljs-keyword\">var<\/span> macdIndicator = indicatorPlot.macd(mapping);\r\n      <span class=\"hljs-comment\">\/\/ set the histogram series<\/span>\r\n      macdIndicator.histogramSeries(<span class=\"hljs-string\">'area'<\/span>);\r\n      macdIndicator\r\n        .histogramSeries().normal().fill(<span class=\"hljs-string\">'green .3'<\/span>).stroke(<span class=\"hljs-string\">'green'<\/span>);\r\n      macdIndicator\r\n        .histogramSeries().normal().negativeFill(<span class=\"hljs-string\">'red .3'<\/span>).negativeStroke(<span class=\"hljs-string\">'red'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the second plot's height<\/span>\r\n      indicatorPlot.height(<span class=\"hljs-string\">'30%'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the chart display for the selected date\/time range<\/span>\r\n      chart.selectRange(<span class=\"hljs-string\">'2020-01-01'<\/span>, <span class=\"hljs-string\">'2022-12-31'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the title of the chart<\/span>\r\n      chart.title(<span class=\"hljs-string\">'TSMC Stock Chart'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ set the container id for the chart<\/span>\r\n      chart.container(<span class=\"hljs-string\">'container'<\/span>);\r\n      <span class=\"hljs-comment\">\/\/ initiate the chart drawing<\/span>\r\n      chart.draw();\r\n    }\r\n  );\r\n});\r\n    <\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\r\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Congratulations! You&#39;ve made it to the end of our candlestick charting journey with JavaScript! It might have been a bit overwhelming at first, but now you&#39;re a pro at visualizing stock data like a boss. If you have any questions or need any help, don&#39;t hesitate to reach out. Happy charting!<\/p>\n<hr \/>\n<p><strong><em>Published with the permission of Awan Shrestha. Originally appeared on <a href=\"https:\/\/dev.to\/awanshrestha\/creating-a-candlestick-chart-with-js-to-analyze-stocks-3b0g\" target=\"_blank\" rel=\"nofollow\">DEV Community<\/a> with the title &#8220;Creating a Candlestick Chart with JS to Analyze Stocks&#8221; on May 1, 2023.<\/p>\n<p>You may also like to check out the <a href=\"https:\/\/www.anychart.com\/blog\/2020\/03\/25\/js-candlestick-chart-steps\/\">JavaScript Candlestick Chart Tutorial<\/a> produced by Anastasia Zoumpliou especially for our blog and originally published here earlier.<\/p>\n<p>See more <a href=\"https:\/\/www.anychart.com\/blog\/category\/javascript-chart-tutorials\/\">JavaScript charting tutorials<\/a> on our blog.<\/p>\n<p>Have you got a cool guest post idea? <a href=\"https:\/\/www.anychart.com\/support\/\">Let us know<\/a>!<\/em><\/strong><\/p>\n<hr \/>\n<p><!-- SyntaxHighlighter --><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/default.min.css\"><link rel=\"stylesheet\" href=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/styles\/atom-one-light.min.css\"><script src=\"\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/9.12.0\/highlight.min.js\"><\/script><script>hljs.initHighlightingOnLoad();<\/script><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Candlestick charts are a fantastic data visualization tool for tracking the price movements of stocks over a period of time. In this tutorial, I&#39;ll show you how to create your own candlestick chart using JavaScript. We\u2019ll be using TSMC (Taiwan Semiconductor Manufacturing Company) as an example, as they&#39;re the largest contract chip maker in the [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":22,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18,263,19,23,13,279,4],"tags":[],"class_list":["post-16510","post","type-post","status-publish","format-standard","hentry","category-anychart-charting-component","category-anystock","category-big-data","category-financial-charts","category-html5","category-javascript","category-javascript-chart-tutorials","category-tips-and-tricks","wpautop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create Candlestick Chart with JavaScript for Stock Analysis<\/title>\n<meta name=\"description\" content=\"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example &amp; analyze market trends like a pro.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Candlestick Chart with JS for Stock Analysis\" \/>\n<meta property=\"og:description\" content=\"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example &amp; analyze market trends like a pro.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\" \/>\n<meta property=\"og:site_name\" content=\"AnyChart News\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/AnyCharts\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-02T12:49:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-18T19:41:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\" \/>\n<meta name=\"author\" content=\"Awan Shrestha\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How to Create Candlestick Chart with JS for Stock Analysis\" \/>\n<meta name=\"twitter:description\" content=\"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example &amp; analyze market trends like a pro.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\" \/>\n<meta name=\"twitter:creator\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:site\" content=\"@AnyChart\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Awan Shrestha\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\"},\"author\":{\"name\":\"Awan Shrestha\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\"},\"headline\":\"How to Create Candlestick Chart with JavaScript for Stock Analysis\",\"datePublished\":\"2023-05-02T12:49:56+00:00\",\"dateModified\":\"2023-05-18T19:41:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\"},\"wordCount\":1967,\"commentCount\":2,\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\",\"articleSection\":[\"AnyChart Charting Component\",\"AnyStock\",\"Big Data\",\"Financial Charts\",\"HTML5\",\"JavaScript\",\"JavaScript Chart Tutorials\",\"Tips and Tricks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\",\"url\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\",\"name\":\"How to Create Candlestick Chart with JavaScript for Stock Analysis\",\"isPartOf\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\",\"datePublished\":\"2023-05-02T12:49:56+00:00\",\"dateModified\":\"2023-05-18T19:41:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\"},\"description\":\"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example & analyze market trends like a pro.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage\",\"url\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\",\"contentUrl\":\"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png\",\"width\":1366,\"height\":768},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.anychart.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Candlestick Chart with JavaScript for Stock Analysis\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#website\",\"url\":\"https:\/\/www.anychart.com\/blog\/\",\"name\":\"AnyChart News\",\"description\":\"AnyChart JS Charts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.anychart.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1\",\"name\":\"Awan Shrestha\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g\",\"caption\":\"Awan Shrestha\"},\"url\":\"https:\/\/www.anychart.com\/blog\/author\/awan-shrestha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Candlestick Chart with JavaScript for Stock Analysis","description":"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example & analyze market trends like a pro.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Candlestick Chart with JS for Stock Analysis","og_description":"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example & analyze market trends like a pro.","og_url":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/","og_site_name":"AnyChart News","article_publisher":"https:\/\/www.facebook.com\/AnyCharts","article_published_time":"2023-05-02T12:49:56+00:00","article_modified_time":"2023-05-18T19:41:17+00:00","og_image":[{"url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png","type":"","width":"","height":""}],"author":"Awan Shrestha","twitter_card":"summary_large_image","twitter_title":"How to Create Candlestick Chart with JS for Stock Analysis","twitter_description":"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example & analyze market trends like a pro.","twitter_image":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png","twitter_creator":"@AnyChart","twitter_site":"@AnyChart","twitter_misc":{"Written by":"Awan Shrestha","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#article","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/"},"author":{"name":"Awan Shrestha","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1"},"headline":"How to Create Candlestick Chart with JavaScript for Stock Analysis","datePublished":"2023-05-02T12:49:56+00:00","dateModified":"2023-05-18T19:41:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/"},"wordCount":1967,"commentCount":2,"image":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png","articleSection":["AnyChart Charting Component","AnyStock","Big Data","Financial Charts","HTML5","JavaScript","JavaScript Chart Tutorials","Tips and Tricks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/","url":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/","name":"How to Create Candlestick Chart with JavaScript for Stock Analysis","isPartOf":{"@id":"https:\/\/www.anychart.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage"},"image":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage"},"thumbnailUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png","datePublished":"2023-05-02T12:49:56+00:00","dateModified":"2023-05-18T19:41:17+00:00","author":{"@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1"},"description":"Learn to create a candlestick chart with JavaScript to visualize stock price movements. Learn with our TSMC example & analyze market trends like a pro.","breadcrumb":{"@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#primaryimage","url":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png","contentUrl":"https:\/\/www.anychart.com\/blog\/wp-content\/uploads\/2023\/04\/candlestick-chart-guide.png","width":1366,"height":768},{"@type":"BreadcrumbList","@id":"https:\/\/www.anychart.com\/blog\/2023\/05\/02\/candlestick-chart-stock-analysis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.anychart.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Create Candlestick Chart with JavaScript for Stock Analysis"}]},{"@type":"WebSite","@id":"https:\/\/www.anychart.com\/blog\/#website","url":"https:\/\/www.anychart.com\/blog\/","name":"AnyChart News","description":"AnyChart JS Charts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.anychart.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/3f107fdcb3fe326a63cd52812f5287c1","name":"Awan Shrestha","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.anychart.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/02ebd18c927547ca82372246dddcc3aebfa98928cb91405eef1a63ece7f17518?s=96&r=g","caption":"Awan Shrestha"},"url":"https:\/\/www.anychart.com\/blog\/author\/awan-shrestha\/"}]}},"_links":{"self":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/comments?post=16510"}],"version-history":[{"count":11,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16510\/revisions"}],"predecessor-version":[{"id":16609,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/posts\/16510\/revisions\/16609"}],"wp:attachment":[{"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/media?parent=16510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/categories?post=16510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.anychart.com\/blog\/wp-json\/wp\/v2\/tags?post=16510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}