Backtrader Community

    • 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/

    Setting a commision changes buy size to be in monetary units?

    Indicators/Strategies/Analyzers
    2
    6
    1528
    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.
    • G
      Gil Keren last edited by

      Hi,

      When I use:
      cerebro.broker.setcommission(commission=0.005)

      and place a buy order:
      self.buy(data=self.data0, size=1)

      I see in 'notify_order' when the order status is order.completed:
      order.executed.price = 1.8768
      cerebro.broker.getvalue(self.datas) = 1.9732
      self.data0[0] = 1.9732

      which indicates that 1 unit of the stock was bought.
      However, when I set
      cerebro.broker.setcommission(commission=0.005, commtype=bt.comminfo.CommInfoBase.COMM_FIXED)
      and use the same buy order I get when the order is completed

      order.executed.price = 1.8768
      cerebro.broker.getvalue(self.datas) = 1.0
      self.data0[0] = 1.9732

      which indicates that the order was places to buy as many units as possible for 1 monetary unit.

      Is this a bug or a feature?
      How can I set a fixed commision in monetary units and still place buy orders where the size parameter indicates units of the stock to buy?

      Thanks!

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

        You are comparing apples and oranges actually.

        @Gil-Keren said in Setting a commision changes buy size to be in monetary units?:

        cerebro.broker.setcommission(commission=0.005)

        Here you don't specify the commission type and the original behavior is to consider you are operating on stocks with a % commission, because you haven't specified a margin (which is meant for future-like assets)

        @Gil-Keren said in Setting a commision changes buy size to be in monetary units?:

        cerebro.broker.setcommission(commission=0.005, commtype=bt.comminfo.CommInfoBase.COMM_FIXED)

        In this case you set commission type and the rest of parameters are taken at face value. The default is to operate with future-like assets.

        To understand the behavior read the parameter explanation for commission schemes

        • Docs - https://www.backtrader.com/docu/commission-schemes/commission-schemes.html
        1 Reply Last reply Reply Quote 0
        • G
          Gil Keren last edited by

          Thanks.
          I read the link you attached for the documentation.
          Note that the size parameter in buy() is interpreted as face value even I set a percentage commission:

          cerebro.broker.setcommission(commission=0.005, commtype=bt.comminfo.CommInfoBase.COMM_PERC)
          self.buy(data=self.data0, size=1)

          Then in notify_order:
          order.executed.price
          1.8768
          order.executed.comm
          0.009384 # This is 0.5% of 1.8768, makes sense so far
          cerebro.broker.getvalue(self.datas)
          1.0 # size parameter in buy() was interpreted as face value, not as amount of shares t buy.

          But my question is the following:
          How do I set the commission to be in in fixed monetary units, but still have the 'size' parameter in 'buy()' interpreted as the amount of shares to buy?

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

            The size value is always the number of shares, contracts or whatever you want to buy.

            Even if you checked the link, it is still being used wrongly. You are not buying shares, you are buying contracts. Either set stocklike=True or use the broker method addcommissioninfo and add an instance of for example: CommInfo_Stocks_Fixed

            Of fix the parameters of your own subclass like here: Docs - Extending Commissions

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

              In any case:

              • getvalue doesn't give the actual size. It gives you the value of your position which is not the size.

              To see the size, use for example:

              • self.position.size in the strategy (this is good for the 1st data in a multi-data approach and single data approaches)

              The value is given as 1.0, because in the absence of meaningful values to setcommission, the system assigns a value of 1.0 to the margin required to keep a future alive (as explained above, you are operating with future-like assets) and that's the value of your position (using the nominal value would in most cases exceed the total value of the account what would probably be puzzling)

              1 Reply Last reply Reply Quote 0
              • G
                Gil Keren last edited by

                Works like a charm, thanks for the detailed answer!

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