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/

    How to calculate average cash position during trade

    General Code/Help
    2
    2
    40
    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.
    • David Ravnsborg
      David Ravnsborg last edited by

      Hello,

      I am looking for some help calculating the average cash position over the length of a backtest. E.g. Imagine you are backtesting over only 5 days. The first day you have $10,000 cash, the second you have $5000 (because you spent $5000 on entering a position), and the final 3 days you have a cash position of $0. This would average out to $3000/day for the average cash position over that period of time: (10000+5000+0+0+0) / 5 = 3000

      The significance is that it would tell me that an average day would have 30% of $10,000 allocated. Or in other words, 70% of my portfolio is invested in non-cash assets at any given time, on average.

      Where can I find this data? Any help would be appreciated.

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

        I'm presuming you are looking for post backtest analysis? I just shared this with another user. You could easily modify this to fit your needs. The dictionary self.rets would have the cash for each day.

        class CashMarket(bt.analyzers.Analyzer):
            """
            Analyzer returning cash and market values
            """
        
            def create_analysis(self):
                self.rets = {}
                self.vals = 0.0
        
            def notify_cashvalue(self, cash, value):
                self.vals = (cash, value)
                self.rets[self.strategy.datetime.datetime()] = self.vals
        
            def get_analysis(self):
                return self.rets
        

        RunBacktest.com

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