{"id":1867,"date":"2023-08-29T14:00:00","date_gmt":"2023-08-29T14:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/tquant-lab-rookie-manual\/"},"modified":"2023-08-29T14:00:00","modified_gmt":"2023-08-29T14:00:00","slug":"tquant-lab-rookie-manual","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/tquant-lab-rookie-manual\/","title":{"rendered":"TQuant Lab Rookie Manual"},"content":{"rendered":"<figure class=\"wp-block-image aligncenter size-large is-resized caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"TQuant Lab \u63d0\u4f9b\u5f37\u5927\u7684\u91cf\u5316\u56de\u6e2c\u7cfb\u7d71\uff0c\u9ad8\u7cbe\u78ba\u5ea6\u7684\u7e3e\u6548\u98a8\u96aa\u8a08\u7b97\u3001\u9ad8\u54c1\u8cea\u7684\u8cc7\u6599\u4f86\u6e90\u3001\u9ad8\u64ec\u771f\u5ea6\u7684\u4ea4\u6613\u74b0\u5883\u6a21\u64ec\uff0c\u5e6b\u52a9\u4f7f\u7528\u8005\u5feb\u901f\u90e8\u5c6c\u5404\u6a23\u7684\u4ea4\u6613\u7b56\u7565\uff0c\u6b61\u8fce\u5404\u4f4d\u9ede\u64ca\u9032\u5165\u6587\u7ae0\u4e86\u89e3\u66f4\u591a\u8cc7\u8a0a\u3002\" class=\"wp-image-17144\" height=\"560\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/image-249.png\" style=\"width:840px;height:560px\" width=\"840\"\/><figcaption class=\"wp-element-caption\">Photo by <a href=\"https:\/\/unsplash.com\/@talhahassan116?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noopener\" target=\"_blank\">Talha Hassan<\/a> on <a class=\"ek-link\" href=\"https:\/\/unsplash.com\/photos\/wdBqEHzo39g?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Highlight<\/h2>\n<ul class=\"wp-block-list\">\n<li>Article Difficulty\uff1a\u2605\u2606\u2606\u2606\u2606<\/li>\n<li>TQuant Lab back-testing introduction<\/li>\n<li>Demonstrate how to use TQuant Lab<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n<p class=\"wp-block-paragraph\">TQuant Lab is a back-testing platform developed by TEJ, extending from the well-known Zipline framework. All securities-related information in TQuant Lab originates from TEJ\u2019s exclusive database, ensuring the high quality of data usage. Furthermore, as it is built on the foundation of Zipline, the program architecture has undergone third-party verification over the course of several years, guaranteeing accuracy in its implementation. Thirdly, TQuant Lab offers adjustable parameters for different trading constraints and costs, facilitating users in creating the most lifelike trading environment. If you are interested, please feel free to contact us for more information.<\/p>\n<h2 class=\"wp-block-heading\">Module Installation<\/h2>\n<p class=\"wp-block-paragraph\">TQuant Lab offers installations for three operating systems or environments: Windows, Mac OS, and Google Colab. Before installing the modules, it is essential to have Anaconda installed. Instructions for installing Anaconda can be found in the <a class=\"ek-link\" href=\"https:\/\/medium.com\/python4u\/anaconda%E4%BB%8B%E7%B4%B9%E5%8F%8A%E5%AE%89%E8%A3%9D%E6%95%99%E5%AD%B8-f7dae6454ab6\" rel=\"noopener\" target=\"_blank\">Anaconda Introduction and Installation Tutorial<\/a>.<\/p>\n<h3 class=\"wp-block-heading\">YML Installation<\/h3>\n<p class=\"wp-block-paragraph\">Please download installation packages down below in advanced.<\/p>\n<ul class=\"wp-block-list\">\n<li>Windows (<a class=\"ek-link\" href=\"https:\/\/minhaskamal.github.io\/DownGit\/#\/home?url=https:%2F%2Fgithub.com%2Ftejtw%2Fzipline-tej%2Fblob%2Fmain%2Fzipline-tej.yml\" rel=\"noopener\" target=\"_blank\">zipline-tej.yml<\/a>)<\/li>\n<li>Mac (<a class=\"ek-link\" href=\"https:\/\/minhaskamal.github.io\/DownGit\/#\/home?url=https:%2F%2Fgithub.com%2Ftejtw%2Fzipline-tej%2Fblob%2Fmain%2Fzipline-tej_mac.yml\" rel=\"noopener\" target=\"_blank\">zipline-tej-mac.yml<\/a>)<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Open the Anaconda Prompt and enter the following code to create a virtual environment and install the necessary modules:<\/p>\n<pre class=\"wp-block-code\"><code>Window User\n# Place yml file in below link\n$ cd &lt;C:\\Users\\username\\Downloads&gt;\n# create virtual environment\n$ conda env create -f zipline-tej.yml\n# activate virtual environment\n$ conda activate zipline-tej\n\nMac User\n# Place yml file in below link\n$ cd &lt;C:\\Users\\username\\Downloads&gt;\n# create virtual environment\n$ conda env create -f zipline-tej_mac.yml\n# activate virtual environment\n$ conda activate zipline-tej<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Manual Installation<\/h3>\n<p class=\"wp-block-paragraph\">If you are well-versed in Python, you can manually create a virtual environment and install the required dependencies one by one.<\/p>\n<pre class=\"wp-block-code\"><code># create virtual env\n$ conda create -n &lt;env_name&gt; python=3.8\n\n# activate virtual env\n$ conda activate &lt;env_name&gt;\n\n# download dependency packages\n$ conda install -c conda-forge -y ta-lib\n$ conda install -y notebook=6.4.11\n$ conda install -c conda-forge nb_conda_kernels\n$ conda install -y xlrd=2.0.1\n$ conda install -y openpyxl=3.0.9\n$ pip install zipline-tej<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Google Colab Installation<\/h3>\n<p class=\"wp-block-paragraph\">Since TQuant Lab currently supports only Python 3.8 version, you can add a Python 3.8 runtime using the following code snippet:<\/p>\n<pre class=\"wp-block-code\"><code>!wget -O mini.sh https:\/\/repo.anaconda.com\/miniconda\/Miniconda3-py38_4.8.2-Linux-x86_64.sh\n!chmod +x mini.sh\n!bash .\/mini.sh -b -f -p \/usr\/local\n!conda install -q -y jupyter\n!conda install -q -y google-colab -c conda-forge\n!python -m ipykernel install --name \"Python3.8\" --user\n!rm mini.sh\n!pip install pandas==1.2.5<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Next, follow these steps:<\/p>\n<ol class=\"wp-block-list\">\n<li>Select \u201cRuntime\u201d in the menu.<\/li>\n<li>Choose \u201cChange runtime type.\u201d<\/li>\n<li>In the \u201cRuntime type\u201d dropdown, select \u201cPython 3.8.\u201d<\/li>\n<li>Click \u201cSave.\u201d<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">After changing the runtime type to Python 3.8, you can run the following code to download <code>zipline-tej<\/code>. If you encounter any version issues with <code>yfinance<\/code> and <code>pandas<\/code>, please proceed despite the warnings.<\/p>\n<pre class=\"wp-block-code\"><code>!conda install -y ta-lib -c conda-forge\n!pip install zipline-tej<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Exchange calendar issue<\/h3>\n<p class=\"wp-block-paragraph\">Since TQuant Lab utilizes Taiwan stock market trading data provided by TEJ, we need to use a specialized trading calendar tailored for the Taiwanese stock market. This is crucial for subsequent back-testing purposes. Below, there are downloadable trading calendar files provided, and it\u2019s necessary to place these files within the designated <code><strong>exchange_calendars<\/strong><\/code> folder. [<a class=\"ek-link\" href=\"https:\/\/minhaskamal.github.io\/DownGit\/#\/home?url=https:\/\/github.com\/tejtw\/zipline-tej\/tree\/main\/exchange_calendars\" rel=\"noopener\" target=\"_blank\">Download<\/a>]<\/p>\n<p class=\"wp-block-paragraph\">After downloading the aforementioned trading calendar files, copy the <code><strong>exchange_calendar_tejxtai.py<\/strong><\/code> file and append it to the <code><strong>calendar_utils.py<\/strong><\/code> file within the <code><strong>exchange_calendars<\/strong><\/code> folder.<\/p>\n<p class=\"wp-block-paragraph\">Navigate to the <strong><code>exchange_calendars<\/code> <\/strong>folder located in the site-packages. Typically, it\u2019s located at the following path: <\/p>\n<pre class=\"wp-block-code\"><code>Window user\n\n**C:\\Users\\username\\Anaconda3\\envs\\zipline-tej\\Lib\\site-packages\\exchange_calendars**\n\nMac user\n\nC:\\Users\\username\\Anaconda3\\envs\\zipline-tej\\Lib\\python3.8\\site-packages\\exchange_calendars<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Full tutorial please refer to <a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Install%20TQuant%20Lab.ipynb\" rel=\"noopener\" target=\"_blank\">Install TQuant Lab<\/a>.<\/p>\n<h2 class=\"wp-block-heading\">Ingest data<\/h2>\n<p class=\"wp-block-paragraph\">TQuant Lab retrieves price and volume data from the database built by TEJ and stores the data in memory as data bundles. You can determine the assets to be imported and the time range by configuring environment variables. The available environment variables for configuration are:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>os.environ[\"TEJAPI_BASE\"]<\/code>: Connects to the TEJ database domain.<\/li>\n<li><code>os.environ[\"TEJAPI_KEY\"]<\/code>: Identifies the user.<\/li>\n<li><code>os.environ[\"ticker\"]<\/code>: Specifies the desired assets to retrieve.<\/li>\n<li><code>os.environ[\"mdate\"]<\/code>: Specifies the desired time range for retrieval.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Once you\u2019ve set up the environment variables, use the command<strong> <code>!zipline ingest -b tquant<\/code> <\/strong>to import the data into a local data bundle. This data bundle will be named \u201ctquant\u201d.<\/p>\n<pre class=\"wp-block-code\"><code>import os\nimport pandas as pd \nimport numpy as np \nimport matplotlib.pyplot as plt\n\nos.environ['TEJAPI_BASE'] = \"https:\/\/api.tej.com.tw\"\nos.environ['TEJAPI_KEY'] = \"YOUR KEY\"\nos.environ['ticker'] = \"IR0001 2330 2337 0050 2545\"\nos.environ['mdate'] = \"20200821 20230821\"\n\n!zipline ingest -b tquant<\/code><\/pre>\n<p class=\"wp-block-paragraph\">After loading the data, we can visualize what we have imported by code down below.<\/p>\n<pre class=\"wp-block-code\"><code>from zipline.data.data_portal import get_bundle\ndata_bundle = get_bundle(\n    bundle_name = 'tquant',\n    calendar_name = 'TEJ',\n    start_dt = pd.Timestamp(\"2020-08-21\", tz = 'utc'),\n    end_dt = pd.Timestamp(\"2023-08-21\", tz = 'utc')\n)\ndata_bundle.head(10)<\/code><\/pre>\n<figure class=\"wp-block-image size-large caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" class=\"wp-image-17179\" height=\"461\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/Screenshot-2023-08-22-090039.png\" width=\"1024\"\/><figcaption class=\"wp-element-caption\">Data frame for bundle<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Strategy back-testing<\/h2>\n<p class=\"wp-block-paragraph\">In this example, let\u2019s consider a \u201cBuy and Hold\u201d investment strategy for Taiwan Semiconductor Manufacturing Company (TSMC) using TQuant Lab. We will create the strategy and perform back-testing. To begin, let\u2019s import the necessary modules:<\/p>\n<pre class=\"wp-block-code\"><code>from zipline.api import set_slippage, set_commission\nfrom zipline.finance import slippage, commission\nfrom zipline.api import order, record, symbol\nfrom zipline import run_algorithm<\/code><\/pre>\n<h3 class=\"wp-block-heading\" id=\"Initialize-function\">Initialize function<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#Initialize-function\" rel=\"noopener\" target=\"_blank\"><\/a><\/h3>\n<p class=\"wp-block-paragraph\"><strong><code>initialize<\/code>\u00a0<\/strong>is one of the key functions for zipline backtesting. This function would be executed once before the backtesting starts. The main purpose of this function is to set up the backtesting environment, mainly setting slippage and commission:<\/p>\n<ol class=\"wp-block-list\">\n<li>zipline.api.set_slippage:<br \/>Set slippage model, TQuant Lab offers 4 different types of model. For detailed information, check out\u00a0<a href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Slippage.ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">Zipline Slippage<\/a>.<\/li>\n<li>zipline.api.set_commission:<br \/>Set commission model, TQuant Lab offers 3 different types of models. For detailed information, check out\u00a0<a href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Commission%20Models.ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">Zipline Commission<\/a>.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">Example:<\/p>\n<pre class=\"wp-block-code\"><code>def initialize(context):\n    set_slippage(slippage.FixedSlippage())\n    set_commission(commission.PerShare(cost=0.00285))<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Besides, we can notice that there is an argument called\u00a0<strong>context<\/strong>\u00a0in\u00a0<code>initialize<\/code>\u00a0function. It is a persistent namespace for you to store variables you need to access from one algorithm iteration to the next. For example, we can set a variable (context.day = 0) to count the number of trading dates and a variable (context.has_ordered = False) to record whether holding the position of TSMC or not.<\/p>\n<p class=\"wp-block-paragraph\">Example:<\/p>\n<pre class=\"wp-block-code\"><code>def initialize(context):\n    context.day = 0\n    context.has_ordered = False\n    set_slippage(slippage.FixedSlippage())\n    set_commission(commission.PerShare(cost=0.00285))<\/code><\/pre>\n<h3 class=\"wp-block-heading\" id=\"Handle_data-function\">Handle_data function<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#Handle_data-function\" rel=\"noopener\" target=\"_blank\"><\/a><\/h3>\n<p class=\"wp-block-paragraph\"><code>handle_data<\/code>\u00a0is another key function for zipline backtesting. This function would be executed every trading day. The main purpose of this function is to establish a trading strategy, place orders, and record trading information.<\/p>\n<p class=\"wp-block-paragraph\">There are two arguments in\u00a0<code>handle_data<\/code>\u00a0\u2013\u00a0<strong>context<\/strong>\u00a0and\u00a0<strong>data<\/strong>.\u00a0<strong>context<\/strong>\u00a0is basically the same as above. To record whether we hold TSMC stock on each trading day, we set:<\/p>\n<pre class=\"wp-block-code\"><code>def handle_data(context, data):\n\n    # plus 1 in each trading day\u3002\n    context.day += 1 \n\n    # check whether we hold TSMC stock in current trading day\n    if not context.has_ordered:<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Then we use\u00a0<code>zipline.order<\/code>\u00a0function to place orders. There are 6 different types of order methods. For detailed information, please check out<a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Order%20(order%20%26%20order_target).ipynb\" rel=\"noopener\" target=\"_blank\">\u00a0Zipline Order (percent &amp; target_percent)<\/a><a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Order%20(percent%20%26%20target_percent).ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">,\u00a0<\/a><a aria-label=\"Zipline Order (value &amp; target_value) (opens in a new tab)\" class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Order%20(percent%20%26%20target_percent).ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">Zipline Order (value &amp; target_value)<\/a><a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Order%20(percent%20%26%20target_percent).ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">,<\/a>\u00a0and<a aria-label=\" Zipline Order (order &amp; order_target) (opens in a new tab)\" class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Order%20(value%20%26%20target_value).ipynb\" rel=\"noreferrer noopener\" target=\"_blank\"> Zipline Order (order &amp; order_target)<\/a><a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/Zipline%20Order%20(percent%20%26%20target_percent).ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">. <\/a>Here, we use the most fundamental one:<\/p>\n<h3 class=\"wp-block-heading\" id=\"zipline.api.order\">zipline.api.order<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#zipline.api.order\" rel=\"noopener\" target=\"_blank\"><\/a><\/h3>\n<p class=\"wp-block-paragraph\">Buy or sell n share of assets.<\/p>\n<p class=\"wp-block-paragraph\">Parameters:<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#Parameters:\" rel=\"noopener\" target=\"_blank\"><\/a><\/p>\n<ul class=\"wp-block-list\">\n<li>asset: zipline.assets.Asset<br \/>target asset<\/li>\n<li>amount: int<br \/>decide n share<\/li>\n<li>limit_price: float, optional<br \/>limit price<\/li>\n<li>stop_price: float, optional<br \/>stop price<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">After adding the\u00a0<code><strong>order(symbol(\"2330\"))<\/strong><\/code>\u00a0for placing an order, we set\u00a0<strong>context.has_ordered<\/strong>\u00a0to True, so we will no longer place an order after this trading day.<\/p>\n<pre class=\"wp-block-code\"><code>def handle_data(context, data):\n\n    context.day += 1 \n    if not context.has_ordered:\n\n        order(symbol(\"2330\", 1000)\n\n        context.has_ordered = True\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Eventually, to record the number of trading days, whether we hold TSMC stock or not, and stock price, we can use\u00a0<strong><code>record<\/code>\u00a0<\/strong>function. This function enables users to record trading information and turn to columns in the data frame from\u00a0<code><strong>run_algorithm<\/strong><\/code>.<\/p>\n<pre class=\"wp-block-code\"><code>record(column name = information)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">In today\u2019s case, we record the numbers of trading days (context.day), whether holding position or not (context.has_ordered), and today\u2019s closed price (data.current(symbol(\u201c2330\u201d), \u201cclose\u201d)). This\u00a0<strong>data<\/strong>\u00a0is equivalent to\u00a0<strong>data<\/strong>\u00a0mentioned in\u00a0<strong><code>handle_data<\/code>\u00a0<\/strong>arguments.\u00a0<strong>data<\/strong>\u00a0restores stocks\u2019 price and volume information. To get current price or volume info, we can use\u00a0<code><strong>data.current()<\/strong><\/code>.<\/p>\n<h3 class=\"wp-block-heading\" id=\"zipline.data.current\">zipline.data.current<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#zipline.data.current\" rel=\"noopener\" target=\"_blank\"><\/a><\/h3>\n<p class=\"wp-block-paragraph\">Call stocks\u2019 current price and column<\/p>\n<p class=\"wp-block-paragraph\">Parameters:<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#Parameters:\" rel=\"noopener\" target=\"_blank\"><\/a><\/p>\n<ul class=\"wp-block-list\">\n<li>assets: zipline.asset.Asset<br \/>target asset<\/li>\n<li>fields: str<br \/>target information, \u2018close\u2019, \u2018open\u2019, \u2018high\u2019, \u2018low\u2019, and \u2018volume\u2019.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">We want to record TSMC close price:<\/p>\n<pre class=\"wp-block-code\"><code>def handle_data(context, data):\n    context.day += 1 \n    if not context.has_ordered:\n        order(symbol(\"2330\", 1000)\n        context.has_ordered = True\n\n    record(\n        trade_days = context.day,\n        has_ordered = context.has_ordered,\n        TSMC = data.current(symbol(\"2330\"), \"close\")\n    )<\/code><\/pre>\n<h3 class=\"wp-block-heading\" id=\"Analyze-function\">Analyze function<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#Analyze-function\" rel=\"noopener\" target=\"_blank\"><\/a><\/h3>\n<p class=\"wp-block-paragraph\"><strong><code>Analyze<\/code>\u00a0<\/strong>function is for performance and risk visualization. There are two arguments for this function. The first one is\u00a0<strong>context<\/strong>, and it is the same as the previously mentioned one. Another one is\u00a0<strong>perf<\/strong>, which is the final data frame from\u00a0<code><strong>run_algorithm<\/strong><\/code>. We can use specific columns in the final data frame for visualization.<\/p>\n<h3 class=\"wp-block-heading\" id=\"Run_algorithm-function\">Run_algorithm function<a href=\"http:\/\/localhost:8888\/notebooks\/Documents\/TEJ%20%E5%AF%A6%E7%BF%92\/TEJ%20Zipline\/%E4%BA%A4%E6%98%93%E7%AD%96%E7%95%A5\/TQuant%20Lab%20Rookie%20Manual.ipynb#Run_algorithm-function\" rel=\"noopener\" target=\"_blank\"><\/a><\/h3>\n<p class=\"wp-block-paragraph\">This function is for running the backtest. It will return a performance-related data frame. For detailed information, please check out\u00a0<a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\/blob\/main\/lecture\/TSMC%20buy%20and%20hold%20strategy.ipynb\" rel=\"noreferrer noopener\" target=\"_blank\">TSMC buy and hold strategy<\/a>. Here, we demonstrate the argument for this function:<\/p>\n<ul class=\"wp-block-list\">\n<li>start:\u00a0<em>pd.Timestamp<\/em>\u00a0or\u00a0<em>datetime<\/em>\u00a0<br \/>Beginning date of back-testing<\/li>\n<li>end:\u00a0<em>pd.Timestamp<\/em>\u00a0or\u00a0<em>datetime<\/em>\u00a0<br \/>Ending fate of back-testing<\/li>\n<li>initialize:\u00a0<em>callable<\/em>\u00a0 <br \/>call <strong>initialize <\/strong>function<\/li>\n<li>capital_base:\u00a0<em>int<\/em>\u00a0<br \/>capital base for back-testing<\/li>\n<li>handle_data:\u00a0<em>callable<\/em>, optional <br \/>call <strong>handle_data <\/strong>function<\/li>\n<li>before_trading_start:\u00a0<em>callable<\/em>, optional <br \/>call <strong>before_trading_start <\/strong>function<\/li>\n<li>analyze:\u00a0<em>callable<\/em>, optional <br \/>call <strong>analyze <\/strong>function<\/li>\n<li>data_frequency:\u00a0<em>{\u201cdaily\u201d, \u201cminute\u201d}<\/em>, optional <br \/>set trading frequency<\/li>\n<li>bundle:\u00a0<em>str<\/em>, optional<br \/>set bundle<\/li>\n<li>trading_calendar:\u00a0<em>TradingCalendar<\/em>, optional <br \/>set exchange calendar<\/li>\n<li>benchmark_returns:\u00a0<em>pd.Series<\/em>, optional <br \/>set benchmark return<\/li>\n<li>treasury_returns:\u00a0<em>pd.Series<\/em>, optional <br \/>set risk-free rate<\/li>\n<\/ul>\n<pre class=\"wp-block-code\"><code>start_date = pd.Timestamp('2020-08-21',tz='utc')\nend_date = pd.Timestamp('2023-08-21',tz='utc')\n\nresults = run_algorithm(start= start_date,  \n                       end=end_date,\n                       initialize=initialize,                       \n                       capital_base=1e6,                       \n                       analyze=analyze,\n                       handle_data=handle_data,\n                       data_frequency='daily',\n                       bundle='tquant'\n                       )<\/code><\/pre>\n<figure class=\"wp-block-image size-large caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" class=\"wp-image-17181\" height=\"440\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/Screenshot-2023-08-22-090152.png\" width=\"1024\"\/><figcaption class=\"wp-element-caption\">returns of analyze function<\/figcaption><\/figure>\n<figure class=\"wp-block-image size-large caption-align-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" class=\"wp-image-17183\" height=\"488\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/Screenshot-2023-08-22-090357.png\" width=\"1024\"\/><figcaption class=\"wp-element-caption\">data frame for risk and performance<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\">The main purpose of this article is to help users understand how to use TQuant Lab. TQuant Lab is a quantitative backtesting platform developed by our company. Compared to self-built backtesting platforms, TQuant Lab offers highly accurate price and volume data, precise performance and risk calculations, and a realistic simulated trading environment. In the future, we will provide more articles about trading strategies deployed on TQuant Lab. Please stay tuned and continue to support us.<\/p>\n<h2 class=\"wp-block-heading\">Source code<\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"https:\/\/gist.github.com\/tej87681088\/d1ad5414be7f6a7952de50920eb648b4\" rel=\"noopener\" target=\"_blank\">Click here to Github<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Extended reading<\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"\/en\/insight\/what-is-the-look-ahead-bias\/\">What is the Look-ahead Bias ?<\/a><\/li>\n<li><a class=\"ek-link\" href=\"\/en\/insight\/how-to-avoid-common-mistakes-during-trading-loss-avoidance\/\">How to avoid common mistakes during trading \u2013 Loss Avoidance<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Related link<\/h2>\n<ul class=\"wp-block-list\">\n<li><a aria-label=\"TEJ API \u8cc7\u6599\u5eab\u9996\u9801 (opens in a new tab)\" class=\"ek-link\" href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API <\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/zipline-tej\" rel=\"noopener\" target=\"_blank\">zipline-tej<\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/discord.com\/channels\/1133312771275104348\/1140917956432363550\" rel=\"noopener\" target=\"_blank\">Discord for TQuant Lab<\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/github.com\/tejtw\/TQuant-Lab\" rel=\"noopener\" target=\"_blank\">TQuant-Lab<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>TQuant Lab offers a robust quantitative back-testing system with high precision performance and risk calculations, top-quality data sources, and a highly realistic simulated trading environment. It aids users in swiftly deploying a wide range of trading strategies. Feel free to click into the article to learn more information.<\/p>\n","protected":false},"featured_media":1866,"template":"","tags":[52,91,97],"insight_category":[16],"class_list":["post-1867","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-quantitative-analysis","tag-beginners-guide","tag-data-analysis","insight_category-quant-data-science"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/1867","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"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/1866"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=1867"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}