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/

    Recording the Trades

    General Code/Help
    2
    6
    258
    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.
    • B
      bchip last edited by

      I would like to record all the trades in a system run.
      I would like to save each trade in a pandas dataframe.

      I use notify_order to call a "Log" function
      which contains the follow
      md = {'dtopen':trade.open_datetime(),'dtclose':trade.open_datetime(),'price':trade.price}
      self.dfTrades = self.dfTrades.append(md,ignore_index=True)

      In the init of the StrategyClass I also add
      super().init()
      self.dfTrades = pd.DataFrame()

      I'm not really getting this right, is there anyway I can get all the trades
      into a pandas dataframe at the end of the run?

      Thank you

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Analyzers track all the information you could want and then export them at the end of the run. Try this link and then let us know if you have any questions.

        https://www.backtrader.com/docu/analyzers/analyzers/

        RunBacktest.com

        B 1 Reply Last reply Reply Quote 0
        • B
          bchip @run-out last edited by

          @run-out
          Thanks. There are some stats that I'm not sure how to get though
          Like Profit Factor, Avg Win/Loss, Profits Per Year
          Most of these I would be able to calculate if I could simply save the
          EntryDate, EntryPrice, ExitDate, ExitPrice, Pos(L/S)

          Is there any other way to get these? or do is it best to capture the trades when closed and save to a dataframe

          B 1 Reply Last reply Reply Quote 0
          • B
            bchip @bchip last edited by

            @bchip
            Think I may have found it under the Transactions analyzer
            https://www.backtrader.com/docu/analyzers-reference/#transactions

            run-out 1 Reply Last reply Reply Quote 1
            • run-out
              run-out @bchip last edited by

              @bchip said in Recording the Trades:

              @bchip
              Think I may have found it under the Transactions analyzer
              https://www.backtrader.com/docu/analyzers-reference/#transactions

              Also, If you cannot find what you need, developing an analyzer is not too hard. Just look at how those analyzers have been created. As a good example, have a look at @ab_trader add on trade list

              RunBacktest.com

              B 1 Reply Last reply Reply Quote 0
              • B
                bchip @run-out last edited by

                @run-out Thank you

                1 Reply Last reply Reply Quote 1
                • 1 / 1
                • First post
                  Last post
                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors