Perncent PnL on each trade
Strategyand the provided
Tradeobject I am able to get the Absolute Gross PnL and Net PnL but for many cases we need Percent PnL to compare one trade to another irrespective of the capital invested in each one.
How can we access such information within
That value is not calculated.
Thank you for replying promptly as always :-)
I understand that it is not calculated but how can we calculate this from within
I believe we should enhance the Trade class/object with more information about the Trade so that we can calculate whatever metrics on a Trade level.
Tradehas attributes like
price(average price of the different orders that make up a trade) and
value. You may use them to calculate your total value (in case
valuewere not right) and from there the %.
See: Docs - Trade
Mostly we want to analyse the returns, etc. for a
Tradeafter it is closed from within the
But when the
notify_trademethod is called after a
Tradeis closed, the supplied
Tradeobject does not have any size, price, etc. i.e.
Trade.priceare set to zero.
When closed, it has no
sizeand therefore no associated meaningful price.
I understand... but there should be a way to see what happened in this trade after it is closed to calculate metrics and analyse the trade.
Will turning trade history on in cerebro/strategy help with this?
Tradeis an entity giving you the actual status after each order. It can increase size, decrease size, increase again, decrease again, increase 10 times in a row, decrease only 1.
To keep track of it, you can activate trade history and then operate on that. See:
- Docs - Cerebro and look for the parameter
- Docs - Strategy and look for the method
or the post
- Docs - Cerebro and look for the parameter
Richard O'Regan last edited by Richard O'Regan
Just chipping in my 2p worth.. hopefully it's worth more than 2p(!)
Praveen I just sent you a private message, explaining how I solved R-multiple trades, but I re-read your question and perhaps what I wrote isn't relevent.
Ok just so I understand you.
Instead of seeing absolute pnl in points, you would like to see this expressed as a percentage. Is this correct?
e.g. for DJ, you you buy and sell and make a profit of +56.8 points.. which may work out to be e.g. +0.1% change etc
Or do you wish to see it as a percent of your account value?
If the former, you would use;
(loss or gain in points from your trade) / initial price of market * 100%
Using code from trade object:
loss or gain is stored Trade.pnl
initial price of market is stored in Trade.price
You can now calculate the percentage quite easily.
If you would like the Analyzer metrics to work on this percentage instead of default points. I found a way to modify Trade() so Trade.pnl gives me R Multiples (based on initial risk) instead of absolute points won or lost. I sent you a private message on what I did. The cool thing is, the analzyers e.g. SQN or my Kelly, now work on R Multiples values not the default points won or lost..
You mention Trade attribute not having size, price etc when closed. I didn't find this. The attributes are all there for me. Assuming I understood you correctly.
E.g. here is a buy and sell order that both executed creating a trade. Notice trade is closed (isclosed=True), but you can still see price and pnl.
Market SELL EXECUTED, Price: 93.68, Cost: -93.68, Comm 0.00
Close BUY EXECUTED, Price: 89.92, Cost: -93.68, Comm 0.00
data:<backtrader.feeds.csvgeneric.GenericCSVData object at 0x11b7e84e0>
I hope I understood your requirements correctly!
Thank you guys for all the input.
I have turned trade history on from
def __init__(self): self.set_tradehistory(True)
Now all updates to Trade are stored for later analysis and I am calculating Percentage Return after each
Tradehas closed from within
profitPercAbs = trade.history.event.order.executed.price / trade.price profitPercAbs = 1 - profitPercAbs if trade.history.event.order.executed.size > 0 else profitPercAbs - 1
Yes Richard from
Tradewe can get Entry Price using
trade.priceand PnL using
trade.pnlbut not the Exit Price
And the PnL we are talking here is PnL per Trade not per unit of Stock/Future, hence we need
sizeto calculate that which is zero after the trade is closed.
Yes Richard from Trade we can get Entry Price using trade.price
That's wrong. It's oly true if your trade is composed by only 1 order. You can have multiple orders which change the
priceof a trade, right until the moment before is closed.
If you only have 1 order, there is no need to follow the
Trade, just follow the orders.
Richard O'Regan last edited by
@Praveen-Baratam Exit price can be calculated by subtracting pnl from entry price.
(but if trade made of many orders , I'm not sure how effective this would be)
RE Size being 0.. very good point, I hadn't considered this. Because when I backtest, I always run for a unit of 1. eg 1 lot on ES future. Hence size was always 1.
@backtrader True. We can keep track of
Ordersand do the necessary calculations. But then since this is a common enough scenario and requirement, the framework itself should provide or be extended to address this.
There should be a way to easily access data/metrics regarding a Trade whether it is composed of two orders (one entry and one exit) or more. These metrics will be important for Scale-in and Scale-out strategies to name a few.
ab_trader last edited by
@Praveen-Baratam it would be nice if you can develop the mechanism and contribute it. It is open source project.
@ab_trader - I am doing my best. I have already submitted pull requests for new Fisher Transform indicator and Simulated Order. And I am sharing all my code snippets here along with explanations whenever relevant.
When I say the framework should have this or that, I dont mean that someone should implement them but that we shall implement it for everybody's benefit.