<?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%9c%8b%e5%ae%89%e5%9f%ba%e9%87%91/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Mon, 17 Oct 2022 05:48:21 +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/treasury_stock_national_security_fund/</link>
					<comments>https://www.finlab.tw/treasury_stock_national_security_fund/#respond</comments>
		
		<dc:creator><![CDATA[Ben]]></dc:creator>
		<pubDate>Mon, 17 Oct 2022 05:37:42 +0000</pubDate>
				<category><![CDATA[籌碼面]]></category>
		<category><![CDATA[FinLab 量化平台]]></category>
		<category><![CDATA[Python新手教學]]></category>
		<category><![CDATA[VIP文章]]></category>
		<category><![CDATA[pandas]]></category>
		<category><![CDATA[Plotly]]></category>
		<category><![CDATA[國安基金]]></category>
		<category><![CDATA[庫藏股]]></category>
		<guid isPermaLink="false">https://www.finlab.tw/?p=4684</guid>

					<description><![CDATA[2022 可說是亂世風雲，中國防疫政策、俄烏戰爭、通膨、美國大幅升息、企業庫存暴增...等等利空連發重挫市場，許多市場指數到 2022年10月都回檔30%，台股也在護國神山走山下，從 18000 點下殺到 13000 點以下，明顯跌入熊市。
令人不安的是救市神主牌國安基金在 2022/7/12 已進場護盤，卻仍止不住跌勢，被質疑是否過早進場送頭？如果連最後一張王牌都無法扛住，那確實麻煩大了。
本篇文章將用 Finlab 模組 與 Pandas 實作 國安基金與庫藏股 的簡易趨勢分析，讓你更加熟悉 Pandas 各種財經時序資料處理的手法。最後輸出的結果，我們會看到國安基金這次是否和以往不一樣？]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size">2022 可說是亂世風雲，中國防疫政策、俄烏戰爭、通膨、美國大幅升息、企業庫存暴增&#8230;等等利空連發重挫市場，許多市場指數到 2022年10月都回檔30%，台股也在護國神山走山下，從 18000 點下殺到 13000 點以下，明顯跌入熊市。<br>令人不安的是救市神主牌國安基金在 2022/7/12 已進場護盤，卻仍止不住跌勢，被質疑是否過早進場送頭？如果連最後一張王牌都無法扛住，那確實麻煩大了。<br>本篇文章將用 Finlab 模組 與 Pandas 實作 <strong>國安基金與庫藏股</strong> 的簡易趨勢分析，讓你更加熟悉 Pandas 各種財經時序資料處理的手法。最後輸出的結果，我們會看到國安基金這次是否和以往不一樣？</p>



<h2>庫藏股資料處理</h2>



<p class="has-medium-font-size">透過FinLab資料庫，我們可取得庫藏股資訊，在之前的「<a href="https://www.finlab.tw/treasury-stock-signal/">庫藏股實施家數｜崩盤後的長線抄底訊號｜左側交易</a>」文章中有相關實作，不過上篇是用「董事會決議庫藏股的日期」當訊號，這次換示範另一種寫法，使用「庫藏股實施期間」來當訊號，更能精準反應當下有在護盤的公司大概的總家數。</p>



<h3>程式範例</h3>



<pre class="wp-block-code"><code lang="python" class="language-python">import pandas as pd
from finlab import data

預定買回期間_起 = data.get('treasury_stock:預定買回期間-起').unstack()
預定買回期間_迄 = data.get('treasury_stock:預定買回期間-迄').unstack()
# date 為董事會決議庫藏股日期
treasury_stock = pd.DataFrame({'預定買回期間_起':預定買回期間_起, '預定買回期間_迄':預定買回期間_迄}).dropna(how='all')
treasury_stock.index.names = ['stock_id', 'date']

# 產生實施期間序列
treasury_stock['period'] = [pd.date_range(s,e).to_list() for s,e in zip(treasury_stock['預定買回期間_起'],treasury_stock['預定買回期間_迄'])] 

# 使用 explode 展開資料
treasury_stock_period = treasury_stock.explode('period')
treasury_stock_period = treasury_stock_period[['period']]

# 計算每日預計實施庫藏股的公司家數
treasury_count = treasury_stock_period.groupby(['period'])['period'].count()
benchmark = data.get('benchmark_return:發行量加權股價報酬指數')

# 日期索引對應加權大盤報酬指數
treasury_count = treasury_count.reindex(benchmark.index).fillna(0)
treasury_count.plot()</code></pre>



<h3>程式說明</h3>



<ul class="has-medium-font-size"><li>組裝資料<br>將「預定買回期間起迄」兩份資料使用 <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.unstack.html" target="_blank" rel="noopener">unstack</a> 方法 將格式從 pivot 轉換成 Series，再組裝成 DataFrame。</li><li>產生庫藏股實施日日期序列<br>將開始與結束日期傳入 <a href="https://pandas.pydata.org/docs/reference/api/pandas.date_range.html" target="_blank" rel="noopener">date_range</a> 方法，得到日期序列。</li><li>統計每日預計實施庫藏股的公司家數<br>在使用 <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html" target="_blank" rel="noopener">groupby </a>與 count 統計每日總家數前，要先使用 <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html" target="_blank" rel="noopener">explode</a> 方法展開上一個步驟得到的日期資料。</li><li>日期索引重置<br>為了等一下畫圖顯示趨勢，將庫藏股家數趨勢的日期對應加權大盤報酬指數，讓兩條序列長度一致，方便呈現。</li><li>簡易繪圖檢視<br>最後使用dataframe內建的 plot 方法，呈現剛剛整理的數據<br><img width="375" height="252" class="wp-image-4685" style="width: 150px;" src="https://www.finlab.tw/wp-content/uploads/2022/10/庫存股簡易趨勢.png" alt="庫存股簡易趨勢" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/庫存股簡易趨勢.png 375w, https://www.finlab.tw/wp-content/uploads/2022/10/庫存股簡易趨勢-300x202.png 300w" sizes="(max-width: 375px) 100vw, 375px" title="國安基金與庫藏股應用教學｜政府軍急了嗎？ 1"><br></li></ul>



<h2>取得國安基金資料</h2>



<p class="has-medium-font-size">現在 FinLab 的資料庫可以取得國安基金的進出場基本資訊了，詳見<a href="https://ai.finlab.tw/database/#national_security_fund" target="_blank" rel="noopener">資料庫</a>說明。<br>簡單一行就能取出囉 <code>data.get('national_security_fund')</code><br>目前國安基金共啟動8次。</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="520" src="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-17-上午11.44.27-1024x520.png" alt="截圖 2022 10 17 上午11.44.27" class="wp-image-4686" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-17-上午11.44.27-1024x520.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-17-上午11.44.27-300x152.png 300w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-17-上午11.44.27-768x390.png 768w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-17-上午11.44.27-1536x780.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/10/截圖-2022-10-17-上午11.44.27.png 1585w" sizes="(max-width: 1024px) 100vw, 1024px" title="國安基金與庫藏股應用教學｜政府軍急了嗎？ 2"></figure>



<h2>趨勢繪圖</h2>



<p class="has-medium-font-size">將國安基金實施期間與庫藏股趨勢疊合，比較兩者訊號時間點。</p>



<h3>程式範例</h3>



<pre class="wp-block-code"><code lang="python" class="language-python">import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Bar(x=treasury_count.index, y=treasury_count.values, marker_color='navy',name="庫藏股實施家數"),
    secondary_y=False,
)

fig.update_traces(marker_color = 'rgba(0,0,250, 0.5)',
                  marker_line_width = 0,
                  selector=dict(type="bar"))

# 大盤指數
fig.add_trace(
    go.Scatter(x=benchmark.index, y=benchmark['發行量加權股價報酬指數'],line = dict(color = '#2C191B'), name="發行量加權股價報酬指數"),
    secondary_y=True,
)

# 國安基金實施期間
for i,row in national_security_fund.iterrows():
    fig.add_vrect(x0=row['啟動時間'], x1=row['退場時間'], 
                  annotation_text=row['時空背景'], annotation_position="top left",
                  fillcolor="green", opacity=0.25, line_width=0)
    
# Add figure title
fig.update_layout(
        title={
            'text': "庫藏股與國安基金護盤趨勢",
            'x': 0.49,
            'y': 0.9,
            'xanchor': 'center',
            'yanchor': 'top'},
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.02,
            xanchor="right",
            x=0.3
        )
)

# Set x-axis title
fig.update_xaxes(title_text="date",
                 rangeselector=dict(
                     buttons=list([
                         dict(count=1,
                              label="1y",
                              step="year",
                              stepmode="backward"),
                         dict(count=3,
                              label="3y",
                              step="year",
                              stepmode="backward"),
                         dict(count=5,
                              label="5y",
                              step="year",
                              stepmode="backward"),
                         dict(count=10,
                              label="10y",
                              step="year",
                              stepmode="backward"),
                         dict(step="all")
                     ])
                 ),
                 rangeslider=dict(
                     visible=True
                 ),
                 type="date")



# Set y-axes titles
fig.update_yaxes(title_text="&lt;b>count&lt;/b>", secondary_y=False)
fig.update_yaxes(title_text="&lt;b>benchmark&lt;/b>", secondary_y=True,showgrid=False)
fig.show()</code></pre>



<h2>救市趨勢分析</h2>



<h3>政府救市三步</h3>



<p class="has-medium-font-size">通常政府救市的步驟如下</p>



<p class="has-medium-font-size">1.鼓勵企業實施庫藏股</p>



<p class="has-medium-font-size">2.限空令 or 找法人喝咖啡</p>



<p class="has-medium-font-size">3.國安感冒糖漿(國家金融安定基金)</p>



<p class="has-medium-font-size">庫藏股對政府最省事，因為不用自己出一毛錢就可能有效果，當然別人先動手最好。<br>企業內部人往往對自家公司價值最了解，有些又有質押股票面臨市值下修的壓力。一般來說，庫藏股這類回燒公司資金的事，經營者多會考量價格是否合理(進入低檔)才買入。<br></p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="418" src="https://www.finlab.tw/wp-content/uploads/2022/10/newplot-18-1024x418.png" alt="newplot 18" class="wp-image-4688" srcset="https://www.finlab.tw/wp-content/uploads/2022/10/newplot-18-1024x418.png 1024w, https://www.finlab.tw/wp-content/uploads/2022/10/newplot-18-300x122.png 300w, https://www.finlab.tw/wp-content/uploads/2022/10/newplot-18-768x313.png 768w, https://www.finlab.tw/wp-content/uploads/2022/10/newplot-18-1536x627.png 1536w, https://www.finlab.tw/wp-content/uploads/2022/10/newplot-18.png 1960w" sizes="(max-width: 1024px) 100vw, 1024px" title="國安基金與庫藏股應用教學｜政府軍急了嗎？ 3"></figure>



<p class="has-medium-font-size"><br><strong>從圖表發現，前幾次國安基金進場前，庫藏股家數(藍柱)都會先明顯變多，內部人護盤也撐不住了，國家隊才會進場。</strong><br>但這次很不一樣，雖然政府在7月初已經 <a href="https://www.gvm.com.tw/article/94817" target="_blank" rel="noopener">鼓勵企業實施庫藏股</a>，卻沒啥公司響應，於是殺盤持續。即使現在跌多，庫藏股仍靜悄悄，是不是股價不到公司派目標價 (前幾年噴太多)？還是公司派看到未來景氣風險，要保留資金過冬？ <br>還沒等到公司派進場，政府軍這次等不及了，先啟動國安基金，發現還是抵擋不住殺盤，才再祭出「<a href="https://www.storm.mg/lifestyle/4560341" target="_blank" rel="noopener">限空令</a>」，SOP似乎和以前不一樣，就怕大招放完後仍抵擋不住，這次沒庫藏股先擋第一線，要自己多扛一點。</p>



<p class="has-medium-font-size">如果政府招式出盡後，還是繼續殺呢？那很可能國安基金這個六連勝的底部訊號也會失效，讓我們繼續看下去 ~ </p>



<p class="has-medium-font-size">下一篇來教大家怎麼將庫藏股和國安基金進出場化作大盤風險濾網加入策略。</p>



<h2>程式範例檔</h2>



<p class="has-medium-font-size"><a href="https://drive.google.com/file/d/1MIVKDhqjnWvjufa7I_VEbTSarX2ips0P/view?usp=sharing" target="_blank" rel="noopener">colab連結</a><br><br>註冊 FinLab 量化平台 VIP 才能取得全時段數據喔。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/treasury_stock_national_security_fund/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4684</post-id>	</item>
	</channel>
</rss>
