{"id":624,"date":"2025-09-26T14:00:00","date_gmt":"2025-09-26T14:00:00","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/tquant-from-0-to-1-day-5-introduction-to-order-placement-methods-in-the-tquant-lab-backtesting-system\/"},"modified":"2026-08-05T02:31:07","modified_gmt":"2026-08-05T02:31:07","slug":"tquant-from-0-to-1-day-5-introduction-to-order-placement-methods-in-the-tquant-lab-backtesting-system","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/tquant-from-0-to-1-day-5-introduction-to-order-placement-methods-in-the-tquant-lab-backtesting-system\/","title":{"rendered":"[TQuant From 0 to 1 \u2013 Day 5] Introduction to Order Placement Methods in the TQuant Lab Backtesting System"},"content":{"rendered":"<h2 class=\"wp-block-heading\">Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"wp-block-paragraph\">In\u00a0TQuant backtesting and live trading, order functions serve as the central link between\u00a0strategy logic\u00a0and\u00a0capital management. Choosing the right order method not only makes the code cleaner and easier to read but also improves the efficiency of\u00a0risk control\u00a0and\u00a0portfolio rebalancing.<\/p>\n<p class=\"wp-block-paragraph\">TQuant provides order functions across\u00a0three dimensions\u00a0\u2014\u00a0<em>share quantity<\/em>,\u00a0<em>capital amount<\/em>, and\u00a0<em>portfolio weight<\/em>. For each dimension, there are two variants: a\u00a0basic order\u00a0and a\u00a0target order. In total, this gives us\u00a0six order placement methods. In the following sections, we will explain the features, parameters, and recommended applications of each.<\/p>\n<h3 class=\"wp-block-heading\">How to Use in Practice<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\">As explained in previous articles, order functions are primarily implemented inside the&nbsp;<code>handle_data<\/code>&nbsp;function, where trading logic is defined. During backtesting, the&nbsp;<code>run_algorithm<\/code>&nbsp;function executes a loop that evaluates the conditions on each trading day to determine whether orders should be placed.<\/p>\n<p class=\"wp-block-paragraph\">This design allows you to set very detailed order logic, enabling strategies that can better adapt to real-world trading scenarios.<\/p>\n<h2 class=\"wp-block-heading\">Introduction to Order Functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3 class=\"wp-block-heading\"><code>order(asset, amount)<\/code>&nbsp;\u2014 Order by Share Quantity<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\"><strong>Function:<\/strong>&nbsp;Buy or sell directly with a specified number of shares. Suitable when precise control of trade quantities is required.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n<ul class=\"wp-block-list\">\n<li><code>asset<\/code>: Target asset (e.g.,&nbsp;<code>symbol('Ticker')<\/code>)<\/li>\n<li><code>amount<\/code>: Number of shares (positive = buy, negative = sell)<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\"><strong>Use Case Suggestion:<\/strong>&nbsp;When you know the fixed number of shares for each trade, or need to operate within a specific share range,&nbsp;<code>order<\/code>&nbsp;is the most straightforward choice.<\/p>\n<h3 class=\"wp-block-heading\"><code>order_target(asset, target_amount)<\/code>&nbsp;\u2014 Order to Target Share Quantity<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\"><strong>Function:<\/strong>&nbsp;Automatically calculates&nbsp;<code>(target shares \u2212 current shares)<\/code>&nbsp;and places an order to adjust holdings to the target. Suitable for position-adjustment strategies that are guided by shareholding targets.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n<ul class=\"wp-block-list\">\n<li><code>asset<\/code>: Target security<\/li>\n<li><code>target_amount<\/code>: Final shareholding quantity<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\"><strong>Use Case Suggestion:<\/strong>&nbsp;If the requirement is to \u201cadjust to a fixed number of shares,\u201d this function saves the effort of manually calculating the difference.<\/p>\n<h3 class=\"wp-block-heading\"><code>order_value(asset, value)<\/code>&nbsp;\u2014 Order by Capital Amount<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\"><strong>Function:<\/strong>&nbsp;Buy or sell with a specified capital amount. The system estimates the number of shares based on the current day\u2019s price. Suitable for operations measured by capital allocation.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n<ul class=\"wp-block-list\">\n<li><code>asset<\/code>: Target security<\/li>\n<li><code>value<\/code>: Buy\/sell amount (positive = buy, negative = sell)<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\"><strong>Use Case Suggestion:<\/strong>&nbsp;When the strategy involves a fixed monetary amount per trade, or when allocating based on a proportion of total portfolio capital,&nbsp;<code>order_value<\/code>&nbsp;is the most intuitive.<\/p>\n<h3 class=\"wp-block-heading\"><code>order_target_value(asset, target_value)<\/code>&nbsp;\u2014 Order to Target Market Value<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\"><strong>Function:<\/strong>&nbsp;Adjust holdings to reach the specified market value&nbsp;<code>(target value \u2212 current value)<\/code>. Convenient for rebalancing at the market-value level.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n<ul class=\"wp-block-list\">\n<li><code>asset<\/code>: Target security<\/li>\n<li><code>target_value<\/code>: Final holding market value<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\"><strong>Use Case Suggestion:<\/strong>&nbsp;In multi-asset portfolios, when strict market-value rebalancing is required, this function is particularly useful.<\/p>\n<h3 class=\"wp-block-heading\"><code>order_percent(asset, percent)<\/code>&nbsp;\u2014 Order by Portfolio Weight<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\"><strong>Function:<\/strong>&nbsp;Buy or sell based on a proportion of the portfolio\u2019s net value. For example,&nbsp;<code>0.1<\/code>&nbsp;means buying a position equal to 10% of net portfolio value.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n<ul class=\"wp-block-list\">\n<li><code>asset<\/code>: Target security<\/li>\n<li><code>percent<\/code>: Proportion of portfolio (positive = buy, negative = sell)<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\"><strong>Use Case Suggestion:<\/strong>&nbsp;When dynamically adjusting asset weights without needing exact shares or capital amounts,&nbsp;<code>order_percent<\/code>&nbsp;is the simplest option.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h3 class=\"wp-block-heading\"><code>order_target_percent(asset, target_percent)<\/code>&nbsp;\u2014 Order to Target Portfolio Weight<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p class=\"wp-block-paragraph\"><strong>Function:<\/strong>&nbsp;Adjusts the asset position to reach a target portfolio weight&nbsp;<code>(target weight \u2212 current weight)<\/code>. Ensures strict portfolio rebalancing.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n<ul class=\"wp-block-list\">\n<li><code>asset<\/code>: Target security<\/li>\n<li><code>target_percent<\/code>: Final target weight (range: \u22121.0 to 1.0)<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\"><strong>Use Case Suggestion:<\/strong>&nbsp;In multi-asset portfolios or rotation strategies,&nbsp;<code>order_target_percent<\/code>&nbsp;is the most direct and intuitive tool for rebalancing.<\/p>\n<h2 class=\"wp-block-heading\">Practical Exercise<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"wp-block-paragraph\">By combining the content from&nbsp;<strong>TQuant Day 4<\/strong>, we can integrate the four major backtesting frameworks with the newly learned order placement methods to build a simple investment strategy.<\/p>\n<h3 class=\"wp-block-heading\">For clarity, we will demonstrate two straightforward strategies:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol class=\"wp-block-list\">\n<li><strong>Buy-and-Hold 0050<\/strong><br \/>At the start of the backtest, buy 0050 and hold it until the end of the testing period.<\/li>\n<li><strong>Dollar-Cost Averaging (DCA) on 0050<\/strong><br \/>Invest 10,000 NTD every 30 trading days until the allocated capital is fully used.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">First, we need to&nbsp;<strong>import the backtesting data<\/strong>. This part will be explained in detail in the next article. For now, just understand that this step involves importing the stock data required for trading into the backtesting system.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Code snippet for importing backtest data:<\/strong><br \/>In this example, the backtesting period is set from&nbsp;<strong>2023-12-31 to 2024-12-31<\/strong>, covering approximately 252 trading days. Readers can add any stock symbols they wish to trade into the&nbsp;<code>pool<\/code>&nbsp;list, and their price\u2013volume data will be imported into the backtesting system.<\/p>\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n<h2 class=\"wp-block-heading\">Display backtest results and visualize with charts<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"447\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/pp-1024x447-1.png\" alt=\"\u00a0TQuant Lab \u56de\u6e2c\u7cfb\u7d71\u4e0b\u55ae\u65b9\u5f0f\u4ecb\u7d39\" class=\"wp-image-34490\" \/><\/figure>\n<h2 class=\"wp-block-heading\">Strategy 2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"447\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/gkgkg-1024x447-1.png\" alt=\"\u00a0TQuant Lab \u56de\u6e2c\u7cfb\u7d71\u4e0b\u55ae\u65b9\u5f0f\u4ecb\u7d39\" class=\"wp-image-34492\" \/><\/figure>\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"wp-block-paragraph\">In this article, we introduced six order placement methods in Zipline, each with its own applicable scenarios, advantages, and limitations. The choice between precise share quantities, capital amounts, or portfolio weights should be guided by strategy logic and risk management requirements. Mastering the correct order functions not only streamlines backtesting code but also ensures accuracy in trade execution, thereby improving both the efficiency and quality of strategy development. Wishing you greater success in quantitative research and live trading applications!<\/p>\n<p class=\"wp-block-paragraph\">We also welcome investors to explore further. In the future, we will continue to introduce how to build various indicators using the TEJ database and backtest their performance. Readers interested in trading backtests are encouraged to consider TQuant Lab\u2019s solutions, which leverage high-quality databases to help you develop trading strategies tailored to your needs.<\/p>\n<p class=\"wp-block-paragraph\">Disclaimer: This analysis is for reference only and does not constitute any product or investment advice.<\/p>\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;\"><strong data-start=\"26\" data-end=\"104\">\u3010TQuant Lab Backtesting System\u3011Solve Your Quantitative Finance Pain Points<\/strong><\/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\">Click here to register and start your free trial<\/a><\/div>\n<\/div>\n<h2 class=\"wp-block-heading\">Next Article \u2013 [TQuant From 0 to 1 &#8211; Day 6] (link coming soon)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h2 class=\"wp-block-heading\">Further Reading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.tejwin.com\/en\/insight\/your-gateway-to-quantitative-trading\/\">\u3010TQuant : From 0 to 1 \u2013 Day 1\u3011 Your Gateway to Quantitative Trading: Become a Quant Expert with TQuant Lab, No Experience Needed!<\/a><\/p>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.tejwin.com\/en\/?post_type=insight&amp;p=31794\">\u3010TQuant : From 0 to 1 \u2013 Day 2\u3011 Avoid the Invisible Killers of Quantitative Trading: Use TQuant Lab to Precisely Manage Commission and Slippage, Giving Your Strategies the Edge!<\/a><\/p>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-from-0-to-1-day-3-building-a-comprehensive-investment-data-perspective-stock-pool-screening-and-data-retrieval-with-tejtoolapi-2\/\"><a href=\"https:\/\/www.tejwin.com\/en\/?post_type=insight&amp;p=31794\">\u3010<\/a>TQuant from 0 to 1 \u2013 Day 3<a href=\"https:\/\/www.tejwin.com\/en\/?post_type=insight&amp;p=31794\">\u3011<\/a>Building a Comprehensive Investment Data Perspective: Stock Pool Screening and Data Retrieval with TejToolAPI<\/a><\/p>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-from-0-to-1-day-4-core-architecture-of-backtesting-what-are-the-key-settings-for-initialize\/\">\u3010TQuant : From 0 to 1 &#8211; Day 4\u3011 Core Architecture of Backtesting: What are the key settings for Initialize?<\/a><\/p>\n<h2 class=\"wp-block-heading\">Related Links<span class=\"ez-toc-section-end\"><\/span><\/h2>\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<li><a href=\"https:\/\/tquant.tejwin.com\" target=\"_blank\" rel=\"noreferrer noopener\">TQuant Lab Official Website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In\u00a0TQuant backtesting and live trading, order functions serve as the central link between\u00a0strategy logic\u00a0and\u00a0capital management. Choosing the right order method not only makes the code cleaner and easier to read but also improves the efficiency of\u00a0risk control\u00a0and\u00a0portfolio rebalancing.<br \/>\nTQuant provides order functions across\u00a0three dimensions\u00a0\u2014\u00a0share quantity,\u00a0capital amount, and\u00a0portfolio weight. For each dimension, there are two variants: a\u00a0basic order\u00a0and a\u00a0target order. In total, this gives us\u00a0six order placement methods. In the following sections, we will explain the features, parameters, and recommended applications of each.<\/p>\n","protected":false},"featured_media":623,"template":"","tags":[50,52,60,70],"insight_category":[16],"class_list":["post-624","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-market-data","tag-quantitative-analysis","tag-quant","tag-tej-api","insight_category-quant-data-science"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/624","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\/623"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=624"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}