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/

    it seems dead loop if just simple cross strategy

    Indicators/Strategies/Analyzers
    2
    3
    65
    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.
    • G
      goldcar last edited by

      In my market, it can send short order not just close the long order. So I make a simple strategy when KDJ's K cross D then place long order, and close short-order at same time(if it has), if K lower then D, then place short order and close long order at same time.

          if self.position.size == 0:
                   if self.k[0] > self.d[0] :
                          self.order = self.buy()
                   else:
                         self.order = self.sell()
      
          elif self.position.size < 0:
                   if self.k[0] > self.d[0] :
                       self.order = self.close()
      
          elif self.position.size > 0:
                   if self.k[0] < self.d[0] :
                      self.order = self.close()
      

      It seems mess . It place orders ramdom, seems the previous order not finished then place new order, or Closing order not finish then come up next order...not sure.. just like dead loop till funds goes down to reject new orders.

      Any suggestion for this?

      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @goldcar last edited by

        @goldcar said in it seems dead loop if just simple cross strategy:

        if self.position.size == 0:
                 if self.k[0] > self.d[0] :
                        self.order = self.buy()
                 else:
                       self.order = self.sell()
        
        elif self.position.size < 0:
                 if self.k[0] > self.d[0] :
                     self.order = self.close()
        
        elif self.position.size > 0:
                 if self.k[0] < self.d[0] :
                    self.order = self.close()
        

        You will only execute one of these conditionals per loop. This means that you cannot close and go short for example, in the same loop, using this code. You should also note that the buys and sells will not happen instantly. So to correct you may wish to try the following:

        if self.k[0] > self.d[0]:
            if self.position:
                self.close()
            self.order = self.buy()
        
        elif self.k[0] < self.d[0]:
            if self.position:
                self.close()
            self.order = self.sell()
        

        RunBacktest.com

        G 1 Reply Last reply Reply Quote 0
        • G
          goldcar @run-out last edited by

          @run-out said in it seems dead loop if just simple cross strategy:

          @goldcar said in it seems dead loop if just simple cross strategy:

          if self.position.size == 0:
                   if self.k[0] > self.d[0] :
                          self.order = self.buy()
                   else:
                         self.order = self.sell()
          
          elif self.position.size < 0:
                   if self.k[0] > self.d[0] :
                       self.order = self.close()
          
          elif self.position.size > 0:
                   if self.k[0] < self.d[0] :
                      self.order = self.close()
          

          You will only execute one of these conditionals per loop. This means that you cannot close and go short for example, in the same loop, using this code. You should also note that the buys and sells will not happen instantly. So to correct you may wish to try the following:

          if self.k[0] > self.d[0]:
              if self.position:
                  self.close()
              self.order = self.buy()
          
          elif self.k[0] < self.d[0]:
              if self.position:
                  self.close()
              self.order = self.sell()
          

          oh...I see. thanks a lot

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