For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Double commission when switching from long to short (or viceversa)

  • I noticed that when creating a sell order when for a strategy that's already long that exceeds the size hold it charges the commission twice. The commission is a custom one (implemented by extending CommInfoBase) and is not a percentage of the total value.

    More specifically:

    • let's say that there is a fixed commission of 5$ for each transaction
    • start by buying 100 shares -> comm charged is 5$
    • sell 200 shares -> comm shared is 10$ (should be 5$)

    Looking at the code in, the commission is charged once because the position is closed and then again for the open of a new position.

    Is this the expected behaviour? Is there a way that I can avoid the double commission?

    Thank you,

  • administrators

    Commision is only charged once per operation. But there is no code and as such:

    • How you switch from long to short .... ???
    • What your custom commission scheme does ... ???

    Implemeting a Fixed commission scheme, in any case, doesn't need a custom commission scheme. You simply have to use bt.CommInfoBase.COMM_FIXED or bt.CommissionInfo.COMM_FIXED (shorter) to either

    • Instantiate a CommissionInfo instance and use broker.addcomissioninfo
    • Directly use broker.setcommissioninfo

    Docs - Broker

  • I'm aware that using a fixed commission does not need a custom commission scheme. I just wanted to provide an example for the sake of brevity. Here is the actual commission scheme used (verified it returns the correct value of 5$ for small transactions):

    class CustomStockCommInfo(bt.CommInfoBase):
        params = (
            ('commtype', bt.CommInfoBase.COMM_FIXED),
        def _getcommission(self, size, price, pseudoexec):
            sub2000 = min(abs(size), 2000)
            over2000 = max(0, abs(size) - 2000)
            cost = sub2000 * 0.01 + over2000 * 0.005
            tot3p = abs(size) * price * 0.03
            ret = min(max(cost, 5), 60, tot3p)
            return ret

    How do I switch from long to short:

    • to initiate the long position I do:
      self.order = = self.datas[0], size = 100)
    • to switch to short, I do:
      self.order = self.sell(data = self.datas[0], size = 200)

  • Also, realized that I might have sounded a bit harsh.

    I want to thank you for the great platform! And for taking the time to reply! I really appreciate it!

  • @Mindyou @backtrader Facing an exactly similar issue when switching from long to short. What might be the reason for this? And any insights on resolving this?

Log in to reply