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/

    Tracking entry signal for trade

    Indicators/Strategies/Analyzers
    2
    6
    2504
    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.
    • RandyT
      RandyT last edited by

      Hope I am not over using my question credits here... :slight_smile:

      I have a requirement to track the signal that triggered a trade entry. Wondering if there is some place to track that as part of the trade transaction.

      If I enter a trade on a particular signal (market in downtrend), I want to exit around a particular set of signals applied to trades made in the downtrend.

      I have cases where that trend changes while the position is open which is setting up another set of exit signals applied in the new market condition.

      Is there a place to do this in the API?

      1 Reply Last reply Reply Quote 0
      • RandyT
        RandyT last edited by

        Looks like addinfo() object might be a way to do what I want but could use a bit of an example of how to apply this if that is the right direction.

        https://www.backtrader.com/docu/order.html?highlight=addinfo

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

          addinfo can help. It simply takes **kwargs and stores then under order.info, where info is just a dict.

          But since this seems to be more trade oriented than order oriented, you would need to track information at trade level.

          Activate:

          • set tradehistory=True in either ´cerebro = Cerebro(**kwargs)orcerebro.run(**kwargs)`

          The trades notified in strategy.notify_trade will then:

          • Record events under trade.history (a list) and these events contain the order which generated then.

          In most cases a trade is made up of just 2 orders:

          • Opening order
          • Closing order

          If you add some information to the opening order with addinfo, you can always retrieve it from the trade with

          • trade.history[0].event.order (the dict has been extended to support dot notation)

          Not really elegant, because one has to keep track of the trades and there is really no advantage with regards to keeping the information in own structures.

          1 Reply Last reply Reply Quote 0
          • RandyT
            RandyT last edited by

            Sorry, walking around in the debugger here and not quite connecting the dots...

            I'm working in Strategy next(), and don't see where I can get to the trade object. Needing this info where I am applying buy/sell rules. Maybe I am being thick.

            setting the value for addinfo as follows:

            oinfo = dict(trend=self.trend)
            self.order = self.sell(data=self.data0, addinfo=oinfo)
            

            I appreciate your help here.

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

              Strategies can receive trade notifications via the notify_trade(self, trade) method, which can be overridden by end users.

              The trade is notified with each order (which is for the same data and has the same tradeid). The idea:

              class MyStrategy(bt.Strategy):
              
                  def notify_trade(self, trade):
                      self.mytrade = trade
              
              
                  def next(self):
              
                      # Something like this when operating on a single data source
                      if self.position:  # in the market
                          # for sure there is an open trade  - get the info that was added to the opening order
                          info = self.trade.history[0].event.order.info
              

              Information which has been stored with addinfo. This alleviates having to keep track of which order actually was the one that opened the trade because the tradehistory keeps that in the index 0.

              1 Reply Last reply Reply Quote 0
              • RandyT
                RandyT last edited by

                Thank you. This is now working as you suggest.

                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(); }); }