Trade/Order Symbol
-
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
-
@laurent-michelizza said in Trade/Order Symbol:
From a trade, is there a way to get the symbol?
A
Trade
has adata
attribute. From there you can use thetrade.data._name
attribute if you have given a name to the feed when adding it tocerebro
. In many cases you want the parameter passed in asdataname
during creation. In that case usetrade.data.p.dataname
-
@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.
-
@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. -
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. -
[deleted post, wrong tab]