<?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>KD &#8211; FinLab</title>
	<atom:link href="https://www.finlab.tw/tag/kd/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Sun, 26 Jul 2020 15:51:08 +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>KD &#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>用KD值選股：你還需搭配這三種指標</title>
		<link>https://www.finlab.tw/%e7%94%a8kd%e5%80%bc%e9%81%b8%e8%82%a1%ef%bc%9a%e9%82%84%e9%9c%80%e6%90%ad%e9%85%8d%e9%80%99%e4%b8%89%e7%a8%ae%e6%8c%87%e6%a8%99/</link>
					<comments>https://www.finlab.tw/%e7%94%a8kd%e5%80%bc%e9%81%b8%e8%82%a1%ef%bc%9a%e9%82%84%e9%9c%80%e6%90%ad%e9%85%8d%e9%80%99%e4%b8%89%e7%a8%ae%e6%8c%87%e6%a8%99/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:38 +0000</pubDate>
				<category><![CDATA[選股策略]]></category>
		<category><![CDATA[KD]]></category>
		<category><![CDATA[技術指標]]></category>
		<category><![CDATA[程式交易]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=758</guid>

					<description><![CDATA[這篇文章將分享一個程式交易策略，以KD值為主，並且搭配其他的指標。有很多人投資是看KD值的，但KD值的看法五花八門，究竟哪一種比較好用？參數這麼多，究竟怎麼調績效比較好？這篇文章示範一個中長期的程式交易策略，並講解製作策略的思路。]]></description>
										<content:encoded><![CDATA[
<p>這篇文章將分享一個程式交易策略，以KD值為主，並且搭配其他的指標。<br>有很多人投資是看KD值的，但KD值的看法五花八門，究竟哪一種比較好用？<br>參數這麼多，究竟怎麼調績效比較好？<br>這篇文章示範一個中長期的程式交易策略，並講解製作策略的思路。</p>



<p>寫策略並模擬回測績效是非常重要的，通過回測，就能一眼看出來你的策略有沒有效。有人說歷史不等於未來，我覺得更正確的說，應該是「歷史不完全等於未來」，每年都有很多不同的黑天鵝，不同的國際政治變化，政黨輪替，然而假如你可以找到一個策略，不論環境如何變化，但是績效一直有穩定的成長，就代表你有可能找到了「歷史與未來會相同的某種選股邏輯」！</p>



<h3 id="KD值">KD值</h3>



<p>今天我們就來試驗一下KD值程式交易選股，<br>首先，我傾向於把指標的參數拉長，原因是這樣比較容易抓的到股價的規律（以日K來說）。<br>不過我個人的研究發現趨勢指標型態不一樣，選出來的股票也會不一樣，<br>絕對不是只要股價會漲就好，而是要符合某些特定的數學條件。</p>



<p>這邊我選擇使用RSV 50, K 20, D 20，當作是參數（台股通常是9、3、3）。<br>當K大於D的時候買入，反之賣出，每天選股並重新平衡資產，可以得到以下的績效：<a href="https://www.finlab.tw/%E7%94%A8KD%E5%80%BC%E9%81%B8%E8%82%A1%EF%BC%9A%E9%82%84%E9%9C%80%E6%90%AD%E9%85%8D%E9%80%99%E4%B8%89%E7%A8%AE%E6%8C%87%E6%A8%99/eq1.png"></a><br><a href="https://www.finlab.tw/%E7%94%A8KD%E5%80%BC%E9%81%B8%E8%82%A1%EF%BC%9A%E9%82%84%E9%9C%80%E6%90%AD%E9%85%8D%E9%80%99%E4%B8%89%E7%A8%AE%E6%8C%87%E6%A8%99/ns1.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img width="606" height="357" src="http://34.96.136.135/wp-content/uploads/2020/07/eq1-4.png" alt="eq1 4" class="wp-image-759" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eq1-4.png 606w, https://www.finlab.tw/wp-content/uploads/2020/07/eq1-4-300x177.png 300w" sizes="(max-width: 606px) 100vw, 606px" title="用KD值選股：你還需搭配這三種指標 1"></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="625" height="161" src="http://34.96.136.135/wp-content/uploads/2020/07/ns1-1.png" alt="ns1 1" class="wp-image-760" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/ns1-1.png 625w, https://www.finlab.tw/wp-content/uploads/2020/07/ns1-1-300x77.png 300w" sizes="(max-width: 625px) 100vw, 625px" title="用KD值選股：你還需搭配這三種指標 2"></figure></div>



<p>紅色的是有考慮手續費，而藍色的則無。</p>



<p>然而這樣的選法是有一些弊端的！（尤其是手續費很高昂，股票換手率太高）<br>所以接下來我們就想辦法來解決，順便提高一下程式交易的績效～</p>



<h3 id="長期趨勢漲，可能會買在高點！">長期趨勢漲，可能會買在高點！</h3>



<p>當K &gt; D時，股價會衝的很高，但是這時候買入就會買在高點了！<br>所以我們在使用長線KD值時，可以增加一個條件來確保不會買在高點：</p>



<p>當天股價 &lt; 10日平均</p>



<p>除了股價不能太高，同時股價也不能太低！</p>



<h3 id="股價必須高於年線">股價必須高於年線</h3>



<p>我們在此參數下選出來的股票，假如股價沒有過年線的話，很容易漲到年線附近時，就被彈了回去，所以乾脆我們選股的時候，就直接選高於年線的，就不會有這個問題了XD</p>



<h3 id="增加一些常用的財務指標">增加一些常用的財務指標</h3>



<p>程式交易除了用技術指標外，我們也可以增加一些財務指標，例如ROE：<br>ROE近1季大於ROE近4季平均</p>



<p>當然也有很多別的選項，我這邊就選一個好用的入門款XD，</p>



<h3 id="三種條件依序加上後的結果">三種條件依序加上後的結果</h3>



<p>可以發現，從下到上依序就是：</p>



<ol><li>使用KD值</li><li>除了上述以外，額外增加：股價短線別太高</li><li>除了上述以外，額外增加：股價長線別太低</li><li>除了上述以外，額外增加：財務數據</li></ol>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="595" height="357" src="http://34.96.136.135/wp-content/uploads/2020/07/eqall.png" alt="eqall" class="wp-image-761" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eqall.png 595w, https://www.finlab.tw/wp-content/uploads/2020/07/eqall-300x180.png 300w" sizes="(max-width: 595px) 100vw, 595px" title="用KD值選股：你還需搭配這三種指標 3"></figure></div>



<p><a href="https://www.finlab.tw/%E7%94%A8KD%E5%80%BC%E9%81%B8%E8%82%A1%EF%BC%9A%E9%82%84%E9%9C%80%E6%90%AD%E9%85%8D%E9%80%99%E4%B8%89%E7%A8%AE%E6%8C%87%E6%A8%99/eqall.png"></a>不過以上是沒有增加手續費的，因為我的回測系統每天都會重新權益平衡，所以有非常多不必要的摩擦成本，此時手續費折扣就非常的重要了！</p>



<p>最後我新增了一些不太重要的條件（為了減少股票檔數），整理成下圖，紅色為有手續費，藍色沒有手續費，只能說以人工調配持股應該會有更好的成果～有興趣可以研究看看！<a href="https://www.finlab.tw/%E7%94%A8KD%E5%80%BC%E9%81%B8%E8%82%A1%EF%BC%9A%E9%82%84%E9%9C%80%E6%90%AD%E9%85%8D%E9%80%99%E4%B8%89%E7%A8%AE%E6%8C%87%E6%A8%99/eqf.png"></a><br><a href="https://www.finlab.tw/%E7%94%A8KD%E5%80%BC%E9%81%B8%E8%82%A1%EF%BC%9A%E9%82%84%E9%9C%80%E6%90%AD%E9%85%8D%E9%80%99%E4%B8%89%E7%A8%AE%E6%8C%87%E6%A8%99/nsf.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="612" height="357" src="http://34.96.136.135/wp-content/uploads/2020/07/eqf.png" alt="eqf" class="wp-image-762" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eqf.png 612w, https://www.finlab.tw/wp-content/uploads/2020/07/eqf-300x175.png 300w" sizes="(max-width: 612px) 100vw, 612px" title="用KD值選股：你還需搭配這三種指標 4"></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="612" height="161" src="http://34.96.136.135/wp-content/uploads/2020/07/nsf.png" alt="nsf" class="wp-image-763" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/nsf.png 612w, https://www.finlab.tw/wp-content/uploads/2020/07/nsf-300x79.png 300w" sizes="(max-width: 612px) 100vw, 612px" title="用KD值選股：你還需搭配這三種指標 5"></figure></div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e7%94%a8kd%e5%80%bc%e9%81%b8%e8%82%a1%ef%bc%9a%e9%82%84%e9%9c%80%e6%90%ad%e9%85%8d%e9%80%99%e4%b8%89%e7%a8%ae%e6%8c%87%e6%a8%99/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">758</post-id>	</item>
		<item>
		<title>Python 時間序列實做！</title>
		<link>https://www.finlab.tw/python-%e6%99%82%e9%96%93%e5%ba%8f%e5%88%97%e5%af%a6%e4%bd%9c%ef%bc%81/</link>
					<comments>https://www.finlab.tw/python-%e6%99%82%e9%96%93%e5%ba%8f%e5%88%97%e5%af%a6%e4%bd%9c%ef%bc%81/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:02 +0000</pubDate>
				<category><![CDATA[財經PYTHON教學]]></category>
		<category><![CDATA[KD]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[程式交易]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=537</guid>

					<description><![CDATA[之前我們介紹了很多爬蟲，可以爬每天、每月、每季的資料，今天就是要交爬完了之後要怎麼運用。這篇文章會教如何將爬下來的資料整理成 time series （時間序列）]]></description>
										<content:encoded><![CDATA[
<p>之前我們介紹了很多爬蟲，可以爬每天、每月、每季的資料，今天就是要交爬完了之後要怎麼運用。這篇文章會教如何將爬下來的資料整理成 time series （時間序列）<a href="https://www.finlab.tw/Python-%E6%99%82%E9%96%93%E5%BA%8F%E5%88%97%E5%AF%A6%E4%BD%9C%EF%BC%81/thumbnail.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="282" height="179" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-2.png" alt="thumbnail 2" class="wp-image-538" title="Python 時間序列實做！ 6"></figure></div>



<h3 id="每日爬蟲">每日爬蟲</h3>



<p>首先，我們將之<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE%E5%8F%B0%E8%82%A1%E6%AF%8F%E6%97%A5%E7%88%AC%E8%9F%B2%E6%95%99%E5%AD%B8/">前一篇：超簡單台股每日爬蟲教學</a>&nbsp;所分享的程式碼變成以下的function，方便我們多次呼叫，才能爬每一天的股價。</p>



<p>每日股價爬蟲</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">import requests
from io import StringIO
import pandas as pd
import numpy as np

def crawl_price(date):
    r = requests.post('http://www.twse.com.tw/exchangeReport/MI_INDEX?response=csv&amp;date=' + str(date).split(' ')[0].replace('-','') + '&amp;type=ALL')
    ret = pd.read_csv(StringIO("\n".join([i.translate({ord(c): None for c in ' '}) 
                                        for i in r.text.split('\n') 
                                        if len(i.split('",')) == 17 and i[0] != '='])), header=0)
    ret = ret.set_index('證券代號')
    ret['成交金額'] = ret['成交金額'].str.replace(',','')
    ret['成交股數'] = ret['成交股數'].str.replace(',','')
    return ret</code></pre>



<p>就跟之前是一樣的，只是被包成 function ，它的 input 是日期，output 是 dataframe，寫程式最重要的就是瞭解 input 跟 output 的型態，就會比較簡單了！</p>



<h3 id="呼叫每日爬蟲，連續爬-n-天">呼叫每日爬蟲，連續爬 n 天</h3>



<p>接下來我們就來利用這個 function 幫我們爬最近9天的資料，並將數據存在 data 這個 variable 中：</p>



<p>一次爬 n_days 天的股價</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">import datetime
import time

data = {}
n_days = 9
date = datetime.datetime.now()
fail_count = 0
allow_continuous_fail_count = 5
while len(data) &lt; n_days:

    print('parsing', date)
    # 使用 crawPrice 爬資料
    try:
        # 抓資料
        data[date.date()] = crawl_price(date)
        print('success!')
        fail_count = 0
    except:
        # 假日爬不到
        print('fail! check the date is holiday')
        fail_count += 1
        if fail_count == allow_continuous_fail_count:
            raise
            break
    
    # 減一天
    date -= datetime.timedelta(days=1)
    time.sleep(10)</code></pre>



<p>想要增加天數，只要調整 n_days 這個參數就行了！<br>一切都順利的話，會顯示成以下這樣：<a href="https://www.finlab.tw/Python-%E6%99%82%E9%96%93%E5%BA%8F%E5%88%97%E5%AF%A6%E4%BD%9C%EF%BC%81/everyday.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="333" height="214" src="http://34.96.136.135/wp-content/uploads/2020/07/everyday.png" alt="everyday" class="wp-image-539" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/everyday.png 333w, https://www.finlab.tw/wp-content/uploads/2020/07/everyday-300x193.png 300w" sizes="(max-width: 333px) 100vw, 333px" title="Python 時間序列實做！ 7"></figure></div>



<p>大部分的天數都是 success 代表成功讀取資料，但是每間隔5天就會顯示兩次 fail，代表那可能是假日，然而不能保證就是了，也可能是你的網路斷掉XD，或是 code 有bug，就要單獨做測試，至少在我的電腦裡，它抓的滿好的。</p>



<h3 id="整理-data-轉成-收盤價-time-series">整理 data 轉成 收盤價 time series</h3>



<p>接下來我們來看一下得到了什麼：<a href="https://www.finlab.tw/Python-%E6%99%82%E9%96%93%E5%BA%8F%E5%88%97%E5%AF%A6%E4%BD%9C%EF%BC%81/data.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="321" src="http://34.96.136.135/wp-content/uploads/2020/07/data-1024x321.png" alt="data" class="wp-image-540" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/data-1024x321.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/data-300x94.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/data-768x241.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/data-1536x481.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/data-2048x641.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="Python 時間序列實做！ 8"></figure></div>



<p>我們像上圖一樣，只拿取 “2019-06-05” 號的股價，會像上面這樣，變換不同的日期，可以得到不同的表。但這並不是我們想要的，我們想要有 time series，也就是看到某個欄位隨時間變動的圖表，於是我們做以下的轉換：</p>



<pre class="wp-block-code"><code lang="python" class="language-python line-numbers">close = pd.DataFrame({k:d['收盤價'] for k,d in data.items()}).transpose()
close.index = pd.to_datetime(close.index)
close</code></pre>



<p>在上面的程式中，現在我們只拿所有股票的 收盤價，並建構 close 這個 dataframe，這個 dataframe，假如你還不知道什麼是 dataframe，或對 dataframe 還不熟，可以參考<a href="https://pandas.pydata.org/pandas-docs/stable/10min.html" rel="noreferrer noopener" target="_blank">連結</a>學習一下。</p>



<p>輸入後會看到以下成品：<a href="https://www.finlab.tw/Python-%E6%99%82%E9%96%93%E5%BA%8F%E5%88%97%E5%AF%A6%E4%BD%9C%EF%BC%81/clean.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="307" src="http://34.96.136.135/wp-content/uploads/2020/07/clean-1024x307.png" alt="clean" class="wp-image-541" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/clean-1024x307.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/clean-300x90.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/clean-768x230.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/clean.png 1110w" sizes="(max-width: 1024px) 100vw, 1024px" title="Python 時間序列實做！ 9"></figure></div>



<p>現在整張表都只剩 收盤價 了，而且還是按照日期排列，超讚的！我也覺得很開心，竟然兩行就處理完，可見pandas真的很厲害！</p>



<p>時間序列還有非常多功用！接下來我們就來<a href="https://www.finlab.tw/Python-%E7%B0%A1%E5%96%AE158%E7%A8%AE%E6%8A%80%E8%A1%93%E6%8C%87%E6%A8%99%E8%A8%88%E7%AE%97">實做一百種指標</a>吧！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/python-%e6%99%82%e9%96%93%e5%ba%8f%e5%88%97%e5%af%a6%e4%bd%9c%ef%bc%81/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">537</post-id>	</item>
	</channel>
</rss>
