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/

    How to get backtrader to recognize current position in next() ?

    General Code/Help
    2
    2
    126
    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.
    • Jeffrey C F Wong
      Jeffrey C F Wong last edited by

      Hi everyone, I have 2 closing conditions but it doens't seem to recognize any of the conditions. At the same time, there is no coding error but no trades. Anyone know why this is happening?

                  if not self.position:
      
                      if self.crossover:                  
                          self.buy()
                  
                      if self.crossunder:        
                          self.sell()
                  
                  if self.position.size!=0:                        
      
                      if self.position is self.buy and self.crossover_exit:
                          self.close()            
                     
                      if self.position is self.sell and self.crossunder_exit:
                          self.close()
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        i've never seen this before and this may be the reason

        if self.position is self.buy
        

        in your case i would simplify the script like this (not tested)

                    if not self.position:
        
                        if self.crossover:                  
                            self.buy()
                    
                        if self.crossunder:        
                            self.sell()
                    
                    if self.position.size != 0:                        
        
                        if self.crossunder_exit or self.crossover_exit:
                            self.close()
        

        if you want to obtain position direction, than for long self.position.size > 0 and for short self.position.size < 0.

        • 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
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors