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/

    Fixed Holding Period: Selling After 10 Bars

    General Code/Help
    2
    3
    326
    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.
    • C
      cschi last edited by

      Here are some brief points about my problem:

      l am trying to exit all positions after holding them for a period of 10 bars (not days).

      I have looked through the package site and the forums. But, most of the materials I find seem to cover date-time based exits, not bar-based exits. The only pipeline I found for a bar-based exit didn't work for my multiple feed problem:

      I managed to "panel" bit the code from the bar-based exit. It executes smoothly. It buys the correct stocks. However, the problem is: it mixes up the exit signals for the stocks.

      Here is the complete code I am using:

      # Trading Strategy
          class FixedExitStrategy(bt.Strategy):
      
              def __init__(self):       
                  pass 
              
              def start(self):
                  self.size = None
      
              def next(self): 
      
                  for i, d in enumerate(self.getdatanames()):
                      pos = self.getposition(data=self.getdatabyname(d))
                      
                      if not pos.size:
                          if self.getdatabyname(d).signal[0] != self.getdatabyname(d).signal[-1]:
                              if self.getdatabyname(d).signal[0] == 1:
                                  self.size = self.getdatabyname(d).position_size[0]
                                  self.buy(data=self.getdatabyname(d), size=self.size)
                                  self.bought_bar = len(self)
      
                      if pos.size:
                          self.bars_since_purchase = len(self) - self.bought_bar
                          if self.bars_since_purchase == 10:   
                              self.close(data=self.getdatabyname(d), size=self.size)
      
      1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld last edited by

        it seems that you are using the same bought_bar and bars_since_purchase variables for all data feeds instead of keeping those attributes per data feed.

        Something like:

        class FixedExitStrategy(bt.Strategy):
        
            def __init__(self):
                pass
        
            def start(self):
                self.bought_bars={d:0 for d in self.getdatanames()}
                self.bought_sizes={d:0 for d in self.getdatanames()}
        
            def next(self):
        
                for i, d in enumerate(self.getdatanames()):
                    data = self.getdatabyname(d)
                    pos = self.getposition(data=data)
        
                    if not pos.size:
                        if data.signal[0] != data.signal[-1]:
                            if data.signal[0] == 1:
                                self.bought_sizes[d] = data.position_size[0]
                                self.buy(data=data, size=self.bought_sizes[d])
                                self.bought_bars[d] = len(self)
        
                    if pos.size:
                        if (len(self) - self.bought_bars[d]) == 10:
                            self.close(data=data, size=self.bought_sizes[d])
        

        And I'm not sure where the self.getdatabyname(d).position_size is coming from - sorry.

        1 Reply Last reply Reply Quote 3
        • C
          cschi last edited by

          Thank you for the help. It works properly now.

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