True strength indicator help



  • I'm trying to create the mentioned indicator, but as newb, I have not grasped the concept of how stuff should be done!

    So this is what I'm trying to do:

    class TrueStrengthIndicator(bt.Indicator):
       ...
       ...
       def __init__(self):
            ....
            ema1 = bt.indicators.ExponentialMovingAverage(self.data[0] - self.data[-1], period=self.params.lperiod)
    

    I know that when the first bar is handled, there is no self.data[-1], but how can I handle this.


  • administrators

    See Docs - Platform Concepts and specifically the section Lines: DELAYED indexing

    During the declaration phase the [x] operator makes no sense, because values are not yet there (even if preloading creates the false impression it does)

    The (-x) returns an object which will later deliver the delayed values. In your case:

    self.data - self.data(-1)
    

    Because you can obviously omit self.data(0)


  • administrators

    It seems a nice indicator. Will get added along small details.

    Should be something like this acoording to Stockcharts - TSI

    class TrueStrengthIndicator(bt.Indicator):
        alias = ('TSI',)
        params = (
            ('period1', 25),
            ('period2', 13),
        )
        lines = ('tsi',)
    
        def __init__(self):
            pc = self.data - self.data(-1)
            smooth1 = bt.ind.EMA(bt.ind.EMA(pc, period=self.p.period1), period=self.p.period2)
            smooth2 = bt.ind.EMA(bt.ind.EMA(abs(pc), period=self.p.period1), period=self.p.period2)
            self.lines.tsi = 100.0 * (smooth1 / smooth2)
    


  • Thx for the input, as you know now, I have to digest this a little bit to fully understand what you have written.



  • @backtrader
    It works perfectly, thx for the input about delayed indexing! I'm not sure how long it would have taken me to find this!


  • administrators

    Probably the best way is to look at other indicators which are already part of the platform.

    See UpDay (which is nothing else but the definition made by Wilder to later define RSI)

    class UpDay(Indicator):
        lines = ('upday',)
    
        def __init__(self):
            self.lines.upday = Max(self.data - self.data(-1), 0.0)
    

    Max is not a typo. Whilst abs can be overridden (for the example above with TSI), max cannot.

    If you look into Ichimoku you can also see line forwarding.



  • @backtrader
    I actually looked at just that example, but I got confused about when to use what, [ ] or ( ). As I said, I'm learning the platform right now, so I'm thankful for the input.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.