{"id":30176,"date":"2024-11-22T13:30:00","date_gmt":"2024-11-22T05:30:00","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=30176"},"modified":"2026-03-02T10:58:29","modified_gmt":"2026-03-02T02:58:29","slug":"algorithmic-trading-sinopac-api","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/","title":{"rendered":"Algorithmic Trading Guide\uff1aLeveraging TQuant Lab Strategies with the SinoPac API for Automated Trading"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter size-full caption-align-center is-style-editorskit-rounded\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1792\" height=\"1024\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29565\" style=\"object-fit:cover\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a.png 1792w, https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a-300x171.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a-1024x585.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a-150x86.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a-768x439.png 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/DALL\u00b7E-2024-10-28-15.32.42-A-modern-16_9-image-showing-an-automated-API-trading-system-interface.-The-scene-displays-a-sophisticated-digital-dashboard-with-graphs-charts-and-a-1536x878.png 1536w\" sizes=\"(max-width: 1792px) 100vw, 1792px\" \/><figcaption class=\"wp-element-caption\">Generated by ChatGPT<\/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-69f12c7df2484\" 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-69f12c7df2484\"  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\/algorithmic-trading-sinopac-api\/#Preface\" >Preface<\/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\/algorithmic-trading-sinopac-api\/#Overview_of_the_SuperTrend_Strategy\" >Overview of the SuperTrend Strategy<\/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\/algorithmic-trading-sinopac-api\/#Identifying_Daily_Trading_Targets\" >Identifying Daily Trading Targets<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/#Building_the_Stock_Universe\" >Building the Stock Universe<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/#Selecting_Trading_Targets\" >Selecting Trading Targets<\/a><\/li><\/ul><\/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\/algorithmic-trading-sinopac-api\/#Algorithmic_Trading_with_SinoPac_API\" >Algorithmic Trading with SinoPac API<\/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\/algorithmic-trading-sinopac-api\/#Setup\" >Setup<\/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\/algorithmic-trading-sinopac-api\/#Logging_in_to_Sinopac_API\" >Logging in to Sinopac API<\/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\/algorithmic-trading-sinopac-api\/#Retrieving_Current_Holdings\" >Retrieving Current Holdings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/#Setting_Up_Order_Callback\" >Setting Up Order Callback<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/#Placing_Orders\" >Placing Orders<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/#Viewing_Realized_P_L\" >Viewing Realized P&amp;L<\/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\/algorithmic-trading-sinopac-api\/#Logging_Out\" >Logging Out<\/a><\/li><\/ul><\/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\/algorithmic-trading-sinopac-api\/#Conclusion\" >Conclusion<\/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\/algorithmic-trading-sinopac-api\/#Why_TEJ_Data_Supports_Taiwan_Market_Investments\" >Why TEJ Data Supports Taiwan Market Investments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.tejwin.com\/en\/insight\/algorithmic-trading-sinopac-api\/#Extended_Reading\" >Extended Reading<\/a><\/li><\/ul><\/nav><\/div>\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 today\u2019s financial world, quantitative finance is increasingly gaining traction. According to Goldman Sachs, over 70% of U.S. stock trades in the first half of 2024 were executed via algorithms, underscoring the sheer scale of algorithmic trading. On the other hand, Taiwan\u2019s stock market, fueled by the AI boom, saw an average daily trading volume of NT$420 billion in the first half of 2024. However, algorithmic trading accounted for just NT$2.8 billion per day\u2014only 0.6% of the total.<\/p>\n\n\n\n<p>Is algorithmic trading really that distant from us? In reality, the proportion of algorithmic trading in Taiwan\u2019s market has been growing at an annual rate of 20%. This article introduces the algorithmic trading API from SinoPac Securities\uff1a<em><a href=\"https:\/\/sinotrade.github.io\/\" target=\"_blank\" rel=\"noopener\">Shioaji<\/a><\/em>. Its native Python ecosystem makes coding accessible and enables SinoPac\u2019s API to capture roughly half of Taiwan\u2019s algorithmic trading market share. Through the <a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-supertrend-strategy\/\">TQuant Lab SuperTrend Strategy<\/a>, we\u2019ll demonstrate how to swiftly master algorithmic trading, setting you on the path to becoming a pioneer in Taiwan\u2019s market.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Overview_of_the_SuperTrend_Strategy\"><\/span>Overview of the SuperTrend Strategy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The SuperTrend Strategy combines the <em>SuperTrend Indicator<\/em> and the <em>ADX Indicator<\/em>. The SuperTrend Indicator consists of upper and lower bands. When the stock price breaks above the upper band, it signals a breakout, indicating a potential upward trend. Conversely, when the price drops below the lower band, it indicates a breach of support and a potential downward trend.<\/p>\n\n\n\n<p>The ADX (Average Directional Index), ranging from 0 to 100, measures trend strength. Higher values signify stronger trends, helping confirm bullish or bearish conditions.<\/p>\n\n\n\n<p>Entry and Exit Rules for the SuperTrend Strategy:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Long Entry\uff1aWhen the closing price breaks above the upper band, signaling a breakout and a potential bullish trend, buy the stock.<\/li>\n\n\n\n<li>Short Entry\uff1aWhen the closing price falls below the lower band and ADX > 50, indicating a bearish trend, sell the stock.<\/li>\n<\/ul>\n\n\n\n<p>For a detailed explanation of the strategy and the backtesting process in TQuant Lab, refer to the guide on <em><a href=\"https:\/\/www.tejwin.com\/en\/insight\/tquant-lab-supertrend-strategy\/\">SuperTrend Strategy: Buy Low, Sell High to Profit from Market Swings<\/a><\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Identifying_Daily_Trading_Targets\"><\/span>Identifying Daily Trading Targets<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Building_the_Stock_Universe\"><\/span>Building the Stock Universe<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Given the characteristics of the SuperTrend Indicator, we aim to select stocks with growth potential and trend-forming tendencies. <\/p>\n\n\n\n<p>The selection process includes\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using <code>get_calendar<\/code> to retrieve the previous trading date.<\/li>\n\n\n\n<li>Using <code>get_universe<\/code> to fetch the list of electronic industry stocks listed on that day.<\/li>\n\n\n\n<li>Filtering the top 100 stocks by market capitalization using the TEJ Tool API.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Selecting_Trading_Targets\"><\/span>Selecting Trading Targets<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The following three steps outline the process of filtering daily trading targets for the SuperTrend Strategy. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We import three months of price and volume data for the selected 100 stocks from TEJ database.<\/li>\n\n\n\n<li>Calculate the SuperTrend and ADX indicators through <code>CustomFactor<\/code> and <code>Pipeline<\/code> functions.<\/li>\n\n\n\n<li>By applying the strategy\u2019s entry and exit conditions to the most recent data, we generate <em>buy_list <\/em>and <em>sell_list<\/em>.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img decoding=\"async\" width=\"660\" height=\"87\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/buy_data.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29777\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/buy_data.png 660w, https:\/\/www.tejwin.com\/wp-content\/uploads\/buy_data-300x40.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/buy_data-150x20.png 150w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><figcaption class=\"wp-element-caption\">Buy Targets Data<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img decoding=\"async\" width=\"652\" height=\"85\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/sell_data.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29781\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/sell_data.png 652w, https:\/\/www.tejwin.com\/wp-content\/uploads\/sell_data-300x39.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/sell_data-150x20.png 150w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><figcaption class=\"wp-element-caption\">Sell Targets Data<\/figcaption><\/figure>\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;\">Start Building Portfolios That Outperform the Market!<\/p>\n<div style=\"margin-top: 32px;\"><strong><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:\/\/www.tejwin.com\/en\/databank-solution\/market-data\/\">TEJ Market Databank<\/a><\/strong><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Algorithmic_Trading_with_SinoPac_API\"><\/span>Algorithmic Trading with SinoPac API<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setup\"><\/span>Setup<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Install the Shioaji package using pip\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install shioaji<\/code><\/pre>\n\n\n\n<p>Before using the Sinopac API, you must <a href=\"https:\/\/sinotrade.github.io\/tutor\/prepare\/open_account\/\" target=\"_blank\" rel=\"noopener\">open a securities account<\/a> with Sinopac Securities, <a href=\"https:\/\/sinotrade.github.io\/tutor\/prepare\/token\/\" target=\"_blank\" rel=\"noopener\">apply for an API key and certificate<\/a>, and <a href=\"https:\/\/sinotrade.github.io\/tutor\/prepare\/terms\/\" target=\"_blank\" rel=\"noopener\">agree to the terms of service<\/a>. Detailed instructions can be found in the documentation linked above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Logging_in_to_Sinopac_API\"><\/span>Logging in to Sinopac API<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This guide uses Shioaji in simulation mode. Import the shioaji package and log in with your api_key and secret_key to access the Sinopac API environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import shioaji as sj\n\napi = sj.Shioaji(simulation=True) # Simulation mode\napi.login(\n    api_key = 'your api_key',     \n    secret_key = 'your secret_key',\n    contracts_cb=lambda security_type: print(f\"{repr(security_type)} fetch done.\") # Fetch Contracts Callback\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Retrieving_Current_Holdings\"><\/span>Retrieving Current Holdings<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>list_positions<\/code> function in Sinopac API provides current holdings, including stock symbols, quantities, average prices, current prices, and profit\/loss, etc.<\/p>\n\n\n\n<p><strong>Note<\/strong>\uff1a If no positions are held, the function returns an empty DataFrame<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>positions = api.list_positions(api.stock_account)\nposition_data = pd.DataFrame(s.__dict__ for s in positions)\nposition_data<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1358\" height=\"141\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/position_example.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29792\" style=\"object-fit:cover\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/position_example.png 1358w, https:\/\/www.tejwin.com\/wp-content\/uploads\/position_example-300x31.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/position_example-1024x106.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/position_example-150x16.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/position_example-768x80.png 768w\" sizes=\"(max-width: 1358px) 100vw, 1358px\" \/><figcaption class=\"wp-element-caption\">Sample Portfolio Information Table<\/figcaption><\/figure>\n\n\n\n<p>Extracting the <em>code <\/em>column from the position data will provide the current list of held stocks, referred to as <em>hold_list<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if position_data.empty:\n    hold_list = &#91;]\nelse:\n    hold_list = position_data&#91;'code'].to_list()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_Up_Order_Callback\"><\/span>Setting Up Order Callback<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We define the order callback function <code>order_cb<\/code> and pass it to <code>set_order_callback<\/code>. Once an order is executed, it will return order details and trade execution messages.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def order_cb(stat, msg):\n    print('my_order_callback')\n    print(stat, msg)\n\napi.set_order_callback(order_cb)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Placing_Orders\"><\/span>Placing Orders<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>place_order<\/code> function is used to execute trades. It requires <em>contract <\/em>(stock information) and <em>order<\/em> (trade details). Key parameters include action (Buy\/Sell), quantity, price type (MKT, MKP), order type (ROD, IOC, FOK), and order lot (Common, Odd), etc.<\/p>\n\n\n\n<p>The trading rules in this article are as follows\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If a stock in the <em>buy_list <\/em>is not in the <em>hold_list<\/em>, a MKT order will be placed to buy one share.<\/li>\n\n\n\n<li>If a stock in the <em>sell_list <\/em>is in the <em>hold_list<\/em>, a MKT order will be placed to sell one share.<\/li>\n<\/ul>\n\n\n\n<p>We automate the order placement process using a loop, demonstrated here with the buying procedure\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in buy_list:\n    print('processing %s' % i)\n    \n    if i not in hold_list:\n        print('%s not in hold_list' % i)\n        print('Ready to buy!')\n        \n        contract = api.Contracts.Stocks.TSE&#91;i]\n\n        order = api.Order(\n            action = 'Buy',\n            price = 0, # MKT, MKP will not use price parameter\n            quantity = 1,\n            price_type = 'MKT', # MKT or MKP\n            order_type = 'IOC', # ROD, IOC, FOK\n            order_lot = 'Common', # Common:\u6574\u80a1, Fixing:\u5b9a\u76e4, Odd:\u76e4\u5f8c\u96f6\u80a1, IntradayOdd:\u76e4\u4e2d\u96f6\u80a1\n            account = api.stock_account\n        )\n\n        trade = api.place_order(contract, order)\n        trade\n\n    else:\n        print('%s in hold_list' % i)\n\n    print('=' * 100)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1102\" height=\"98\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/order.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29803\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/order.png 1102w, https:\/\/www.tejwin.com\/wp-content\/uploads\/order-300x27.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/order-1024x91.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/order-150x13.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/order-768x68.png 768w\" sizes=\"(max-width: 1102px) 100vw, 1102px\" \/><figcaption class=\"wp-element-caption\">Order Callback<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"1102\" height=\"66\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/deal-1.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29809\" style=\"object-fit:cover\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/deal-1.png 1102w, https:\/\/www.tejwin.com\/wp-content\/uploads\/deal-1-300x18.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/deal-1-1024x61.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/deal-1-150x9.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/deal-1-768x46.png 768w\" sizes=\"(max-width: 1102px) 100vw, 1102px\" \/><figcaption class=\"wp-element-caption\">Deal Callback<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Viewing_Realized_P_L\"><\/span>Viewing Realized P&amp;L<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>list_profit_loss<\/code> function provides realized profit\/loss data for a specified date range, including stock symbols, prices, quantities, and profit\/loss details<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>profitloss = api.list_profit_loss(api.stock_account,'2024-10-28','2024-10-28')\nprofitloss_data = pd.DataFrame(pnl.__dict__ for pnl in profitloss)\nprofitloss_data<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full caption-align-center\"><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"285\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/profit_loss.png\" alt=\"Algorithmic Trading\" class=\"wp-image-29811\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/profit_loss.png 701w, https:\/\/www.tejwin.com\/wp-content\/uploads\/profit_loss-300x122.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/profit_loss-150x61.png 150w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><figcaption class=\"wp-element-caption\">Realized Profit and Loss Table<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Logging_Out\"><\/span>Logging Out<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Due to API usage limits, it\u2019s recommended to log out after completing your operations<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>api.logout()\n# True<\/code><\/pre>\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 article demonstrates how to integrate the Sinopac API with TQuant Lab to implement the SuperTrend Strategy, achieving algorithmic trading. The advantages of algorithmic trading are evident: once the trading strategy and execution environment are set up, running the script on rebalancing days automates the entire process, from placing orders to tracking portfolio performance, significantly reducing manual intervention and time costs.<\/p>\n\n\n\n<p>Beyond the features covered here, the Sinopac API offers many other functionalities for users to explore. Those interested can consult the <a href=\"https:\/\/sinotrade.github.io\/\" target=\"_blank\" rel=\"noopener\">Shioaji Documentation<\/a> to dive deeper into algorithmic trading!<\/p>\n\n\n\n<p class=\"has-text-align-center\"><em><strong>Please note that the strategy and target discussed in this article are for reference only and do not constitute any recommendation for specific commodities or investments<\/strong>. <\/em><\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-100 has-custom-font-size\" style=\"font-size:22px\"><a class=\"wp-block-button__link has-background wp-element-button\" href=\"https:\/\/www.tejwin.com\/en\/databank-solution\/financial-data\/\" style=\"border-radius:16px;background:linear-gradient(135deg,rgb(243,224,131) 0%,rgb(102,197,166) 50%,rgb(51,132,181) 100%)\"><strong>Access to Comprehensive Quantitative Data<\/strong><br><strong>Start Building Portfolios That Outperform the Market Today!<\/strong><\/a><\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_TEJ_Data_Supports_Taiwan_Market_Investments\"><\/span>Why TEJ Data Supports Taiwan Market Investments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>TEJ provides extensive, high-quality financial data covering the Taiwan stock market, tailored for market trend analysis. As the Taiwan Stock Exchange recently peaked due to rising semiconductor stocks, TEJ\u2019s data allows investors to monitor trends closely and craft strategies tailored to Taiwan\u2019s unique market dynamics.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0978b8\" class=\"has-inline-color\">Taiwan Economic Journal (TEJ)<\/mark><\/strong>, a financial database established in Taiwan for over 30 years, serves local financial institutions and academic institutions, and has long-term cooperation with internationally renowned data providers, providing high-quality financial data for five financial markets in Asia.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><mark style=\"background-color:#ebc766\" class=\"has-inline-color has-black-color\">Complete Coverage<\/mark><\/strong>\uff1a Includes all listed companies on stock markets in Taiwan, China, Hong Kong, Japan, Korea, etc.\u00a0<\/li>\n\n\n\n<li><strong><mark style=\"background-color:#ebc766\" class=\"has-inline-color\">Comprehensive Analysis of Enterprises<\/mark><\/strong>\uff1a Operational aspects, financial aspects, securities market performance, ESG sustainability, etc.\u00a0<\/li>\n\n\n\n<li><strong><mark style=\"background-color:#ebc766\" class=\"has-inline-color\">High-Quality Database<\/mark><\/strong>\uff1a TEJ data is cleaned, checked, enhanced, and integrated to ensure it meets the information needs of financial and market analysis.\u00a0<\/li>\n<\/ul>\n\n\n\n<p>With TEJ&#8217;s assistance, you can access relevant information about major stock markets in Asia, such as securities market, financials data, enterprise operations, board of directors, sustainability data, etc., providing investors with timely and high-quality content!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-a89b3969 wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-width wp-block-button__width-100 has-custom-font-size\" style=\"font-size:21px\"><a class=\"wp-block-button__link has-background has-text-align-center wp-element-button\" href=\"https:\/\/www.tejwin.com\/en\/contact\/\" style=\"border-radius:16px;background:linear-gradient(135deg,rgb(160,209,216) 0%,rgb(51,145,181) 50%,rgb(50,95,191) 100%)\"><strong>Learn More About Our Databases and Solutions?<br>Contact Us and Get the Free Trial Today!<\/strong><\/a><\/div>\n<\/div>\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\/en\/insight\/masterlink-securities-api\/\">MasterLink Securities API Beginner&#8217;s Guide\uff1a How to Use TQuant Lab Strategies for Automated Trading<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/questions-about-tquant-lab\/\">Newbie Troubleshooting: Answering All Your Questions About TQuant Lab.<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This article will guide you through leveraging the TQuant Lab SuperTrend strategy in combination with the SinoPac API to quickly master the essentials of algorithmic trading. Let\u2019s become pioneers of algorithmic trading in the Taiwan stock market together!<\/p>\n","protected":false},"featured_media":29731,"template":"","tags":[2987,2988,2990],"insight-category":[50,1356],"class_list":["post-30176","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-quant","tag-quantitative-analysis","tag-quantitive-trading","insight-category-fintech","insight-category-tquant-lab-en"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/30176","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":24,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/30176\/revisions"}],"predecessor-version":[{"id":44015,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/30176\/revisions\/44015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/29731"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=30176"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=30176"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=30176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}