<?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/%E5%BF%AB%E6%A8%82%E8%A1%A8/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Mon, 28 Nov 2022 09:53: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>選股回測系統豆知識 (2)｜持股比例上限設定</title>
		<link>https://www.finlab.tw/backtest_system_position_limit/</link>
					<comments>https://www.finlab.tw/backtest_system_position_limit/#respond</comments>
		
		<dc:creator><![CDATA[Ben]]></dc:creator>
		<pubDate>Mon, 28 Nov 2022 09:43:14 +0000</pubDate>
				<category><![CDATA[Python新手教學]]></category>
		<category><![CDATA[FinLab 量化平台]]></category>
		<category><![CDATA[選股策略]]></category>
		<category><![CDATA[創新高]]></category>
		<category><![CDATA[回測]]></category>
		<category><![CDATA[快樂表]]></category>
		<guid isPermaLink="false">https://www.finlab.tw/?p=4940</guid>

					<description><![CDATA[策略回測最怕測到快樂表，什麼是快樂表？簡而言之就是紙上談兵的表現嚇嚇叫，但實際操作時卻很難貼合回測，通常這類情況多是流動性風險問題，而另一種快樂表就是回測數據的解讀錯誤，像是看到超高年化報酬率就很嗨，卻沒注意到在勝率普通的情況下，高報酬是不是剛好靠幾檔交易重壓而生成？若真是如此，那運氣成分可能佔比很大。
本文會利用一些簡單的技巧去避免統計的陷阱，讓你注意到策略中因持股重壓導致的失真問題。]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size">策略回測最怕測到快樂表，什麼是快樂表？簡而言之就是紙上談兵的表現嚇嚇叫，但實際操作時卻很難貼合回測，通常這類情況多是<a href="https://www.finlab.tw/customized_liquidityanalysis/">流動性風險</a>問題，而另一種快樂表就是回測數據的解讀錯誤，像是看到超高年化報酬率就很嗨，卻沒注意到在勝率普通的情況下，高報酬是不是剛好靠幾檔交易重壓而生成？若真是如此，那運氣成分可能佔比很大。<br>本文會利用一些簡單的技巧去避免統計的陷阱，讓你注意到策略中因持股重壓導致的失真問題。</p>



<h2>持股檔數設定</h2>



<p class="has-medium-font-size">小資族的資金少，選股策略在初步開發完後第一個問題是選到檔數太多，以之前介紹過的創新高延續動能策略為例，可能某幾期選股在大牛市高達百檔，但實務上根本不可能買那麼多。</p>



<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", stop_loss=0.2, name="創新高延續動能策略", upload=False)
    report.display()</code></pre>



<figure class="wp-block-image size-full"><img width="800" height="600" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-40.png" alt="newplot 40" class="wp-image-4941" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-40.png 800w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-40-300x225.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-40-768x576.png 768w" sizes="(max-width: 800px) 100vw, 800px" title="選股回測系統豆知識 (2)｜持股比例上限設定 1"><figcaption>創新高延續動能策略原型</figcaption></figure>



<p class="has-medium-font-size">此時我們可以針對小資族去客製化務實的條件，每期選股價前10低的標的來當持股組合。回測結果發現報酬率降低一些，但持股檔數上限控制在10檔以內，且低價的優先，讓小資族更能參與整張現股的操作，避免零股交易較高的滑價風險，讓回測更貼近現實交易貼合的可能性。</p>



<pre class="wp-block-code"><code lang="python" class="language-python">with data.universe(market='TSE_OTC'):
    close = data.get("price:收盤價")
    # 近5日內有3日以上的股價創前200日新高
    position = (close == close.rolling(200).max()).sustain(5,3)
    position *= close
    position = position[position &gt; 0].is_smallest(10)
    report = sim(position, resample="2W", name="創新高延續動能策略", upload=False)
    report.display()</code></pre>



<figure class="wp-block-image size-full"><img loading="lazy" width="800" height="600" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-42.png" alt="newplot 42" class="wp-image-4942" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-42.png 800w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-42-300x225.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-42-768x576.png 768w" sizes="(max-width: 800px) 100vw, 800px" title="選股回測系統豆知識 (2)｜持股比例上限設定 2"><figcaption>低價股優先測定持股檔數上限</figcaption></figure>



<h2>單檔持股比例上限</h2>



<p class="has-medium-font-size">以為這樣就沒問題了嗎？你若仔細看上面「低價股優先測定持股檔數上限」的策略，會發現每期的標的數量仍參差不定，牛市有時候10檔，熊市有時候1檔，由於 position 預設分配持股比例是用檔數做平均，因此若當期只選到一隻，而沒在 <a href="https://doc.finlab.tw/reference/backtest/#finlab.backtest.sim" target="_blank" rel="noopener">sim 回測函數</a> 內特別設定 position_limit，那就是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/11/newplot-41.png" alt="newplot 41" class="wp-image-4943" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-41.png 800w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-41-300x225.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-41-768x576.png 768w" sizes="(max-width: 800px) 100vw, 800px" title="選股回測系統豆知識 (2)｜持股比例上限設定 3"></figure>



<p class="has-medium-font-size">如何解決此問題呢？<a href="https://doc.finlab.tw/reference/backtest/#finlab.backtest.sim" target="_blank" rel="noopener">sim 回測函數</a> 內設定 position_limit 當單檔標的持股比例上限，控制倉位風險。預設為None，也就是不設上限。範例：0.1，代表單檔標的最多持有 10 % 部位，若加上持股檔數上限10檔，當只選到8檔時，總持股上限則為80%，剩下 20% 為現金，在選到檔數較少的時候，自然產生空手避險的效果，而不重壓標地。<br>position_limit 越高一般來說波動越大 ; position_limit  越低則波動低，報酬率與最大回撤普遍都會縮小，站在風控角度，一般建議都要設 position_limit &lt; 0.2，避免個股非系統性風險影響整體策略。<br>「<a href="https://www.finlab.tw/breakthrough_stock_picking_strategies/">創新高延續動能策略</a>」的 position_limit 要用多少比較好？我們可以用 ReportCollection 來檢測不同數值的回測效果，程式如下：</p>



<pre class="wp-block-code"><code lang="python" class="language-python">def run_strategy(position_limit):
    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=position_limit, stop_loss=0.2, name="創新高延續動能策略", upload=False)
        return report


# # 產生回測組合
reports = {ind/10: run_strategy(ind/10) for ind in range(1,11)}
# 放入回測報告組合比較器
collection = ReportCollection(reports)
# 產生回測數據分群柱狀圖
collection.plot_creturns().show()

import plotly.graph_objects as go

def show_index_bar(collection, item = 'max_drawdown'):
    stats = collection.get_stats()
    df = round(stats.loc[item],2).sort_values()
    fig = go.Figure([go.Bar(x=df.index, y=df.values, text=df.values, textposition='auto',)])
    fig.update_layout(title_text=item)
    return fig

# 年化報酬
show_index_bar(collection, item = 'daily_mean').show()
show_index_bar(collection, item = 'max_drawdown').show()
show_index_bar(collection, item = 'daily_sharpe').show()</code></pre>



<h2>回測分析</h2>



<p class="has-medium-font-size">可以發現 position_limit  = 0.2 時 的年化報酬率、夏普率、MDD (最大回撤) 表現最好，能有效降低重壓波動風險。如果做出來發現 position_limit  = 1 的數據最好，那就要留意快樂表的可能性，可能好績效靠是重壓導致的，這時最好考慮低 position_limit 的情況</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="271" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-43-1024x271.png" alt="newplot 43" class="wp-image-4945" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-43-1024x271.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-43-300x79.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-43-768x203.png 768w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-43-1536x407.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-43.png 1982w" sizes="(max-width: 1024px) 100vw, 1024px" title="選股回測系統豆知識 (2)｜持股比例上限設定 4"></figure>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="271" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-44-1024x271.png" alt="newplot 44" class="wp-image-4948" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-44-1024x271.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-44-300x79.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-44-768x203.png 768w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-44-1536x407.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-44.png 1982w" sizes="(max-width: 1024px) 100vw, 1024px" title="選股回測系統豆知識 (2)｜持股比例上限設定 5"></figure>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="271" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-46-1024x271.png" alt="newplot 46" class="wp-image-4947" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-46-1024x271.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-46-300x79.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-46-768x203.png 768w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-46-1536x407.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-46.png 1982w" sizes="(max-width: 1024px) 100vw, 1024px" title="選股回測系統豆知識 (2)｜持股比例上限設定 6"></figure>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="271" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-45-1-1024x271.png" alt="newplot 45 1" class="wp-image-4950" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-45-1-1024x271.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-45-1-300x79.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-45-1-768x203.png 768w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-45-1-1536x407.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-45-1.png 1982w" sizes="(max-width: 1024px) 100vw, 1024px" title="選股回測系統豆知識 (2)｜持股比例上限設定 7"></figure>



<figure class="wp-block-image size-full"><img loading="lazy" width="800" height="600" src="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-48.png" alt="newplot 48" class="wp-image-4953" srcset="https://www.finlab.tw/wp-content/uploads/2022/11/newplot-48.png 800w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-48-300x225.png 300w, https://www.finlab.tw/wp-content/uploads/2022/11/newplot-48-768x576.png 768w" sizes="(max-width: 800px) 100vw, 800px" title="選股回測系統豆知識 (2)｜持股比例上限設定 8"><figcaption>position_limit = 0.2 的回測情況 </figcaption></figure>



<h2>結論</h2>



<p class="has-medium-font-size"><a href="https://colab.research.google.com/drive/11xHowJGeHmhpNCbCjOPfcsBI5yT6ZsOq?usp=sharing" target="_blank" rel="noopener">colab 範例檔</a><br>你的策略有驗證過持股檔數與單檔持股上限的問題嗎？<br>調整一個  position_limit  參數 就能降低快樂表和大波動的風險，可說是新手必學的參數設定啊！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/backtest_system_position_limit/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4940</post-id>	</item>
	</channel>
</rss>
