For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Confused - How to find out, "am I LONG or am I Short?"...only: "Yes you are!" ?



  • Hey guys,
    it is so basic but a solution is not researchable for me...
    May I ask: How to find out, if my actual position is LONG or Short within "next" of the strategy-class?

    Every Quickstart-example has this Checkout:

            # Check if we are in the market
            if not self.position:
    

    Perfect, also possible to check position.size, position.price, len(self.position).
    But not if it is SELL or BUY?
    https://www.backtrader.com/docu/position/

    "notify_order" has order-Methods:

    • isbuy(): returns bool indicating if the order buys
    • issell(): returns bool indicating if the order sells

    https://www.backtrader.com/docu/order
    But how to use them within "next" of the strategy-class?

    Should look like this somehow

    # Check if we are in the market
            if not self.position:
    
                if buy_signal:
                    self.order = self.buy()
    
                if self.sell_signial:
                    self.order = self.sell()
            else:
                if bt.And(we-are-long, self.sell_signial):
                    self.order = self.close()
                
                if bt.And(we-are-short, self.buy_signial):
                    self.order = self.close()
    

    But how to define "we-are-short", "we-are-long" ???
    I tried alot, but only error-messages.
    Any ideas?



  • check for position size:

    if self.position.size < 0:
        # you are short
    elif self.position.size > 0:
        # you are long
    else:
         # no open position
    


  • hey yes,
    Works!
    Sorry to mention: I printed out position.size to see, if it is going negativ...but it was allways positive, Long and Short.

    But anyway...somehow it is not...maybe I was looking wrong, or formating-error.

    Now it works with no error.
    Thanks so much for quick reply!


Log in to reply
 

});