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/

    Trade/Order Symbol

    General Code/Help
    3
    6
    1565
    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.
    • L
      Laurent Michelizza last edited by Laurent Michelizza

      Hi,

      From a trade, is there a way to get the symbol?

      As an example, in the code below, I would like to extract the trade symbol:

          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
              self.debug('OPERATION PROFIT, GROSS {:.2f}, NET {:.2f}'.format(trade.pnl, trade.pnlcomm))
      

      And add it to the debug output.

      Also, is there a way to do this for an order:

              if order.status in [order.Completed]:
                  if order.isbuy():
                      message = 'BUY EXECUTED, Price: {:.2f}, Cost: {:.2f}, Comms {:.2f}'
                  else:
                      message = 'SELL EXECUTED, Price: {:.2f}, Cost: {:.2f}, Comms {:.2f}'
                  self.debug(message.format(order.executed.price, order.executed.value, order.executed.comm))
      

      Thanks

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

        @laurent-michelizza said in Trade/Order Symbol:

        From a trade, is there a way to get the symbol?

        A Trade has a data attribute. From there you can use the trade.data._name attribute if you have given a name to the feed when adding it to cerebro. In many cases you want the parameter passed in as dataname during creation. In that case use trade.data.p.dataname

        1 Reply Last reply Reply Quote 1
        • L
          Laurent Michelizza last edited by

          @backtrader said in Trade/Order Symbol:

          .p.dataname

          Great thanks. I am now using trade.data._name as I am passing the dataframe in dataname somehow (instancing with Pandas). My IDE is complaining about access to a protected member though. Would it be possible to have an accessor?

          It seems to be working similarly on order which is great.

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

            @laurent-michelizza said in Trade/Order Symbol:

            My IDE is complaining about access to a protected member though. Would it be possible to have an accessor?

            The problem is that lines in data feeds can also be accessed using self.data.line_name. Because the objects are extensible and one can add new lines, it could well be conceived a line named ... name. That was the reason to keep it as _name.

            _xxxx is the convention for something which shouldn't be used.Note: convention and should. Given the dynamic nature of python, one can actually access everything.

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

              Great, thanks.

              I also found the method getdataname() which works the same, which is great. Unfortunately, I still have to use .data._name on an order. No biggie though. :) Thanks.

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

                [deleted post, wrong tab]

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