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/

    How to write algorithm for both long and short selling?

    Indicators/Strategies/Analyzers
    2
    5
    2316
    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.
    • S
      Sasha last edited by Sasha

      my code works correctly only for long positions, and in this algorithm I added a code of short position, which I wrote manually, but the code does not work correctly
      In the documentation I read about the signal, but did not understand how to make a profit, and stop loss separately for long and short positions
      For example,
      For short positions
      I have 100$
      BUY EXECUTED, Price: 37.27
      SELL EXECUTED, Price: 40.42
      OPERATION PROFIT, GROSS 3.15, NET 3.15
      here I stayed in + (cash=100+3.15), and had to be in - (cash=100-3.15)

      S 1 Reply Last reply Reply Quote 0
      • S
        Sasha @Sasha last edited by

        @sasha said in How to write algorithm for both long and short selling?:

        my code works correctly only for long positions, and in this algorithm I added a code of short position, which I wrote manually, but the code does not work correctly
        In the documentation I read about the signal, but did not understand how to make a profit, and stop loss separately for long and short positions
        For example, I have 1000 in stock. In Long I buy a share for 100, then I sell for 102.
        profit = 2, total 1002
        for this code works well
        For short positions,
        For shorts
        I have 100$
        BUY EXECUTED, Price: 37.27
        SELL EXECUTED, Price: 40.42
        OPERATION PROFIT, GROSS 3.15, NET 3.15
        here I stayed in + (cash=100+3.15), and had to be in - (cash=100-3.15)

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

          @sasha

          If you want to share your failure in coding and docs understanding, than It is enough to have a single post about it. I am sorry for this, but try harder and you will succeed.

          If you want to ask the question and get an answer, than you may want to ask the question and maybe even to post your code.

          • 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
          S 1 Reply Last reply Reply Quote 2
          • S
            Sasha @ab_trader last edited by

            @ab_trader Thanks for the advice. I just wanted to know how to write the code for the short and long positions.

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

              @sasha said in How to write algorithm for both long and short selling?:

              I just wanted to know how to write the code for the short and long positions.

              if not self.position:
                  # open long
                  if <long entry signal>:
                      self.buy()
                  # open short
                  elif <short entry signal>:
                      self.sell()
              
              else:
                  # close long or short
                  if <long exit signal> or <short exit signal>:
                      self.close()
              
              • 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
              1 Reply Last reply Reply Quote 2
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors