For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Calling Trade.pnl within next()

  • Hello,

    I have a very beginner question here that I am trying to figure out.

    I am trying to get a unrealized PnL per position each time next() goes through another line in its data feed. Can someone point out how I would call trade.pnl within next()?

    Thanks in advance

  • administrators

    You cannot call trade.pnl

    • pnl is an attribute of a Trade instance and can be queried but not called
    • Trade instances are notified in notify_trade. You can keep a reference to the notified instance


    • Trade instances are not updated with each price change, they are updated with orders (i.e.: opening a position, increasing/reducing a position, closing a position).

    You can track the value of an asset with getvalue (Docs - Broker) and use the difference to the original to assess the profit and loss.

  • @backtrader thanks so much for the tip!

Log in to reply