Order
-
Hi! I would like to clarify for the definition of order.executed.value and how the trade.pnl is calculated in backtrader.
If I am using multiple datasets, is it possible if I calculate trade.pnl for each datasets? And if I am backtesting for FX, how do i obtain the net profit in terms of pips?
Thankss :)
-
A
Trade
is always a per-data thing. Several orders will be grouped to form a trade. Unless the questions is not understood, theTrade
is already doing what you want.The
order.value
is the value of your position. For a long position of100
shares at price10
, the value is100 * 10 = 1000
. It is always given as a positive value. You can multiply it by the sign of the sign if you want to distinguish between short and long positions.For futures, the value of an order is margin needed to cover that position.
There is no automatic calculation to obtain the net profit in terms of pips. A custom
CommissionInfo
scheme would be needed. You are probably referring to calculations like the ones explained here: -
Thanks @backtrader ! For the case of multiple data feeds, is it possible to refer to the trade.pnl of each dataset? (Maybe sth like Trade.pnl(self.data1))
-
Thanks @backtrader ! For the case of multiple data feeds, is it possible to refer to the trade.pnl of each dataset? (Maybe sth like Trade.pnl(self.data1))
No. Each
Trade
you get notified overnotify_trade
is for a data and it has adata
attribute.def notify_trade(self, trade): thedata = trade.data
Although not foreseen as such, you can access the list of trades per data at
self._trades
in the strategy, which is a dictionary first with the data as the key and then the specifictradeid
. If not tradeid has been specified (the usual case), the trades will be under the defaulttradeid=0
tradesfordata = self._trades[data][0] # list of trades for data
-
Thanks @backtrader !! Is it possible to access the opening price and closing price of an order?
-
Orders have a request price and an execution price.
For trades: Docs - Trade
-
Thanks @backtrader . For the order's opening price and closing price, how do i check the value of the opening and closing price?
Also, for the statistics that are provided by backtrader, can we check for the following info too ? The info includes
largest profit trade, largest loss trade, average profit trade, average loss trade, maximum number of consecutive wins, maximum number of consecutive loss, and number of trades. -
@KT If a built-in indicator is not defined (and maybe check PyFolio and read about PyFolio integration), you may need to define your own analyzers to do this: see documentation on analyzers, and an analyzers reference with list of provided analyzers.
-
@Curtis-Miller Thanks!! I will check the PyFolio