<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>策略開發豆知識 &#8211; FinLab</title>
	<atom:link href="https://www.finlab.tw/tag/%e7%ad%96%e7%95%a5%e9%96%8b%e7%99%bc%e8%b1%86%e7%9f%a5%e8%ad%98/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Mon, 21 Nov 2022 15:30:10 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.9</generator>

<image>
	<url>https://www.finlab.tw/wp-content/uploads/2020/07/favicon.png</url>
	<title>策略開發豆知識 &#8211; FinLab</title>
	<link>https://www.finlab.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">179699571</site>	<item>
		<title>台股財報資料豆知識 ｜ 時序索引操作</title>
		<link>https://www.finlab.tw/tw_stock_financial_statement_time_series_knowledge/</link>
					<comments>https://www.finlab.tw/tw_stock_financial_statement_time_series_knowledge/#respond</comments>
		
		<dc:creator><![CDATA[Ben]]></dc:creator>
		<pubDate>Fri, 28 Oct 2022 10:31:13 +0000</pubDate>
				<category><![CDATA[Python新手教學]]></category>
		<category><![CDATA[FinLab 量化平台]]></category>
		<category><![CDATA[生產力]]></category>
		<category><![CDATA[FinlabDataframe]]></category>
		<category><![CDATA[台股]]></category>
		<category><![CDATA[策略開發豆知識]]></category>
		<category><![CDATA[財報]]></category>
		<category><![CDATA[資料處理]]></category>
		<guid isPermaLink="false">https://www.finlab.tw/?p=4720</guid>

					<description><![CDATA[目前正逢財報季，有些公司已經上傳第三季財報，許多已基本面為選股主軸的用戶都開始緊盯自己的程式是否跑出新的指引？許多新手用戶對財報資料的發佈時間和資料操作不熟悉，這篇文章將娓娓道來台股財報資料的時序細節，花幾分鐘，你會了解財報「發布日、截止日、FinlabDataFrame的資料索引轉換方法，讓你上手財報資料選股策略。]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size">目前正逢財報季，有些公司已經上傳第三季財報，許多已基本面為選股主軸的用戶都開始緊盯自己的程式是否跑出新的指引？藉於新手用戶對財報資料的發佈時間和資料操作不熟悉，這篇文章將娓娓道來台股財報資料的時序細節，花幾分鐘，你會了解財報「發布日、截止日、FinlabDataFrame的資料索引轉換方法，讓你上手財報資料選股策略的前處理。</p>



<h2>財報發布日定義</h2>



<p class="has-medium-font-size">FinLab 資料庫系統會每天更新、檢查財報資料，財報發布日以<a href="https://doc.twse.com.tw/server-java/t57sb01?step=1&amp;colorchg=1&amp;co_id=1101&amp;year=&amp;seamon=&amp;mtype=A&amp;" target="_blank" rel="noopener">財報電子檔</a>內的資料上傳日當基準資料。你可能疑惑怎麼在新聞或公司重訊已看到 EPS 多少了，但抓下的財報或財報電子檔卻沒有當季資料？那是因為有些公司的財報電子檔上傳日會比董事會重訊發布晚個幾天，通常是因為公司內部或會計作業問題，某些會計項目可能還會再校對或微調等等，實際數字以公司財報完整電子檔上傳才是比較穩當的做法。 大部分的公司會在財報截止日前一週內才上傳完畢。</p>



<h2>財報截止日定義</h2>



<p class="has-medium-font-size">所有公司都必須在每季財報截止日前上傳財報電子書，若沒有即時上傳，通常代表財務狀況出問題、會計師審核上出狀況&#8230;若沒在規定期限內補正，後續面臨暫停交易或下市風險。<br>台股上市櫃公司一年要上傳4次財報，興櫃只要上傳第二季和第四季財報，監理強度不同。各產業截止日細節如下列供參：</p>



<h3>上市櫃一般公司</h3>



<p class="has-medium-font-size">季別/財報截止日對應為下列對應，遇假日則遞延到次一個交易日。</p>



<ul class="has-medium-font-size"><li>Q1：當年5/15</li><li>Q2：當年8/14。若為2021年後的KY股，因監管法規改變，改成8/31。</li><li>Q3：當年11/14</li><li>Q4：次年3/31</li></ul>



<h3>保險業</h3>



<p class="has-medium-font-size">季別/財報截止日對應為下列對應，遇假日則遞延到次一個交易日。</p>



<ul class="has-medium-font-size"><li>Q1：當年4/30</li><li>Q2：當年8/31</li><li>Q3：當年10/30</li><li>Q4：次年3/31</li></ul>



<h3>金融業</h3>



<p class="has-medium-font-size">這邊指的是非保險的金融業，如銀行、證券、票券、金控業者。季別/財報截止日對應為下列對應，遇假日則遞延到次一個交易日。</p>



<ul class="has-medium-font-size"><li>Q1：當年5/15</li><li>Q2：當年8/31</li><li>Q3：當年11/14</li><li>Q4：次年3/31</li></ul>



<h2>量化平台財報資料操作</h2>



<h3>取得資料</h3>



<p class="has-medium-font-size">至<a href="https://ai.finlab.tw/database/#financial_statement" target="_blank" rel="noopener">財報資料庫</a>複製財報項目的使用方法，即可取得財報資料。<br>欄位是每家公司的股號，包含所有公開發行公司，索引則是每季的季別，ex : 2013Q1。<br>新手會疑惑為什麼看到一堆NaN ?不要緊張，不是資料出問題，而是這些公司當時未上市櫃或按規定不用發布財報。</p>



<figure class="wp-block-image size-large"><img width="1024" height="565" src="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.25.31-1024x565.png" alt="截圖 2022 10 28 下午5.25.31" class="wp-image-4721" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.25.31-1024x565.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.25.31-300x166.png 300w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.25.31-768x424.png 768w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.25.31-1536x848.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.25.31-2048x1130.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="台股財報資料豆知識 ｜ 時序索引操作 1"></figure>



<p class="has-medium-font-size">另外提一個小技巧，如果你想要拿掉未上市櫃公司來方便觀察資料，可以使用pandas的<a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.dropna.html" target="_blank" rel="noopener">dropna</a>進行操作。下面的例子會排除掉每一欄位沒有20格有數值的欄位，篩出上市櫃有較久歷史的公司，就可以顯示中間部分的資料，像是1101台泥等等。</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="534" src="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.34.53-1024x534.png" alt="截圖 2022 10 28 下午5.34.53" class="wp-image-4722" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.34.53-1024x534.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.34.53-300x157.png 300w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.34.53-768x401.png 768w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.34.53-1536x802.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.34.53-2048x1069.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="台股財報資料豆知識 ｜ 時序索引操作 2"></figure>



<h3>時序轉換</h3>



<p class="has-medium-font-size">上面原始財報數據的日期索引是如「2022-Q1」這類的文字格式，雖然回測將不同尺度的資料取交集時會自動將時序轉成財報發布日的datetime格式，但若有其他用途，想將時序索引從文字轉換成財報發布日的datetime格式，該如何做呢？</p>



<h4>季別文字轉財報發佈日</h4>



<p class="has-medium-font-size">轉非常的簡單，只要加上 <code>index_str_to_date()</code> ，就能將資料轉換，這是<a href="https://doc.finlab.tw/reference/dataframe/#finlab.dataframe.FinlabDataFrame.index_str_to_date" target="_blank" rel="noopener">FinlabDataFrame</a>類別內建的方法。<br>從下圖所示，可以看到加完 <code>index_str_to_date()</code>後，索引改變了。<br>要注意的是，如果公司還沒有正式上傳「<a href="https://doc.twse.com.tw/server-java/t57sb01?step=1&amp;colorchg=1&amp;co_id=1101&amp;year=&amp;seamon=&amp;mtype=A&amp;" target="_blank" rel="noopener">財報電子書</a>」，雖然<a href="https://mops.twse.com.tw/mops/web/t203sb02" target="_blank" rel="noopener">XBRL資訊平台</a>已經有數值可下載，但由於財報電子書上傳日資料未定，資料仍會被歸到截止日的欄位。<br>現在正臨2022-Q3財報季，沒有財報電子書上傳日的數值都會放到最後兩欄的財報截止日，2022-10-31是保險業Q3截止日、2022-11-14則是一般上市櫃公司的Q3截止日，不用擔心回測會用到未來資料，回測會再去與收盤價的交易日索引做對應，遇假日會遞延到下一個交易日。<br>你可能會好奇，公司每季只會發布一次財報，為何日期會那麼多天？那是因為只要A日有任一家公司有上傳紀錄，為方便回測，索引都會加入發布日，如果其他公司在A日沒發布財報，其他公司則會套用近一期數值做填充。</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="486" src="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.48.13-1024x486.png" alt="截圖 2022 10 28 下午5.48.13" class="wp-image-4723" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.48.13-1024x486.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.48.13-300x142.png 300w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.48.13-768x364.png 768w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.48.13-1536x728.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午5.48.13-2048x971.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="台股財報資料豆知識 ｜ 時序索引操作 3"></figure>



<h4>季別文字轉財報截止日</h4>



<p class="has-medium-font-size">若想使用財報截止日做回測與選股，可以使用 <code>deadline()</code> ，就能將資料轉換，這是<a href="https://doc.finlab.tw/reference/dataframe/#finlab.dataframe.FinlabDataFrame.deadline" target="_blank" rel="noopener">FinlabDataFrame</a> 類別內建的方法。注意財報資料與其他時間尺度的資料做交集時，預設是使用財報發布日，所以若要使用截止日當條件，一定要加上 <code>deadline()</code>，使用範例如下。</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="366" src="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午6.06.38-1024x366.png" alt="截圖 2022 10 28 下午6.06.38" class="wp-image-4724" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午6.06.38-1024x366.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午6.06.38-300x107.png 300w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午6.06.38-768x274.png 768w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午6.06.38-1536x548.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-28-下午6.06.38-2048x731.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="台股財報資料豆知識 ｜ 時序索引操作 4"></figure>



<p class="has-medium-font-size">截止日日期索引包括一般公司、保險業、金融業的規定日期，若當日沒數值，則會拿近一筆數值來補，以方便回測。</p>



<h2>結論</h2>



<p class="has-medium-font-size">經過上述說明，是不是對 FinLab 量化平台的財報資料操作與原理更為熟悉了呢？<br>如果想進一步學習用財報來選股，可參考這篇<a href="https://www.finlab.tw/basic_score_strategy/">財務指標計分法的教學</a>～<br>如果還有其他相關問題，歡迎隨時與我們討論喔！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/tw_stock_financial_statement_time_series_knowledge/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4720</post-id>	</item>
		<item>
		<title>如何用指標計分來選股? &#124; Python 資料分級處理</title>
		<link>https://www.finlab.tw/basic_score_strategy/</link>
					<comments>https://www.finlab.tw/basic_score_strategy/#respond</comments>
		
		<dc:creator><![CDATA[Ben]]></dc:creator>
		<pubDate>Tue, 27 Sep 2022 05:09:39 +0000</pubDate>
				<category><![CDATA[Python新手教學]]></category>
		<category><![CDATA[FinLab 量化平台]]></category>
		<category><![CDATA[VIP文章]]></category>
		<category><![CDATA[基本面分析]]></category>
		<category><![CDATA[pandas]]></category>
		<category><![CDATA[策略開發豆知識]]></category>
		<guid isPermaLink="false">https://www.finlab.tw/?p=4593</guid>

					<description><![CDATA[前陣子韓老師線上直播時，有同學許願想幫標的打分數，利用分數來篩選。 今天FinLab的神燈精靈就來幫你實現願望 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size">前陣子韓老師<a href="https://fb.watch/fOmbkSHRnn/" target="_blank" rel="noopener">線上直播</a>時，有同學許願想幫標的打分數，利用分數來篩選。</p>



<p></p>



<figure class="wp-container-2 wp-block-gallery-1 wp-block-gallery has-nested-images columns-default is-cropped">
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="320" data-id="4594" src="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-27-下午12.21.45-1024x320.png" alt="截圖 2022 09 27 下午12.21.45" class="wp-image-4594" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-27-下午12.21.45-1024x320.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-27-下午12.21.45-300x94.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-27-下午12.21.45-768x240.png 768w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-27-下午12.21.45-1536x480.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-27-下午12.21.45-2048x640.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用指標計分來選股? | Python 資料分級處理 5"></figure>
</figure>



<p></p>



<p class="has-medium-font-size">今天FinLab的神燈精靈就來幫你實現願望，示範如何用 pandas 撰寫指標計分，並實際應用到策略開發。</p>



<h2>方法一、Pandas qcut</h2>



<p class="has-medium-font-size"><a href="https://pandas.pydata.org/docs/reference/api/pandas.qcut.html" target="_blank" rel="noopener">qcut </a>是 Pandas模組中基於分位數的離散化函數。剛好適用這位同學的需求。簡單打個範例來看看qcut的效果。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">import pandas as pd
# range(20)數列按10分位數切分級
pd.qcut(x=range(20), q=10, labels=False)
# output
# array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9])</code></pre>



<p class="has-medium-font-size">從以下範例中，可以得知x參數為要處理的1維序列資料 ; q為分級的設定 ; label如果為 False，則返回整數分級。<br>輸出結果將 1-20 的序列分成 10 等份，注意分級從 0 開始。</p>



<p class="has-medium-font-size">認識了qcut以後，我們要將該函數應用到FinLab的資料格式，就可以得到財務指標分級資料。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">with data.universe(market='TSE_OTC'):
    df = data.get('fundamental_features:ROE稅後')
    rank_df = df.T
    for date in rank_df.columns:
        # 方便認知，將0~9變1~10
        rank_df[date] = pd.qcut(rank_df[date].rank(method='first'), 10, labels=False)+1
    rank_df = rank_df.T</code></pre>



<h2></h2>



<h2>方法二、程式簡化</h2>



<p class="has-medium-font-size">但 qcut 函數有個缺點是他不適用 DataFrame 資料型態，只能用在序列，所以我們要一行行操作再組裝，程式碼變的瑣碎。<br>其實我們能用 <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rank.html" target="_blank" rel="noopener">rank</a> 函數直接去做簡化，運用函數的axis參數做整列分級運數，pct參數將數值轉成百分位數以方便之後使用 <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.mul.html" target="_blank" rel="noopener">mul</a> 方法將數據乘上欲分級距，得數值後再將數據全+1，讓最小分級從1開始，而不是0開始，最後使用「無條件捨去法 (np.floor)」取得級距值。<br>最後由於排第一名的會跑到多出來的 rank (因為只有他是滿分 1 分)，所以要加上<a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.clip.html" target="_blank" rel="noopener">clip</a>去限縮分數上限，ex:若用10等分級距，第一名會是11分，用clip讓數據限縮到10分內。<br>程式範例如下：</p>



<pre class="wp-block-code"><code lang="python" class="language-python">with data.universe(market='TSE_OTC'):
    rank_df = (data.get('fundamental_features:ROE稅後')
        .rank(axis=1, pct=True, ascending=True)
        .mul(10)
        .add(1)
        .apply(np.floor).clip(0,10))</code></pre>



<p class="has-medium-font-size">程式是不是變乾淨很多呢？</p>



<h3>函數封裝</h3>



<pre class="wp-block-code"><code lang="python" class="language-python">def qcut_feature(data_name='fundamental_features:ROE稅後', q_range=10, ascending=True):
    import numpy as np

    rank_df = (data.get(data_name)
               .rank(axis=1, pct=True, ascending=ascending)
               .mul(q_range)
               .add(1)
               .apply(np.floor).clip(0,q_range))
    return rank_df
</code></pre>



<p class="has-medium-font-size"><br><br>參數說明如下：</p>



<ul class="has-medium-font-size"><li>data_name：設定要處理的FinLab資料庫中的財務指標。</li><li>q_range：變數設定分割的級距。</li><li>ascending：的用途在有些指標我們希望數值越高則分數越高，如ROE ; 有些指標我們希望數值越低則分數越高，如負債比率，所以用 ascending 來控制指標升降屬性，ascending=False時，會反轉序列排序，產生數值越低則分數越高的作用。</li></ul>



<p class="has-medium-font-size">來執行程式並來檢視一下數據吧！可以發現護國神山的 ROE 長年都在前段班。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">roe_score = qcut_feature(data_name='fundamental_features:ROE稅後')
roe_score['2330'].plot()</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" width="384" height="262" src="https://www.finlab.tw/wp-content/uploads/2022/09/下載.png" alt="下載" class="wp-image-4595" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/下載.png 384w, https://www.finlab.tw/wp-content/uploads/2022/09/下載-300x205.png 300w" sizes="(max-width: 384px) 100vw, 384px" title="如何用指標計分來選股? | Python 資料分級處理 6"></figure>



<h2>策略開發</h2>



<pre class="wp-block-code"><code lang="python" class="language-python">from finlab import data
from finlab.backtest import sim


def qcut_feature(data_name='fundamental_features:ROE稅後', q_range=10, ascending=True):
    import numpy as np

    rank_df = (data.get(data_name)
               .rank(axis=1, pct=True, ascending=ascending)
               .mul(q_range
               .add(1)
               .apply(np.floor).clip(0,q_range))
    return rank_df


with data.universe(market='TSE_OTC'):
    # 預想越高越好
    roe_score = qcut_feature(data_name='fundamental_features:ROE稅後')
    營業毛利率_score = qcut_feature('fundamental_features:營業毛利率')
    稅前淨利年增率_score = qcut_feature('fundamental_features:稅前淨利成長率')
    應收帳款週轉率_score = qcut_feature('fundamental_features:應收帳款週轉率')

    # 預想越低越好
    負債比率_score = qcut_feature('fundamental_features:負債比率',ascending=False)

    all_score = roe_score + 營業毛利率_score + 稅前淨利年增率_score + 應收帳款週轉率_score + 負債比率_score 

    # 設定總分要求
    position = all_score &gt;= 40
    report = sim(position,resample='M',position_limit=0.1,name="財務指標計分回測範例",upload=True)</code></pre>



<p class="has-medium-font-size">利用前述的qcut_feature函數，範例用5個財報指標來計分，並將分數加總。<br>策略條件要求總分要高於40分，也就是平均一個分數要達8分以上，算是不低的要求。<br>如果只用此指標做月週期回測，結果如下，報酬率比大盤優一點點，每月檔數穩定會選到100檔上下。</p>



<figure class="wp-block-image size-full"><img loading="lazy" width="800" height="600" src="https://www.finlab.tw/wp-content/uploads/2022/09/newplot-15.png" alt="newplot 15" class="wp-image-4596" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/newplot-15.png 800w, https://www.finlab.tw/wp-content/uploads/2022/09/newplot-15-300x225.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/newplot-15-768x576.png 768w" sizes="(max-width: 800px) 100vw, 800px" title="如何用指標計分來選股? | Python 資料分級處理 7"></figure>



<h3>優化</h3>



<p class="has-medium-font-size">標的數量偏多，實際上我們資金有限，買不了那麼多，這時我們可以想想，同樣的財務指標總分下，越低價的股票是不是越有上漲的空間呢？</p>



<pre class="wp-block-code"><code lang="python" class="language-python">from finlab import data
from finlab.backtest import sim


def qcut_feature(data_name='fundamental_features:ROE稅後', q_range=10, ascending=True):
    import numpy as np

    rank_df = (data.get(data_name)
               .rank(axis=1, pct=True, ascending=ascending)
               .mul(q_range)
               .add(1)
               .apply(np.floor).clip(0,q_range))
    return rank_df


with data.universe(market='TSE_OTC'):
    # 預想越高越好
    roe_score = qcut_feature(data_name='fundamental_features:ROE稅後')
    營業毛利率_score = qcut_feature('fundamental_features:營業毛利率')
    稅前淨利年增率_score = qcut_feature('fundamental_features:稅前淨利成長率')
    應收帳款週轉率_score = qcut_feature('fundamental_features:應收帳款週轉率')

    # 預想越低越好
    負債比率_score = qcut_feature('fundamental_features:負債比率',ascending=False)

    all_score = roe_score + 營業毛利率_score + 稅前淨利年增率_score + 應收帳款週轉率_score + 負債比率_score 

    # 設定總分要求
    position = all_score &gt;= 40

    # 選前10低價股
    close = data.get('price:收盤價')
    position = (position*close).astype(float)
    position = position[position&gt;0].is_smallest(10)
    report = sim(position,resample='M',position_limit=0.1,name="財務指標計分回測範例",upload=True)</code></pre>



<p class="has-medium-font-size"><br>所以我們上面程式後段再從原先清單選出每期股價前 10 低的條件，則報酬率明顯拉出差距，也更貼近小資族的使用情境。</p>



<figure class="wp-block-image size-full"><img loading="lazy" width="800" height="600" src="https://www.finlab.tw/wp-content/uploads/2022/09/newplot-17.png" alt="newplot 17" class="wp-image-4597" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/newplot-17.png 800w, https://www.finlab.tw/wp-content/uploads/2022/09/newplot-17-300x225.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/newplot-17-768x576.png 768w" sizes="(max-width: 800px) 100vw, 800px" title="如何用指標計分來選股? | Python 資料分級處理 8"></figure>



<h2>結論</h2>



<p class="has-medium-font-size">qcut 是不是很好用呢？又認識一個pandas的新工具！<br>附上<a href="https://colab.research.google.com/drive/1zCyk-GeWtuswnMVvUTMN6n-rTFfIH2ph?usp=sharing" target="_blank" rel="noopener">colab範例檔</a>讓大家練習～來試試打造自己的指標計分策略吧！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/basic_score_strategy/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4593</post-id>	</item>
		<item>
		<title>突破策略豆知識 &#124; 如何避免假突破?</title>
		<link>https://www.finlab.tw/breakthrough_stock_picking_strategies/</link>
					<comments>https://www.finlab.tw/breakthrough_stock_picking_strategies/#respond</comments>
		
		<dc:creator><![CDATA[Ben]]></dc:creator>
		<pubDate>Thu, 22 Sep 2022 05:13:35 +0000</pubDate>
				<category><![CDATA[技術面]]></category>
		<category><![CDATA[FinLab 量化平台]]></category>
		<category><![CDATA[VIP文章]]></category>
		<category><![CDATA[投資新手]]></category>
		<category><![CDATA[股票策略]]></category>
		<category><![CDATA[選股策略]]></category>
		<category><![CDATA[FinlabDataframe]]></category>
		<category><![CDATA[創新高]]></category>
		<category><![CDATA[動能]]></category>
		<category><![CDATA[策略開發豆知識]]></category>
		<guid isPermaLink="false">https://www.finlab.tw/?p=4452</guid>

					<description><![CDATA[動能策略很常用到「股價創新高」這個條件，所謂的動能策略簡單說就是追突破，通說認為創新高的股票比較少套牢壓力，上 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size">動能策略很常用到「股價創新高」這個條件，所謂的動能策略簡單說就是追突破，通說認為創新高的股票比較少套牢壓力，上漲的延續性較高。反之股價無創新高的股票，可能在上漲過程跌跌撞撞，一漲上去可能就被套牢賣壓打下來。<br>但股價創新高這麼廣為人知的突破策略真的有用嗎？趕緊來回測驗證一下</p>



<h2>股價創新高策略範例</h2>



<h3>程式碼</h3>



<pre class="wp-block-code"><code lang="python" class="language-python">from finlab.backtest import sim
from finlab import data

# 標的範圍為上市櫃普通股
with data.universe(market='TSE_OTC'):
    # 取得收盤價
    close = data.get("price:收盤價")
    # 股價創近200日新高
    position = (close == close.rolling(200).max())
    # 每兩週再平衡，單檔最大持股比例限制20%，停損20%
    report = sim(position, resample="2W", position_limit=0.2, stop_loss=0.2, name="股價創新高策略")
    report.display()</code></pre>



<h3>選股條件</h3>



<p class="has-medium-font-size">程式很簡單，這是一個偏短線的技術面策略，不用幾行就可以開發完成，只有了一個股價創新高條件和一些回測基本設定。<br>主要條件為:</p>



<ul class="has-medium-font-size"><li>標的範圍為上市櫃普通股</li><li>股價創近200日新高，天數越高，則壓力越小</li><li>每兩週再平衡</li><li>單檔最大持股比例限制20%，避免個股單壓的非系統性風險</li><li>停損20%</li></ul>



<h3>回測結果</h3>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="747" src="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.43.29-1024x747.png" alt="截圖 2022 09 22 下午12.43.29" class="wp-image-4456" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.43.29-1024x747.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.43.29-300x219.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.43.29-768x560.png 768w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.43.29-1536x1120.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.43.29.png 1890w" sizes="(max-width: 1024px) 100vw, 1024px" title="突破策略豆知識 | 如何避免假突破? 9"></figure>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="841" src="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.39.15-1024x841.png" alt="截圖 2022 09 22 下午12.39.15" class="wp-image-4455" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.39.15-1024x841.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.39.15-300x246.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.39.15-768x631.png 768w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.39.15.png 1394w" sizes="(max-width: 1024px) 100vw, 1024px" title="突破策略豆知識 | 如何避免假突破? 10"></figure>



<p class="has-medium-font-size">年化報酬率為17.7%，幾乎每年都獲利，至今只有兩個年份虧損，困難年份2008、2018、2022年的表現優於大盤，但困難年份 2011 表現不好。以報酬率表現來說，該因子表現不錯。<br>但夏普率和最大下跌和大盤風險差不多，並無明顯優勢。</p>



<h2>策略優化</h2>



<p class="has-medium-font-size">上述策略的缺點在只有近一天突破就買，你可能會「買到假突破的標的」、「錯過前幾天突破小拉回的好標地」。<br>一個動能夠強的標的，短期不會只有一天創新高，而是「<strong>連續挑戰新高</strong>」，創新高越多次，代表動能越強，這樣的訊號除了確認訊號強度，也可把前幾天有突破，近期小回檔的強勢股納進來，將時間條件更有彈性，比較不會錯過飆股，你時間定的越死，會很吃選股日當日訊號的運氣。</p>



<p class="has-medium-font-size">為了更確認訊號及囊括近期強勢標的，我會使用<a href="https://doc.finlab.tw/reference/dataframe/#finlab.dataframe.FinlabDataFrame.sustain" data-type="URL" data-id="https://doc.finlab.tw/reference/dataframe/#finlab.dataframe.FinlabDataFrame.sustain" target="_blank" rel="noopener">FinlabDataFrame的語法糖 sustain</a> 去判斷訊號強度延續性。</p>



<h3>程式範例</h3>



<pre class="wp-block-code"><code lang="python" class="language-python">from finlab.backtest import sim
from finlab import data
with data.universe(market='TSE_OTC'):
    close = data.get("price:收盤價")
    # 近5日內有3日以上的股價創前200日新高
    position = (close == close.rolling(200).max()).sustain(5,3)
    report = sim(position, resample="2W", position_limit=0.2, stop_loss=0.2, name="創新高延續動能策略")
    report.display()</code></pre>



<p class="has-medium-font-size"><br></p>



<h3>選股條件</h3>



<p class="has-medium-font-size">只將創新高條件加入強度延續性判斷，為公平比較，其餘條件相同。</p>



<ul class="has-medium-font-size"><li>標的範圍為上市櫃普通股</li><li>近5日內有3日以上（含）的股價創前200日新高。</li><li>每兩週再平衡</li><li>單檔最大持股比例限制20%，避免個股單壓的非系統性風險</li><li>停損20%</li></ul>



<h3>回測結果</h3>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="705" src="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.07-1024x705.png" alt="截圖 2022 09 22 下午12.59.07" class="wp-image-4457" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.07-1024x705.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.07-300x206.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.07-768x529.png 768w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.07-1536x1057.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.07.png 1601w" sizes="(max-width: 1024px) 100vw, 1024px" title="突破策略豆知識 | 如何避免假突破? 11"></figure>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="791" src="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.49-1024x791.png" alt="截圖 2022 09 22 下午12.59.49" class="wp-image-4458" srcset="https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.49-1024x791.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.49-300x232.png 300w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.49-768x593.png 768w, https://www.finlab.tw/wp-content/uploads/2022/09/截圖-2022-09-22-下午12.59.49.png 1391w" sizes="(max-width: 1024px) 100vw, 1024px" title="突破策略豆知識 | 如何避免假突破? 12"></figure>



<p class="has-medium-font-size">年化報酬率為25.9%，比原先的17.7%大幅上升，幾乎每年都獲利，困難年份2008、2011、2018、2022年的表現優於大盤。<br>夏普率明顯提升到1.1，最大下跌和下跌幅度曲線比原先收斂不少，長時間都能比大盤有更小的回檔風險。</p>



<h2>結論</h2>



<p class="has-medium-font-size">經過小小的觀念和語法調整，卻能讓策略大幅優化！是不是很神奇呢？<br>如果你的策略也有用到「股價創新高的條件」之類的突破策略，可以試試加入 sustain 語法，看看會不會有優化的效果。甚至將 sustain 語法應用到其他條件。<br></p>



<h3>相關資料</h3>



<ul class="has-medium-font-size"><li><a href="https://colab.research.google.com/drive/1M0XxnAMZoqoOrJQP9dyJVer5Q7YyRFOA?usp=sharing" target="_blank" rel="noopener">colab 程式範例檔</a></li></ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/breakthrough_stock_picking_strategies/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4452</post-id>	</item>
	</channel>
</rss>
