SAR Source Code
class SARIndicator(object):
def __init__(self,
high: pd.Series,
low: pd.Series,
close: pd.Series,
period: int = 4,
step: float = 0.02,
max_step: float = 0.20,
fillna: bool = False,
):
self._high = high.copy()
self._low = low.copy()
self._close = close.copy()
self._length = self._close.__len__()
self._period = period - 1
self._step = step
self._max_step = max_step
self._fillna = fillna
self._run()
def _run(self):
up_trend = True
acceleration_factor = self._step
up_trend_high = self._high.iloc[0]
down_trend_low = self._low.iloc[0]
self._psar = pd.Series([np.nan] * self._length, index=self._close.index)
self._psar_up = pd.Series([np.nan] * self._length, index=self._close.index)
self._psar_down = pd.Series([np.nan] * self._length, index=self._close.index)
self._psar_indicator = pd.Series([np.nan] * self._length, index=self._close.index)
self._psar_af = pd.Series([np.nan] * self._length, index=self._close.index)
for i in range(1, self._length):
if i < self._period:
up_trend_high = max(self._high.iloc[i], up_trend_high)
down_trend_low = min(self._low.iloc[i], down_trend_low)
continue
if up_trend:
down_trend_low = min(self._low.iloc[i], down_trend_low)
if np.isnan(self._psar.iloc[i - 1]):
self._psar.iloc[i] = down_trend_low
else:
self._psar.iloc[i] = self._psar.iloc[i - 1] + (
acceleration_factor * (up_trend_high - self._psar.iloc[i - 1])
)
self._psar.iloc[i] = round(self._psar.iloc[i], 2)
if self._psar.iloc[i] > self._low.iloc[i]:
up_trend = False
self._psar.iloc[i] = up_trend_high
down_trend_low = self._low.iloc[i]
acceleration_factor = self._step
else:
if self._high.iloc[i] > up_trend_high:
up_trend_high = self._high.iloc[i]
acceleration_factor = min(
acceleration_factor + self._step, self._max_step
)
else:
up_trend_high = max(self._high.iloc[i], up_trend_high)
self._psar.iloc[i] = self._psar.iloc[i - 1] - (
acceleration_factor * (self._psar.iloc[i - 1] - down_trend_low)
)
self._psar.iloc[i] = round(self._psar.iloc[i], 2)
if self._psar.iloc[i] < self._high.iloc[i]:
up_trend = True
self._psar.iloc[i] = down_trend_low
up_trend_high = self._high.iloc[i]
acceleration_factor = self._step
else:
if self._low.iloc[i] < down_trend_low:
down_trend_low = self._low.iloc[i]
acceleration_factor = min(
acceleration_factor + self._step, self._max_step
)
if up_trend:
self._psar_up.iloc[i] = 1
self._psar_indicator.iloc[i] = 1
else:
self._psar_down.iloc[i] = 1
self._psar_indicator.iloc[i] = -1
self._psar_af.iloc[i] = acceleration_factor
def psar(self):
"""
返回SAR数值
:return:
"""
return pd.Series(self._psar, name='psar')
def psar_up(self):
"""
返回上升趋势
:return:
"""
return pd.Series(self._psar_up, name='psar_up')
def psar_down(self):
"""
返回下降趋势
:return:
"""
return pd.Series(self._psar_down, name='psar_down')
def psar_indicator(self):
"""
返回多头和空头
:return:
"""
return pd.Series(self._psar_indicator, name='indicator')
def psar_acceleration_factor(self):
"""
返回加速因子
:return:
"""
return pd.Series(self._psar_af, name='acceleration_factor')
References
1、https://school.stockcharts.com/doku.php?id=technical_indicators:parabolic_sar
2、https://www.investopedia.com/terms/p/parabolicindicator.asp
3、https://www.spreadsheetml.com/technicalindicators/parabolicSAR.shtml
4、https://technical-analysis-library-in-python.readthedocs.io/en/latest/ta.html#momentum-indicators
欢迎关注~ SandQuant 专注于全球金融数据和量化投资策略
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)