股價創新高動能
安裝套件¶
In [1]:
Copied!
!pip install finlab > log.txt
!pip install finlab > log.txt
股價創新高動能¶
In [2]:
Copied!
from finlab.backtest import sim
from finlab import data
# 標的範圍為上市櫃普通股
with data.universe(market='TSE_OTC'):
# 取得收盤價
close = data.get("price:收盤價")
# 股價創近200日新高
position = (close == close.rolling(200).max())
# 每兩週再平衡,單檔最大持股比例限制20%,停損20%
report = sim(position, resample="2W", position_limit=0.2, stop_loss=0.2, name="股價創新高策略", upload=False)
report.display()
from finlab.backtest import sim
from finlab import data
# 標的範圍為上市櫃普通股
with data.universe(market='TSE_OTC'):
# 取得收盤價
close = data.get("price:收盤價")
# 股價創近200日新高
position = (close == close.rolling(200).max())
# 每兩週再平衡,單檔最大持股比例限制20%,停損20%
report = sim(position, resample="2W", position_limit=0.2, stop_loss=0.2, name="股價創新高策略", upload=False)
report.display()
輸入成功!
WARNING:finlab.data:security_categories -- Daily data usage: 339.0 / 5000 MB WARNING:finlab.data:price:收盤價 -- Daily data usage: 360.5 / 5000 MB WARNING:finlab.data:etl:adj_close -- Daily data usage: 388.9 / 5000 MB
2732 0.2 2754 0.2 3289 0.2 4102 0.2 5288 0.2 Name: 2022-10-09 00:00:00, dtype: float64
Timestamp('2022-10-09 00:00:00')
創新高延續動能策略¶
In [3]:
Copied!
from finlab.backtest import sim
from finlab import data
with data.universe(market='TSE_OTC'):
close = data.get("price:收盤價")
# 近5日內有3日以上的股價創前200日新高
position = (close == close.rolling(200).max()).sustain(5,3)
report = sim(position, resample="2W", position_limit=0.2, stop_loss=0.2, name="創新高延續動能策略", upload=False)
report.display()
from finlab.backtest import sim
from finlab import data
with data.universe(market='TSE_OTC'):
close = data.get("price:收盤價")
# 近5日內有3日以上的股價創前200日新高
position = (close == close.rolling(200).max()).sustain(5,3)
report = sim(position, resample="2W", position_limit=0.2, stop_loss=0.2, name="創新高延續動能策略", upload=False)
report.display()
5288 0.2 6101 0.2 8996 0.2 Name: 2022-10-09 00:00:00, dtype: float64
Timestamp('2022-10-09 00:00:00')