Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    True strength indicator help

    General Code/Help
    indicator tsi
    2
    7
    1645
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kolten last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        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)

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          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)
          
          K 1 Reply Last reply Reply Quote 0
          • K
            kolten last edited by

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

            1 Reply Last reply Reply Quote 0
            • K
              kolten @backtrader last edited by

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

              1 Reply Last reply Reply Quote 0
              • B
                backtrader administrators last edited by

                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.

                K 1 Reply Last reply Reply Quote 0
                • K
                  kolten @backtrader last edited by

                  @backtrader

                  or ( ). As I said, I'm learning the platform right now, so I'm thankful for the input.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                  $(document).ready(function () { app.coldLoad(); }); }