Perncent PnL on each trade



  • Through notify_trade in Strategy and the provided Trade object 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 notify_trade?


  • administrators

    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 notify_trade method?

    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.


  • administrators

    Each Trade has attributes like size, 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 value were not right) and from there the %.

    See: Docs - Trade



  • Mostly we want to analyse the returns, etc. for a Trade after it is closed from within the notify_trade method.

    But when the notify_trade method is called after a Trade is closed, the supplied Trade object does not have any size, price, etc. i.e. Trade.size and Trade.price are set to zero.


  • administrators

    When closed, it has no size and 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?


  • administrators

    A Trade is 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:

    or the post



  • 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

    ref:248
    data:<backtrader.feeds.csvgeneric.GenericCSVData object at 0x11b7e84e0>
    tradeid:0
    size:0
    price:93.68
    value:0.0
    commission:0.0
    pnl:1.4029855591444824
    pnlcomm:1.4029855591444824
    justopened:False
    isopen:False
    isclosed:True
    baropen:2
    dtopen:736333.9999999999
    barclose:2
    dtclose:736333.9999999999
    barlen:0
    historyon:False
    history:[]
    status:2

    I hope I understood your requirements correctly!



  • Thank you guys for all the input.

    I have turned trade history on from Strategy

    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 Trade has closed from within notify_trade as below.

    profitPercAbs = trade.history[1].event.order.executed.price / trade.price
    profitPercAbs = 1 - profitPercAbs if trade.history[1].event.order.executed.size > 0 else profitPercAbs - 1
    


  • Yes Richard from Trade we can get Entry Price using trade.price and PnL using trade.pnl but not the Exit Price

    And the PnL we are talking here is PnL per Trade not per unit of Stock/Future, hence we need size to calculate that which is zero after the trade is closed.


  • administrators

    @Praveen-Baratam said in Perncent PnL on each trade:

    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 size and price of 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.



  • @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 Orders and 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.



  • @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.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.