多空對沖
安裝套件¶
In [1]:
Copied!
!pip install finlab > log.txt
!pip install finlab > log.txt
放空指數保護多單持股¶
擔心大盤指數下跌影響長抱股,又不想賣掉手中價值股的保護方法。
In [8]:
Copied!
from finlab import data
from finlab import backtest
pb = data.get('price_earning_ratio:股價淨值比')
# 股價淨值比前20%低
position = pb < pb.quantile_row(0.2)
position /= position.sum(axis=1)
# 多單總部位70%
position *= 0.7
# 空單總部位30%
position['0050'] = -0.3
report=backtest.sim( position, upload=False, resample='Q')
report.display()
from finlab import data
from finlab import backtest
pb = data.get('price_earning_ratio:股價淨值比')
# 股價淨值比前20%低
position = pb < pb.quantile_row(0.2)
position /= position.sum(axis=1)
# 多單總部位70%
position *= 0.7
# 空單總部位30%
position['0050'] = -0.3
report=backtest.sim( position, upload=False, resample='Q')
report.display()
WARNING:finlab.data:price_earning_ratio:股價淨值比 -- Daily data usage: 251.8 / 5000 MB WARNING:finlab.data:security_categories -- Daily data usage: 251.8 / 5000 MB
1102 0.002023 1103 0.002023 1104 0.002023 1108 0.002023 1109 0.002023 ... 9929 0.002023 9934 0.002023 9944 0.002023 9946 0.002023 0050 -0.300000 Name: 2022-12-31 00:00:00, Length: 347, dtype: float64
Timestamp('2022-12-31 00:00:00')
In [ ]:
Copied!