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/

    How best to track trade PNL

    General Discussion
    2
    4
    641
    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.
    • Eddy Bennett
      Eddy Bennett last edited by

      I am struggling to work how how to output the PNL of a trade once it is in play, this is so that I can monitor a trade and close it if it's PNL reaches -1% of the account size.

      I've been looking at the self.position variable which on output shows:

      Position:  --- Position Begin
      - Size: -66666
      - Price: 1.54975
      - Price orig: 0.0
      - Closed: 0
      - Opened: -66666
      - Adjbase: 1.61085
      --- Position End
      

      The only aspect that seems to change on each iteration of next() is Adjbase, is that showing the change in price? If so I can work out the PNL from that perhaps.

      I've dug around the docs but struggling to find the information I need, perhaps there is an easy way to do this? Any help appreciated.

      Thanks

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        bt has trade object, therefore you can track trade pnl. I didn;t try it by myself, but as an idea - strategy has the attributes _tradespending and _trades which contains all trades. So it can be possible to get necessary data from there.

        Useful links - Docs - Strategy and Docs - Broker - Trades.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 1
        • Eddy Bennett
          Eddy Bennett last edited by

          Ah perfect, thanks @ab_trader.

          1 Reply Last reply Reply Quote 0
          • Eddy Bennett
            Eddy Bennett last edited by

            How do you access the attribute values out of _trades? I've tried this:

                    for k, v in self._trades.items():
            
                       print(k.tradeid)
            

            and get this error:

            AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'tradeid'
            
            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors