Navigation

    Backtrader Community

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

    kane

    @kane

    1
    Reputation
    67
    Profile views
    2
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    kane Unfollow Follow

    Best posts made by kane

    • RE: Momentum strategy not working properly

      The momentum indicator and Cerebro both look good. The problem is in Strategy. @tianjixuetu is correct when he said that you need to test the target_data and not the first data.

      More specifically, what's happening is that in rebalance_profolio this section of code:

              for i, d in enumerate(self.rankings):
                  if self.getposition(self.data).size > 0:
                      if (i > num_stocks * 0.05 or d < self.inds[d]["mav"]) and self.savedPositions.get(d._name, None) != None:
                          self.order = self.close(d, exectype=bt.Order.Close)
      

      is iterating over a list of stocks (in ranked order) but it's determining the position size from just the first data set that it finds. You need something like the following (note lined 2):

              for i, d in enumerate(self.rankings):
                  if self.getposition(d.data).size > 0:
                      if (i > num_stocks * 0.05 or d < self.inds[d]["mav"]) and self.savedPositions.get(d._name, None) != None:
                          self.order = self.close(d, exectype=bt.Order.Close)
      
      

      There appears to be a similar problem with the buy loop also.

      posted in General Code/Help
      K
      kane

    Latest posts made by kane

    • RE: ImportError: cannot import name MIN_PER_HOUR - when trying to plot

      @thorsten99 I installed matplotlib 3.3.0 just yesterday and started having problems around dates. Different from yours (ImportError: cannot import 'warnings') but it might not be a coincidence. Try installing matplotlib version 3.2.2.

      posted in General Code/Help
      K
      kane
    • RE: Momentum strategy not working properly

      The momentum indicator and Cerebro both look good. The problem is in Strategy. @tianjixuetu is correct when he said that you need to test the target_data and not the first data.

      More specifically, what's happening is that in rebalance_profolio this section of code:

              for i, d in enumerate(self.rankings):
                  if self.getposition(self.data).size > 0:
                      if (i > num_stocks * 0.05 or d < self.inds[d]["mav"]) and self.savedPositions.get(d._name, None) != None:
                          self.order = self.close(d, exectype=bt.Order.Close)
      

      is iterating over a list of stocks (in ranked order) but it's determining the position size from just the first data set that it finds. You need something like the following (note lined 2):

              for i, d in enumerate(self.rankings):
                  if self.getposition(d.data).size > 0:
                      if (i > num_stocks * 0.05 or d < self.inds[d]["mav"]) and self.savedPositions.get(d._name, None) != None:
                          self.order = self.close(d, exectype=bt.Order.Close)
      
      

      There appears to be a similar problem with the buy loop also.

      posted in General Code/Help
      K
      kane