{"id":2353,"date":"2022-03-15T02:31:56","date_gmt":"2022-03-15T02:31:56","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/momentum-select\/"},"modified":"2022-03-15T02:31:56","modified_gmt":"2022-03-15T02:31:56","slug":"momentum-select","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/momentum-select\/","title":{"rendered":"Momentum select"},"content":{"rendered":"<p class=\"wp-block-paragraph\" id=\"95a7\">along for the ride<\/p>\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_17R8mpZbVJPHxqsqmgj1lOA.jpg\"\/><figcaption class=\"wp-element-caption\">Photo by\u00a0<a href=\"https:\/\/unsplash.com\/@kenziem?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Mackenzie Marco<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/s\/photos\/price?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"ef42\"><strong>Highlights<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2606\u2606\u2606<\/li>\n<li>Advice: This article adopts TEJ database to select the stock. Before reading this,\u00a0<a class=\"ek-link\" href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-15-momentum-trade-53e538e272dc\" rel=\"noopener\" target=\"_blank\">\u3010Quant(15)\u3011 Momentum trad<\/a>e is recommended to explore to better understand the basic procedure of backtesting.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"3404\"><strong>Preface<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"ca83\">we introduce the<a class=\"ek-link\" href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-15-momentum-trade-53e538e272dc\" rel=\"noopener\" target=\"_blank\">\u00a0Momentum trad<\/a>e last time ,now we are going to teach you how to find this stock ,if we are not highly involved the marker,we often found in the news which already price in ,so what we want to do is find the stock automatic.It also can save our time .<\/p>\n<p class=\"wp-block-paragraph\" id=\"e654\">we write the function more flexible.you can change the parameter by yourself ! Following is the way to our set the parameter :<\/p>\n<ol class=\"wp-block-list\">\n<li>when the trading volume is 5 times more than the average of previous 4 days.<\/li>\n<li>stock price rise at least 3%<\/li>\n<li>the trade volume is more than 500<\/li>\n<\/ol>\n<h2 class=\"wp-block-heading\" id=\"3bd7\"><strong>The Editing Environment and Modules Required<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"0199\">Window10 Spyder(anaconda31)<\/p>\n<h2 class=\"wp-block-heading\" id=\"6b2d\"><strong>Database<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"4790\"><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=42\" rel=\"noreferrer noopener\" target=\"_blank\">Security Transaction Data Table<\/a>\uff1aListed securities with unadjusted price and index. Code is \u2018TWN\/EWPRCD\u2019<\/p>\n<p class=\"wp-block-paragraph\" id=\"65cd\"><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=107\" rel=\"noreferrer noopener\" target=\"_blank\">Securities attribute information Data Table<\/a>\u00a0: Listed securities industry category and name\uff0c Code is \u2018TWN\/ANPRCSTD\u2019<\/p>\n<pre class=\"wp-block-preformatted\">import pandas as pd<br\/>import matplotlib.pyplot as plt<br\/>import numpy as np<br\/>#################TEJ<br\/>import tejapi<br\/>tejapi.ApiConfig.api_key = 'Your Key'<br\/>tejapi.ApiConfig.ignoretz = True<br\/>###############draw k line <br\/>from mplfinance.original_flavor import candlestick_ohlc<br\/>from matplotlib.dates import date2num ,num2date<br\/>import matplotlib.ticker as ticker<br\/>import matplotlib.dates<\/pre>\n<h2 class=\"wp-block-heading\" id=\"9568\"><strong>Data Selection<\/strong><\/h2>\n<pre class=\"wp-block-preformatted\">data=tejapi.get('TWN\/EWNPRCSTD' ,chinese_column_name=True )<br\/>select=data[\"\u4e0a\u5e02\u5225\"].unique()<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1FOQCcm3O2cad6TvfKxSSrw.png\"\/><\/figure>\n<pre class=\"wp-block-preformatted\">select=select[1:3]<br\/>condition =(data[\"\u4e0a\u5e02\u5225\"].isin(select)) &amp; ( data[\"\u8b49\u5238\u7a2e\u985e\u540d\u7a31\"]==\"\u666e\u901a\u80a1\" )<br\/>data=data[condition] ######\u8a2d\u689d\u4ef6<br\/>twid=data[\"\u8b49\u5238\u78bc\"].to_list()<\/pre>\n<p class=\"wp-block-paragraph\" id=\"ff6b\">select [\u2018coid\u2019,\u2019open_d\u2019,\u2019close_d\u2019,\u2019high_d\u2019,\u2019low_d\u2019 ,\u2019mdate\u2019, \u2018volume\u2019, \u2018close_adj\u2019 ]<\/p>\n<pre class=\"wp-block-preformatted\">opts={'columns': ['coid','open_d','close_d','high_d','low_d' ,'mdate', 'volume', 'close_adj']}<br\/>start='2022-1-01'<br\/>end=\"2022-03-8\"<br\/>tw=tejapi.get('TWN\/EWPRCD',coid=twid,<br\/>                mdate={'gt':start,'lt':end},<br\/>                paginate=True,        <br\/>                chinese_column_name=True,<br\/>                opts=opts<br\/>                )<br\/>a=tw<br\/>a=tw.groupby(by=[\"\u8b49\u5238\u78bc\"])<br\/>b=list(a) #######\u02c7<\/pre>\n<p class=\"wp-block-paragraph\" id=\"b2d6\">use the pandas groupby function , and we have to put there into a list ,so we can observe the data<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1lMA8en1NN6NTqmzc2Nz_Og.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"ddba\">we should open it<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1zmsBcsE8xgDbfRUf3q5-gw.png\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"2843\"><strong>Data Processing<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"6023\"><strong>Step 1. set the indicator<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">def selectstock(b,number,minnum,num) : # b\u70ba\u7528groupby\u505a\u51fa\u4f86\u7684list<br\/>    output=[]<br\/>    for i in range(len(b)):<br\/>        a=b[i][1]<br\/>        a[\"\u4e94\u65e5\u5747\u91cf\"]=a[\"\u6210\u4ea4\u91cf(\u5343\u80a1)\"].rolling(5).sum()<br\/>        a[\"\u4e94\u65e5\u5747\u50f9\"]=a[\"\u6536\u76e4\u50f9-\u9664\u6b0a\u606f\"].rolling(5).mean()<br\/>        <br\/>        a[\"\u524d\u5e7e\u65e5\u5e73\u5747\"]=(a[\"\u4e94\u65e5\u5747\u91cf\"]-a[\"\u6210\u4ea4\u91cf(\u5343\u80a1)\"]) \/ 4<br\/>        a[\"\u6210\u4ea4\u91cf\"+str(number)+\"\u500d\u5594\"]=a[\"\u6210\u4ea4\u91cf(\u5343\u80a1)\"]-a['\u524d\u5e7e\u65e5\u5e73\u5747'] *number <br\/>        a.drop('\u4e94\u65e5\u5747\u91cf',axis=1)<br\/>        if a[\"\u6210\u4ea4\u91cf(\u5343\u80a1)\"].mean() &gt; minnum  :<br\/>            output.append(a)<br\/>    stockineed=[]for j in output:<br\/>        j.reset_index(drop=True,inplace=True)<br\/>        if j[\"\u6210\u4ea4\u91cf\"+str(number)+\"\u500d\u5594\"][len(j)-1] &gt; 0 :<br\/>            <br\/>            if j[\"\u6536\u76e4\u50f9-\u9664\u6b0a\u606f\"][len(j)-1] &gt; j[\"\u6536\u76e4\u50f9-\u9664\u6b0a\u606f\"][len(j)-2]*num :<br\/>                stock=j[\"\u8b49\u5238\u78bc\"][0]<br\/>                stockineed.append(stock)<br\/>    return stockineed<\/pre>\n<p class=\"wp-block-paragraph\" id=\"fc0f\">this function have 3 parameter (number,minnum,num)<\/p>\n<p class=\"wp-block-paragraph\" id=\"81ad\">number : select 5 times more than previous four day<\/p>\n<p class=\"wp-block-paragraph\" id=\"66d8\">minnum : the trade volume minium we select \u201d500\u2033<\/p>\n<p class=\"wp-block-paragraph\" id=\"beef\">num : stock price growth percentage we select 3 %<\/p>\n<pre class=\"wp-block-preformatted\">stockineed=selectstock(b,5,500,1.03)<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1EYraNpF1NNZnrHHV0iiEiw.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"747c\">there is 8 stocks confrom to our conditions<\/p>\n<pre class=\"wp-block-preformatted\">condition=tw[\"\u8b49\u5238\u78bc\"].isin(stockineed)<br\/>tw1=tw[condition]<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1I_lCbBMlTa6s0T99GO4ORw.png\"\/><\/figure>\n<pre class=\"wp-block-preformatted\">a=tw1.groupby(\"\u8b49\u5238\u78bc\")<br\/>a=list(a)  <br\/>for i in a:<br\/>    i[1].set_index(\"\u65e5\u671f\",inplace=True)  ###set date to index<\/pre>\n<p class=\"wp-block-paragraph\" id=\"cdd1\">use the groupby function again and we can see each group of the stcok<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1lMA8en1NN6NTqmzc2Nz_Og.png\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"5548\"><strong>Step 2 visualize the stocks<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"df87\">we try to get the stock name and the catagory in other database ,so we can combine all of them in one picture<\/p>\n<pre class=\"wp-block-preformatted\">opts={'columns': ['coid','stk_name','mkt','tejindnm',]}<br\/>data1=tejapi.get('TWN\/ANPRCSTD' ,<br\/>                opts=opts,<br\/>                chinese_column_name=True,paginate=True,<br\/>                coid=stockineed)<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1l1U6lIDLaTIUW-ksYrw2IA.png\"\/><\/figure>\n<pre class=\"wp-block-preformatted\">def getplot(finaldata):for i in range(len(finaldata)):<br\/>        <br\/>        out=finaldata[i][1]<br\/>        name=data1[data1[\"\u8b49\u5238\u78bc\"]==finaldata[i][0]]        <br\/>        outputname0=name.loc[i,'\u8b49\u5238\u540d\u7a31']##\u70ba\u4e86\u5f97\u5230\u6a19\u984c<br\/>        outputname1=name.loc[i,'\u4e0a\u5e02\u5225'] ##<br\/>        outputname2=name.loc[i,\"TEJ\u7522\u696d\u540d\"]##<br\/>        # out.reset_index(drop=True,inplace=True)<br\/>        if outputname1 == \"TSE\":<br\/>            outputname1 =\"\u4e0a\u5e02\"<br\/>        else :<br\/>            outputname1= \"\u4e0a\u6ac3\"<br\/>        fig = plt.figure(figsize=(6,6))<br\/>        grid = plt.GridSpec(3, 3, wspace=0.4, hspace=0.3)<br\/>        ax1=plt.subplot(grid[:2, :])<br\/>        ax2=plt.subplot(grid[2, :])<br\/>       <br\/>        date= [i for i in range(len(out))] <br\/>##### \u7372\u53d6 0\u958b\u59cb\u7684\u9806\u5e8f \u56e0\u70ba\u5982\u679c\u4f7f\u7528\u539f\u65e5\u671f\u5047\u65e5\u6703\u6709\u7a7a\u7f3a<br\/>       <br\/>        out_index= [tuple([date[i],out.\u958b\u76e4\u50f9[i],out.\u6700\u9ad8\u50f9[i],out.\u6700\u4f4e\u50f9[i],out.\u6536\u76e4\u50f9[i]]) for i  in range(len(out))]<br\/>        candlestick_ohlc(ax1, out_index, width=0.6, colorup='r', colordown='g', alpha=0.75)ax1.set_xticks(range(0, len(out.index), 10))<br\/>        ax1.set_xticklabels(out.index[::10])<br\/>        <br\/>        ax1.set_title([str(out[\"\u8b49\u5238\u78bc\"][0]),outputname0,outputname1,outputname2]) <br\/>        ax1.set_ylabel('Price')<br\/>        ax1.grid(linestyle=\"--\",alpha=0.8)<br\/>       <br\/>        red_pred = np.where(out[\"\u6536\u76e4\u50f9\"] &gt;= out[\"\u958b\u76e4\u50f9\"],out[\"\u6210\u4ea4\u91cf(\u5343\u80a1)\"], 0)<br\/>        blue_pred = np.where(out[\"\u6536\u76e4\u50f9\"] &lt;  out[\"\u958b\u76e4\u50f9\"], out[\"\u6210\u4ea4\u91cf(\u5343\u80a1)\"], 0)<br\/>        out1=out.reset_index(drop=True )<br\/>        ax2.bar(out1.index,red_pred, facecolor=\"red\")<br\/>        ax2.bar(out1.index,blue_pred,facecolor=\"green\")<br\/>        ax2.set_xticks(range(0, len(out.index), 5))<br\/>        ax2.set_xticklabels(out.index[::5])<br\/>        ax2.set_ylabel('vol')plt.legend(loc='best')<br\/>        fig.autofmt_xdate()<br\/>        fig.tight_layout()<br\/>        plt.show()<\/pre>\n<p class=\"wp-block-paragraph\" id=\"8329\">in this finction we can plot the stocks which have their price and volumes<\/p>\n<pre class=\"wp-block-preformatted\">getplot(a) ###\u524d\u9762\u805a\u5408\u5f8c\u7684List(a)<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1QViXXv3rrTNf2ReOcHSrbA.png\"\/><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1ZWieFoncIPmXdDuUDZTMTg.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"9f4b\">here is the stock~ maybe we can add some ma line to make the picture more abundant,and you can use this to seclct stock everyday~<\/p>\n<h2 class=\"wp-block-heading\" id=\"282a\"><strong>Conclusion<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"e629\">By using this function we still can find the strong stock in this period,so maybe this is the way to find the stocks.We can also match the strategy that we introduce last time . Maybe can bring high rate of return!<\/p>\n<p class=\"wp-block-paragraph\" id=\"dd31\">After all, the application of technical indicator varies from person to person. As a result, if readers are interested in diverse trading backtesting, welcome to purchase the plan offered in\u00a0<a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a>. Construct trading strategies fitting you with high quality database.<\/p>\n<h2 class=\"wp-block-heading\" id=\"4c6a\"><strong>Source Code<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"https:\/\/gist.github.com\/tej87681088\/a698e572e4c71038f823265d44828496#file-tejapi_medium-13-ipynb\" rel=\"noopener\" target=\"_blank\">Click here to go Github<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"e448\"><strong>Extended Reading<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"\/en\/insight\/modular-backtesting-system\/\">Modular Backtesting System<\/a><\/li>\n<li><a class=\"ek-link\" href=\"https:\/\/medium.com\/tej-api-financial-data-anlaysis\/quant-15-momentum-trade-53e538e272dc\" rel=\"noopener\" target=\"_blank\">Momentum trade<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"216c\"><strong>Related Link<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>along for the ride Highlights Preface we introduce the\u00a0Momentum trade last time ,now we are going to teach you how to find this stock ,if we are not highly involved the marker,we often found in the news which already price in ,so what we want to do is find the stock automatic.It also can save [\u2026]<\/p>\n","protected":false},"featured_media":2344,"template":"","tags":[70,71,50,65],"insight_category":[13],"class_list":["post-2353","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-tej-api","tag-quantitative-strategy","tag-market-data","tag-python","insight_category-quant-research"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/2353","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\/2344"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=2353"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=2353"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=2353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}