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

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

  • 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.percent_of_portfolio = order.executed.value / self.portfolio_value		   
    				'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f, Size %.2f, port_value %.2f percent_of_portfolio %.2f' %
    			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.size))

  • administrators

    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)

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

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

    self.log("buy result: data:{},buy_price:{},buy_cost:{},commission:{}".format(,

Log in to reply