大盤指標濾網
安裝套件¶
In [1]:
Copied!
!pip install finlab > log.txt
!pip install finlab > log.txt
加入大盤指標濾網¶
大盤指標轉空時,出清持股
In [9]:
Copied!
from finlab import data
from finlab.backtest import sim
import numpy as np
import pandas as pd
# 市場多空排列家數
def ls_order_position(short=5,mid=10,long=30):
close = data.get("price:收盤價")
short_ma = close.average(short)
mid_ma = close.average(mid)
long_ma = close.average(long)
long_order = (short_ma>=mid_ma) & (mid_ma>=long_ma)
long_order = long_order.sum(1)
short_order = (short_ma<mid_ma) & (mid_ma<long_ma)
short_order = short_order.sum(1)
entry = long_order > short_order
cond = ~close.isna()
position = cond & entry
return position
close = data.get("price:收盤價")
# 多頭排列
buy = (close > close.shift(20)) & (close > close.shift(60))
# 套入大盤指標
position = buy & ls_order_position()
report = sim(position, name="ls_order_position",upload=False)
report.display()
from finlab import data
from finlab.backtest import sim
import numpy as np
import pandas as pd
# 市場多空排列家數
def ls_order_position(short=5,mid=10,long=30):
close = data.get("price:收盤價")
short_ma = close.average(short)
mid_ma = close.average(mid)
long_ma = close.average(long)
long_order = (short_ma>=mid_ma) & (mid_ma>=long_ma)
long_order = long_order.sum(1)
short_order = (short_ma short_order
cond = ~close.isna()
position = cond & entry
return position
close = data.get("price:收盤價")
# 多頭排列
buy = (close > close.shift(20)) & (close > close.shift(60))
# 套入大盤指標
position = buy & ls_order_position()
report = sim(position, name="ls_order_position",upload=False)
report.display()
Series([], Name: 2022-09-19 00:00:00, dtype: float64)
Timestamp('2022-09-19 00:00:00')
In [ ]:
Copied!