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?



  • 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

    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()


  • @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



  • 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.



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