美股 布林通道抄底策略
安裝套件¶
In [ ]:
Copied!
!pip install finlab > log.txt
!pip install ta-lib-bin
!pip install finlab > log.txt
!pip install ta-lib-bin
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting ta-lib-bin Downloading ta_lib_bin-0.4.26-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.9/2.9 MB 29.5 MB/s eta 0:00:00 Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from ta-lib-bin) (1.22.4) Installing collected packages: ta-lib-bin Successfully installed ta-lib-bin-0.4.26
抄底策略¶
台版的護國神山逆勢抄底策略大家應該不陌生,運用於美股中,我們加入增加站上90日均的條件,並加入排序選出10檔持有,每周換股。可再自行設定停損停利喔
In [ ]:
Copied!
from finlab import data
from finlab.backtest import sim
close = data.get('us_price:close')
low = data.get('us_price:low')
high = data.get('us_price:high')
upperband,middleband,lowerband = data.indicator("BBANDS",adjust_price=False, resample="D", market='US_STOCK', timeperiod=30,nbdevup=float(2.5),nbdevdn=float(2.5),matype=0)
entries = (lowerband > close) & (close > close.average(90)) & (close > 3)
exits = (close > middleband) | close.isna()
# 以150日低點與高點比值做排序,只篩出數值較大的前10檔
rank = low.rolling(150).min() / high.rolling(150).max()
position = entries.hold_until(exits, nstocks_limit=10, rank=rank)
report = sim(position, resample='W', position_limit=0.1, name='美股_布林通道抄底策略')
# report.display()
from finlab import data
from finlab.backtest import sim
close = data.get('us_price:close')
low = data.get('us_price:low')
high = data.get('us_price:high')
upperband,middleband,lowerband = data.indicator("BBANDS",adjust_price=False, resample="D", market='US_STOCK', timeperiod=30,nbdevup=float(2.5),nbdevdn=float(2.5),matype=0)
entries = (lowerband > close) & (close > close.average(90)) & (close > 3)
exits = (close > middleband) | close.isna()
# 以150日低點與高點比值做排序,只篩出數值較大的前10檔
rank = low.rolling(150).min() / high.rolling(150).max()
position = entries.hold_until(exits, nstocks_limit=10, rank=rank)
report = sim(position, resample='W', position_limit=0.1, name='美股_布林通道抄底策略')
# report.display()
/usr/local/lib/python3.10/dist-packages/finlab/analysis/alphaBetaAnalysis.py:22: RuntimeWarning: invalid value encountered in double_scalars
annualized_rate_of_return | sharpe | max_drawdown | win_ratio | |
---|---|---|---|---|
17.25% | 0.64 | -41.3% | 70.0% |
entry_date | exit_date | entry_sig_date | exit_sig_date | position | period | entry_index | exit_index | return | trade_price@entry_date |
---|