Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Kjiessar
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 12
    • Posts 33
    • Best 6
    • Groups 0

    Kjiessar

    @Kjiessar

    7
    Reputation
    4
    Profile views
    33
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Kjiessar Unfollow Follow

    Best posts made by Kjiessar

    • RE: Setting only the fromdate parameter in Yahoo Data Feed

      @borutf
      Would agree, looks like a bug:

      if self.p.todate is not None:
                  period2 = (self.p.todate.date() - posix).total_seconds()
                  urlargs.append('period2={}'.format(int(period2)))
      
      if self.p.todate is not None:
                  period1 = (self.p.fromdate.date() - posix).total_seconds()
                  urlargs.append('period1={}'.format(int(period1)))
      
      

      yahoo.py

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • Contributing Documentation

      Is there any way to contribute to the (already good) documentation?
      I think some images and examples at some point could help to get rid of some of the.... low quality questions (not excluding mine).

      posted in General Discussion
      Kjiessar
      Kjiessar
    • RE: Live Data Feed time steps

      @kjiessar
      Its a bit embarassing to say but I found the issue

          data = store.getdata(dataname=symbol, name=symbol,
                                  timeframe=bt.TimeFrame.Minutes, 
                                  compression=1, ohlcv_limit=70 )
      #fromDate is missing
      

      I had the parameter but with the wrong name, becuase of all the dynamic handling of the parameters, no error was raised.

      Sorry for the trouble.

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Formatting of output csv from bt.Writer

      If you want to go with writer and don't fear to post-process with pandas you can use this:

      from backtrader import WriterBase
      import collections
      import io
      import itertools
      import sys
      from backtrader.utils.py3 import (map, with_metaclass, string_types,
                                        integer_types)
      import backtrader as bt
      class WriterPlain(WriterBase):
          '''The system wide writer class.
          It can be parametrized with:
            - ``out`` (default: ``sys.stdout``): output stream to write to
              If a string is passed a filename with the content of the parameter will
              be used.
              If you wish to run with ``sys.stdout`` while doing multiprocess optimization, leave it as ``None``, which will
              automatically initiate ``sys.stdout`` on the child processes.
            - ``close_out``  (default: ``False``)
              If ``out`` is a stream whether it has to be explicitly closed by the
              writer
            - ``csv`` (default: ``False``)
              If a csv stream of the data feeds, strategies, observers and indicators
              has to be written to the stream during execution
              Which objects actually go into the csv stream can be controlled with
              the ``csv`` attribute of each object (defaults to ``True`` for ``data
              feeds`` and ``observers`` / False for ``indicators``)
            - ``csv_filternan`` (default: ``True``) whether ``nan`` values have to be
              purged out of the csv stream (replaced by an empty field)
            - ``csv_counter`` (default: ``True``) if the writer shall keep and print
              out a counter of the lines actually output
            - ``indent`` (default: ``2``) indentation spaces for each level
            - ``separators`` (default: ``['=', '-', '+', '*', '.', '~', '"', '^',
              '#']``)
              Characters used for line separators across section/sub(sub)sections
            - ``seplen`` (default: ``79``)
              total length of a line separator including indentation
            - ``rounding`` (default: ``None``)
              Number of decimal places to round floats down to. With ``None`` no
              rounding is performed
          '''
          params = (
              ('out', None),
              ('close_out', False),
      
              ('csv', False),
              ('csvsep', ','),
              ('csv_filternan', True),
              ('csv_counter', True),
      
              ('indent', 2),
              ('separators', ['=', '-', '+', '*', '.', '~', '"', '^', '#']),
              ('seplen', 79),
              ('rounding', None),
          )
      
          def __init__(self):
              self._len = itertools.count(1)
              self.headers = list()
              self.values = list()
      
          def _start_output(self):
              # open file if needed
              if not hasattr(self, 'out') or not self.out:
                  if self.p.out is None:
                      self.out = sys.stdout
                      self.close_out = False
                  elif isinstance(self.p.out, string_types):
                      self.out = open(self.p.out, 'w')
                      self.close_out = True
                  else:
                      self.out = self.p.out
                      self.close_out = self.p.close_out
      
          def start(self):
              self._start_output()
      
              if self.p.csv:
                  # self.writelineseparator()
                  self.writeiterable(self.headers, counter='Id')
      
          def stop(self):
              if self.close_out:
                  self.out.close()
      
          def next(self):
              if self.p.csv:
                  self.writeiterable(self.values, func=str, counter=next(self._len))
                  self.values = list()
      
          def addheaders(self, headers):
              if self.p.csv:
                  self.headers.extend(headers)
              pass
      
          def addvalues(self, values):
              print(values)
              if self.p.csv:
                  if self.p.csv_filternan:
                      values = map(lambda x: x if x == x else '', values)
                  self.values.extend(values)
      
          def writeiterable(self, iterable, func=None, counter=''):
              if self.p.csv_counter:
                  iterable = itertools.chain([counter], iterable)
      
              if func is not None:
                  iterable = map(lambda x: func(x), iterable)
      
              line = self.p.csvsep.join(iterable)
              self.writeline(line)
      
          def writeline(self, line):
              self.out.write(line + '\n')
      
          def writelines(self, lines):
              for l in lines:
                  self.out.write(l + '\n')
      
          def writelineseparator(self, level=0):
              sepnum = level % len(self.p.separators)
              separator = self.p.separators[sepnum]
      
              line = ' ' * (level * self.p.indent)
              line += separator * (self.p.seplen - (level * self.p.indent))
              self.writeline(line)
      
          def writedict(self, dct, level=0, recurse=False):
              pass
      

      It is the WriterFile, without seperator and with empty writedict.

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Last value of limted pattern length

      To answer my own question:

      def __init__(self):
          self.PATTERN_CDLHARAMICROSS = bt.talib.CDLHARAMICROSS(self.datas[0].open, self.datas[0].high, self.datas[0].low, self.datas[0].close)
      def next(self):
              self.log(self.PATTERN_CDLHARAMICROSS[0])
      

      gives exactly what i wanted. No doubled arrays and limiting the time line makes no sense here if it is applied to the whole line.
      Sorry for the stupid question.

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Simplest Way to Add Stop Loss & Take Profit

      @frenzyy
      Alternative
      https://www.backtrader.com/docu/strategy/#how-to-buysellclose

      take a look at the parameters
      price, limit, plimit and exectype

      posted in General Code/Help
      Kjiessar
      Kjiessar

    Latest posts made by Kjiessar

    • Multi-Broker

      Hi,
      I still trying to optimize my simulation runs.
      Optstrategy is good but I still have the issue with memory leaks and if it crashes all results are lost, so could split up and let run multiple of them but still...

      What I was thinking was to create a Broker, that handles multiple other Broker, so that at each next a strategy with it's own account (broker) is called.

      I don't got enough inside into the interplay of cerebro and broker if that would be an option.

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Transaction Analyzer: price <backtrader.linebuffer.LineBuffer object [...]>

      @kjiessar
      Aaand again. After long debugging session I found the culprit and it wasn't even in the code I had shown.

      Errorness:

      self.sell(data=self.data0, exectype=bt.Order.Limit, price=self.datas[0].close, size=entry["size"], valid=datetime.timedelta(minutes=self.p.tis))
      

      correct one:

      self.sell(data=self.data0, exectype=bt.Order.Limit, price=self.datas[0].close[0], size=entry["size"], valid=datetime.timedelta(minutes=self.p.tis))
      

      Note the proce parameter and the last [0]

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Transaction Analyzer: price <backtrader.linebuffer.LineBuffer object [...]>

      @kjiessar

      And more Information:
      GitHub: BT Transaction Analyzer

          def next(self):
              # super(Transactions, self).next()  # let dtkey update
              entries = []
              for i, dname in self._idnames:
                  pos = self._positions.get(dname, None)
                  if pos is not None:
                      size, price = pos.size, pos.price
                      if size:
                          entries.append([size, price, i, dname, -size * price])
      
              if entries:
                  self.rets[self.strategy.datetime.datetime()] = entries
      
              self._positions.clear()
      
      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Transaction Analyzer: price <backtrader.linebuffer.LineBuffer object [...]>

      @kjiessar
      In the above code sample a run is performed, just tried to put in the relevant information

      Some more info not sure if it can help but thought couldn't hurt

      Trade:

      1970-01-01 19:45:46,-5.0,2.224e-05,0,,0.0001112

      1970-01-01 19:45:54,-5.0,<backtrader.linebuffer.LineBuffer object at 0x0000029F82F3DF70>,0,,0.00011485

      OHLC

      1970-01-01 19:45:53+00:00,1629356760,2.271e-05,2.271e-05,2.271e-05,2.271e-05,0.0,0

      1970-01-01 19:45:54+00:00,1629356820,2.279e-05,2.297e-05,2.279e-05,2.297e-05,0.0,0

      1970-01-01 19:45:55+00:00,1629356880,2.275e-05,2.275e-05,2.275e-05,2.275e-05,0.0,0

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Simplest Way to Add Stop Loss & Take Profit

      @frenzyy
      Alternative
      https://www.backtrader.com/docu/strategy/#how-to-buysellclose

      take a look at the parameters
      price, limit, plimit and exectype

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • Transaction Analyzer: price <backtrader.linebuffer.LineBuffer object [...]>

      Hi,
      the output of the analyser gives object str instead of the actual price.
      Any idea what I'm doing wrong here?

          cerebro.addanalyzer(bt.analyzers.Transactions, _name='transactions')
          d =strat.analyzers.getbyname("transactions").get_analysis()
          print(d)
      

      One correct and one outputted wrong

      OrderedDict(
      [(datetime.datetime(1970, 1, 1, 19, 9, 27),
      [[23.10281162, 2.05e-05, 0, '', -0.00047360763821]]),
      [...]
      [[-23.10281162, <backtrader.linebuffer.LineBuffer object at 0x000001847D964400>, 0, '', 0.0005489228040912]]),
      [...]])

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Live Data Feed time steps

      @kjiessar
      Its a bit embarassing to say but I found the issue

          data = store.getdata(dataname=symbol, name=symbol,
                                  timeframe=bt.TimeFrame.Minutes, 
                                  compression=1, ohlcv_limit=70 )
      #fromDate is missing
      

      I had the parameter but with the wrong name, becuase of all the dynamic handling of the parameters, no error was raised.

      Sorry for the trouble.

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • Live Data Feed time steps

      Hi,

      I'm using ccxtbt or my own fork of it ccxtbt-fork.

      My issue is, that my strategy assumes time slices of a minute and the strategy produces slices of around 10 seconds (could be unrelated and based on runtime [debugging therfore slow]).

          data = store.getdata(dataname=symbol, name=symbol,
                                  timeframe=bt.TimeFrame.Minutes, 
                                  compression=1, ohlcv_limit=70 )
      

      Thats how i create the feed. I changed code in the broker (fixed bugs with order handling). But don't think I changed anything regarding the handling of the time.

      I just reinstalled the original version to check if I messed up but couldn't find any difference.

      So my expected behaviour is, that my Strategy is called once a Minute.
      Am I expecting somthing on wrong assumption? ( Don't think so, why choose timeframe and compression if not used)

      Any idea where to start to look? Is that a bug or just missing feature not yet implemented in bt-ccxt-store?

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • RE: Warning bt.analyzers.Transactions multiple transactions

      @kjiessar
      Aaaaand 5 minutes later. Trying to fix it, it seems that the issue is something else. Will come back as soon as I found out

      posted in General Code/Help
      Kjiessar
      Kjiessar
    • Warning bt.analyzers.Transactions multiple transactions

      Hi,
      I know it seems stupid but can happen, that more tan one transaction is triggering at a time.
      The bt.analyzers.Transactions will use a map and use the date as key putting in only 1 of the transactions, overriding the beforehand added.
      Code Link

      def next(self):
      [...]
              if entries:
                  self.rets[self.strategy.datetime.datetime()] = entries
      

      This can compromise your analysis, just be aware of it.

      posted in General Code/Help
      Kjiessar
      Kjiessar