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

Print Trade (simple question)

  • Hi Guys,

    this might be a simple one but I cant seem to figure it out. After running a strategy successfully how do you

    1. print off each trade, date, value
    2. and if it was a positive or negative as shown on the standard chart?

    I have implemented the first part like this:

        def next(self):
            if not self.position:
                if self.rsi < self.params.rsi_low:
                if self.rsi > self.params.rsi_high:

    but I am sure there must be a way to dump this information after Cerebro has completed. I think the clue is somewhere in implementing an observer but I have not been able to figure it out yet - help would be much appreciated!

  • Hi,

    read up on Analyzers (, especially and

    Basically you attach the analyzer(s) to the Cerebro instance, run the test and get the analyzers after the test is finished.

  • Hi fintrading,

    many thanks for the pointers - now I am able to get my trades. The only bit I can't yet figure out is where to see if the trade was positive or negative - like you see on the standard plot. Is this somewhere under observers?

    Thanks again!

  • I guess i am trying to show trades and not transactions so that i can see the pnl. I can see that thid can be done through trades class but just need an example to understand how it works. Thanks!

  • In the Docs - Quickstart it is shown how to print trade pnl:

        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0]
            print('%s, %s' % (dt.isoformat(), txt))
        def notify_trade(self, trade):
            if not trade.isclosed:
            self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                     (trade.pnl, trade.pnlcomm))

  • You may also want co check history of the trades. I think it can be extracted from strategy object returned after cerebro run. But I can be wrong, never used it.

    Reference Docs - Trade

  • Great!

    so the information that I am looking for is contained in:


    which is in the main strategy.

    How can I access this info please?

  • administrators

    I probably missed something but:

    • Trade is documented including profit and loss: Docs - Trade

    There is a screenshot from a debugging session showing you exactly where trades are stored and an example from @ab_trader with notify_trade, which you can use (and it would be my recommendation) store the trades.

    • What is exactly your problem?
    • Which is the information you cannot access?

  • Yes sorry. I think i need to be more specific. I am building a strategy which builds patterns based on successful trades. The best way for me to process this would be by dumping all trades into a pandas dataframe with headers shown in the trades sections of docs i.e. ref, status, tradeid, etc. In your opinion what would be the easiest way of achieving this? Thanks again!

  • administrators

    Collect trades during notify_trade and add them to your dataframe.

  • Haha - it was hiding in plain sight! or I was just being blind! Thanks a lot guys!

Log in to reply