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/

    Trades Observer with multiple trades/products

    General Code/Help
    1
    1
    213
    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.
    • M
      momentum last edited by

      Hi,

      if there are multiple trades on different products, then pnlplus and pnlminus in Trades observer will hold just the pnl of the last trade traversed.

          def next(self):
              for trade in self._owner._tradespending:
                  if trade.data not in self.ddatas:
                      continue
      
                  if not trade.isclosed:
                      continue
      
                   if trade.pnl >= 0:
                      self.lines.pnlplus[0] =  trade.pnl
                  else:
                      self.lines.pnlminus[0] =  trade.pnl
      

      I guess something like this is needed

          def next(self):
              for trade in self._owner._tradespending:
                  if trade.data not in self.ddatas:
                      continue
      
                  if not trade.isclosed:
                      continue
      
                  if trade.pnl >= 0:
                      self.lines.pnlplus[0] = np.nansum([self.lines.pnlplus[0], trade.pnl])
                  else:
                      self.lines.pnlminus[0] = np.nansum([self.lines.pnlminus[0], trade.pnl])
      

      I am using version 1.9.70.122 in case that is relevant.

      Cheers

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
      $(document).ready(function () { app.coldLoad(); }); }