台積電逆勢抄底
抄底就是所謂的逆勢策略,能不能用量化的方式實測呢?
布林通道是最廣為人知的逆勢方法,原理是運用均線和標準差,大部分 K 線都涵蓋在 d日 MA 上下 n 個標準差的範圍之內操作。
d數值越大,價格曲線越平滑,能減少波動雜訊。
n數值越大,能產生越極端的訊號,一般n值範圍依據68–95–99.7法則(68–95–99.7 rule),設定在1-3,在常態分布中,距平均值小於一個標準差、二個標準差、三個標準差以內的百分比。當股價跌破下軌通道,可能出現超賣訊號,會在之後均值回歸,產生反彈到中軌通道附近。
In [1]:
Copied!
!pip install finlab
!pip install ta-lib-bin
!pip install finlab
!pip install ta-lib-bin
布林通道策略¶
In [2]:
Copied!
from finlab import data
from finlab.backtest import sim
close=data.get('price:收盤價')
upperband, middleband, lowerband = data.indicator('BBANDS', resample='D', nbdevup=float(2.5), nbdevdn=float(2.5),timeperiod=40)
entries = lowerband > close
exits = close > middleband
# 設定賣出條件
position = entries.hold_until(exits)['2330']
report = sim(position,upload=False,mae_mfe_window=40, name='護國神山救難隊')
from finlab import data
from finlab.backtest import sim
close=data.get('price:收盤價')
upperband, middleband, lowerband = data.indicator('BBANDS', resample='D', nbdevup=float(2.5), nbdevdn=float(2.5),timeperiod=40)
entries = lowerband > close
exits = close > middleband
# 設定賣出條件
position = entries.hold_until(exits)['2330']
report = sim(position,upload=False,mae_mfe_window=40, name='護國神山救難隊')
輸入成功!
WARNING:finlab.data:price:收盤價 -- Daily data usage: 113.8 / 5000 MB WARNING:finlab.data:price:開盤價 -- Daily data usage: 135.3 / 5000 MB WARNING:finlab.data:price:最高價 -- Daily data usage: 156.9 / 5000 MB WARNING:finlab.data:price:最低價 -- Daily data usage: 178.3 / 5000 MB WARNING:finlab.data:price:成交股數 -- Daily data usage: 208.3 / 5000 MB WARNING:finlab.data:etl:adj_close -- Daily data usage: 236.8 / 5000 MB WARNING:finlab.data:security_categories -- Daily data usage: 236.9 / 5000 MB
取得波動分析報告¶
In [3]:
Copied!
report.display_mae_mfe_analysis(violinmode='overlay').show()
report.display_mae_mfe_analysis(violinmode='overlay').show()
取得報酬率圖表¶
In [5]:
Copied!
# 取得報酬率圖表
report.display()
# 取得報酬率圖表
report.display()
2330 1.0 Name: 2022-09-30 00:00:00, dtype: float64
Timestamp('2022-09-30 00:00:00')
取得交易紀錄¶
In [4]:
Copied!
trade_record = report.trades
trade_record
trade_record = report.trades
trade_record
Out[4]:
stock_id | entry_date | exit_date | entry_sig_date | exit_sig_date | position | period | entry_index | exit_index | return | trade_price@entry_date | trade_price@exit_date | mae | gmfe | bmfe | mdd | pdays | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
trade_index | |||||||||||||||||
0 | 2330 台積電 | 2007-08-02 | 2007-09-28 | 2007-08-01 | 2007-09-27 | 1.0 | 38 | 71 | 109 | -0.003135 | 63.8 | 63.6 | -0.075235 | 0.001567 | 0.001567 | -0.076682 | 1.0 |
1 | 2330 台積電 | 2007-11-22 | 2007-12-07 | 2007-11-21 | 2007-12-06 | 1.0 | 11 | 148 | 159 | 0.045075 | 59.9 | 62.6 | -0.023372 | 0.045075 | 0.020033 | -0.042553 | 7.0 |
2 | 2330 台積電 | 2008-01-08 | 2008-02-01 | 2008-01-07 | 2008-01-31 | 1.0 | 18 | 180 | 198 | 0.096429 | 56.0 | 61.4 | -0.114286 | 0.096429 | 0.026786 | -0.137391 | 6.0 |
3 | 2330 台積電 | 2008-07-04 | 2008-08-11 | 2008-07-03 | 2008-08-08 | 1.0 | 25 | 299 | 324 | 0.103769 | 59.0 | 60.6 | -0.023729 | 0.103769 | 0.057627 | -0.080268 | 22.0 |
4 | 2330 台積電 | 2008-10-28 | 2008-12-11 | 2008-10-27 | 2008-12-10 | 1.0 | 32 | 378 | 410 | 0.130982 | 39.7 | 44.9 | -0.073048 | 0.209068 | 0.209068 | -0.233333 | 22.0 |
5 | 2330 台積電 | 2010-02-06 | 2010-03-09 | 2010-02-05 | 2010-03-08 | 1.0 | 15 | 701 | 716 | 0.056897 | 58.0 | 61.3 | -0.010345 | 0.056897 | 0.000000 | -0.026756 | 14.0 |
6 | 2330 台積電 | 2011-06-28 | 2011-07-27 | 2011-06-27 | 2011-07-26 | 1.0 | 21 | 1042 | 1063 | 0.056115 | 72.5 | 73.4 | -0.014388 | 0.060432 | 0.057554 | -0.068027 | 20.0 |
7 | 2330 台積電 | 2011-08-08 | 2011-08-30 | 2011-08-05 | 2011-08-29 | 1.0 | 16 | 1071 | 1087 | 0.058282 | 65.2 | 69.0 | -0.029141 | 0.064417 | 0.000000 | -0.043413 | 11.0 |
8 | 2330 台積電 | 2012-05-24 | 2012-06-01 | 2012-05-23 | 2012-05-31 | 1.0 | 6 | 1268 | 1274 | -0.008685 | 80.6 | 79.9 | -0.008685 | 0.055831 | 0.055831 | -0.061105 | 4.0 |
9 | 2330 台積電 | 2012-06-05 | 2012-07-02 | 2012-06-04 | 2012-06-29 | 1.0 | 19 | 1276 | 1295 | 0.065217 | 78.2 | 83.3 | -0.005115 | 0.065217 | 0.000000 | -0.035539 | 16.0 |
10 | 2330 台積電 | 2013-07-22 | 2013-09-03 | 2013-07-19 | 2013-09-02 | 1.0 | 30 | 1556 | 1586 | 0.051546 | 97.0 | 102.0 | -0.026804 | 0.056701 | 0.056701 | -0.079024 | 16.0 |
11 | 2330 台積電 | 2015-06-09 | 2015-06-26 | 2015-06-08 | 2015-06-25 | 1.0 | 12 | 2019 | 2031 | 0.050360 | 139.0 | 146.0 | 0.000000 | 0.053957 | 0.000000 | -0.017301 | 12.0 |
12 | 2330 台積電 | 2015-08-11 | 2015-09-17 | 2015-08-10 | 2015-09-16 | 1.0 | 27 | 2062 | 2089 | 0.003831 | 130.5 | 131.0 | -0.118774 | 0.003831 | 0.000000 | -0.118774 | 1.0 |
13 | 2330 台積電 | 2016-01-08 | 2016-01-26 | 2016-01-07 | 2016-01-25 | 1.0 | 12 | 2166 | 2178 | 0.026119 | 134.0 | 137.5 | -0.018657 | 0.041045 | 0.000000 | -0.025362 | 8.0 |
14 | 2330 台積電 | 2016-05-04 | 2016-05-24 | 2016-05-03 | 2016-05-23 | 1.0 | 14 | 2239 | 2253 | 0.027211 | 147.0 | 151.0 | -0.020408 | 0.047619 | 0.006803 | -0.027027 | 9.0 |
15 | 2330 台積電 | 2017-12-07 | 2018-01-04 | 2017-12-06 | 2018-01-03 | 1.0 | 19 | 2635 | 2654 | 0.057395 | 226.5 | 239.5 | -0.008830 | 0.057395 | 0.017660 | -0.026030 | 13.0 |
16 | 2330 台積電 | 2018-04-23 | 2018-06-07 | 2018-04-20 | 2018-06-06 | 1.0 | 32 | 2722 | 2754 | 0.015453 | 226.5 | 230.0 | -0.026490 | 0.028698 | 0.002208 | -0.051502 | 19.0 |
17 | 2330 台積電 | 2018-06-26 | 2018-07-16 | 2018-06-25 | 2018-07-13 | 1.0 | 14 | 2766 | 2780 | 0.041958 | 214.5 | 223.5 | -0.011655 | 0.046620 | 0.000000 | -0.011655 | 10.0 |
18 | 2330 台積電 | 2018-10-12 | 2018-12-04 | 2018-10-11 | 2018-12-03 | 1.0 | 37 | 2842 | 2879 | -0.012658 | 237.0 | 234.0 | -0.080169 | 0.006329 | 0.006329 | -0.085954 | 2.0 |
19 | 2330 台積電 | 2019-01-07 | 2019-01-23 | 2019-01-04 | 2019-01-22 | 1.0 | 12 | 2902 | 2914 | 0.035211 | 213.0 | 220.5 | -0.009390 | 0.046948 | 0.000000 | -0.015837 | 11.0 |
20 | 2330 台積電 | 2020-03-13 | 2020-04-20 | 2020-03-12 | 2020-04-17 | 1.0 | 24 | 3184 | 3208 | 0.058453 | 290.0 | 304.0 | -0.136525 | 0.067158 | 0.000000 | -0.136525 | 3.0 |
21 | 2330 台積電 | 2021-05-13 | 2021-06-01 | 2021-05-12 | 2021-05-31 | 1.0 | 13 | 3468 | 3481 | 0.093236 | 547.0 | 598.0 | 0.000000 | 0.093236 | 0.000000 | -0.014363 | 13.0 |
22 | 2330 台積電 | 2021-08-20 | 2021-08-27 | 2021-08-19 | 2021-08-26 | 1.0 | 5 | 3538 | 3543 | 0.085145 | 552.0 | 599.0 | 0.000000 | 0.085145 | 0.000000 | 0.000000 | 5.0 |
23 | 2330 台積電 | 2022-03-08 | 2022-05-31 | 2022-03-07 | 2022-05-30 | 1.0 | 57 | 3668 | 3725 | 0.000048 | 563.0 | 560.0 | -0.098171 | 0.071480 | 0.071480 | -0.158333 | 27.0 |
24 | 2330 台積電 | 2022-07-04 | 2022-07-22 | 2022-07-01 | 2022-07-21 | 1.0 | 14 | 3748 | 3762 | 0.143182 | 440.0 | 503.0 | -0.010227 | 0.143182 | 0.013636 | -0.037473 | 13.0 |
25 | 2330 台積電 | 2022-10-03 | NaT | 2022-09-30 | NaT | 1.0 | 14 | 3812 | -1 | -0.065947 | 417.0 | NaN | -0.065947 | 0.081535 | 0.081535 | -0.136364 | 4.0 |
加入停損停利¶
搶反彈如搶銀行,不管有沒有搶到都要跑!
極端的暴跌行情下搶反彈,87%勝率、平均持有19個交易日,平均獲利5%。
持有前10日內若多數時間保持獲利狀態,則顯示抄在底部的機率高,反之若多數時間為虧損,則可能抄錯底,再破底風險變高,若跌幅超過15%則為發生樣本外的特殊崩盤情況,應停損出場。
平均一年不到3次,要把握機會?
漲幅超過10%產生停利訊號,跌幅超過15%產生停損訊號。
In [6]:
Copied!
from finlab import data
from finlab.backtest import sim
close=data.get('price:收盤價')
upperband, middleband, lowerband = data.indicator('BBANDS', resample='D', nbdevup=float(2.5), nbdevdn=float(2.5), timeperiod=40)
entries = lowerband > close
exits = close > middleband
position = entries.hold_until(exits, take_profit=0.1, stop_loss=-0.15)['2330']
report = sim(position, upload=False, mae_mfe_window=40, name='護國神山救難隊')
report.display_mae_mfe_analysis(violinmode='overlay').show()
report.display()
from finlab import data
from finlab.backtest import sim
close=data.get('price:收盤價')
upperband, middleband, lowerband = data.indicator('BBANDS', resample='D', nbdevup=float(2.5), nbdevdn=float(2.5), timeperiod=40)
entries = lowerband > close
exits = close > middleband
position = entries.hold_until(exits, take_profit=0.1, stop_loss=-0.15)['2330']
report = sim(position, upload=False, mae_mfe_window=40, name='護國神山救難隊')
report.display_mae_mfe_analysis(violinmode='overlay').show()
report.display()
2330 1.0 Name: 2022-09-30 00:00:00, dtype: float64
Timestamp('2022-09-30 00:00:00')
In [ ]:
Copied!