{"id":17951,"date":"2023-09-26T14:00:00","date_gmt":"2023-09-26T06:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=17951"},"modified":"2026-02-25T17:24:02","modified_gmt":"2026-02-25T09:24:02","slug":"tquant-lab-momentum-trade","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/","title":{"rendered":"TQuant Lab Momentum Trade"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-large caption-align-center\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\/image-275-1024x683.png\" alt=\"Momentum Trade\" class=\"wp-image-17934\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/image-275-1024x683.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-275-300x200.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-275-150x100.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-275-768x512.png 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-275.png 1470w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Photo by <a href=\"https:\/\/unsplash.com\/@eiskonen?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener\">Hans Eiskonen<\/a> on <a href=\"https:\/\/unsplash.com\/photos\/wn57cSQ7VzI?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" 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-69f10f9d57bd8\" 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-69f10f9d57bd8\"  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\/tquant-lab-momentum-trade\/#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\/tquant-lab-momentum-trade\/#Preface\" >Preface<\/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\/tquant-lab-momentum-trade\/#Editing_Environment_and_Module_Requirements\" >Editing Environment and Module Requirements<\/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\/tquant-lab-momentum-trade\/#Data_Ingest\" >Data Ingest<\/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\/tquant-lab-momentum-trade\/#Editing_the_Momentum_Trade_Strategy\" >Editing the Momentum Trade Strategy<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Creating_the_Pipeline_Function\" >Creating the Pipeline Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Creating_the_initialize_Function\" >Creating the initialize Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Creating_the_handle_data_Function\" >Creating the handle_data Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Creating_the_analyze_Function\" >Creating the analyze Function<\/a><\/li><\/ul><\/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\/tquant-lab-momentum-trade\/#Executing_the_Trading_Strategy\" >Executing the 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\/tquant-lab-momentum-trade\/#Visualization_and_Performance_Analysis\" >Visualization and Performance Analysis<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Generating_Dataframe_needed_for_pyfolio\" >Generating Dataframe needed for pyfolio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Generating_Portfolio_Performance_Table\" >Generating Portfolio Performance Table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Generating_Portfolio_Component_table\" >Generating Portfolio Component table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Visualize_Cumulative_Returns\" >Visualize Cumulative Returns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Visualize_the_six-month_Rolling_Volatility\" >Visualize the six-month Rolling Volatility<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Visualize_six-month_Rolling_Sharpe_Ratio\" >Visualize six-month Rolling Sharpe Ratio<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#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-19\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Further_Reading\" >Further Reading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-momentum-trade\/#Related_Links\" >Related Links<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Highlight\"><\/span>Highlight<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Article Difficulty: \u2605\u2605\u2606\u2606\u2606 <\/li>\n\n\n\n<li>Using Volume Growth as a Screening Indicator <\/li>\n\n\n\n<li>Reading Recommendation: This article utilizes volume growth as a trading indicator, visually observing trading signals and entry\/exit points. It then calculates returns using functions, with all necessary code for these strategies tested using TQuant Lab. <\/li>\n\n\n\n<li>This article is adapted from <a href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-15-momentum-trade-53e538e272dc\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Momentum Trade<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span>Preface<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In recent years, momentum trading has become a frequent topic of discussion in stock market strategies. In the stock market, we often hear discussions about the price-volume relationship, where price is considered a leading indicator of volume, among other concepts. This article aims to explore the backtesting effects of increasing trading volume as an entry strategy. Unlike some additional technical indicators that have predefined criteria, this method is designed to be more flexible, allowing readers to make their own adjustments in programming. Detailed parameter settings for customization will be mentioned in the later part of the code. This article employs the following strategy for backtesting:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Buy signal: volume is 2.5 times the average of the previous &#8220;4&#8221; days.<\/li>\n\n\n\n<li>Sell signal: Exit is triggered when the volume falls below 0.75 times the average of the previous &#8220;5&#8221; days.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Editing_Environment_and_Module_Requirements\"><\/span>Editing Environment and Module Requirements <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This article is written using Windows 11 and Jupyter Notebook as the editor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd \nimport numpy as np \nimport tejapi\nimport os \nimport pyfolio as pf\nfrom zipline.api import set_slippage, set_commission, set_benchmark, attach_pipeline, order, order_target, symbol, pipeline_output\nfrom zipline.finance import commission, slippage\nfrom zipline.data import bundles\nfrom zipline import run_algorithm\nfrom zipline.pipeline import Pipeline\nfrom zipline.pipeline.filters import StaticAssets\nfrom zipline.pipeline.factors import SimpleMovingAverage\nfrom zipline.pipeline.data import EquityPricing<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Data_Ingest\"><\/span>Data Ingest<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Set the following environment variables:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>TEJAPI_BASE: API connection domain.<\/li>\n\n\n\n<li>TEJAPI_KEY: API key.<\/li>\n\n\n\n<li>mdate: Time interval for data retrieval.<\/li>\n\n\n\n<li>ticker: Stock targets, including the broad market return index (IR0001), 2330 (TSMC), 3443 (GUC), 2337 (MXIC).<\/li>\n<\/ol>\n\n\n\n<p>Then use the command <code>!zipline ingest -b tquant<\/code> to fetch the data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>os.environ&#91;'TEJAPI_BASE'] = 'https:\/\/api.tej.com.tw'\nos.environ&#91;'TEJAPI_KEY'] = 'yourkey'\nos.environ&#91;'mdate'] = '20120702 20220702'\nos.environ&#91;'ticker'] = 'IR0001 2330 3443 2337'\n\n!zipline ingest -b tquant<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Editing_the_Momentum_Trade_Strategy\"><\/span><br>Editing the Momentum Trade Strategy <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_Pipeline_Function\"><\/span>Creating the Pipeline Function <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>Pipeline()<\/code> function allows users to simultaneously process various quantitative indicators and price-volume data related to different assets. In this case, we use it to handle:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Four-day simple moving average of trading volume for each stock.<\/li>\n\n\n\n<li>Five-day simple moving average of trading volume for each stock.<\/li>\n\n\n\n<li>Daily trading volume for each stock.<\/li>\n<\/ol>\n\n\n\n<p>Additionally, we use <code>screen <\/code>and <code>StaticAssets <\/code>to filter out the broad market data (IR0001) during the daily calculation of the above indicators. This allows us to skip the calculation of the market index when computing the four-day simple moving average of trading volume, a five-day simple moving average of trading volume, and the daily trading volume for each stock.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bundle = bundles.load('tquant')\nir0001_asset = bundle.asset_finder.lookup_symbol('IR0001',as_of_date = None)\n\ndef make_pipeline():\n    sma_vol_win_4 = SimpleMovingAverage(inputs=&#91;EquityPricing.volume], window_length=4)\n    sma_vol_win_5 = SimpleMovingAverage(inputs=&#91;EquityPricing.volume], window_length=5)\n    curr_vol = EquityPricing.volume.latest\n    \n    return Pipeline(\n        columns = {\n            'sma_4':sma_vol_win_4,\n            'sma_5':sma_vol_win_5,\n            'curr_vol':curr_vol\n        },\n        screen = ~StaticAssets(&#91;ir0001_asset])\n    )<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_initialize_Function\"><\/span>Creating the initialize Function <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>initialize<\/code> function is used to define the daily trading environment before trading begins. In this example, we set:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Liquidity slippage.<\/li>\n\n\n\n<li>Transaction fees.<\/li>\n\n\n\n<li>Using the market return as the benchmark.<\/li>\n\n\n\n<li>Incorporating the Pipeline into the trading process.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>def initialize(context):\n    set_slippage(slippage.VolumeShareSlippage())\n    set_commission(commission.PerShare(cost=0.00285))\n    set_benchmark(symbol('IR0001'))\n    attach_pipeline(make_pipeline(), 'mystrategy')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_handle_data_Function\"><\/span>Creating the handle_data Function <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>handle_data<\/code> function is used to handle daily trading strategies or actions. In this function:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>condition1<\/code>: If the daily trading volume is greater than 2.5 times the four-day simple moving average and the cash position is greater than 0, generate a buy signal.<\/li>\n\n\n\n<li><code>condition2<\/code>: If the daily trading volume is less than 0.75 times the five-day simple moving average, generate a sell signal.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>def handle_data(context, data):\n    out_dir = pipeline_output('mystrategy')\n    for i in out_dir.index: \n        sma_vol_4 = out_dir.loc&#91;i, 'sma_4']\n        sma_vol_5 = out_dir.loc&#91;i, 'sma_5']\n        curr_vol = out_dir.loc&#91;i, 'curr_vol']\n        \n        condition1 = (curr_vol &gt; 2.5 * sma_vol_4) and (context.portfolio.cash &gt; 0)\n        condition2 = (curr_vol &lt; 0.75 * sma_vol_5)\n        \n        if condition1:\n            order(i, 10)\n        elif condition2:\n            order_target(i, 0)\n        else:\n            pass<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_the_analyze_Function\"><\/span>Creating the analyze Function <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>analyze<\/code> function is typically used for generating performance charts. In this case, it will be used for plotting with pyfolio, so it can be skipped in this example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def analyze(context, perf):\n    pass\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Executing_the_Trading_Strategy\"><\/span>Executing the Trading Strategy <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can execute the trading strategy using the <code>run_algorithm<\/code> function with the settings you provided. Here&#8217;s a sample code snippet to run the strategy from July 2, 2012, to July 2, 2022, using the TQuant dataset with an initial capital of NTD 10,000. The results, including daily performance and trade details, will be stored in the <code>results<\/code> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>results = run_algorithm(\n    start = pd.Timestamp('2012-07-02', tz='UTC'),\n    end = pd.Timestamp('2022-07-02', tz ='UTC'),\n    initialize=initialize,\n    bundle='tquant',\n    analyze=analyze,\n    capital_base=1e4,\n    handle_data = handle_data\n)\n\nresults<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img decoding=\"async\" width=\"712\" height=\"377\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131727.png\" alt=\"\" class=\"wp-image-17937\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131727.png 712w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131727-300x159.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131727-150x79.png 150w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><figcaption class=\"wp-element-caption\">results<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Visualization_and_Performance_Analysis\"><\/span>Visualization and Performance Analysis<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generating_Dataframe_needed_for_pyfolio\"><\/span>Generating Dataframe needed for pyfolio<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Then, we use pyfolio to visualize and analyze performance. At first, we can use <code>extract_rets_pos_txn_from_zipline<\/code>&nbsp;to separate above<code> results <\/code>data frame into 3 categories:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>returns<\/li>\n\n\n\n<li>positions<\/li>\n\n\n\n<li>transactions<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from pyfolio.utils import extract_rets_pos_txn_from_zipline\nreturns, positions, transactions = extract_rets_pos_txn_from_zipline(results)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generating_Portfolio_Performance_Table\"><\/span>Generating Portfolio Performance Table<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Use <code>show_perf_stats()<\/code>&nbsp;to generate portfolio performance table, this function enables us to quickly calculate the portfolio`s performance and risk. For detailed source code, please check out the GitHub hyperlink below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img decoding=\"async\" width=\"205\" height=\"623\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131905.png\" alt=\"\" class=\"wp-image-17939\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131905.png 205w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131905-99x300.png 99w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-131905-49x150.png 49w\" sizes=\"(max-width: 205px) 100vw, 205px\" \/><figcaption class=\"wp-element-caption\">Performance and Risk Table<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generating_Portfolio_Component_table\"><\/span>Generating Portfolio Component table<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>With&nbsp;<code>show_and_plot_top_positions()<\/code>&nbsp;and&nbsp;<code>get_percent_alloc()<\/code>, one can visualize the portfolio components easily. For detailed source code, please check out the GitHub hyperlink below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"894\" height=\"618\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132013.png\" alt=\"\" class=\"wp-image-17941\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132013.png 894w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132013-300x207.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132013-150x104.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132013-768x531.png 768w\" sizes=\"(max-width: 894px) 100vw, 894px\" \/><figcaption class=\"wp-element-caption\">Component Table<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Visualize_Cumulative_Returns\"><\/span>Visualize Cumulative Returns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>With&nbsp;<code>plot_rolling_returns()<\/code>, one can plot the cumulative returns for portfolio and benchmark. For detailed source code, please check out the GitHub hyperlink below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"212\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132100.png\" alt=\"\" class=\"wp-image-17943\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132100.png 355w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132100-300x179.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132100-150x90.png 150w\" sizes=\"(max-width: 355px) 100vw, 355px\" \/><figcaption class=\"wp-element-caption\">Cumulative Returns<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Visualize_the_six-month_Rolling_Volatility\"><\/span>Visualize the six-month Rolling Volatility<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>With&nbsp;<code>plot_rolling_volatility()<\/code>, one can plot the six-month rolling volatility for portfolio and benchmark. For detailed source code, please check out the GitHub hyperlink below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"352\" height=\"227\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132315.png\" alt=\"\" class=\"wp-image-17945\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132315.png 352w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132315-300x193.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132315-150x97.png 150w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><figcaption class=\"wp-element-caption\">Rolling Volatility<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Visualize_six-month_Rolling_Sharpe_Ratio\"><\/span>Visualize six-month Rolling Sharpe Ratio<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>With&nbsp;<code>plot_rolling_sharpe()<\/code>, one can plot the six-month rolling Sharpe ratio for portfolio and benchmark. For detailed source code, please check out the GitHub hyperlink below.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"356\" height=\"218\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132409.png\" alt=\"\" class=\"wp-image-17947\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132409.png 356w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132409-300x184.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/Screenshot-2023-09-11-132409-150x92.png 150w\" sizes=\"(max-width: 356px) 100vw, 356px\" \/><figcaption class=\"wp-element-caption\">Rolling Sharpe<\/figcaption><\/figure>\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<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/gist.github.com\/tej87681088\/2780c7c95c57d91ff7802f58467ce0f0\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click Here to GitHub<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Further_Reading\"><\/span>Further 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\/en\/insight\/tquant-lab-bollinger-bands-trading-strategy\/\" class=\"ek-link\">TQuant Lab Bollinger Bands Trading Strategy<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-rookie-manual\/\" class=\"ek-link\">TQuant Lab Rookie Manual<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Related_Links\"><\/span>Related Links<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\">TQuant Lab Github<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" target=\"_blank\" rel=\"noreferrer noopener\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In recent years, momentum trading has become a frequent topic of discussion in stock market strategies. In the stock market, we often hear discussions about the price-volume relationship, where price is considered a leading indicator of volume, among other concepts. This article aims to explore the back-testing effects of increasing trading volume as an entry strategy.<\/p>\n","protected":false},"featured_media":17935,"template":"","tags":[2987,2989,3166,3016],"insight-category":[690,50,885,1356],"class_list":["post-17951","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-quant","tag-quantitive-analysis","tag-tquant-lab-2","tag-trading","insight-category-data-analysis","insight-category-fintech","insight-category-tquant-lab","insight-category-tquant-lab-en"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/17951","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":2,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/17951\/revisions"}],"predecessor-version":[{"id":43984,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/17951\/revisions\/43984"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/17935"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=17951"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=17951"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=17951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}