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

Tracking entry signal for trade

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

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

  • administrators

    addinfo can help. It simply takes **kwargs and stores then under, 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.


    • set tradehistory=True in either ´cerebro = Cerebro(**kwargs)**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.

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

  • administrators

    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 =[0]

    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.

  • Thank you. This is now working as you suggest.

Log in to reply