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/

    Creating a Long/Short/Exit Strategy?

    General Code/Help
    3
    5
    1504
    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.
    • R
      roony16 last edited by

      I'm relatively new to bt and have been having trouble trying to find out how to code in a long/short strategy.
      My strategy in pseudo code:
      if macd > signal & rsi > rsi_ma:
      long
      if macd < signal:
      exit long
      if macd < signal & rsi < rsi_ma:
      short
      if macd > 0:
      exit short

      I can't figure out how to exit a long position or short position given a indicator crossover.

      I see the self. position.size > 0 but need something more like self.position = long.

      Any help?

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @roony16 last edited by

        @roony16 said in Creating a Long/Short/Exit Strategy?:

        I see the self. position.size > 0 but need something more like self.position = long.

        • > 0 is long (you have taken)
        • == 0 is no position
        • < 0 is short (you have given)

        Or, what are you expecting?

        Note:
        Please format your code (see the top of the forum). Paying attention to detail is what will make an algotrader ... a good one.

        1 Reply Last reply Reply Quote 1
        • R
          roony16 last edited by

          @backtrader Thank you - I figured the position.size was either 0 or > 0 indicating that you either had an open position or no position.

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            The existence of a position (regardless of direction) is the opposite of pos == 0 and that's not > 0. The opposite is not pos or the (theoretically) less recommended pos != 0

            M 1 Reply Last reply Reply Quote 0
            • M
              Mcruzg @backtrader last edited by

              @backtrader why is this theoretically not recommended?

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