Backtrader Community

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

    Problem with custom ohlc vars

    General Discussion
    2
    8
    797
    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.
    • A
      arrr last edited by arrr

      I'm trying to maintain code compatibility with my production environment, where I'm storing ohlc data in a dict:
      self.ohlc = {-3: {'o': 153.252, 'h': 155.84, 'l': 149.49, 'c': 152.42, 'vol': 155395.333}, -2: {'o': 152.417, 'h': 162.4, 'l': 149.78, 'c': 155.65, 'vol': 255543.915}, -1: {'o': 155.7, 'h': 161.04, 'l': 150.88, 'c': 158.48, 'vol': 297664.05400000006}, 0: {'o': 158.71, 'h': 167.0, 'l': 158.51, 'c': 162.0, 'vol': 189163.71099999998}}
      where self.ohlc[0]['l] is the low if the current bar and self.ohlc[-3]['h'] stands for high 3 bars ago.
      I was trying to replicate the same with backtrader by doing:

          def make_ohlc(self):
              ohlc_dict = {}
              for x in range(-3, 1):
                  ohlc = {'o': self.datas[0].open[x], 'h': self.datas[0].high[x], 'l': self.datas[0].low[x], 'c': self.datas[0].close[x]}
                  ohlc_dict[x] = ohlc
              return ohlc_dict
      

      and calling it from init:
      self.ohlc = self.make_ohlc()
      But once I do that - I'm getting weird error:

        File "/Users/arrrr/PycharmProjects/backtrader/reverse.py", line 217, in <module>
          cerebro.run()
        File "/Users/arrrr/PycharmProjects/backtrader/venv/lib/python3.6/site-packages/backtrader/cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "/Users/arrrr/PycharmProjects/backtrader/venv/lib/python3.6/site-packages/backtrader/cerebro.py", line 1217, in runstrategies
          strat = stratcls(*sargs, **skwargs)
        File "/Users/arrrr/PycharmProjects/backtrader/venv/lib/python3.6/site-packages/backtrader/metabase.py", line 88, in __call__
          _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
        File "/Users/arrrr/PycharmProjects/backtrader/venv/lib/python3.6/site-packages/backtrader/metabase.py", line 78, in doinit
          _obj.__init__(*args, **kwargs)
        File "/Users/arrrr/PycharmProjects/backtrader/reverse.py", line 50, in __init__
          self.make_ohlc()
        File "/Users/arrrr/PycharmProjects/backtrader/reverse.py", line 55, in make_ohlc
          ohlc = {'o': self.dataopen[x], 'h': self.datahigh[x], 'l': self.datalow[x],
        File "/Users/arrrr/PycharmProjects/backtrader/venv/lib/python3.6/site-packages/backtrader/linebuffer.py", line 163, in __getitem__
          return self.array[self.idx + ago]
      IndexError: array index out of range
      

      However if I call same self.make_ohlc() from self.next() - it works (it's obvious that calling it every bar is not a proper way to handle this)
      Any ideas what I'm doing wrong?

      B 3 Replies Last reply Reply Quote 0
      • B
        backtrader administrators @arrr last edited by

        @arrr said in Problem with custom ohlc vars:

        and calling it from __init:__

        __init__ is there to initialize things. Data feeds are not producing data yet.

        You probably want to read:

        • Docs - Platform Concepts

        and the lifecycle of a strategy (right at the beginning)

        • Docs - Strategy
        1 Reply Last reply Reply Quote 1
        • B
          backtrader administrators @arrr last edited by

          @arrr said in Problem with custom ohlc vars:

          it's obvious that calling it every bar is not a proper way to handle this

          I missed that before. The question is: why?

          backtrader isn't pandas (which many people apparently believe). Evaluation of things happens with each new incoming tick, which is given to the user during next, where you have access to the data.

          A 1 Reply Last reply Reply Quote 0
          • A
            arrr @backtrader last edited by

            @backtrader this gonna work fine, considering I'm using data replay?

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

              @arrr said in Problem with custom ohlc vars:

              this gonna work fine, considering I'm using data replay?

              What should work fine? The info you have provided in this thread is close to none. You simply say that you cannot get data in __init__, which is something obvious.

              A 1 Reply Last reply Reply Quote 0
              • A
                arrr @backtrader last edited by

                @backtrader said in Problem with custom ohlc vars:

                @arrr said in Problem with custom ohlc vars:

                this gonna work fine, considering I'm using data replay?

                What should work fine? The info you have provided in this thread is close to none. You simply say that you cannot get data in __init__, which is something obvious.

                I mean calling self.make_ohlc() on every next() and operating what it returns as ohlc data

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

                  @arrr said in Problem with custom ohlc vars:

                  However if I call same self.make_ohlc() from self.next() - it works

                  I fail to understand what the problem is. You have for sure a very clear idea about what troubles you, but you have already said it works. What you want to do with the data is of course unknown.

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    arrr @backtrader last edited by

                    @backtrader said in Problem with custom ohlc vars:

                    @arrr said in Problem with custom ohlc vars:

                    However if I call same self.make_ohlc() from self.next() - it works

                    I fail to understand what the problem is. You have for sure a very clear idea about what troubles you, but you have already said it works. What you want to do with the data is of course unknown.

                    Sorry for confusing explanation. It technically works, but I did not check if it returns correct data and, as for me, the code looks pretty weird :)

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors