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/

    100% in the market strategy

    General Code/Help
    2
    3
    782
    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.
    • B
      blonc last edited by blonc

      I seem to be having a hard time building a strategy that is long/short 100% of the time. I will try to give as much info as I can think of that is needed for help, if you need more info just let me know.

             #this is called to place first order, i put one , but it still places 2 trades (see logs
              if self.position.size == 0:
                  if self.buy_sig:
                      self.buy(size=1)
                  elif self.sell_sig:
                      self.sell(size=1)
      
              #signal to buy
              if self.position.size <= 0:
                  if self.buy_sig:
                      self.buy()
              #signal to sell
              if self.position.size >= 0:
                  if self.sell_sig:
                      self.sell()
      

      I am using a sizer to reverse the order:
      cerebro.addsizer(bt.sizers.FixedReverser, stake=1)

      this is what the output log looks like to better help describe the needed results:

      2016-01-06, Buy Sig nan Sell Sig nan Position 0
      2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 0
      2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 2
      2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 2
      2016-01-07, Buy Sig 1.0 Sell Sig 0.0 Position 2
      2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 2
      2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 2
      2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position 2
      2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position 0
      2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
      2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
      2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
      2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
      
      

      the problems

      • when i enter my first trade, even with size set it still places a size order of 2, trying to reverse the order
        *when looking at the logs you can see we have a position of +2 and when the sell signal comes in it skips a 'line' then places the trade the next line. what is needed to be done to get that to flip from +2 to -2 in the same line?
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by ab_trader

        For buy signal your code issues buy order twice - in the first if and in the second if. This is a reason why you have position equal 2. In order to avoid this you need to make it as follows:

               #this is called to place first order, i put one , but it still places 2 trades (see logs
                if self.position.size == 0:
                    if self.buy_sig:
                        self.buy(size=1)
                    elif self.sell_sig:
                        self.sell(size=1)
        
                #signal to buy
                if self.position.size < 0:
                    if self.buy_sig:
                        self.buy()
                #signal to sell
                if self.position.size > 0:
                    if self.sell_sig:
                        self.sell()
        
        • 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 1
        • B
          blonc last edited by

          that solved both problems, i see the mistake now. silly clearly. thank you for the help =)

          2016-01-06, Buy Sig nan Sell Sig nan Position 0
          2016-01-06, Buy Sig nan Sell Sig nan Position 0
          2016-01-06, Buy Sig nan Sell Sig nan Position 0
          2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 0
          2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 1
          2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 1
          2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 1
          2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 1
          2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position 1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
          2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
          
          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors