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/

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

    General Code/Help
    2
    3
    39
    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.
    • T
      TraderOnes last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • D
        dasch last edited by

        check for position size:

        if self.position.size < 0:
            # you are short
        elif self.position.size > 0:
            # you are long
        else:
             # no open position
        
        1 Reply Last reply Reply Quote 0
        • T
          TraderOnes last edited by

          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!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }