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?
-
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))
-
order.executed
contains thedata
on which the action took place.Try
data._name
if you have named it when adding it tocerebro
víaadddata(xxx, name='yyyy')
If the
dataname
parameter in your data feed is something meaningful like a ticker name, try:data.p.dataname
(thedataname
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(order.p.data._name, order.executed.price,order.executed.value,order.executed.comm))
order.p.data._name