<?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/%E8%82%A1%E5%83%B9%E9%A0%90%E6%B8%AC/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Sun, 26 Jul 2020 12:08:25 +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>超簡單用Python預測股價</title>
		<link>https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae-machine-learning-%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/</link>
					<comments>https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae-machine-learning-%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:38 +0000</pubDate>
				<category><![CDATA[財經PYTHON教學]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[程式交易]]></category>
		<category><![CDATA[股價預測]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=735</guid>

					<description><![CDATA[多虧了python，用最先進的統計模型來預測股價，程式交易超簡單！不用安裝程式，今天我們雲端寫code，適合完完全全的初學者！非常多非常多的人都用 python 在開發程式，因此，有很多高深數學軟體，都會有很多民間高手來實做，而我們只要會用就好了！學python從這篇開始！]]></description>
										<content:encoded><![CDATA[
<p>多虧了python，用最先進的統計模型來預測股價，程式交易超簡單！<br>不用安裝程式，今天我們雲端寫code，適合完完全全的初學者！<br>非常多非常多的人都用 python 在開發程式，<br>因此，有很多高深數學軟體，都會有很多民間高手來實做，<br>而我們只要會用就好了！學python從這篇開始！<br><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j4.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img width="770" height="498" src="http://34.96.136.135/wp-content/uploads/2020/07/j4.png" alt="j4" class="wp-image-736" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j4.png 770w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-300x194.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-768x497.png 768w" sizes="(max-width: 770px) 100vw, 770px" title="超簡單用Python預測股價 1"></figure></div>



<p>我們還是來稍微科普一下相關的程式交易知識好了</p>



<h3 id="什麼是model（模型）">什麼是model（模型）</h3>



<p>模型就是一種能夠預測真實資料的數學公式，<br>通常會是一個function，例如 f 好了，<br>假如我們有一條時間序列 f(t) = at + b<br>只要知道了變數 a 跟 b，就可以計算出 t 每一刻的 f(t) 是多少</p>



<p>要怎麼知道 a 跟 b 是多少？<br>只要看其中兩個歷史時刻的位置即可：例如我們知道 f(1) = 1 且 f(2) = 2<br>則我們可以推算出 a = 1 且 b = 0，<br>並且預知 f(3) = 3 、 f(4) = 4 、 f(5) = 5 …</p>



<p>股價模型也是一樣，但是這個模型會比較複雜，不會只有 a、b 而已<br>不過規則是一樣的，我們要用歷史股價去計算類似 a、b 的變數，通常稱之為 fitting<br>找到 a、b 後就能夠用模型來預測股價了！</p>



<h3 id="用什麼模型預測股價呢？">用什麼模型預測股價呢？</h3>



<p>Facebook 最近推出了一個新型的時間序列預測模型Prophet，中文叫作預言家，<br>預言家可以預測一些週期性的時間序列<br>例如下圖，一個FB每天PO文的數量圖，隨時間關係圖：<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp1.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="947" height="491" src="http://34.96.136.135/wp-content/uploads/2020/07/pp1.png" alt="pp1" class="wp-image-737" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp1.png 947w, https://www.finlab.tw/wp-content/uploads/2020/07/pp1-300x156.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp1-768x398.png 768w" sizes="(max-width: 947px) 100vw, 947px" title="超簡單用Python預測股價 2"></figure></div>



<p>截圖自<a href="https://peerj.com/preprints/3190.pdf" rel="noreferrer noopener" target="_blank">fb論文</a></p>



<p>圖中，每個時間都只會對應到一個點，就是每天的PO文數量<br>我們可以觀察到一些現象：</p>



<h3 id="以每週來檢視">以每週來檢視</h3>



<p>上圖中，顏色的不同代表每週的變化量，禮拜六看起來明顯就比較少人用FB，<br>（週末都跑去玩了？XD）</p>



<h3 id="以每年來檢視">以每年來檢視</h3>



<p>我們也可以發現，以年度來說，暑假時PO文數會下降<br>（大家都去放暑假了？）</p>



<p>每個週期下都有各自的規律，傳統的模型能夠成功預測嗎？</p>



<h3 id="傳統模型的限制">傳統模型的限制</h3>



<p>其實已經有很多模型了，但為什麼FB還要推出一個新的呢？<br>原因是，以前的模型並沒有辦法多週期性的預測 f(t) ：<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp2.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="773" height="671" src="http://34.96.136.135/wp-content/uploads/2020/07/pp2.png" alt="pp2" class="wp-image-738" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp2.png 773w, https://www.finlab.tw/wp-content/uploads/2020/07/pp2-300x260.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp2-768x667.png 768w" sizes="(max-width: 773px) 100vw, 773px" title="超簡單用Python預測股價 3"></figure></div>



<p>這張圖的每個row是不同的模型<br>圖中每條「實線」就是預測模型的結果，<br>可以看出以往的模型，預測出來結果並不是很準</p>



<h3 id="預言家模型">預言家模型</h3>



<p>下圖中，可以發現預言家模型比上述的模型預測的更準確<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp3.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="946" height="373" src="http://34.96.136.135/wp-content/uploads/2020/07/pp3.png" alt="pp3" class="wp-image-739" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp3.png 946w, https://www.finlab.tw/wp-content/uploads/2020/07/pp3-300x118.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp3-768x303.png 768w" sizes="(max-width: 946px) 100vw, 946px" title="超簡單用Python預測股價 4"></figure></div>



<p>預言家模型最厲害的地方，<br>就是用多條 Fourier Series，去擬合（fitting）時間序列<br>在這邊就不把可怕的公式放上來了，<br>有興趣可以參考&nbsp;<a href="https://peerj.com/preprints/3190.pdf" rel="noreferrer noopener" target="_blank">fb論文</a><br>可以將週期調整至「每年」「每季」「每週」！<br>模型簡單可以寫成這樣：</p>



<p>f(t) = year(t) + season(t) + week(t) + trend(t)</p>



<p>甚至在fitting完（找到a、b等參數後）可以將 f(t) 的成分拆解出來看：<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp5.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="950" height="741" src="http://34.96.136.135/wp-content/uploads/2020/07/pp5.png" alt="pp5" class="wp-image-740" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp5.png 950w, https://www.finlab.tw/wp-content/uploads/2020/07/pp5-300x234.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp5-768x599.png 768w" sizes="(max-width: 950px) 100vw, 950px" title="超簡單用Python預測股價 5"></figure></div>



<p>而最後的 f(t) 就是這些曲線的疊加，<br>由上圖第三張，我們可以額外發現，聖誕節的時候，使用FB的人數下降非常快<br>外國的聖誕節，就是與家人團聚的時光，（像是我們的農曆新年）<br>所以不太會用手機的緣故</p>



<p>這篇論文大意就講到這裡，<br>詳情可以參考：<a href="https://peerj.com/preprints/3190.pdf" rel="noreferrer noopener" target="_blank">此論文</a></p>



<h3 id="用預言家預測股票">用預言家預測股票</h3>



<p>看完了學術的部分，接下來我們就實際的來使用預言家模型預測股價吧！<br>要預測的精準，我們要找有週期性的股票來分析，<br>這邊我就隨性的拿台積電當作是標的來預測囉～</p>



<h3 id="超簡單環境設定">超簡單環境設定</h3>



<p>安裝python、安裝jupyter？<br>不用，google提供了強大的雲端notebook，讓我們可以直接玩程式：<br><a href="https://colab.research.google.com/" rel="noreferrer noopener" target="_blank">https://colab.research.google.com/</a><br>打開連結，開啟新的notebook，在雲端寫程式！<br>當然你有python也可以開自己的，快的多</p>



<p>這次為了方便大家學習，我已經將環境建設好了！<br>在任意cell輸入：</p>



<pre class="wp-block-code"><code class="">!git clone https://github.com/koreal6803/Stocker.git
!pip install -r Stocker/requirements.txt</code></pre>



<p>即可完成！<br>需要一段安裝時間，使用google colab的話，可以去休息一下，喝個水再回來XD<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j0.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="67" src="http://34.96.136.135/wp-content/uploads/2020/07/j0-1024x67.png" alt="j0" class="wp-image-741" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j0-1024x67.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/j0-300x20.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j0-768x50.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/j0.png 1240w" sizes="(max-width: 1024px) 100vw, 1024px" title="超簡單用Python預測股價 6"></figure></div>



<p>上述代碼中，每行開頭的驚嘆號，可以讓你把jupyter 當作 terminal 在用，<br>（不過假如windows用戶沒有git可能就無法…點上面google 線上程式連結）</p>



<h3 id="去除煩人的-warrning">去除煩人的 warrning</h3>



<p>接下來，我們先來刪除惱人的warrning，最近pandas越來越煩<br>一堆warrning…看了心煩直接刪除</p>



<pre class="wp-block-code"><code class="">import warnings

### 去除煩人的 warrning
warnings.filterwarnings('ignore')</code></pre>



<h3 id="讀入台積電股價">讀入台積電股價</h3>



<p>接下來就讀入附帶的台積點股價：</p>



<pre class="wp-block-code"><code class="">import pandas as pd

### 讀入series
df = pd.read_csv('Stocker/price.csv', index_col='date', parse_dates=['date'])
price = df.squeeze()
price.head()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="403" height="151" src="http://34.96.136.135/wp-content/uploads/2020/07/j1.png" alt="j1" class="wp-image-742" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j1.png 403w, https://www.finlab.tw/wp-content/uploads/2020/07/j1-300x112.png 300w" sizes="(max-width: 403px) 100vw, 403px" title="超簡單用Python預測股價 7"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j1.png"></a></p>



<p>其中的squeeze就是將dataframe變成series的function，<br>你可以用</p>



<pre class="wp-block-code"><code class="">print(df)
print(df.squeeze)</code></pre>



<p>來查看其中差別</p>



<h3 id="創建Stocker">創建Stocker</h3>



<p>這個Stocker並不是我寫的package，<br>而是利用<a href="https://github.com/WillKoehrsen/Data-Analysis/tree/master/stocker" rel="noreferrer noopener" target="_blank">國外別人寫好的</a><br>，再額外修改成更方便的版本</p>



<p>用法很簡單，把股價的series丟進去就可以了：</p>



<pre class="wp-block-code"><code class="">from Stocker.stocker import Stocker
tsmc = Stocker(price)</code></pre>



<h3 id="預測中期走勢">預測中期走勢</h3>



<p>接下來我們就可以用它來預測中期走勢，只需要一行</p>



<pre class="wp-block-code"><code class="">model, model_data = tsmc.create_prophet_model(days=90)</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="694" height="467" src="http://34.96.136.135/wp-content/uploads/2020/07/j2.png" alt="j2" class="wp-image-743" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j2.png 694w, https://www.finlab.tw/wp-content/uploads/2020/07/j2-300x202.png 300w" sizes="(max-width: 694px) 100vw, 694px" title="超簡單用Python預測股價 8"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j2.png"></a>其中綠色的就是預測出來的股價，而淺綠色區域就是信賴區間，股價大約就會落於此區間內，<br>程式中的信賴區間為80％，也代表了，每五天就約有一天的股價會落於信賴區間之外，<br>可以看到預測時間越久（沒實際股價的地方），不確定性越大，所以綠色的區間也就稍微變大<br>可以用來當作布林區間的感覺</p>



<h3 id="這樣預測準確嗎？">這樣預測準確嗎？</h3>



<p>不確定，所以我們可以把時間還原到一年前，來看看假如當時預測的結果，跟至今是不是有差別，<br>我們不讓Stocker使用近一年的股價來fitting，出來的曲線跟實際的股價做比較：</p>



<pre class="wp-block-code"><code class="">tsmc.evaluate_prediction()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="790" height="467" src="http://34.96.136.135/wp-content/uploads/2020/07/j3.png" alt="j3" class="wp-image-744" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j3.png 790w, https://www.finlab.tw/wp-content/uploads/2020/07/j3-300x177.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j3-768x454.png 768w" sizes="(max-width: 790px) 100vw, 790px" title="超簡單用Python預測股價 9"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j3.png"></a>上圖中，紅色虛線就是開始預測的當下，紅色線以後的時間都是預言家預測的結果<br>而藍色則為預測的真實股價，黃色則是信賴區間</p>



<h3 id="不準怎麼辦？">不準怎麼辦？</h3>



<p>沒關係，我們可以調整短中長期的參數，讓它預測短中長期股價：</p>



<pre class="wp-block-code"><code class="">tsmc.changepoint_prior_analysis(changepoint_priors=[0.001, 0.05, 0.1, 0.2])</code></pre>



<p>下圖中，可以看到藍色的線比較偏趨勢，不隨當下股價起舞，算是長期的趨勢預測<br>而黃色則是非常貼近股價，算是短期的預測<br>使用者可以自行調整參數，來決定短中長線的應用<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j4.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="770" height="498" src="http://34.96.136.135/wp-content/uploads/2020/07/j4-1.png" alt="j4 1" class="wp-image-745" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j4-1.png 770w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-1-300x194.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-1-768x497.png 768w" sizes="(max-width: 770px) 100vw, 770px" title="超簡單用Python預測股價 10"></figure></div>



<h3 id="拿來預測股價了！">拿來預測股價了！</h3>



<p>我們藉由上述的參數枚舉，可以知道哪一個參數（<code>changepoint_prior</code>）是最好的<br>我們可以藉由執行<code>predict_future</code>&nbsp;來預測股價囉！</p>



<pre class="wp-block-code"><code class="">tsmc.predict_future(days=100)</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="761" height="603" src="http://34.96.136.135/wp-content/uploads/2020/07/j5.png" alt="j5" class="wp-image-746" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j5.png 761w, https://www.finlab.tw/wp-content/uploads/2020/07/j5-300x238.png 300w" sizes="(max-width: 761px) 100vw, 761px" title="超簡單用Python預測股價 11"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j5.png"></a>藉由Stocker，我們可以很輕易的預測股價，<br>至於其準確性，還有待各位看官們回測看看囉！</p>



<p>有上左方課程的同學，可以很輕易的使用Stocker來檢驗歷史報酬率是多少！<br>可以試著找到最佳的參數，並盡量選擇有週期性的股票～<br>期待你們的成果！</p>



<p>此程式的一些連結：<br><a href="https://github.com/koreal6803/Stocker" rel="noreferrer noopener" target="_blank">此程式的github</a><br><a href="https://colab.research.google.com/drive/1KFhAhgtUcVVS1nMKlZicMjzGQZWKA6sc" rel="noreferrer noopener" target="_blank">此程式的notebook</a><br><a href="https://towardsdatascience.com/stock-prediction-in-python-b66555171a2" rel="noreferrer noopener" target="_blank">國外Stocker文章</a><br><a href="https://github.com/WillKoehrsen/Data-Analysis/tree/master/stocker" rel="noreferrer noopener" target="_blank">國外github</a><br><a href="https://github.com/facebook/prophet" rel="noreferrer noopener" target="_blank">預言家github</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae-machine-learning-%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">735</post-id>	</item>
	</channel>
</rss>
