Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Calling Trade.pnl within next()

    General Code/Help
    2
    3
    610
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      Rstrong last edited by

      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

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        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

        But

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

        1 Reply Last reply Reply Quote 0
        • R
          Rstrong last edited by

          @backtrader thanks so much for the tip!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors