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/

    Never Closes Position

    Indicators/Strategies/Analyzers
    execution orders help-needed
    3
    9
    181
    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.
    • S
      SSbilis last edited by

      Hello,
      I have a problem with the order execution of my algorithm. Even though the conditions are met it only executes the order once at the beginning of the time period and then it just holds the position forever.

          def next(self):
              self.status = "neutral"
      
              if not self.position:
      
                  if self.beta.spread[0] >= self.beta.spread_2sd[0]:
                      self.log(f'BUY CREATE {1} ETH, @ {self.datas[0].close[0]} ')
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy(data=self.datas[0], size=1)
      
                      self.log(f'SHORT CREATE  {self.beta.beta[0]:.3f} BTC, @ {self.datas[1].close[0]}')
                      self.order = self.sell(data=self.datas[1], size=self.beta.beta[0])
                      self.status = "long spread"
      
      
                  elif self.beta.spread[0] <= self.beta.spread_m2sd[0]:
                      self.log(f'BUY CREATE {self.beta.beta[0]:.3f} BTC, @ {self.datas[1].close[0]}')
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy(data=self.datas[1], size=self.beta.beta[0])
      
                      self.log(f'SHORT CREATE {1} ETH, @ {self.datas[0].close[0]}')
                      self.order = self.sell(data=self.datas[0], size=1)
                      self.status = "short spread"
      
              elif self.position.size !=0:
      
                  if self.status == "long spread" and self.beta.spread[0] == self.beta.spread_sma15[0]:
      
                      self.log(f'CLOSE LONG {1} ETH, @ {self.datas[0].close[0]}')
                      self.order = self.sell(data=self.datas[0], size=1)
                      # Keep track of the created order to avoid a 2nd order
      
                      self.log(f'COVER SHORT {self.beta.beta[0]:.3f} BTC, @ {self.data[1].close[0]}')
                      self.order = self.buy(data=self.data1, size=self.beta.beta[0])
      
                      self.status = "neutral"
      
      
      
                  elif self.status == "short spread" and self.beta.spread[0] == self.beta.spread_sma15[0]:
      
                      self.log(f'COVER SHORT {1} ETH, @ {self.datas[0].close[0]}')
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy(data=self.datas[0].close, size=1)
      
                      self.log(f'COVER LONG {self.beta.beta[0]:.3f} BTC, @ {self.datas[1].close[0]}')
                      self.order = self.sell(data=self.datas[1].close, size=self.beta.beta[0])
      
                      self.status = "neutral"
      
      A 1 Reply Last reply Reply Quote 0
      • A
        ab_trader @SSbilis last edited by

        @ssbilis typically orders are not executed if there is not enough cash on the account. i would recommend you to add more logging in your script to see what is actually going on.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        S 1 Reply Last reply Reply Quote 1
        • S
          SSbilis @ab_trader last edited by

          @ab_trader
          Cash isnt the problem. If I remove the
          if not self.position it makes a bunch of trades, but I dont want it to do that.
          I dont want it to re-enter if I already have an open position. Also, I dont understand why it never closes the position utilizing the conditions that are after the else:
          What logging do you recommend?
          Currently, I have

              def log(self, txt, dt=None):
                  dt = dt or self.data.datetime[0]
                  if isinstance(dt, float):
                      dt = bt.num2date(dt)
                  print("%s, %s" % (dt.date(), txt))
          

          Which ends up looking like this

          Starting Portfolio Value: 1000000.00
          2020-11-09, BUY CREATE 0.533 BTC, @ 15332.32
          2020-11-09, SHORT CREATE 1 ETH, @ 444.16
          ==================================================
          Starting Value - 1000000.00
          Ending   Value - 1016074.43
          ==================================================
          Final Portfolio Value: 1016074.43
          
          run-out A 2 Replies Last reply Reply Quote 0
          • run-out
            run-out @SSbilis last edited by

            @ssbilis I'm not sure your conditions are being met on the second half of the transaction. There is a log statement at the beginning of each and you have no log outputs. The problem is likely in here although I have no way of knowing:

            self.beta.spread[0] == self.beta.spread_sma15[0]
            

            RunBacktest.com

            S 1 Reply Last reply Reply Quote 1
            • S
              SSbilis @run-out last edited by

              @run-out I don't get it. This should work. You can even see on the graph that the condition is clearly met.
              Would it help if I provided more of my code?

              Untitled.png

              run-out 1 Reply Last reply Reply Quote 0
              • A
                ab_trader @SSbilis last edited by

                @ssbilis said in Never Closes Position:

                What logging do you recommend?

                Usually I recommend to print all data related to the issue in the script. In your case everything related to position closing algorithm which apparently doesn't work. So the following maybe printed (based on your script): self.status, self.beta.spread[0], self.beta.spread_sma15[0], self.beta.beta[0] as well as prices which are used to execute orders. Cash as well.

                Than looking thru this data you can see if your conditions met or not. Until that time I would agree with @run-out.

                • If my answer helped, hit reputation up arrow at lower right corner of the post.
                • Python Debugging With Pdb
                • New to python and bt - check this out
                1 Reply Last reply Reply Quote 1
                • run-out
                  run-out @SSbilis last edited by

                  @ssbilis In this line:

                  @run-out said in Never Closes Position:

                  self.beta.spread[0] == self.beta.spread_sma15[0]

                  These two values must be exactly equal for this to be true. Is that the case?

                  RunBacktest.com

                  S 2 Replies Last reply Reply Quote 1
                  • S
                    SSbilis @run-out last edited by

                    @run-out I dont know.
                    But I changed the conditions to
                    self.beta.spread[0] <= self.beta.spread_sma15[0]
                    and
                    self.beta.spread[0] >= self.beta.spread_sma15[0]
                    and I get the same error

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

                      @run-out Well I fixed it.
                      It was a stupid mistake.
                      I am initializing self.status outside of the if statement and it is becoming ='neutral' after each trade, which is why then the closing executions never meet part of the condition

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