{"id":23275,"date":"2024-05-10T13:00:00","date_gmt":"2024-05-10T05:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=23275"},"modified":"2024-12-23T16:03:42","modified_gmt":"2024-12-23T08:03:42","slug":"williams-r-2","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/","title":{"rendered":"TQuant Lab Williams %R, \u00a0looking for stock price turning points"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1470\" height=\"980\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/image-425.png\" alt=\"Williams %R\" class=\"wp-image-23217\" style=\"aspect-ratio:1.5;object-fit:cover\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/image-425.png 1470w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-425-300x200.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-425-1024x683.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-425-150x100.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-425-768x512.png 768w\" sizes=\"(max-width: 1470px) 100vw, 1470px\" \/><figcaption class=\"wp-element-caption\">Photo by <a href=\"https:\/\/unsplash.com\/@m_____me?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">m.<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/selective-focus-photography-of-graph-ZzOa5G8hSPI?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Unsplash<\/a><\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f10af2d18de\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f10af2d18de\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Highlight\" >Highlight<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Introduction\" >Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Williams_R_strategy\" >Williams %R strategy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Editing_Environment_and_Module_Requirements\" >Editing Environment and Module Requirements&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Data_Import\" >Data Import<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Creating_Pipeline_Function\" >Creating Pipeline Function<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Creating_Custom_Factor\" >Creating Custom Factor<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Creating_Initialize_Function\" >Creating Initialize Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Creating_Handle_data_Function\" >Creating Handle_data Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Executing_Trading_Strategy\" >Executing Trading Strategy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Evaluating_performance_using_Pyfolio\" >Evaluating performance using&nbsp;Pyfolio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Source_Code\" >Source Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#Extended_Reading\" >Extended Reading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.tejwin.com\/en\/insight\/williams-r-2\/#About\" >About<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Highlight\"><\/span><strong>Highlight<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2606\u2606\u2606<\/li>\n\n\n\n<li>Introduce Williams %R and indicator entry timing.<\/li>\n\n\n\n<li>Use the TQuant Lab backtesting platform to write Williams %R strategies and backtest risk and performance.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span><strong>Introduction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Williams %R is also called the Williams index, wmsr, or W%R. It was created by the famous American trader Larry Williams in 1973. It is a standard indicator in technical analysis.<\/p>\n\n\n\n<p>The William indicator is the earliest indicator to judge whether the market price is overbought or oversold. The KD line and other indicators commonly used by investors to judge overbought or oversold are developed based on the William indicator.<\/p>\n\n\n\n<p>The Williams %R is calculated as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>(Highest price on N days &#8211; closing price) \u00f7 (Highest price on N days &#8211; lowest price on N days) \u00d7 100% \u00d7 (-1)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>According to the above formula, it can be known that the value of the Williams %R ranges from 0 to -100. When the indicator&#8217;s value is between 0 and -50, the stock price is in a strong stage; when the indicator&#8217;s value falls between -51 and -100, the stock price is in a weak stage. In addition, when the indicator value is less than -80, it means that the stock price may be in an oversold state, which is a good time to buy; and when the indicator value is greater than -20, it means that the stock price may be in an overbought state, and it is a good time to sell.<\/p>\n\n\n\n<p>In addition, the time parameters of the Williams %R have a considerable impact on it, so investors need to set parameters that suit them according to their habits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Williams_R_strategy\"><\/span>Williams %R strategy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This article uses the Williams %R to determine the timing of overbought and oversold stocks. It uses the following entry and exit rules to establish a trading strategy and perform backtesting:<\/p>\n\n\n\n<p>Buy the stock when the Williams %R \u2264 -80 because the market is too cold and the stock is in the oversold stage.<\/p>\n\n\n\n<p>When the Williams %R \u2265 -20, sell the stock because the market is overheated and the stock is in the overbought stage.<\/p>\n\n\n\n<p>In addition, this article sets the N value to 100 days instead of the more common pane used in calculating the Williams %R. Finally, the 14 days often used in the industry will be used for backtest comparison.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Editing_Environment_and_Module_Requirements\"><\/span><strong>Editing Environment and Module Requirements<\/strong>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This article uses Windows 11 and Jupyter Notebook as the editor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Data_Import\"><\/span>Data Import<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This strategy lasts for three years, from 2021-01-01 to 2023-12-31, focusing on electronics stocks: Hon Hai Precision (2317), Yageo (2327), TSMC (2330), Acer (2353), Asus (2357), and Quanta (2382) are used as examples, and the return index (IR0001) is used as the broader market for performance comparison.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nos.environ&#91;'TEJAPI_KEY'] = \"your key\" \nos.environ&#91;'TEJAPI_BASE'] = \"https:\/\/api.tej.com.tw\"\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom zipline.data import bundles\nfrom zipline.utils.calendar_utils import get_calendar\nfrom zipline.sources.TEJ_Api_Data import get_Benchmark_Return\n\nstart = '2021-01-01'\nend = '2023-12-31'\n\nos.environ&#91;'ticker'] = 'IR0001 2317 2327 2330 2353 2357 2382'\nos.environ&#91;'mdate'] = start+' '+end\n!zipline ingest -b tquant<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Pipeline_Function\"><\/span><strong>Creating Pipeline Function<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Custom_Factor\"><\/span><strong>Creating Custom Factor<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>CustomFactor allows users to design the required customization factors. In this case, we use it to handle:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Williams %R (Wmsr)<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from zipline.data import bundles\nfrom zipline.sources.TEJ_Api_Data import get_Benchmark_Return\nfrom zipline.pipeline.mixins import SingleInputMixin\nfrom zipline.pipeline.data import TWEquityPricing\nfrom zipline.pipeline.factors import CustomFactor\nfrom zipline.pipeline import Pipeline\nfrom zipline.TQresearch.tej_pipeline import run_pipeline\nfrom zipline.pipeline.filters import StaticAssets\nfrom zipline.utils.math_utils import nanmax, nanmin\n\nclass Wmsr(CustomFactor):\n    inputs = &#91;TWEquityPricing.close, TWEquityPricing.high, TWEquityPricing.low]\n    window_length = 100\n    \n    def compute(self, today, assets, out, close, high, low):\n        highest_highs = nanmax(high, axis=0)\n        lowest_lows = nanmin(low, axis=0)\n        williams_index = ((highest_highs - close&#91;-1]) \/ (highest_highs - lowest_lows)) * 100 * -1\n        \n        out&#91;:] = williams_index<\/code><\/pre>\n\n\n\n<p>Pipeline() provides users with the function of quickly processing multiple targets&#8217; quantitative indicators and price and volume data. In this case, we use it to process:<\/p>\n\n\n\n<p>The current stock price of the stock (curr_price)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>start_dt, end_dt = pd.Timestamp(start, tz='utc'), pd.Timestamp(end, tz='utc')\nbundle = bundles.load('tquant')\nbenchmark_asset = bundle.asset_finder.lookup_symbol('IR0001',as_of_date = None)\n\ndef make_pipeline():\n    wmsr = Wmsr()\n    curr_price = TWEquityPricing.close.latest\n    \n    return Pipeline(\n        columns = {\n            'curr_price':curr_price,\n            'Williams_index' : wmsr,\n        },\n        screen = ~StaticAssets(&#91;benchmark_asset])\n    )\nmy_pipeline = run_pipeline(make_pipeline(),start_dt, end_dt)\nmy_pipeline.tail(20)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58311.jpg\" alt=\"Williams %R\" class=\"wp-image-23200\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58311.jpg 1280w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58311-300x169.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58311-1024x576.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58311-150x84.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58311-768x432.jpg 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><figcaption class=\"wp-element-caption\">Pipeline partial data<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Initialize_Function\"><\/span><strong>Creating Initialize Function<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The initialize() function is used to define the daily trading environment before the start of trading. In this example, we set:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Slippage costs<\/li>\n\n\n\n<li>Transaction costs model for the Taiwan stock market<\/li>\n\n\n\n<li>Weighted return index (<em>IR0001<\/em>) as the benchmark<\/li>\n\n\n\n<li>Import the Williams %R designed by Pipeline into the trading process<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from zipline.finance import slippage, commission\nfrom zipline.api import *\nfrom zipline.api import set_slippage, set_commission, set_benchmark, attach_pipeline, order, order_target, symbol, pipeline_output, record\n\ndef initialize(context):\n    set_slippage(slippage.VolumeShareSlippage(volume_limit = 0.025, price_impact = 0.1))\n    set_commission(commission.Custom_TW_Commission(min_trade_cost = 20, discount = 1.0, tax = 0.003))\n    attach_pipeline(make_pipeline(), 'mystrats')\n    set_benchmark(symbol('IR0001'))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Handle_data_Function\"><\/span><strong>Creating Handle_data Function<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <strong><code>handle_data()<\/code><\/strong> function is important for constructing the Williams %R strategy. It is called every day after the start of backtesting, and its main tasks include setting trading strategies, placing orders, and recording trading information.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span data-preserver-spaces=\"true\" style=\"font-size: revert; color: initial;\">When Williams %R \u2264 -80 and current funds \u2265 current&nbsp;price of the stock&nbsp;* 1000, buy the stock.<\/span><\/li>\n\n\n\n<li><span data-preserver-spaces=\"true\" style=\"font-size: revert; color: initial;\">When Williams %R \u2265 -20, sell holdings.<\/span><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>def handle_data(context, data):\n    out_dir = pipeline_output('mystrats')\n    \n    for i in out_dir.index:\n        curr_price = out_dir.loc&#91;i, 'curr_price']\n        w_value = out_dir.loc&#91;i, 'Williams_index']\n        stock_position = context.portfolio.positions&#91;i].amount\n        cash_position = context.portfolio.cash\n\n        if stock_position == 0 and cash_position &gt;= curr_price * 1000:\n            if w_value &lt;= -80:\n                order(i, 1000)\n        elif stock_position &gt; 0:\n            if w_value &lt;= -80 and cash_position &gt;= curr_price * 1000:\n                order(i, 1000)\n                \n            elif w_value &gt;= -20:\n                order_target(i, 0)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Executing_Trading_Strategy\"><\/span><strong>Executing Trading Strategy<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Use the <code><strong>run_algorithm()<\/strong><\/code> function to execute the Williams %R strategy as defined above. Set the trading period from start_dt (2021-01-01) to end_dt (2023-12-31), using the dataset tquant, with an initial capital of 20 million dollars. The output results represent the daily performance and trade details.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from zipline import run_algorithm\n\nstart_date = pd.Timestamp('2021-01-01',tz='utc')\nend_date = pd.Timestamp('2023-12-31',tz='utc')\n\nresults = run_algorithm(start = start_date,  \n                        end = end_date,\n                        initialize = initialize,                      \n                        capital_base = 1e7,                      \n                        handle_data = handle_data,\n                        data_frequency = 'daily',\n                        bundle = 'tquant'\n                        )\nresults<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58312.jpg\" alt=\"Williams %R\" class=\"wp-image-23202\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58312.jpg 1280w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58312-300x169.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58312-1024x576.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58312-150x84.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58312-768x432.jpg 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><figcaption class=\"wp-element-caption\">Backtest Details<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Evaluating_performance_using_Pyfolio\"><\/span>Evaluating performance using&nbsp;Pyfolio<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import pyfolio as pf\nfrom pyfolio.utils import extract_rets_pos_txn_from_zipline\n\nreturns, positions, transactions = pf.utils.extract_rets_pos_txn_from_zipline(results)\nbenchmark_rets = results.benchmark_return\n\n# Creating a Full Tear Sheet\npf.create_full_tear_sheet(returns, positions = positions, transactions = transactions,\n                          benchmark_rets = benchmark_rets,\n                          round_trips=False)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58313.jpg\" alt=\"Williams %R\" class=\"wp-image-23204\" style=\"object-fit:cover\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58313.jpg 1280w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58313-300x169.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58313-1024x576.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58313-150x84.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58313-768x432.jpg 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><figcaption class=\"wp-element-caption\">Backtest Performance &amp; Benchmark Comparison Chart \uff08100 days\uff09<\/figcaption><\/figure>\n\n\n\n<p>In these 34 months, the Williams %R strategy obtained an annualized return of 11.696%, with a cumulative return of 37.708%. The profit performance was generally slightly better than the market; however, the annualized volatility and maximum drawdown were 18.221% and -34.638% respectively, showing that the risk of market fluctuations using the Williams %R strategy is more significant. When used, because the fluctuations are too large, it is impossible to rely solely on the Williams %R for quantitative trading. It can be combined with the <a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-ichimoku-kinko-hyo\/\" class=\"ek-link\">TQuant Lab Ichimoku Kinko Hyo Strategy, A Self-contained Technical Analysis Indicator<\/a> in the previous article, a set of self-contained technical analysis indicators such as the moving index loss or the moving average. The method matches the Williams %R to increase the stability of investment.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"720\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58314-2.jpg\" alt=\"Williams %R\" class=\"wp-image-23214\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58314-2.jpg 1280w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58314-2-300x169.jpg 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58314-2-1024x576.jpg 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58314-2-150x84.jpg 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/\u7c21\u58314-2-768x432.jpg 768w\" sizes=\"(max-width: 1280px) 100vw, 1280px\" \/><figcaption class=\"wp-element-caption\">Backtest Performance &amp; Benchmark Comparison Chart\uff0814 days\uff09<\/figcaption><\/figure>\n\n\n\n<p>This chart only changes the time parameter and shows that the Williams %R time parameter has a huge impact. Since the Williams %R is more sensitive to price than other technical indicators, if you only follow the fast-line (short-day) Williams %R, buying and selling may be performed too frequently, leading to misjudgments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This strategy uses the Williams %R to conduct backtest simulations, using the indicator to enter the market when it is oversold and exit when it is overbought. The backtest results show that investors sometimes get more rewards compared to the broader market, but investors will bear the excessive market pressure. Fluctuation risk and time parameters have a huge impact on indicators. Therefore, investors must pay attention to their habits and adjust them to suitable parameters when investing. Also, note that the Williams %R is only a technical indicator and should be combined with other auxiliary judgments when investing to increase the stability and reliability of the investment.<\/p>\n\n\n\n<p>In the future, I will continue to introduce various indicators constructed using the TEJ database and backtest their performance. Therefore, readers interested in trading backtests are encouraged to explore <a href=\"https:\/\/tquant.tejwin.com\/\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\"><strong>TQuant Lab<\/strong><\/a>&#8216;s related solutions, utilizing high-quality databases to build their trading strategies.<\/p>\n\n\n\n<p><strong>A friendly reminder:<\/strong>&nbsp;This strategy is for reference only and does not constitute any recommendation for commodities or investments.<\/p>\n\n\n\n<div style=\"border: 1px black; border-style: solid none; text-align: center; border-color: #296580; padding: 24px; margin-top: 24px; margin-bottom: 24px;\">\n<p style=\"margin: 0px; font-size: 24px; font-weight: bold; line-height: 1.5;\">[TQuant Lab] Solving Your Quantitative Finance Pain Points<\/p>\n<p style=\"margin: 0px; font-size: 24px; font-weight: bold; line-height: 1.5;\">Comprehensively providing all the tools needed for trading backtesting.<\/p>\n<div style=\"margin-top: 32px;\"><a style=\"border: none; border-radius: 4px; background-color: #296580; color: white; font-size: 20px; width: fit-content; text-decoration: none; padding: 12px 30px 12px 30px;\" href=\"https:\/\/tquant.tejwin.com\/\" target=\"_blank\" rel=\"noopener\">Register and Start Your Trial<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Source_Code\"><\/span>Source Code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/blob\/1738f739afc13449042dfa55ee58587d159f9fd4\/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A8%99.ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\"><\/a><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Application\/blob\/1738f739afc13449042dfa55ee58587d159f9fd4\/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A8%99.ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click here to Github<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Extended_Reading\"><\/span>Extended Reading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tejwin.com\/insight\/esg-etf\/\" class=\"ek-link\"><\/a><a href=\"https:\/\/www.tejwin.com\/wp-admin\/post.php?post=22962&amp;action=edit&amp;lang=en&amp;update_needed=1&amp;trid=328125&amp;language_code=en\" class=\"ek-link\"><\/a><a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-ichimoku-kinko-hyo\/\" class=\"ek-link\">TQuant Lab Ichimoku Kinko Hyo Strategy, A Self-contained Technical Analysis Indicator<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/insight\/tquant-lab-kd-%e6%8c%87%e6%a8%99%e7%ad%96%e7%95%a5\/\" class=\"ek-link\"><\/a><a href=\"https:\/\/www.tejwin.com\/wp-admin\/post.php?post=22519&amp;action=edit&amp;lang=en&amp;update_needed=1&amp;trid=328047&amp;language_code=en\" class=\"ek-link\">TQuant Lab KD Indicator Strategy: Exploring Stock Price Reversal Timing?<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"About\"><\/span>About<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"ek-link\">TQuant Lab Github<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/tquant.tejwin.com\/\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">TQuant Lab Home Page<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This time, the Williams %R  strategy was used for backtesting. The Williams indicator is also called the Williams index, wmsr, or W%R. Its English name is The Williams Percent Range. It was created by the famous American trader Larry Williams in 1973. It is a standard indicator in technical analysis. One. The KD line and other indicators commonly used by investors to judge overbought or oversold are developed based on the William indicator.<\/p>\n","protected":false},"featured_media":23218,"template":"","tags":[3063,3176,2990,3166,2537],"insight-category":[690,50,1356],"class_list":["post-23275","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-backtesting-2","tag-python-2","tag-quantitive-trading","tag-tquant-lab-2","tag-2537","insight-category-data-analysis","insight-category-fintech","insight-category-tquant-lab-en"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/23275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":6,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/23275\/revisions"}],"predecessor-version":[{"id":23284,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/23275\/revisions\/23284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/23218"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=23275"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=23275"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=23275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}