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/

    Issues with closing multiple positions

    General Code/Help
    2
    6
    64
    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
      booboothefool last edited by

      Hi, during live trading, I noticed if have multiple positions open such as:

      EUR/USD Buy 1
      EUR/USD Buy 2
      EUR/USD Buy 3
      

      and I call self.close() there are instances where either they just won't close, or it'll only close one of the positions, instead of the entire net position. My code involves adding many times to a position, so I guess multiple ones get formed that way.

      Has anyone else encountered this? If so, what is the remedy? I want to make sure positions get closed when they get the signal, else it gets bad when they go the other direction.

      I was also wondering if you open a position, then restart the program, does it have a way of knowing the net position that was entered before the program was started?

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

        on oanda, close will always close the whole position. You could create own limit orders which reduces the total open positions.

        B 1 Reply Last reply Reply Quote 0
        • B
          booboothefool @dasch last edited by

          @dasch Hello my friend! This is what I mean:

          TREND ENDED, CLOSE
          2020-06-03: Order ref: 3 / Type Sell / Status Submitted / ExecType Market / Size -2936.0 / Alive True / Price 0.0 / Position 2936.0
          LIVE, 4, Data0, 0536, 06-03 21:05:00, O 0.692110, H 0.692110, L 0.691570, C 0.691590, ema_fast 0.691988, ema_mid 0.692127, ema_slow 0.692318, C[1] 0.692110, ema_anchor_fast 0.692384, ema_anchor_slow 0.691245, ema_anchor_baseline 0.672130, -0.0005199999999999649 🔴, PnL -7.046400
          trend=0
          TREND ENDED, CLOSE
          2020-06-03: Order ref: 4 / Type Sell / Status Submitted / ExecType Market / Size -2936.0 / Alive True / Price 0.0 / Position 2936.0
          LIVE, 4, Data0, 0537, 06-03 21:10:00, O 0.691590, H 0.692010, L 0.691590, C 0.692010, ema_fast 0.691993, ema_mid 0.692110, ema_slow 0.692290, C[1] 0.692110, ema_anchor_fast 0.692384, ema_anchor_slow 0.691245, ema_anchor_baseline 0.672130, +0.00041999999999997595 🟢, PnL -5.813280
          trend=0
          TREND ENDED, CLOSE
          2020-06-03: Order ref: 5 / Type Sell / Status Submitted / ExecType Market / Size -2936.0 / Alive True / Price 0.0 / Position 2936.0
          LIVE, 4, Data0, 0538, 06-03 21:15:00, O 0.692060, H 0.692060, L 0.691100, C 0.691100, ema_fast 0.691795, ema_mid 0.691966, ema_slow 0.692182, C[1] 0.692110, ema_anchor_fast 0.692384, ema_anchor_slow 0.691245, ema_anchor_baseline 0.672130, -0.0009099999999999664 🔴, PnL -8.485040
          trend=0
          TREND ENDED, CLOSE
          

          As you can see, it tries many times to close the position. Market order is Submitted but not Accepted or Completed. So order stays and I lose money! :C

          Screenshot 2020-06-03 18.33.19.png

          B 1 Reply Last reply Reply Quote 0
          • B
            booboothefool @booboothefool last edited by

            Actually, this is strange.

            I think it is saying it was Long => 2936, and tried to Close => 0, but closed twice, so now it is Short => -2936.

            I do not mean to reverse the position, simply close once so it should be 0 and the position should be gone. I will add some more clear logs to see if it works as intended.

            1 Reply Last reply Reply Quote 0
            • B
              booboothefool last edited by

              @dasch Ok, I looked into this more.

              Say I have the open position:
              USD/CHF 2359 Sell

              But I am seeing logs like:

              NO DOWNTREND, CLOSE SELL => BUY
              2020-06-04: Order ref: 4 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
              LIVE, 4, Data0, 0522, 06-04 01:05:00, O 0.961700, H 0.961730, L 0.961550, C 0.961560, ema_fast 0.961758, ema_mid 0.961704, ema_slow 0.961619, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, -9.999999999998899e-05 🔴, PnL -1.431505
              trend=0
              NO DOWNTREND, CLOSE SELL => BUY
              2020-06-04: Order ref: 5 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
              LIVE, 4, Data0, 0523, 06-04 01:10:00, O 0.961520, H 0.961600, L 0.961500, C 0.961530, ema_fast 0.961708, ema_mid 0.961679, ema_slow 0.961611, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, -2.999999999997449e-05 🟢, PnL -1.139425
              trend=0
              NO DOWNTREND, CLOSE SELL => BUY
              2020-06-04: Order ref: 6 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
              LIVE, 4, Data0, 0524, 06-04 01:15:00, O 0.961570, H 0.961720, L 0.961550, C 0.961720, ema_fast 0.961710, ema_mid 0.961685, ema_slow 0.961621, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, +0.00019000000000002348 🟢, PnL -2.989265
              trend=0
              NO DOWNTREND, CLOSE SELL => BUY
              2020-06-04: Order ref: 7 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
              LIVE, 4, Data0, 0525, 06-04 01:20:00, O 0.961680, H 0.961720, L 0.961600, C 0.961700, ema_fast 0.961708, ema_mid 0.961687, ema_slow 0.961628, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, -2.0000000000020002e-05 🟢, PnL -2.794545
              trend=0
              NO DOWNTREND, CLOSE SELL => BUY
              2020-06-04: Order ref: 8 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
              LIVE, 4, Data0, 0526, 06-04 01:25:00, O 0.961740, H 0.961750, L 0.961700, C 0.961750, ema_fast 0.961717, ema_mid 0.961696, ema_slow 0.961639, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, +4.999999999999449e-05 🟢, PnL -3.281345
              trend=0
              NO DOWNTREND, CLOSE SELL => BUY
              2020-06-04: Order ref: 9 / Type Buy / Status Submitted / ExecType Market / Size 9736.0 / Alive True / Price 0.0 / Position -9736.0
              LIVE, 4, Data0, 0527, 06-04 01:30:00, O 0.961700, H 0.961910, L 0.961670, C 0.961890, ema_fast 0.961756, ema_mid 0.961724, ema_slow 0.961662, C[1] 0.961660, ema_anchor_fast 0.961527, ema_anchor_slow 0.961770, ema_anchor_baseline 0.964495, +0.000140000000000029 🟢, PnL -4.644385
              trend=0
              NO DOWNTREND, CLOSE SELL => BUY
              

              This is wrong because backtrader thinks I have a position -9736.0, so it tries to buy 9736. In reality, I have position -2359, so it should be trying to buy 2359.

              In addition, all of these orders get canceled.

              USD/CHF	9 736	Buy	Market					0.96189			Cancelled		6043	
              USD/CHF	9 736	Buy	Market					0.96190			Cancelled		6057	
              USD/CHF	9 736	Buy	Market					0.96189			Cancelled		6063	
              USD/CHF	9 736	Buy	Market					0.96208			Cancelled		6073	
              USD/CHF	9 736	Buy	Market					0.96216			Cancelled		6085
              

              So it seems I am unable to properly close positions. Backtrader appears to be storing the wrong size and the close orders get canceled.

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

                this is not a backtrader issue, but mostly a btoandav20 issue or something with your strategy.

                do you store the position size somewhere?

                but without some workable code I cannot help you that much with your issues.

                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(); }); }