M

@backtrader @ab_trader
I finally implement the Indicator as below code, it seems work. but two small questions, when the ATR is coupled? and how do I set the minperiod when data with different time frames, I worked around by using len(self.l.art)>0, else it would raise errors.
import numpy as np
import backtrader as bt
from ext.ult.ult import *
from ext.Ind.OCHLInd import OCHLInd
# https://www.tradingview.com/script/mtWhbCRf-RS-MTF-Volatility-Stop-V0/
class MtfVSInd(bt.Indicator):
"""
"""
lines = ('trend', 'vs', 'max_close', 'min_close', 'atr')
params = (
('period', 5),
('factor', 1.618),
('mode', 0) # debug modes
)
plotinfo = dict(subplot=False)
plotlines = dict(vs=dict(_plotskip=False),
trend=dict(_plotskip=True, _method='bar', alpha=0.50, width=1.0),
max_close=dict(_plotskip=True, color='black'),
min_close=dict(_plotskip=True, color='red'),
atr=dict(_plotskip=True),
)
def _plotinit(self):
if self.p.mode == 1:
self.plotinfo.subplot = True
self.plotlines.vs._plotskip = True
self.plotlines.trend._plotskip = True
self.plotlines.max_close._plotskip = True
self.plotlines.min_close._plotskip = True
self.plotlines.atr._plotskip = False
elif self.p.mode == 2:
self.plotinfo.subplot = True
self.plotlines.vs._plotskip = True
self.plotlines.trend._plotskip = False
self.plotlines.max_close._plotskip = True
self.plotlines.min_close._plotskip = True
self.plotlines.atr._plotskip = True
elif self.p.mode == 3:
self.plotinfo.subplot = True
self.plotlines.vs._plotskip = True
self.plotlines.trend._plotskip = True
self.plotlines.max_close._plotskip = False
self.plotlines.min_close._plotskip = False
self.plotlines.atr._plotskip = True
def __init__(self):
# the ATR is Day time frame
self.l.atr = bt.ind.ATR(self.data1, period=self.p.period)
# get the avg(high+low)
self.hl = OCHLInd(self.data0, type='hl')
super(MtfVSInd, self).__init__()
def next(self):
if len(self.l.atr) > 0:
if np.isnan(self.l.trend[-1]):
self.l.trend[0] = 1
elif self.data0.l.close[0] > self.l.vs[-1]:
self.l.trend[0] = 1
elif self.data0.l.close[0] < self.l.vs[-1]:
self.l.trend[0] = -1
else:
self.l.trend[0] = self.l.trend[-1]
chg_trend = self.l.trend[0] - self.l.trend[-1]
if chg_trend != 0:
self.l.max_close[0] = self.data0.l.close[0]
elif self.data0.l.close[0] >= self.l.max_close[-1]:
self.l.max_close[0] = self.data0.l.close[0]
else:
self.l.max_close[0] = self.l.max_close[-1]
if chg_trend != 0:
self.l.min_close[0] = self.data0.l.close[0]
elif self.data0.l.close[0] <= self.l.min_close[-1]:
self.l.min_close[0] = self.data0.l.close[0]
else:
self.l.min_close[0] = self.l.min_close[-1]
if np.isnan(self.l.vs[-1]):
self.l.vs[0] = self.hl[0]
elif chg_trend > 0:
self.l.vs[0] = self.data0.l.close[0] - self.p.factor * self.l.atr[0]
elif self.l.trend[0] > 0:
self.l.vs[0] = max(self.l.vs[-1], self.l.max_close[0] - self.p.factor * self.l.atr[0])
elif chg_trend < 0:
self.l.vs[0] = self.data0.l.close[0] + self.p.factor * self.l.atr[0]
elif self.l.trend[0] < 0:
self.l.vs[0] = min(self.l.vs[-1], self.l.min_close[0] + self.p.factor * self.l.atr[0])
else:
self.l.vs[0] = self.l.vs[-1]