Backtrader Community

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

    momentum

    @momentum

    8
    Reputation
    361
    Profile views
    31
    Posts
    0
    Followers
    1
    Following
    Joined Last Online

    momentum Unfollow Follow

    Best posts made by momentum

    • RE: I want to apply multiple filters to a stock universe of 193 stocks

      I guess by filters you mean indicators and not data filters. This is an example having a list of indicators (in this case SimpleMovingAverage applied on close), one per data feed.

      class TestStrategy(bt.Strategy):
      
          params = (
              ('maperiod', 15),
          )
      
          def __init__(self):
              self.smas = []
              for d in self.datas:
                  self.smas.append(bt.indicators.SimpleMovingAverage(d.lines.close, period=self.params.maperiod))
      
      posted in General Discussion
      M
      momentum
    • RE: An indicator that knows if I have a position or not

      The strategy can turn on/off a parameter of the indicator according to whether in trade or not. Have a look at the dynamic indicator example
      https://www.backtrader.com/blog/posts/2018-02-06-dynamic-indicator/dynamic-indicator.html

      posted in Indicators/Strategies/Analyzers
      M
      momentum
    • RE: A day buy and close trade with cheat on open and stop loss

      Another solution that does not use cheat on open. It is a cheat-on-close broker and you buy on open by disabling the coc and sell on close by enabling it.

      class TestStrategy(bt.Strategy):
      
          def next(self):
              order1 = self.close(self.datas[0], coc=True)
              order2 = self.buy(self.datas[0], coc=False)
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          cerebro.broker.set_coc(True)
          ....
      
      posted in General Code/Help
      M
      momentum
    • RE: I want to apply multiple filters to a stock universe of 193 stocks

      Haven't tested it, but I think your csv file looks more like the following.

      params = (
      # ('nullvalue', float('NaN')),
      ('dtformat', '%Y-%m-%d %H:%M:%S'),
      ('datetime', 5),
      ('Open', 0),
      ('High', 1),
      ('Low', 2),
      ('Close', 3),
      ('Volume', 4),
      )
      
      posted in General Discussion
      M
      momentum
    • RE: An indicator that knows if I have a position or not

      I even like better the _nextforce = True pointed out to me by @backtrader some time ago.

      class CustomIndicator(bt.Indicator):
          _nextforce = True
      ...
      
      posted in Indicators/Strategies/Analyzers
      M
      momentum
    • optimization and sharperatio and periodstats analyzers

      Hi,

      when I am using SharpeRatio or PeriodStats analyzers with optimization, I get the following error

      TypeError: __init__() missing 9 required positional arguments: 'status', 'dt', 'barlen', 'size', 'price', 'value', 'pnl', 'pnlcomm', and 'tz'
      

      Both analyzers are based on TimeReturn which does not give any error if added as analyzer.

      Any feedback would be really helpful.

      Thanks

      posted in General Code/Help
      M
      momentum
    • RE: Let strategies communicate with each other

      Without having tested it, perhaps you can use in next of strategy B self.cerebro.runningstrats[self._id ^ 1] to get access to strategy A

      posted in Indicators/Strategies/Analyzers
      M
      momentum
    • Trades Observer with multiple trades/products

      Hi,

      if there are multiple trades on different products, then pnlplus and pnlminus in Trades observer will hold just the pnl of the last trade traversed.

          def next(self):
              for trade in self._owner._tradespending:
                  if trade.data not in self.ddatas:
                      continue
      
                  if not trade.isclosed:
                      continue
      
                   if trade.pnl >= 0:
                      self.lines.pnlplus[0] =  trade.pnl
                  else:
                      self.lines.pnlminus[0] =  trade.pnl
      

      I guess something like this is needed

          def next(self):
              for trade in self._owner._tradespending:
                  if trade.data not in self.ddatas:
                      continue
      
                  if not trade.isclosed:
                      continue
      
                  if trade.pnl >= 0:
                      self.lines.pnlplus[0] = np.nansum([self.lines.pnlplus[0], trade.pnl])
                  else:
                      self.lines.pnlminus[0] = np.nansum([self.lines.pnlminus[0], trade.pnl])
      

      I am using version 1.9.70.122 in case that is relevant.

      Cheers

      posted in General Code/Help
      M
      momentum

    Latest posts made by momentum

    • optimization and sharperatio and periodstats analyzers

      Hi,

      when I am using SharpeRatio or PeriodStats analyzers with optimization, I get the following error

      TypeError: __init__() missing 9 required positional arguments: 'status', 'dt', 'barlen', 'size', 'price', 'value', 'pnl', 'pnlcomm', and 'tz'
      

      Both analyzers are based on TimeReturn which does not give any error if added as analyzer.

      Any feedback would be really helpful.

      Thanks

      posted in General Code/Help
      M
      momentum
    • Optstrategy, preload and runonce relation

      Hi,

      I wonder why when runonce is False, then the data are not preloaded in optimization. I also wonder what happens when someone is using _nextforce = True instead. It seems to me that preloading will take place as the indicators that will call _disable_runonce are added afterwards.

      Thanks

              iterstrats = itertools.product(*self.strats)
              if not self._dooptimize or self.p.maxcpus == 1:
                  # If no optimmization is wished ... or 1 core is to be used
                  # let's skip process "spawning"
                  for iterstrat in iterstrats:
                      runstrat = self.runstrategies(iterstrat)
                      self.runstrats.append(runstrat)
                      if self._dooptimize:
                          for cb in self.optcbs:
                              cb(runstrat)  # callback receives finished strategy
              else:
                  if self.p.optdatas and self._dopreload and self._dorunonce:
                      for data in self.datas:
                          data.reset()
                          if self._exactbars < 1:  # datas can be full length
                              data.extend(size=self.params.lookahead)
                          data._start()
                          if self._dopreload:
                              data.preload()
      
                  pool = multiprocessing.Pool(self.p.maxcpus or None)
                  for r in pool.imap(self, iterstrats):
                      self.runstrats.append(r)
                      for cb in self.optcbs:
                          cb(r)  # callback receives finished strategy
      
                  pool.close()
      
      posted in General Code/Help
      M
      momentum
    • RE: Can somebody explain me how to use the heikin ashi indicator into a strategy

      You don't pass to the SimpleMovingAverage the ha_close line (self.hk.close) but rather a specific value of it (self.hk.close[0]).

      If you look at the implementation of class HeikinAshi, it has line alias

          lines = ('ha_open', 'ha_high', 'ha_low', 'ha_close',)
      
          linealias = (
              ('ha_open', 'open',),
              ('ha_high', 'high',),
              ('ha_low', 'low',),
              ('ha_close', 'close',),
          )
      
      posted in General Code/Help
      M
      momentum
    • RE: Reading PANDA CVS

      The error has to do with division with zero, so I would suspect it has to do with the indicator. There is a parameter for RSI the safediv that you can set to True.

      If you think that problem is with reading data, then I would suggest you check that bar(s) in next has the correct data

      Hope this helps

      posted in General Discussion
      M
      momentum
    • Rollover with data feeds added in cerebro

      Hi,

      I am trying to change the rollover class to use it with input data feeds added in cerebro. I guess in this case next for the data feeds is also called by cerebro and thus no need to be called within rollover. I wonder how I can check for the end of the data feed without using the check for _next being False.

      Many thanks

      posted in General Code/Help
      M
      momentum
    • RE: cerebro.addbroker()?
      cerebro.setbroker(<instance of your broker class>)
      
      posted in General Code/Help
      M
      momentum
    • RE: I want to apply multiple filters to a stock universe of 193 stocks

      Haven't tested it, but I think your csv file looks more like the following.

      params = (
      # ('nullvalue', float('NaN')),
      ('dtformat', '%Y-%m-%d %H:%M:%S'),
      ('datetime', 5),
      ('Open', 0),
      ('High', 1),
      ('Low', 2),
      ('Close', 3),
      ('Volume', 4),
      )
      
      posted in General Discussion
      M
      momentum
    • RE: Let strategies communicate with each other

      Without having tested it, perhaps you can use in next of strategy B self.cerebro.runningstrats[self._id ^ 1] to get access to strategy A

      posted in Indicators/Strategies/Analyzers
      M
      momentum
    • RE: I want to apply multiple filters to a stock universe of 193 stocks

      I guess by filters you mean indicators and not data filters. This is an example having a list of indicators (in this case SimpleMovingAverage applied on close), one per data feed.

      class TestStrategy(bt.Strategy):
      
          params = (
              ('maperiod', 15),
          )
      
          def __init__(self):
              self.smas = []
              for d in self.datas:
                  self.smas.append(bt.indicators.SimpleMovingAverage(d.lines.close, period=self.params.maperiod))
      
      posted in General Discussion
      M
      momentum
    • RE: An indicator that knows if I have a position or not

      I even like better the _nextforce = True pointed out to me by @backtrader some time ago.

      class CustomIndicator(bt.Indicator):
          _nextforce = True
      ...
      
      posted in Indicators/Strategies/Analyzers
      M
      momentum