Hi all,
I think I have a pretty simple question, but I'm having trouble finding examples for what I want in the documentation.
The code I am trying to mimic in backtrader:
lrc = list(talib.LINEARREG(df['Close Price'], timeperiod=clen))
lrs = []
slrs = []
for i in range(clen,len(lrc)-1):
lrs.append(lrc[i+1]-lrc[i])
lrs = pd.Series(lrs)
for i in range(1,len(lrs)):
slrs.append(talib.EMA(lrs[-(20+i):-i], timeperiod=slen).values[-1])
The code I am trying to write:
def linreg(self):
lrs = previous lrc - current lrc
slrs = EMA(lrs,timeperiod=50)
return(slrs)
def __init__(self):
# Keep a reference to the "close" line in the data[0] dataseries
self.dataclose = self.datas[0].close
self.datahigh = self.datas[0].high
self.datalow = self.datas[0].low
# To keep track of pending orders and buy price/commission
self.order = None
self.buyprice = None
self.buycomm = None
# Add a MovingAverageSimple indicator
self.sma = bt.indicators.SimpleMovingAverage(
self.datas[0], period=self.params.maperiod)
# Add Linear Regression indicator
self.lrc = bt.talib.LINEARREG(self.data,timeperiod=59)
What I want to do:
I want to create a list of new values by subtracting the current lrc value from the previous bar's lrc value. Then, I want to take the EMA of this new list of values and store in slrs.
Visual examples:
closePrices = [1,2,3,4,5,6,7,8,9......]
lrc = [ema with a rolling window ]
lrs = [rolling subtraction of lrc[0]-lrc[1]]
slrs = [ema of lrs with a rolling window ]
Please let me know if I need to clarify anything! Thanks!