Backtrader Community

    • 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 do i check an order type?

    General Code/Help
    3
    5
    1305
    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.
    • Hamid Zargar
      Hamid Zargar last edited by Hamid Zargar

      I'm new to algo trading/programming and I'm trying to get my head around all this.

      I'm making a simple moving average crossover strategy for learning purposes and I was wondering how do I check the position type (whether its a long or short position) so I can exit my positions? I can't seem to find anything in the docs for this and I bet it's a simple line or attribute in one of the classes that I'm missing. If someone could post the line of code for checking an position type I would greatly appreciate it.

      Thanks,

      Hamid Zargar 1 Reply Last reply Reply Quote 0
      • Hamid Zargar
        Hamid Zargar @Hamid Zargar last edited by Hamid Zargar

        @hamid-zargar

        here is the code, I need to replace if self.buy and if self.sell with the appropriate check

            else:
                #TO CLOSE A LONG POSITION
                if self.buy() and (self.ma1 < self.ma2):
                    # SELL, SELL, SELL!!! (with all possible default parameters)
                    self.log('SELL CREATE, %.2f' % self.dataclose[0])
        
                    # Keep track of the created order to avoid a 2nd order
                    self.order = self.sell()
        
                #TO CLOSE A SHORT POSITION
                elif self.sell() and (self.ma1 > self.ma2):
                    # BUY!!! BUY!!! BUY!!!  (with all possible default parameters)
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
        
                    # Keep track of the created order to avoid a 2nd order
                    self.order = self.buy()
        
        Robin Dhillon 1 Reply Last reply Reply Quote 0
        • Robin Dhillon
          Robin Dhillon @Hamid Zargar last edited by

          @hamid-zargar the best way to resolve your issue is to head to the docs. You can essentially add order info to whenever you are generating an order so for example in a long situation:

          long_order = self.buy()
          long_order.addinfo(name = 'long entry')
          

          or you can essentially add a log statement/print each time you are sending an order in next()that is long or short.
          either way, give the docs a good read.
          Cheers

          1 Reply Last reply Reply Quote 0
          • A
            ab_trader last edited by ab_trader

            Docs - Quickstart - Do not only buy ... but SELL:

                    # Check if we are in the market
                    if not self.position:
            
                        # Not yet ... we MIGHT BUY if ...
                        if self.dataclose[0] < self.dataclose[-1]:
                                # current close less than previous close
            
                                if self.dataclose[-1] < self.dataclose[-2]:
                                    # previous close less than the previous close
            
                                    # BUY, BUY, BUY!!! (with default parameters)
                                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
            
                                    # Keep track of the created order to avoid a 2nd order
                                    self.order = self.buy()
            
                    else:
            
                        # Already in the market ... we might sell
                        if len(self) >= (self.bar_executed + 5):
                            # SELL, SELL, SELL!!! (with all possible default parameters)
                            self.log('SELL CREATE, %.2f' % self.dataclose[0])
            
                            # Keep track of the created order to avoid a 2nd order
                            self.order = self.sell()
            

            Docs - Position

            position.size > 0 - long postition
            position.size < 0 - long postition

            Also you can use self.close() to close the position.

            • 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
            Hamid Zargar 1 Reply Last reply Reply Quote 0
            • Hamid Zargar
              Hamid Zargar @ab_trader last edited by

              @ab_trader Thanks! I came to the same conclusion a couple hours after

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