For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Using EMA on a list of values



  • 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!



  • Do you mean to subtract lrc[-1]?


  • administrators

    @noah-bastola said in Using EMA on a list of values:

    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.

    Sorry but backtrader doesn't work that way. It doesn't create list of values and doesn't use lists of values as input.

    It creates objects which deliver values during the evaluation of the Strategy and it uses objects as input.

    You can subtract the previous value in a delayed manner:

    • Docs - Platform Concepts - Section: Delayed Indexing and then applying regular subtraction amongst objects during initialization

    or

    • Docs - Platform Concepts - Section: Indexing 0 and -1 and applying regular subtraction to the values obtained with index access during next evaluation

    You can use the results as input to mostly any other indicator like an EMA or an SMA (some indicators require multiple inputs and some indicators require data with multiple -specific- lines)

    In any case and in addition to the doc section linked above (Platform Concepts) I would recommend


  • administrators


Log in to reply
 

});