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/

    How can I add the name of the data feed to the print log for a buy or sell?

    General Code/Help
    3
    4
    91
    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.
    • Picture Perfect
      Picture Perfect last edited by

      Hi all -

      Does anyone know how to add the name of the data feed to the log for a buy or sell? I looked through the order object documentation, but didn't see something part of order.executed that makes sense to add.

      if order.status in [order.Completed]:
      		if order.isbuy():
      			self.portfolio_value = self.broker.get_value()	
      			self.percent_of_portfolio = order.executed.value / self.portfolio_value		   
      			#'''
      			self.log(
      				'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, Size %.2f, port_value %.2f percent_of_portfolio %.2f' %
      				(order.executed.price,
      				 order.executed.value,
      				 order.executed.comm,
      				 order.executed.size,
      				 self.portfolio_value,
      				 self.percent_of_portfolio))
      			#'''
      			self.buyprice = order.executed.price
      			self.buycomm = order.executed.comm
      
      		else:  # Sell
      			#'''
      			self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, Size %.2f, Value %.2f' %
      					 (order.executed.price,
      					  order.executed.value,
      					  order.executed.comm,
      					  order.executed.size,
      					  order.executed.price * order.executed.size))
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        order.executed contains the data on which the action took place.

        Try data._name if you have named it when adding it to cerebro vía adddata(xxx, name='yyyy')

        If the dataname parameter in your data feed is something meaningful like a ticker name, try: data.p.dataname (the dataname parameter can be anything, for example a handle to a database)

        1 Reply Last reply Reply Quote 1
        • Picture Perfect
          Picture Perfect last edited by

          Thanks @backtrader, didn't expect to get such a fast reply!

          tianjixuetu 1 Reply Last reply Reply Quote 0
          • tianjixuetu
            tianjixuetu @Picture Perfect last edited by

            @Picture-Perfect
            if you set data name in add data,you can use:

            self.log("buy result: data:{},buy_price:{},buy_cost:{},commission:{}".format(order.p.data._name,
                                        order.executed.price,order.executed.value,order.executed.comm))
            

            order.p.data._name

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